-
Notifications
You must be signed in to change notification settings - Fork 102
KG-217. Add ability to customize span data after been created on Koog side for a specific client #566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KG-217. Add ability to customize span data after been created on Koog side for a specific client #566
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package ai.koog.agents.features.opentelemetry.extension | ||
|
||
import ai.koog.agents.features.opentelemetry.attribute.Attribute | ||
import ai.koog.agents.features.opentelemetry.attribute.CustomAttribute | ||
import ai.koog.agents.features.opentelemetry.event.EventBodyField | ||
import ai.koog.agents.features.opentelemetry.event.GenAIAgentEvent | ||
import ai.koog.agents.features.opentelemetry.span.GenAIAgentSpan | ||
|
||
internal fun GenAIAgentEvent.toSpanAttributes(span: GenAIAgentSpan, verbose: Boolean = false) { | ||
Check warning on line 9 in agents/agents-features/agents-features-opentelemetry/src/jvmMain/kotlin/ai/koog/agents/features/opentelemetry/extension/GenAIAgentEventExt.kt
|
||
devcrocod marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Convert event attributes to Span Attributes | ||
attributes.forEach { attribute -> span.addAttribute(attribute) } | ||
|
||
// Convert Body Fields to Span Attributes | ||
bodyFields.forEach { bodyField -> | ||
val attribute = bodyField.toGenAIAttribute(verbose) | ||
span.addAttribute(attribute) | ||
} | ||
} | ||
|
||
internal fun EventBodyField.toGenAIAttribute(verbose: Boolean): Attribute { | ||
Check warning on line 20 in agents/agents-features/agents-features-opentelemetry/src/jvmMain/kotlin/ai/koog/agents/features/opentelemetry/extension/GenAIAgentEventExt.kt
|
||
return CustomAttribute(key, value, verbose = verbose) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ai.koog.agents.features.opentelemetry.extension | ||
|
||
import ai.koog.agents.features.opentelemetry.attribute.Attribute | ||
import ai.koog.agents.features.opentelemetry.attribute.toSdkAttributes | ||
import ai.koog.agents.features.opentelemetry.event.EventBodyField | ||
import ai.koog.agents.features.opentelemetry.event.GenAIAgentEvent | ||
import ai.koog.agents.features.opentelemetry.span.GenAIAgentSpan | ||
import ai.koog.agents.features.opentelemetry.span.SpanEndStatus | ||
import io.opentelemetry.api.trace.Span | ||
import io.opentelemetry.api.trace.SpanBuilder | ||
import io.opentelemetry.api.trace.StatusCode | ||
|
||
internal fun Span.setSpanStatus(endStatus: SpanEndStatus? = null) { | ||
Check warning on line 13 in agents/agents-features/agents-features-opentelemetry/src/jvmMain/kotlin/ai/koog/agents/features/opentelemetry/extension/SpanExt.kt
|
||
devcrocod marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val statusCode = endStatus?.code ?: StatusCode.OK | ||
val statusDescription = endStatus?.description ?: "" | ||
this.setStatus(statusCode, statusDescription) | ||
} | ||
|
||
internal fun SpanBuilder.setAttributes(attributes: List<Attribute>) { | ||
setAllAttributes(attributes.toSdkAttributes()) | ||
} | ||
|
||
internal fun Span.setAttributes(attributes: List<Attribute>) { | ||
setAllAttributes(attributes.toSdkAttributes()) | ||
} | ||
|
||
internal fun Span.setEvents(events: List<GenAIAgentEvent>) { | ||
events.forEach { event -> | ||
// The 'opentelemetry-java' SDK does not have support for event body fields at the moment. | ||
// Pass body fields as attributes until an API is updated. | ||
val attributes = buildList { | ||
addAll(event.attributes) | ||
if (event.bodyFields.isNotEmpty()) { | ||
add(event.bodyFieldsAsAttribute()) | ||
} | ||
} | ||
|
||
addEvent(event.name, attributes.toSdkAttributes()) | ||
} | ||
} | ||
|
||
internal inline fun <reified TBodyField> GenAIAgentSpan.eventBodyFieldToAttribute( | ||
Check warning on line 42 in agents/agents-features/agents-features-opentelemetry/src/jvmMain/kotlin/ai/koog/agents/features/opentelemetry/extension/SpanExt.kt
|
||
event: GenAIAgentEvent, | ||
attributeCreate: (TBodyField) -> Attribute | ||
) where TBodyField : EventBodyField { | ||
event.bodyFields.filterIsInstance<TBodyField>().forEach { bodyField -> | ||
val attributeFromEvent = attributeCreate(bodyField) | ||
this.addAttribute(attributeFromEvent) | ||
} | ||
|
||
this.removeEvent(event) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ai.koog.agents.features.opentelemetry.integration | ||
|
||
import ai.koog.agents.features.opentelemetry.span.GenAIAgentSpan | ||
|
||
/** | ||
* Adapter abstract class for post-processing GenAI agent spans. | ||
* | ||
* This class allows customization of how GenAI agent spans are processed after they are created. | ||
* Implementations can modify span data, add additional attributes or events, or perform any other | ||
* post-processing logic needed before the span is completed. | ||
* | ||
* The abstract class provides a single method called after a span is created but before it is finished. | ||
*/ | ||
internal abstract class SpanAdapter { | ||
Check warning on line 14 in agents/agents-features/agents-features-opentelemetry/src/jvmMain/kotlin/ai/koog/agents/features/opentelemetry/integration/SpanAdapter.kt
|
||
|
||
/** | ||
* Invoked before the specified GenAIAgentSpan is started. This method allows implementations to | ||
* perform any setup or customization required prior to the span being initialized and used. | ||
* | ||
* @param span The GenAI agent span to process | ||
*/ | ||
open fun onBeforeSpanStarted(span: GenAIAgentSpan) { } | ||
Check warning on line 22 in agents/agents-features/agents-features-opentelemetry/src/jvmMain/kotlin/ai/koog/agents/features/opentelemetry/integration/SpanAdapter.kt
|
||
|
||
/** | ||
* Invoked before the specified GenAIAgentSpan is finished. This method allows implementations | ||
* to perform any final processing, modifications, or cleanup tasks required before the span | ||
* is completed. | ||
* | ||
* @param span The GenAI agent span to process before it is finished. | ||
*/ | ||
open fun onBeforeSpanFinished(span: GenAIAgentSpan) { } | ||
Check warning on line 31 in agents/agents-features/agents-features-opentelemetry/src/jvmMain/kotlin/ai/koog/agents/features/opentelemetry/integration/SpanAdapter.kt
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.