OpenTelemetry GenAI Observability (openhusky-otel)
openhusky-otel provides zero-lock-in OpenTelemetry GenAI tracing for AgentRuntime and LlmClient while keeping openhusky-core, openhusky-ai, and openhusky-session completely free of OpenTelemetry API/SDK dependencies.
<dependency>
<groupId>dev.openhusky</groupId>
<artifactId>openhusky-otel</artifactId>
<version>0.3.0</version>
</dependency>implementation("dev.openhusky:openhusky-otel:0.3.0")Quick Setup (OpenHuskyOtel.install)
Install instrumentation onto any AgentRuntime.Builder in a single call before .build():
import dev.openhusky.otel.OpenHuskyOtel;
import dev.openhusky.otel.OpenHuskyOtelConfig;
import io.opentelemetry.api.GlobalOpenTelemetry;
AgentRuntime.Builder builder = AgentRuntime.builder()
.provider(myProvider)
.model(myModel)
.toolsFrom(myTools);
OpenHuskyOtel.install(builder, GlobalOpenTelemetry.get(),
OpenHuskyOtelConfig.builder()
.agentName("support-desk")
.captureContent(false) // Default: PII-safe (no prompt/arg/result text)
.recordLifecycleSpanEvents(true) // Default: records lifecycle Span Events
.emitLegacyGenAiSystem(true) // Default: sets both gen_ai.provider.name & gen_ai.system
.build());
AgentRuntime runtime = builder.build();For standalone LlmClient instrumentation without AgentRuntime, wrap it with OpenHuskyOtel.llmInterceptor(openTelemetry, config).
OpenHuskyOtelConfig Options
| Option | Type | Default | Description |
|---|---|---|---|
agentName | String | null | Suffixes the root span name as invoke_agent <agentName> and sets gen_ai.agent.name. |
captureContent | boolean | false | When true, records gen_ai.tool.call.arguments, gen_ai.tool.call.result, and steering text (capped at 4096 chars). |
recordLifecycleSpanEvents | boolean | true | Emits structured Span Events onto the active invoke_agent span. |
emitLegacyGenAiSystem | boolean | true | Emits gen_ai.system alongside gen_ai.provider.name for older collector compatibility. |
Span Hierarchy & Virtual-Thread Context Propagation
OpenHuskyOtel.install(...) registers an eventListener, a StepInterceptor, and a ToolInterceptor backed by OtelRunState:
invoke_agent support-desk (SpanKind.INTERNAL)
├── chat claude-sonnet-4-5-20250929 (SpanKind.CLIENT, stepIndex=1)
├── execute_tool search_catalog (SpanKind.INTERNAL, virtual thread #1)
├── execute_tool check_inventory (SpanKind.INTERNAL, virtual thread #2)
├── execute_tool research_subagent (SpanKind.INTERNAL)
│ └── invoke_agent research-specialist (nested SubagentTool parent-child span!)
└── chat claude-sonnet-4-5-20250929 (SpanKind.CLIENT, stepIndex=2)How Virtual-Thread Propagation Works
Because AgentLoop executes PARALLEL_SAFE tools on separate virtual threads and SubagentTool invokes a child AgentRuntime inside execute_tool, OtelRunState stores the active run's OpenTelemetry Context in a ConcurrentHashMap<String, ActiveRun> keyed by runId—and never leaves a Scope open across event callbacks. Each StepInterceptor and ToolInterceptor invocation attaches parentCtx = contextOrCurrent(runId) and activates span.makeCurrent() strictly within a try (Scope ...) block, so:
- Parallel
ToolBatchExecutorvirtual threads inherit the exactinvoke_agentparent span. - Nested
SubagentToolcalls run insideexecute_tool's activeScope, automatically making the childinvoke_agentspan a child ofexecute_tool <subagent>.
GenAI Semantic Attributes & Lifecycle Span Events
Recorded Span Attributes
| Span | Attributes |
|---|---|
invoke_agent | gen_ai.operation.name="invoke_agent", gen_ai.conversation.id, gen_ai.agent.name, gen_ai.provider.name, gen_ai.system, gen_ai.request.model, gen_ai.usage.input_tokens, gen_ai.usage.output_tokens, gen_ai.usage.cache_read.input_tokens, gen_ai.usage.cache_creation.input_tokens, openhusky.session.id, openhusky.run.id, openhusky.principal.id, openhusky.tenant.id, openhusky.termination.reason, openhusky.stop.reason, error.type |
chat <model> | gen_ai.operation.name="chat", gen_ai.provider.name, gen_ai.system, gen_ai.request.model, gen_ai.request.temperature, gen_ai.request.max_tokens, gen_ai.response.model, gen_ai.response.finish_reasons, token usage attributes, openhusky.session.id, openhusky.run.id, openhusky.step.index, error.type |
execute_tool <tool> | gen_ai.operation.name="execute_tool", gen_ai.tool.name, gen_ai.tool.call.id, openhusky.session.id, openhusky.run.id, openhusky.tenant.id, error.type (tool_error on ToolResult.isError()), plus gen_ai.tool.call.arguments & gen_ai.tool.call.result when captureContent=true |
Lifecycle Span Events
When recordLifecycleSpanEvents is true, OtelRunState records the following Span Events onto the active invoke_agent span:
openhusky.compaction_applied(messages_before,messages_after)openhusky.steering_applied(skipped_tools_count, optionalsteering_textifcaptureContent=true)openhusky.approval_requested(approval_id,tool_call_id,tool_name)openhusky.approval_resolved(approval_id,tool_name,approved)openhusky.doom_loop_detected(tool_name,consecutive_count,action)openhusky.model_retry_scheduled(attempt,max_retries,delay_ms,reason)
Privacy & Spring Boot OTLP Integration
- PII-Safe by Default:
captureContentdefaults tofalseso customer prompts, tool arguments, tool outputs, and steering text are never exported to external telemetry backends unless explicitly enabled. Even when enabled, strings are truncated to4096characters (...[truncated]). - Spring Boot Auto-Configuration: When
openhusky-spring-boot-starter,openhusky-otel, and anOpenTelemetrybean (e.g., fromspring-boot-starter-actuator+opentelemetry-exporter-otlp) are on the classpath,OpenHuskyOtelAutoConfigurationregisters anAgentRuntimeCustomizerautomatically, configurable viaopenhusky.otel.enabled,openhusky.otel.agent-name, andopenhusky.otel.capture-content.