Skip to content

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.

xml
<dependency>
    <groupId>dev.openhusky</groupId>
    <artifactId>openhusky-otel</artifactId>
    <version>0.3.0</version>
</dependency>
kotlin
implementation("dev.openhusky:openhusky-otel:0.3.0")

Quick Setup (OpenHuskyOtel.install) ​

Install instrumentation onto any AgentRuntime.Builder in a single call before .build():

java
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 ​

OptionTypeDefaultDescription
agentNameStringnullSuffixes the root span name as invoke_agent <agentName> and sets gen_ai.agent.name.
captureContentbooleanfalseWhen true, records gen_ai.tool.call.arguments, gen_ai.tool.call.result, and steering text (capped at 4096 chars).
recordLifecycleSpanEventsbooleantrueEmits structured Span Events onto the active invoke_agent span.
emitLegacyGenAiSystembooleantrueEmits 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:

text
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 ToolBatchExecutor virtual threads inherit the exact invoke_agent parent span.
  • Nested SubagentTool calls run inside execute_tool's active Scope, automatically making the child invoke_agent span a child of execute_tool <subagent>.

GenAI Semantic Attributes & Lifecycle Span Events ​

Recorded Span Attributes ​

SpanAttributes
invoke_agentgen_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, optional steering_text if captureContent=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: captureContent defaults to false so 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 to 4096 characters (...[truncated]).
  • Spring Boot Auto-Configuration: When openhusky-spring-boot-starter, openhusky-otel, and an OpenTelemetry bean (e.g., from spring-boot-starter-actuator + opentelemetry-exporter-otlp) are on the classpath, OpenHuskyOtelAutoConfiguration registers an AgentRuntimeCustomizer automatically, configurable via openhusky.otel.enabled, openhusky.otel.agent-name, and openhusky.otel.capture-content.

Released under the MIT License. Built with Java 21 Virtual Threads.