Skip to content

Commit

Permalink
added logger in in-memory-metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
José Carlos Montañez authored and José Carlos Montañez committed Sep 4, 2024
1 parent accfc6e commit e908824
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.xebia.functional.xef.metrics

import arrow.atomic.AtomicLong
import io.github.oshai.kotlinlogging.KLogger

class InMemoryCounterMetric : CounterMetric {
class InMemoryCounterMetric(val name: String, val logger: KLogger) : CounterMetric {
private val count = AtomicLong(0)

override fun increment(n: Long) {
count.incrementAndGet()
logger.info { "Counter $name incremented to ${count.get()}" }
}

override fun decrement(n: Long) {
count.decrementAndGet()
logger.info { "Counter $name decremented to ${count.get()}" }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ class LogsMetric(private val level: Level = Level.INFO) : Metric {

override suspend fun createCounter(name: String): CounterMetric {
logger.at(level) { message = "${writeIndent(numberOfBlocks.get())}> Created counter: $name" }
val counter = InMemoryCounterMetric()
val counter = InMemoryCounterMetric(name, logger)
countersMap[name] = counter
return counter
}

override suspend fun getCounter(name: String): CounterMetric {
logger.at(level) { message = "${writeIndent(numberOfBlocks.get())}> Get counter: $name" }
return countersMap[name] ?: InMemoryCounterMetric()
return countersMap[name] ?: InMemoryCounterMetric(name, logger)
}

private fun writeIndent(times: Int = 1) = (1..indentSize * times).fold("") { a, _ -> "$a " }
Expand Down

0 comments on commit e908824

Please sign in to comment.