Skip to content

Commit 119dfa8

Browse files
committed
refactor: add meter interval to metric exporter individually, update tests
1 parent dfa0d63 commit 119dfa8

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

agents/agents-features/agents-features-opentelemetry/src/jvmMain/kotlin/ai/koog/agents/features/opentelemetry/feature/OpenTelemetryConfig.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class OpenTelemetryConfig : FeatureConfig() {
6767

6868
private val customResourceAttributes = mutableMapOf<AttributeKey<*>, Any>()
6969

70-
private val customMetricExporters = mutableListOf<MetricExporter>()
70+
private val customMetricExporters = mutableListOf<Pair<MetricExporter, Duration>>()
7171

7272
private var _sdk: OpenTelemetrySdk? = null
7373

@@ -140,17 +140,14 @@ public class OpenTelemetryConfig : FeatureConfig() {
140140
public val meter: Meter
141141
get() = sdk.getMeter(_instrumentationScopeName)
142142

143-
private var meterInterval: Duration
144-
145143
/**
146144
* Adds a MetricExporter to the OpenTelemetry configuration.
147145
* This exporter will be useds to export metrics collected during the application's execution.
148146
*
149147
* @param exporter The MetricExporter instance to be added to the list of custom metric exporters.
150148
*/
151149
public fun addMeterExporter(exporter: MetricExporter, meterInterval: Duration = DEFAULT_METER_INTERVAL) {
152-
customMetricExporters.add(exporter)
153-
this.meterInterval = meterInterval
150+
customMetricExporters.add(exporter to meterInterval)
154151
}
155152

156153
/**
@@ -288,7 +285,7 @@ public class OpenTelemetryConfig : FeatureConfig() {
288285

289286
val metricExporters = createMetricExporters()
290287

291-
metricExporters.forEach { exporter ->
288+
metricExporters.forEach { (exporter, meterInterval) ->
292289
val reader = PeriodicMetricReader
293290
.builder(exporter)
294291
.setInterval(meterInterval)
@@ -345,15 +342,15 @@ public class OpenTelemetryConfig : FeatureConfig() {
345342
}
346343
}
347344

348-
private fun createMetricExporters(): List<MetricExporter> = buildList {
345+
private fun createMetricExporters(): List<Pair<MetricExporter, Duration>> = buildList {
349346
if (customMetricExporters.isEmpty()) {
350347
logger.debug { "No custom metric exporters configured. Use log metric exporter by default." }
351-
add(LoggingMetricExporter.create())
348+
add(LoggingMetricExporter.create() to DEFAULT_METER_INTERVAL)
352349
}
353350

354-
customMetricExporters.forEach { exporter ->
351+
customMetricExporters.forEach { (exporter, interval) ->
355352
logger.debug { "Adding metric exporter: ${exporter::class.simpleName}" }
356-
add(exporter)
353+
add(exporter to interval)
357354
}
358355
}
359356

agents/agents-features/agents-features-opentelemetry/src/jvmTest/kotlin/ai/koog/agents/features/opentelemetry/attribute/GenAIAttributesTest.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,6 @@ class GenAIAttributesTest {
8989
assertEquals("call-123", attribute.value)
9090
}
9191

92-
@Test
93-
fun `test status attribute`() {
94-
val attribute = KoogAttributes.Koog.Tool.Call.Status(KoogAttributes.Koog.Tool.Call.StatusType.SUCCESS)
95-
assertEquals("gen_ai.tool.call.status", attribute.key)
96-
assertEquals("success", attribute.value)
97-
}
98-
9992
@Test
10093
fun `test tool name attribute`() {
10194
val attribute = GenAIAttributes.Tool.Name("search")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ai.koog.agents.features.opentelemetry.attribute
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
6+
class KoogAttributesTest {
7+
@Test
8+
fun `test status attribute`() {
9+
val attribute = KoogAttributes.Koog.Tool.Call.Status(KoogAttributes.Koog.Tool.Call.StatusType.SUCCESS)
10+
assertEquals("koog.tool.call.status", attribute.key)
11+
assertEquals("success", attribute.value)
12+
}
13+
}

0 commit comments

Comments
 (0)