From 5474d77ca44f20ba896b072241a4a9e5b92bd0dd Mon Sep 17 00:00:00 2001 From: George Smyrnaios Date: Thu, 14 Nov 2024 17:54:02 +0100 Subject: [PATCH] Refactor tag handling to use type aliases. Updated various classes to use `Tags` and `Tag` type aliases instead of generic `Map`. This improves code readability and consistency throughout the codebase. --- .../io/github/smyrgeorge/log4k/CoLogger.kt | 21 +++++++++++-------- .../github/smyrgeorge/log4k/TracingEvent.kt | 6 +++--- .../simple/SimpleMeteringCollectorAppender.kt | 15 ++++++------- .../github/smyrgeorge/log4k/impl/typealias.kt | 1 + 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/log4k/src/commonMain/kotlin/io/github/smyrgeorge/log4k/CoLogger.kt b/log4k/src/commonMain/kotlin/io/github/smyrgeorge/log4k/CoLogger.kt index afc9249..0980e30 100644 --- a/log4k/src/commonMain/kotlin/io/github/smyrgeorge/log4k/CoLogger.kt +++ b/log4k/src/commonMain/kotlin/io/github/smyrgeorge/log4k/CoLogger.kt @@ -6,6 +6,9 @@ import io.github.smyrgeorge.log4k.Level.INFO import io.github.smyrgeorge.log4k.Level.TRACE import io.github.smyrgeorge.log4k.Level.WARN import io.github.smyrgeorge.log4k.TracingEvent.Span +import io.github.smyrgeorge.log4k.impl.MutableTags +import io.github.smyrgeorge.log4k.impl.Tag +import io.github.smyrgeorge.log4k.impl.Tags import io.github.smyrgeorge.log4k.impl.registry.CollectorRegistry import kotlin.coroutines.CoroutineContext import kotlin.coroutines.coroutineContext @@ -84,17 +87,17 @@ abstract class CoLogger( data class LoggingContext( val span: Span?, - val attributes: Map + val tags: Tags ) : CoroutineContext.Element { class Builder { private var span: Span? = null - private var attributes: MutableMap = mutableMapOf() + private var tags: MutableTags = mutableMapOf() fun with(span: Span): Builder = apply { this.span = span } - fun with(f: (MutableMap) -> Unit): Builder = apply { f(attributes) } - fun with(vararg attributes: Pair): Builder = apply { this.attributes.putAll(attributes) } - fun with(attributes: Map): Builder = apply { this.attributes.putAll(attributes) } - fun build(): LoggingContext = LoggingContext(span, attributes) + fun with(f: (MutableTags) -> Unit): Builder = apply { f(tags) } + fun with(vararg tags: Tag): Builder = apply { this.tags.putAll(tags) } + fun with(tags: Tags): Builder = apply { this.tags.putAll(tags) } + fun build(): LoggingContext = LoggingContext(span, tags) } companion object : CoroutineContext.Key { @@ -102,9 +105,9 @@ abstract class CoLogger( fun builder(): Builder = Builder() fun of(): LoggingContext = LoggingContext(null, emptyMap()) fun of(span: Span): LoggingContext = LoggingContext(span, emptyMap()) - fun of(span: Span, attributes: Map): LoggingContext = LoggingContext(span, attributes) - fun of(span: Span, vararg attributes: Pair): LoggingContext = - LoggingContext(span, attributes.toMap()) + fun of(span: Span, tags: Tags): LoggingContext = LoggingContext(span, tags) + fun of(span: Span, vararg tags: Tag): LoggingContext = + LoggingContext(span, tags.toMap()) } override val key: CoroutineContext.Key diff --git a/log4k/src/commonMain/kotlin/io/github/smyrgeorge/log4k/TracingEvent.kt b/log4k/src/commonMain/kotlin/io/github/smyrgeorge/log4k/TracingEvent.kt index e29f4ca..2101d58 100644 --- a/log4k/src/commonMain/kotlin/io/github/smyrgeorge/log4k/TracingEvent.kt +++ b/log4k/src/commonMain/kotlin/io/github/smyrgeorge/log4k/TracingEvent.kt @@ -2,8 +2,8 @@ package io.github.smyrgeorge.log4k import io.github.smyrgeorge.log4k.TracingEvent.Span.Local import io.github.smyrgeorge.log4k.TracingEvent.Span.Remote -import io.github.smyrgeorge.log4k.impl.OpenTelemetry import io.github.smyrgeorge.log4k.impl.MutableTags +import io.github.smyrgeorge.log4k.impl.OpenTelemetry import io.github.smyrgeorge.log4k.impl.Tags import io.github.smyrgeorge.log4k.impl.extensions.toName import kotlinx.datetime.Clock @@ -187,7 +187,7 @@ sealed interface TracingEvent { tags = tags + mapOf( OpenTelemetry.EXCEPTION_TYPE to error::class.toName(), OpenTelemetry.EXCEPTION_ESCAPED to escaped, - OpenTelemetry.EXCEPTION_MESSAGE to error.message, + OpenTelemetry.EXCEPTION_MESSAGE to (error.message ?: ""), OpenTelemetry.EXCEPTION_STACKTRACE to error.stackTraceToString(), ) ) @@ -266,7 +266,7 @@ sealed interface TracingEvent { data class Event( val name: String, val timestamp: Instant, - val tags: Map, + val tags: Tags, ) // https://opentelemetry.io/docs/specs/otel/trace/api/#set-status diff --git a/log4k/src/commonMain/kotlin/io/github/smyrgeorge/log4k/impl/appenders/simple/SimpleMeteringCollectorAppender.kt b/log4k/src/commonMain/kotlin/io/github/smyrgeorge/log4k/impl/appenders/simple/SimpleMeteringCollectorAppender.kt index c97d6d9..2c21bf0 100644 --- a/log4k/src/commonMain/kotlin/io/github/smyrgeorge/log4k/impl/appenders/simple/SimpleMeteringCollectorAppender.kt +++ b/log4k/src/commonMain/kotlin/io/github/smyrgeorge/log4k/impl/appenders/simple/SimpleMeteringCollectorAppender.kt @@ -3,6 +3,7 @@ package io.github.smyrgeorge.log4k.impl.appenders.simple import io.github.smyrgeorge.log4k.Appender import io.github.smyrgeorge.log4k.Meter import io.github.smyrgeorge.log4k.MeteringEvent +import io.github.smyrgeorge.log4k.impl.Tags import io.github.smyrgeorge.log4k.impl.extensions.toName import kotlinx.datetime.Instant @@ -137,7 +138,7 @@ class SimpleMeteringCollectorAppender : Appender { val kind: Meter.Instrument.Kind val unit: String? val description: String? - val tags: Map? + val tags: Tags? var value: Number var updatedAt: Instant? @@ -157,7 +158,7 @@ class SimpleMeteringCollectorAppender : Appender { appendLine() } - fun Map.format(): String = + fun Tags.format(): String = entries.joinToString(prefix = "{", postfix = "}") { (k, v) -> "$k=\"$v\"" } data class Info( @@ -169,7 +170,7 @@ class SimpleMeteringCollectorAppender : Appender { abstract class AbstractCounter( override val name: String, - override val tags: Map?, + override val tags: Tags?, override val kind: Meter.Instrument.Kind, override val unit: String?, override val description: String?, @@ -194,7 +195,7 @@ class SimpleMeteringCollectorAppender : Appender { abstract class AbstractRecorder( override val name: String, - override val tags: Map?, + override val tags: Tags?, override val kind: Meter.Instrument.Kind, override val unit: String?, override val description: String?, @@ -209,7 +210,7 @@ class SimpleMeteringCollectorAppender : Appender { class Counter( name: String, - tags: Map?, + tags: Tags?, kind: Meter.Instrument.Kind, unit: String?, description: String?, @@ -219,7 +220,7 @@ class SimpleMeteringCollectorAppender : Appender { class UpDownCounter( name: String, - tags: Map?, + tags: Tags?, kind: Meter.Instrument.Kind, unit: String?, description: String?, @@ -240,7 +241,7 @@ class SimpleMeteringCollectorAppender : Appender { class Gauge( name: String, - tags: Map?, + tags: Tags?, kind: Meter.Instrument.Kind, unit: String?, description: String?, diff --git a/log4k/src/commonMain/kotlin/io/github/smyrgeorge/log4k/impl/typealias.kt b/log4k/src/commonMain/kotlin/io/github/smyrgeorge/log4k/impl/typealias.kt index 88a278b..d6a547d 100644 --- a/log4k/src/commonMain/kotlin/io/github/smyrgeorge/log4k/impl/typealias.kt +++ b/log4k/src/commonMain/kotlin/io/github/smyrgeorge/log4k/impl/typealias.kt @@ -1,4 +1,5 @@ package io.github.smyrgeorge.log4k.impl +typealias Tag = Pair typealias Tags = Map typealias MutableTags = MutableMap