@@ -8,61 +8,60 @@ import io.opentelemetry.api.trace.Span
8
8
import io.opentelemetry.api.trace.Tracer
9
9
import io.opentelemetry.context.Context
10
10
11
- class OpenTelemetryMetric (
12
- private val config : OpenTelemetryConfig = OpenTelemetryConfig .DEFAULT
13
- ) : Metric {
11
+ class OpenTelemetryMetric (private val config : OpenTelemetryConfig = OpenTelemetryConfig .DEFAULT ) :
12
+ Metric {
14
13
15
- private val conversations = mutableListOf<Pair <ConversationId , Context >>()
14
+ private val conversations = mutableListOf<Pair <ConversationId , Context >>()
16
15
17
- private val openTelemetry = config.newInstance()
16
+ private val openTelemetry = config.newInstance()
18
17
19
- override suspend fun <A > promptSpan (conversation : Conversation , prompt : Prompt , block : suspend Metric .() -> A ): A {
20
- val cid = conversation.conversationId ? : return block()
18
+ override suspend fun <A > promptSpan (
19
+ conversation : Conversation ,
20
+ prompt : Prompt ,
21
+ block : suspend Metric .() -> A
22
+ ): A {
23
+ val cid = conversation.conversationId ? : return block()
21
24
22
- val parentContext = cid.getParentConversation()
25
+ val parentContext = cid.getParentConversation()
23
26
24
- val span = getTracer()
25
- .spanBuilder(" Prompt: ${prompt.messages.lastOrNull()?.content ? : " empty" } " )
26
- .setParent(parentContext)
27
- .startSpan()
27
+ val span =
28
+ getTracer()
29
+ .spanBuilder(" Prompt: ${prompt.messages.lastOrNull()?.content ? : " empty" } " )
30
+ .setParent(parentContext)
31
+ .startSpan()
28
32
29
- return try {
30
- val output = block()
31
- span.makeCurrent().use {
32
- span.setAttribute(" number-of-messages" , prompt.messages.count().toString())
33
- span.setAttribute(" last-message" , prompt.messages.lastOrNull()?.content ? : " empty" )
34
- }
35
- output
36
- } finally {
37
- span.end()
38
- }
33
+ return try {
34
+ val output = block()
35
+ span.makeCurrent().use {
36
+ span.setAttribute(" number-of-messages" , prompt.messages.count().toString())
37
+ span.setAttribute(" last-message" , prompt.messages.lastOrNull()?.content ? : " empty" )
38
+ }
39
+ output
40
+ } finally {
41
+ span.end()
39
42
}
43
+ }
40
44
41
- override fun log (conversation : Conversation , message : String ) {
42
- val cid = conversation.conversationId ? : return
45
+ override fun log (conversation : Conversation , message : String ) {
46
+ val cid = conversation.conversationId ? : return
43
47
44
- val parentContext = cid.getParentConversation()
48
+ val parentContext = cid.getParentConversation()
45
49
46
- val span: Span = getTracer().spanBuilder(message)
47
- .setParent(parentContext)
48
- .startSpan()
49
- span.end()
50
- }
51
-
52
- private fun ConversationId.getParentConversation (): Context {
53
- val parent = conversations.find { it.first == this }?.second
54
- return if (parent == null ) {
55
- val newParent = getTracer()
56
- .spanBuilder(value)
57
- .startSpan()
58
- newParent.end()
59
- val newContext = Context .current().with (newParent)
60
- conversations.add(this to newContext)
61
- newContext
62
- } else parent
63
- }
50
+ val span: Span = getTracer().spanBuilder(message).setParent(parentContext).startSpan()
51
+ span.end()
52
+ }
64
53
65
- private fun getTracer (scopeName : String? = null): Tracer =
66
- openTelemetry.getTracer(scopeName ? : config.defaultScopeName)
54
+ private fun ConversationId.getParentConversation (): Context {
55
+ val parent = conversations.find { it.first == this }?.second
56
+ return if (parent == null ) {
57
+ val newParent = getTracer().spanBuilder(value).startSpan()
58
+ newParent.end()
59
+ val newContext = Context .current().with (newParent)
60
+ conversations.add(this to newContext)
61
+ newContext
62
+ } else parent
63
+ }
67
64
65
+ private fun getTracer (scopeName : String? = null): Tracer =
66
+ openTelemetry.getTracer(scopeName ? : config.defaultScopeName)
68
67
}
0 commit comments