Documentation
¶
Overview ¶
Package otelgenai provides an OpenTelemetry GenAI semantic-conventions instrumentation for Genkit.
The application owns SDK setup: configure a TracerProvider / MeterProvider / LoggerProvider (e.g. via the OTel SDK) before constructing Genkit. When no provider is configured, go.opentelemetry.io/otel returns non-recording spans and no-op instruments, so this provider is effectively inert.
Wire it up with tracing.SetInstrumentation:
tracing.SetInstrumentation(otelgenai.New(&otelgenai.Options{
ContentCapturingMode: otelgenai.SpanOnly,
EmitToolSpans: true,
}))
g := genkit.Init(ctx, genkit.WithPlugins(&googlegenai.GoogleAI{}))
Configuration is process-wide on purpose: Genkit actions (e.g. a model obtained straight from a plugin) can run without a Genkit instance, and they must be instrumented too.
The provider replaces Genkit's default OpenTelemetry encoding (the genkit:* span attributes) and composes with the built-in dev instrumentation, which feeds the Developer UI on a separate pipeline. Do not combine it with the googlecloud or firebase telemetry plugins: those read the genkit:* attributes (their metrics stop) and their redaction does not cover gen_ai.* content.
See the spec: https://github.com/open-telemetry/semantic-conventions-genai
Index ¶
Constants ¶
const CaptureContentEnvVar = "OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"
CaptureContentEnvVar is the spec's canonical opt-in env var for capturing message content. It is consulted when Options.ContentCapturingMode is unset.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ContentCapturingMode ¶
type ContentCapturingMode string
ContentCapturingMode mirrors the OTel GenAI ContentCapturingMode: where captured message content is recorded.
Content may contain PII and is often large, so the default is NoContent. EventOnly puts structured content on a dedicated log event and is preferred for production; SpanOnly puts it on span attributes as a JSON string (easy to eyeball, but subject to backend attribute/envelope limits), best for development.
const ( NoContent ContentCapturingMode = "NO_CONTENT" SpanOnly ContentCapturingMode = "SPAN_ONLY" EventOnly ContentCapturingMode = "EVENT_ONLY" SpanAndEvent ContentCapturingMode = "SPAN_AND_EVENT" )
type Instrumentation ¶
type Instrumentation struct {
// contains filtered or unexported fields
}
Instrumentation is a tracing.Instrumentation that emits OpenTelemetry telemetry following the OTel GenAI semantic conventions.
func New ¶
func New(opts *Options) *Instrumentation
New builds an Instrumentation from opts. A nil opts uses the defaults. opts is read once, here; changing it afterwards has no effect.
The tracer, meter, and logger are resolved here, once. With the global providers that still picks up an SDK installed later: the OTel globals hand out delegating instruments that switch to the real provider when it is set. Resolving per span instead would take the SDK provider's lock on every span start.
func (*Instrumentation) StartSpan ¶
func (g *Instrumentation) StartSpan(ctx context.Context, info *tracing.SpanInfo) (context.Context, tracing.Span)
StartSpan dispatches on the Genkit action type and opens a span encoded with the OTel GenAI conventions. The returned span records the result when Genkit ends it.
type Options ¶
type Options struct {
// ContentCapturingMode selects where spec-shaped GenAI message content
// (gen_ai.system_instructions, gen_ai.input.messages, gen_ai.output.messages)
// is recorded. Content may contain PII, so the default is NoContent. When
// the zero value is passed, the env var CaptureContentEnvVar is consulted;
// an explicit value here overrides it.
ContentCapturingMode ContentCapturingMode
// CaptureActionIO records raw Genkit action input/output as genkit.input /
// genkit.output JSON attributes on every span (model, tool, flow, etc.).
// Independent of ContentCapturingMode. May contain PII, off by default.
// Inline data: media in model requests and responses is replaced with a
// size marker; other actions' values (e.g. flow inputs) are recorded as is.
CaptureActionIO bool
// EmitToolSpans emits execute_tool spans for tool actions. Off by default.
EmitToolSpans bool
// DisableMetrics turns off the spec's GenAI client metrics (token usage,
// operation duration). Metrics are on by default (the zero value): low
// cardinality and cheap.
DisableMetrics bool
// ScopeName is the instrumentation scope name for the tracer/logger/meter.
// Defaults to "genkit-genai".
ScopeName string
// TracerProvider creates the spans. Nil means the global provider.
TracerProvider oteltrace.TracerProvider
// MeterProvider creates the GenAI client metrics. Nil means the global
// provider.
MeterProvider metric.MeterProvider
// LoggerProvider receives the gen_ai.client.inference.operation.details
// events (EventOnly / SpanAndEvent). Nil means the global provider.
LoggerProvider otellog.LoggerProvider
}
Options configures an Instrumentation.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
genai
Package genai holds the mapping from Genkit data to OpenTelemetry GenAI semantic conventions: attribute names, value vocabularies, message shapes, and the spec's client metrics.
|
Package genai holds the mapping from Genkit data to OpenTelemetry GenAI semantic conventions: attribute names, value vocabularies, message shapes, and the spec's client metrics. |