Documentation
¶
Overview ¶
Package tracing wires OpenTelemetry tracing into a jargo voice agent.
The service processors emit spans through the global tracer, so instrumentation costs nothing until a TracerProvider is installed — without one, Tracer returns a no-op. Call Init at startup to export spans over OTLP, and let the task trace the session so the conversation, its turns and the STT, LLM and TTS calls of each turn nest under a single trace:
shutdown, err := tracing.Init(ctx, tracing.Config{ServiceName: "voicebot"})
defer shutdown(context.Background())
...
task := pipeline.NewWorker(pipe, pipeline.WorkerConfig{
EnableTracing: true,
ConversationID: sessionID,
})
task.Run(ctx)
Index ¶
- func GenerateConversationID() string
- func Init(ctx context.Context, cfg Config) (func(context.Context) error, error)
- func ProviderName(service, kind string) string
- func SetGeminiLiveAttributes(span trace.Span, a GeminiLiveAttributes)
- func SetLLMAttributes(span trace.Span, a LLMAttributes)
- func SetOpenAIRealtimeAttributes(span trace.Span, a OpenAIRealtimeAttributes)
- func SetSTTAttributes(span trace.Span, a STTAttributes)
- func SetSTTUsage(ctx context.Context, model string, audio time.Duration)
- func SetTTSAttributes(span trace.Span, a TTSAttributes)
- func SetTTSUsage(ctx context.Context, model string, characters int)
- func SetTokenUsage(ctx context.Context, u frames.LLMTokenUsage)
- func Tracer() trace.Tracer
- type Config
- type GeminiLiveAttributes
- type LLMAttributes
- type OpenAIRealtimeAttributes
- type STTAttributes
- type TTSAttributes
- type TracingContext
- func (c *TracingContext) ConversationContext() trace.SpanContext
- func (c *TracingContext) ConversationID() string
- func (c *TracingContext) Parent(ctx context.Context) context.Context
- func (c *TracingContext) SetConversationContext(sc trace.SpanContext, id string)
- func (c *TracingContext) SetTurnContext(sc trace.SpanContext)
- func (c *TracingContext) TurnContext() trace.SpanContext
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateConversationID ¶ added in v0.1.0
func GenerateConversationID() string
GenerateConversationID returns a fresh conversation id, for a session that does not carry an identifier of its own.
func Init ¶
Init installs a global TracerProvider that batches spans to an OTLP HTTP collector, and returns a shutdown function that flushes and stops it. Call the returned function on exit. The service processors begin emitting spans as soon as the provider is installed.
func ProviderName ¶ added in v0.1.0
ProviderName is the gen_ai.provider.name value for a service, derived from the service's name by dropping the suffix naming its kind ("OpenAILLM" becomes "openai") and lowercasing what is left. Services whose provider name the conventions spell differently are looked up instead.
The suffix is removed only from the end of the name, not wherever it appears, so a provider whose own name ends in the kind it implements keeps it: "XTTSTTS" is the XTTS provider's speech synthesis, and reduces to "xtts" rather than to "x".
func SetGeminiLiveAttributes ¶ added in v0.1.0
func SetGeminiLiveAttributes(span trace.Span, a GeminiLiveAttributes)
SetGeminiLiveAttributes records one Gemini Live operation on its span.
func SetLLMAttributes ¶ added in v0.1.0
func SetLLMAttributes(span trace.Span, a LLMAttributes)
SetLLMAttributes records a generation on its span.
func SetOpenAIRealtimeAttributes ¶ added in v0.1.0
func SetOpenAIRealtimeAttributes(span trace.Span, a OpenAIRealtimeAttributes)
SetOpenAIRealtimeAttributes records one Realtime operation on its span.
func SetSTTAttributes ¶ added in v0.1.0
func SetSTTAttributes(span trace.Span, a STTAttributes)
SetSTTAttributes records a transcription segment on its span.
func SetSTTUsage ¶ added in v0.1.0
SetSTTUsage records billable transcription usage on the span in ctx: the provider model and the duration of audio sent for transcription. The unit is milliseconds because usage values have to be whole numbers — a fractional one is discarded — and rounding to whole seconds would throw away most of a short turn. Configure the model's price per millisecond accordingly: a rate quoted per minute is that rate divided by 60000.
func SetTTSAttributes ¶ added in v0.1.0
func SetTTSAttributes(span trace.Span, a TTSAttributes)
SetTTSAttributes records a synthesis on its span.
func SetTTSUsage ¶ added in v0.1.0
SetTTSUsage records one synthesis's billable usage on the span in ctx: the provider model and the number of characters handed to it. Characters are counted in runes rather than bytes, because that is the unit TTS providers bill in — an accented character is one character, not the two bytes it occupies in UTF-8.
func SetTokenUsage ¶ added in v0.1.0
func SetTokenUsage(ctx context.Context, u frames.LLMTokenUsage)
SetTokenUsage records LLM token usage on the span in ctx, under the OpenTelemetry GenAI gen_ai.usage.* keys.
The input and output counts are always recorded. The cache and per-modality breakdowns are recorded whenever the service accounted for them, a reported zero included: on a model that caches, no cache read this generation is a measurement, and one worth telling apart from a model that does not cache at all. A count the service left unreported carries no attribute.
Types ¶
type Config ¶
type Config struct {
// ServiceName labels the traces; defaults to "jargo".
ServiceName string
// ServiceVersion is an optional version label.
ServiceVersion string
// Endpoint overrides the OTLP HTTP endpoint (host:port). Empty honors the
// standard OTEL_EXPORTER_OTLP_ENDPOINT environment variable.
Endpoint string
// Insecure sends over plain HTTP instead of HTTPS.
Insecure bool
// SampleRatio is the head-sampling ratio in (0,1]. Zero (or less) always
// samples — jargo traces are low-volume, one trace per session.
SampleRatio float64
}
Config configures OTLP export.
type GeminiLiveAttributes ¶ added in v0.1.0
type GeminiLiveAttributes struct {
// Model is the model identifier the session runs against.
Model string
// Operation names what the span covers (setup, a response, a tool call).
Operation string
// VoiceID is the voice the model speaks in. Empty leaves the attribute off.
VoiceID string
// Language is the language of the session. Empty leaves the attribute off.
Language string
// Modalities are the response modalities configured. Empty leaves the
// attribute off.
Modalities string
// Tools are the names of the tools offered, one entry per tool. A tool with
// no name contributes to the count but not to the names.
Tools []string
// ToolsSerialized is the full tool definitions. Empty leaves them off.
ToolsSerialized string
// Transcript is speech transcribed this operation. Empty leaves it off.
Transcript string
// TranscriptIsInput reports whether the transcript is the user's speech
// rather than the model's. Nil leaves the attribute off.
TranscriptIsInput *bool
// TextOutput is text the model produced. Empty leaves the attribute off.
TextOutput string
// AudioDataSize is the size of the audio payload in bytes. Nil leaves the
// attribute off.
AudioDataSize *int
// Settings are the session settings, recorded under settings.*. A grouped
// setting is expanded one level.
Settings map[string]any
// Extra are additional attributes, written last so they override.
Extra map[string]any
}
GeminiLiveAttributes describes one operation of a Gemini Live session.
type LLMAttributes ¶ added in v0.1.0
type LLMAttributes struct {
// Service is the generating service's name, which the provider name is
// derived from.
Service string
// Model is the model identifier that answered.
Model string
// Stream reports whether the generation streamed. It is always recorded.
Stream bool
// Messages is the serialized conversation sent to the model. Empty leaves
// the attribute off.
Messages string
// Output is the model's reply text. Empty leaves the attribute off.
Output string
// Tools is the serialized tool configuration. Empty leaves the attribute
// off.
Tools string
// ToolCount is how many tools were offered. Nil leaves the attribute off.
ToolCount *int
// ToolChoice is how the model was told to choose among them. Empty leaves
// the attribute off.
ToolChoice string
// SystemInstructions is the system prompt in force. Empty leaves the
// attribute off.
SystemInstructions string
// TTFB is the time to first byte in seconds. Nil leaves the attribute off.
TTFB *float64
// Parameters are the generation parameters. The ones the conventions name
// are recorded under gen_ai.request.*, the rest under param.*.
Parameters map[string]any
// ExtraParameters are provider-specific parameters, recorded under extra.*.
ExtraParameters map[string]any
// Extra are additional attributes, written last so they override.
Extra map[string]any
}
LLMAttributes describes one generation for its span.
type OpenAIRealtimeAttributes ¶ added in v0.1.0
type OpenAIRealtimeAttributes struct {
// Model is the model identifier the session runs against.
Model string
// Operation names what the span covers (setup, a request, a response).
Operation string
// SessionProperties are the session configuration, recorded under session.*.
// A grouped property is expanded one level.
SessionProperties map[string]any
// Transcript is speech transcribed this operation. Empty leaves it off.
Transcript string
// TranscriptIsInput reports whether the transcript is the user's speech
// rather than the model's. Nil leaves the attribute off.
TranscriptIsInput *bool
// ContextMessages is the serialized conversation sent to the model, recorded
// as the span's input. Empty leaves the attribute off.
ContextMessages string
// Tools are the names of the tools offered, one entry per tool.
Tools []string
// ToolsSerialized is the full tool definitions. Empty leaves them off.
ToolsSerialized string
// FunctionCalls are the names of the calls the model requested, in the order
// it requested them.
FunctionCalls []string
// AudioDataSize is the size of the audio payload in bytes. Nil leaves the
// attribute off.
AudioDataSize *int
// Extra are additional attributes, written last so they override.
Extra map[string]any
}
OpenAIRealtimeAttributes describes one operation of an OpenAI Realtime session.
type STTAttributes ¶ added in v0.1.0
type STTAttributes struct {
// Service is the transcribing service's name, which the provider name is
// derived from.
Service string
// Model is the model identifier the transcription ran against.
Model string
// OperationName names the operation; empty means "stt".
OperationName string
// Transcript is the text transcribed. Empty leaves the attribute off.
Transcript string
// Final reports whether the transcript is the finalized one. Nil leaves the
// attribute off.
Final *bool
// Language is the language transcribed. Empty leaves the attribute off.
Language string
// UserID names whose speech was transcribed. Empty leaves the attribute off.
UserID string
// VADEnabled reports whether voice activity detection is in use. It is
// always recorded.
VADEnabled bool
// TTFB is the time to first byte in seconds. Nil leaves the attribute off.
TTFB *float64
// Settings are the service settings the caller gave, recorded under
// settings.*.
Settings map[string]any
// Extra are additional attributes, written last so they override.
Extra map[string]any
}
STTAttributes describes one transcription segment for its span.
type TTSAttributes ¶ added in v0.1.0
type TTSAttributes struct {
// Service is the synthesizing service's name, which the provider name is
// derived from.
Service string
// Model is the model identifier the synthesis ran against.
Model string
// VoiceID is the voice spoken in.
VoiceID string
// Text is what was synthesized. Empty leaves the attribute off.
Text string
// OperationName names the operation; empty means "tts".
OperationName string
// CharacterCount is how many characters were synthesized. Nil leaves the
// attribute off; zero is recorded.
CharacterCount *int
// TTFB is the time to first byte in seconds. Nil leaves the attribute off.
TTFB *float64
// Settings are the service settings the caller gave, recorded under
// settings.*.
Settings map[string]any
// Extra are additional attributes, written last so they override.
Extra map[string]any
}
TTSAttributes describes one speech synthesis for its span.
type TracingContext ¶ added in v0.1.0
type TracingContext struct {
// contains filtered or unexported fields
}
TracingContext is the tracing state of one running pipeline: the span the whole conversation hangs from, and the span of the turn being spoken right now. A task creates one per session and hands it to the processors at setup. The turn observer writes it as the conversation and its turns begin and end; the services read it to parent their spans, so a span raised from a goroutine that no longer holds the frame's context still lands under the turn it belongs to.
The zero value is ready to use, and a nil *TracingContext reads as a pipeline with no tracing: it reports no conversation, no turn, and leaves a parent context alone. Safe for concurrent use.
func NewTracingContext ¶ added in v0.1.0
func NewTracingContext() *TracingContext
NewTracingContext builds an empty tracing context.
func (*TracingContext) ConversationContext ¶ added in v0.1.0
func (c *TracingContext) ConversationContext() trace.SpanContext
ConversationContext is the conversation span, invalid when none is open.
func (*TracingContext) ConversationID ¶ added in v0.1.0
func (c *TracingContext) ConversationID() string
ConversationID names the conversation being traced, empty when none is open.
func (*TracingContext) Parent ¶ added in v0.1.0
func (c *TracingContext) Parent(ctx context.Context) context.Context
Parent returns ctx re-parented to the span a service span should hang from: the turn being spoken, or the conversation when no turn is open. Everything else about ctx — its deadline, its cancellation, its values — is left as it was, so a caller can start its span from the returned context and go on using it for the work the span covers.
With neither a turn nor a conversation open, ctx is returned unchanged and the span lands wherever ctx already pointed.
func (*TracingContext) SetConversationContext ¶ added in v0.1.0
func (c *TracingContext) SetConversationContext(sc trace.SpanContext, id string)
SetConversationContext records the conversation span everything else hangs from, along with the id naming that conversation. An invalid span context clears both, which is how a conversation is closed.
func (*TracingContext) SetTurnContext ¶ added in v0.1.0
func (c *TracingContext) SetTurnContext(sc trace.SpanContext)
SetTurnContext records the turn being spoken. An invalid span context clears it, which is how a turn is closed.
func (*TracingContext) TurnContext ¶ added in v0.1.0
func (c *TracingContext) TurnContext() trace.SpanContext
TurnContext is the span of the turn being spoken, invalid between turns.