Skip to content
Source on GitHub (opens in a new tab)
import "github.com/barevoidco/zelk/adapters/otel"

Package otel adapts OpenTelemetry to zelk’s observe.Observer. It lives in its own module so zelk’s core never depends on OpenTelemetry. The adapter is built from providers the host owns: zelk never starts, configures or shuts them down.

const Scope = "github.com/barevoidco/zelk"

Scope is the instrumentation scope zelk’s spans and metrics are reported under.

type Observer struct {
// contains filtered or unexported fields
}

Observer is an observe.Observer over OpenTelemetry providers. It is safe for concurrent use.

func New(tp trace.TracerProvider, mp metric.MeterProvider) *Observer

New returns an Observer reporting spans to tp and metrics to mp. A nil provider reports nothing of its kind.

Example

The host owns its OpenTelemetry providers and hands them to zelk, which reports executions, turns, model calls and tool calls as spans. Here a span recorder stands in for an exporter, and a nil MeterProvider reports no metrics.

spans := tracetest.NewSpanRecorder()
tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spans))
ctx := context.Background()
p, err := replay.New(provider.Caps{}, replay.Reported("met", "Hi."))
if err != nil {
panic(err)
}
a, err := zelk.New(ctx, p, zelk.WithModel("m"), zelk.WithPermissionMode(permission.ModeBypass),
zelk.WithObserver(zelkotel.New(tp, nil)))
if err != nil {
panic(err)
}
s, err := a.Session(ctx, "traced-1")
if err != nil {
panic(err)
}
x, err := s.Start(ctx, zelk.WithCommands(zelk.UserMessage("Say hi.")))
if err != nil {
panic(err)
}
if _, err := x.Wait(ctx); err != nil {
panic(err)
}
if err := a.Close(ctx); err != nil {
panic(err)
}
if err := tp.Shutdown(ctx); err != nil {
panic(err)
}
var names []string
for _, s := range spans.Ended() {
names = append(names, s.Name())
}
slices.Sort(names)
fmt.Println(slices.Compact(names))
// Output: [zelk.execution zelk.model_call zelk.turn]

Output:

[zelk.execution zelk.model_call zelk.turn]
func (o *Observer) Add(ctx context.Context, name string, n int64, as ...observe.Attr)

Add adds n to the counter named metric.

func (o *Observer) StartSpan(ctx context.Context, name string, opts observe.SpanOptions) (context.Context, observe.Span)

StartSpan starts an OpenTelemetry span under opts.Parent if valid, else under the span in ctx, if any.