Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem: no metrics enabled for file based log #263

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ abstract class BufferingLogWriter<T>(
}
private val queue = QueuePublisher<T>(queueLimit, onError = onQueueError)

init {
metrics.queue = queue
}

override fun submit(event: T) {
metrics.produced()
queue.offer(event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,21 @@ abstract class CurrentLogWriter<T>(
}

is LogTargetConfig.File -> {
val metrics = createMetrics()
val file = Path.of(targetConfig.filename)
log.info("Writing $category to $file")
requireNotNull(fileOptions)
logWriter = FileLogWriter<T>(
file, serializer,
startSleep = fileOptions.startSleep, flushSleep = fileOptions.flushSleep,
batchLimit = fileOptions.batchLimit
batchLimit = fileOptions.batchLimit,
metrics = metrics
)
logWriter.start()
}

is LogTargetConfig.Socket -> {
val metrics = if (Global.metricsExtended) {
LogMetrics.Enabled(category.name.lowercase(Locale.getDefault()))
} else {
LogMetrics.None()
}

val metrics = createMetrics()
log.info("Sending $category to ${targetConfig.host}:${targetConfig.port}")
val encoding = when (targetConfig.encoding) {
LogTargetConfig.Encoding.NEW_LINE -> LogEncodingNewLine()
Expand All @@ -75,6 +72,12 @@ abstract class CurrentLogWriter<T>(
}
}

private fun createMetrics() = if (Global.metricsExtended) {
LogMetrics.Enabled(category.name.lowercase(Locale.getDefault()))
} else {
LogMetrics.None()
}

data class FileOptions(
val startSleep: Duration,
val flushSleep: Duration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class FileLogWriter<T>(
serializer: (T) -> ByteArray?,
private val startSleep: Duration,
private val flushSleep: Duration,
private val batchLimit: Int = 5000
) : LogWriter<T>, BufferingLogWriter<T>(serializer, LogEncodingNewLine(), queueLimit = batchLimit) {
private val batchLimit: Int = 5000,
metrics: LogMetrics = LogMetrics.None(),
) : LogWriter<T>, BufferingLogWriter<T>(serializer, LogEncodingNewLine(), queueLimit = batchLimit, metrics = metrics) {

companion object {
private val log = LoggerFactory.getLogger(FileLogWriter::class.java)
Expand Down
21 changes: 18 additions & 3 deletions src/main/kotlin/io/emeraldpay/dshackle/monitoring/LogMetrics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,42 @@
*/
package io.emeraldpay.dshackle.monitoring

import io.emeraldpay.dshackle.commons.QueuePublisher
import io.micrometer.core.instrument.Counter
import io.micrometer.core.instrument.Gauge
import io.micrometer.core.instrument.Metrics

interface LogMetrics {

var queue: QueuePublisher<*>?

fun produced()
fun collected()
fun dropped()

class Enabled(category: String) : LogMetrics {
private val produced = Counter.builder("monitoring_logs_produce")
override var queue: QueuePublisher<*>? = null

private val produced = Counter.builder("monitoringLogs_produce")
.description("Log events produced by Dshackle")
.tag("type", category)
.register(Metrics.globalRegistry)
private val collected = Counter.builder("monitoring_logs_collect")
private val collected = Counter.builder("monitoringLogs_collect")
.description("Log events successfully sent to a storage")
.tag("type", category)
.register(Metrics.globalRegistry)
private val dropped = Counter.builder("monitoring_logs_drop")
private val dropped = Counter.builder("monitoringLogs_drop")
.description("Log events dropped w/o sending")
.tag("type", category)
.register(Metrics.globalRegistry)

init {
Gauge.builder("monitoringLogs_queueSize") { queue?.size?.toDouble() ?: 0.0 }
.description("Log events queue size")
.tag("type", category)
.register(Metrics.globalRegistry)
}

override fun produced() {
produced.increment()
}
Expand All @@ -53,6 +66,8 @@ interface LogMetrics {

class None : LogMetrics {

override var queue: QueuePublisher<*>? = null

override fun produced() {
}

Expand Down