Spans and Traces
Copy page
OpenTelemetry spans for distributed tracing and observability in the Inkeep Agent Framework
Overview
The Inkeep Agent Framework uses OpenTelemetry for distributed tracing and observability. Spans provide detailed visibility into the execution flow of agents, context resolution, tool execution, and other framework operations.
Getting Started with Spans
1. Import Required Dependencies
2. Get the Tracer
Creating and Using Spans
Setting Span Attributes
Basic Attributes
Adding Events to Spans
Recording Important Milestones
Error Events
Error Handling and Status
Using setSpanWithError Utility
The framework provides a convenient setSpanWithError
utility function that handles error recording and status setting:
Best Practices
1. Consistent Naming Convention
The span naming convention follows a hierarchical structure that mirrors your code organization.
Naming Rules
- Class First: Start with the class/module name (e.g.,
agent
,context
,tool
) - Function Second: Follow with the specific function/method (e.g.,
generate
,resolve
) - Use Underscores: For multi-word functions, use underscores (e.g.,
tool_execute
,cache_lookup
) - Consistent Casing: Use lowercase with underscores for consistency
Configuration and Setup
Environment Variables
Instrumentation Setup
The framework automatically sets up OpenTelemetry instrumentation in src/instrumentation.ts
:
Examples in the Codebase
Agent Operations
See src/agents/Agent.ts
for span usage in agent generation and tool execution.
Example from Agent.ts:
Summary
Spans provide powerful observability into your Inkeep Agent Framework operations. By following these patterns:
- Use
getTracer()
for consistent tracing - Use consistent naming
- Set meaningful attributes for searchability
- Handle errors properly using
setSpanWithError
for consistent error handling - Use
startActiveSpan
for automatic lifecycle management
This will give you comprehensive visibility into your agent operations, making debugging and performance optimization much easier.