Skip to content

Commit

Permalink
Refactor event handling and update version to 0.4.0
Browse files Browse the repository at this point in the history
Refactored event handling to support named events and additional attributes in TracingEvent. Enhanced the TracingEvent interface for better clarity and flexibility. Updated project version to 0.4.0 to reflect these significant changes.
  • Loading branch information
smyrgeorge committed Oct 20, 2024
1 parent a17e231 commit e3bc26e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 39 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ RootLogger.Tracing.register(SimpleConsoleTracingAppender())

// Create the span and then start it.
val span: TracingEvent.Span = trace.span("test").start()
span.event("this is a test event")
span.event(name = "test-event")
span.tracer
// Close the span manually.
span.end()
Expand All @@ -100,7 +100,12 @@ val parent: TracingEvent.Span = trace.span("parent")
// Starts immediately the span.
trace.span("test", parent) {
// Send events that are related to the current span.
it.event("this is a test event")
it.event(name = "event-1")
// Include attributes in the event.
it.event(name = "event-2", attributes = mapOf("key" to "value"))
it.event(name = "event-2") { attrs: MutableMap<String, Any?> ->
attrs["key"] = "value"
}
// Automatically closes at the end of te scope.
}
```
Expand Down Expand Up @@ -156,7 +161,12 @@ repeat(10) {
// Starts immediately the span.
trace.span("test") {
// Send events that are related to the current span.
it.event("this is a test event")
it.event(name = "event-1")
// Include attributes in the event.
it.event(name = "event-2", attributes = mapOf("key" to "value"))
it.event(name = "event-2") { attrs: MutableMap<String, Any?> ->
attrs["key"] = "value"
}
// Automatically closes at the end of te scope.
}

Expand Down
3 changes: 1 addition & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
group = "io.github.smyrgeorge"
version = "0.3.0"
version = "0.4.0"

// Common plugin versions here.
plugins {
alias(libs.plugins.dokka)
alias(libs.plugins.kotlin.multiplatform) apply false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlinx.coroutines.sync.withLock
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant

@Suppress("MemberVisibilityCanBePrivate", "unused")
@Suppress("MemberVisibilityCanBePrivate")
interface TracingEvent {
val id: String
val level: Level
Expand Down Expand Up @@ -44,25 +44,31 @@ interface TracingEvent {
this
}

fun event(msg: String, vararg args: Any?): Unit = withLock {
fun event(name: String, f: (MutableMap<String, Any?>) -> Unit) {
val attributes = mutableMapOf<String, Any?>()
f(attributes)
event(name, attributes)
}

fun event(name: String, attributes: Map<String, Any?> = emptyMap()): Unit = withLock {
// If not started, return
if (!started) return@withLock
// If already ended, return.
if (closed) return@withLock
if (!shouldLog()) return@withLock
val event = Event(
id = "$id-${idx()}",
name = name,
spanId = id,
attributes = attributes,
level = level,
tracer = tracer.name,
message = msg,
arguments = args,
timestamp = Clock.System.now()
)
RootLogger.trace(event)
}

fun end(): Unit = withLock {
fun end(error: Throwable? = null): Unit = withLock {
// If not started, return
if (!started) return@withLock
// If already ended, return.
Expand All @@ -73,12 +79,16 @@ interface TracingEvent {
id = id,
level = level,
tracer = tracer.name,
status = error?.let { Status.ERROR } ?: Status.OK,
error = error,
)
RootLogger.trace(event)
}

private fun <T> withLock(f: () -> T): T = runBlocking { mutex.withLock { f() } }

enum class Status { OK, ERROR }

data class Start(
override var id: String,
val name: String,
Expand All @@ -91,46 +101,23 @@ interface TracingEvent {

data class Event(
override val id: String,
val name: String,
val spanId: String,
val attributes: Map<String, Any?>,
override val level: Level,
override val tracer: String,
val message: String,
val arguments: Array<out Any?>,
override val timestamp: Instant = Clock.System.now(),
override val thread: String? = null
) : TracingEvent {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
other as Event
if (id != other.id) return false
if (spanId != other.spanId) return false
if (level != other.level) return false
if (tracer != other.tracer) return false
if (message != other.message) return false
if (timestamp != other.timestamp) return false
if (thread != other.thread) return false
return true
}

override fun hashCode(): Int {
var result = id.hashCode()
result = 31 * result + spanId.hashCode()
result = 31 * result + level.hashCode()
result = 31 * result + tracer.hashCode()
result = 31 * result + message.hashCode()
result = 31 * result + timestamp.hashCode()
result = 31 * result + (thread?.hashCode() ?: 0)
return result
}
}
) : TracingEvent

data class End(
override var id: String,
override val level: Level,
override val tracer: String,
override val timestamp: Instant = Clock.System.now(),
override val thread: String? = null,
private var status: Status,
private var error: Throwable?
) : TracingEvent
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ class MainTests {
// Starts immediately the span.
trace.span("test", parent) {
// Send events that are related to the current span.
it.event("this is a test event")
it.event(name = "event-1")
// Include attributes in the event.
it.event(name = "event-2", attributes = mapOf("key" to "value"))
it.event(name = "event-2") { attrs ->
attrs["key"] = "value"
}
// Automatically closes at the end of te scope.
}

Expand Down

0 comments on commit e3bc26e

Please sign in to comment.