Skip to content

Commit

Permalink
Refactor tag handling to use type aliases.
Browse files Browse the repository at this point in the history
Updated various classes to use `Tags` and `Tag` type aliases instead of generic `Map<String, Any>`. This improves code readability and consistency throughout the codebase.
  • Loading branch information
smyrgeorge committed Nov 14, 2024
1 parent ff0d0ac commit 5474d77
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
21 changes: 12 additions & 9 deletions log4k/src/commonMain/kotlin/io/github/smyrgeorge/log4k/CoLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -84,27 +87,27 @@ abstract class CoLogger(

data class LoggingContext(
val span: Span?,
val attributes: Map<String, Any?>
val tags: Tags
) : CoroutineContext.Element {

class Builder {
private var span: Span? = null
private var attributes: MutableMap<String, Any?> = mutableMapOf()
private var tags: MutableTags = mutableMapOf()
fun with(span: Span): Builder = apply { this.span = span }
fun with(f: (MutableMap<String, Any?>) -> Unit): Builder = apply { f(attributes) }
fun with(vararg attributes: Pair<String, Any?>): Builder = apply { this.attributes.putAll(attributes) }
fun with(attributes: Map<String, Any?>): 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<LoggingContext> {
val EMPTY: LoggingContext = of()
fun builder(): Builder = Builder()
fun of(): LoggingContext = LoggingContext(null, emptyMap())
fun of(span: Span): LoggingContext = LoggingContext(span, emptyMap())
fun of(span: Span, attributes: Map<String, Any>): LoggingContext = LoggingContext(span, attributes)
fun of(span: Span, vararg attributes: Pair<String, Any>): 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<LoggingContext>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
)
)
Expand Down Expand Up @@ -266,7 +266,7 @@ sealed interface TracingEvent {
data class Event(
val name: String,
val timestamp: Instant,
val tags: Map<String, Any?>,
val tags: Tags,
)

// https://opentelemetry.io/docs/specs/otel/trace/api/#set-status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -137,7 +138,7 @@ class SimpleMeteringCollectorAppender : Appender<MeteringEvent> {
val kind: Meter.Instrument.Kind
val unit: String?
val description: String?
val tags: Map<String, Any>?
val tags: Tags?
var value: Number
var updatedAt: Instant?

Expand All @@ -157,7 +158,7 @@ class SimpleMeteringCollectorAppender : Appender<MeteringEvent> {
appendLine()
}

fun Map<String, Any>.format(): String =
fun Tags.format(): String =
entries.joinToString(prefix = "{", postfix = "}") { (k, v) -> "$k=\"$v\"" }

data class Info(
Expand All @@ -169,7 +170,7 @@ class SimpleMeteringCollectorAppender : Appender<MeteringEvent> {

abstract class AbstractCounter(
override val name: String,
override val tags: Map<String, Any>?,
override val tags: Tags?,
override val kind: Meter.Instrument.Kind,
override val unit: String?,
override val description: String?,
Expand All @@ -194,7 +195,7 @@ class SimpleMeteringCollectorAppender : Appender<MeteringEvent> {

abstract class AbstractRecorder(
override val name: String,
override val tags: Map<String, Any>?,
override val tags: Tags?,
override val kind: Meter.Instrument.Kind,
override val unit: String?,
override val description: String?,
Expand All @@ -209,7 +210,7 @@ class SimpleMeteringCollectorAppender : Appender<MeteringEvent> {

class Counter(
name: String,
tags: Map<String, Any>?,
tags: Tags?,
kind: Meter.Instrument.Kind,
unit: String?,
description: String?,
Expand All @@ -219,7 +220,7 @@ class SimpleMeteringCollectorAppender : Appender<MeteringEvent> {

class UpDownCounter(
name: String,
tags: Map<String, Any>?,
tags: Tags?,
kind: Meter.Instrument.Kind,
unit: String?,
description: String?,
Expand All @@ -240,7 +241,7 @@ class SimpleMeteringCollectorAppender : Appender<MeteringEvent> {

class Gauge(
name: String,
tags: Map<String, Any>?,
tags: Tags?,
kind: Meter.Instrument.Kind,
unit: String?,
description: String?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package io.github.smyrgeorge.log4k.impl

typealias Tag = Pair<String, Any>
typealias Tags = Map<String, Any>
typealias MutableTags = MutableMap<String, Any>

0 comments on commit 5474d77

Please sign in to comment.