Documentation
¶
Overview ¶
Package slog implements the Crucible telemetry interfaces on top of the standard library's log/slog. It proves the telemetry seam end to end using zero external dependencies: spans and metric instruments are emitted as structured log records.
Import path: github.com/stablekernel/crucible/telemetry/slog
It is intended for development, tests, and environments where structured logs are the only observability sink. For production tracing/metrics backends, use (or write) an otel or datadog adapter against the same interfaces.
Emission shape ¶
- Tracer.Start logs at debug with msg "span.start" and a "span" group carrying name, attributes, and a generated span id. The returned context carries the span id so nested spans log a "span.parent" id, reproducing span parentage in the logs.
- Span.End logs "span.end" with name, id, status, and elapsed duration.
- Span.RecordError logs at error with "span.error".
- Counter/Histogram/Gauge log at debug with msg "metric" and a "metric" group carrying name, kind, value, unit, and attributes.
All durations are sourced from an injectable clock so emission is deterministic in tests.
Example ¶
Example wires the slog adapter into a consuming module's telemetry Provider, so spans and metrics are emitted as structured logs with no external dependency.
package main
import (
"context"
"log/slog"
"os"
"github.com/stablekernel/crucible/telemetry"
crucibleslog "github.com/stablekernel/crucible/telemetry/slog"
)
func main() {
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
// Drop time and the elapsed duration so the example output is stable.
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey && len(groups) == 0 {
return slog.Attr{}
}
if a.Key == "elapsed" {
return slog.Attr{}
}
return a
},
}))
tel := telemetry.Nop().Apply(
telemetry.WithTracer(crucibleslog.NewTracer(crucibleslog.WithLogger(logger))),
telemetry.WithMeter(crucibleslog.NewMeter(crucibleslog.WithLogger(logger))),
)
ctx, span := tel.Tracer.Start(context.Background(), "sink.Sink")
tel.Meter.Counter("sink.sunk").Add(ctx, 1)
span.End()
}
Output: level=DEBUG msg=span.start span.name=sink.Sink span.id=1 level=DEBUG msg=metric metric.name=sink.sunk metric.kind=counter metric.value=1 level=DEBUG msg=span.end span.name=sink.Sink span.id=1 span.status=unset
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Meter ¶
type Meter struct {
// contains filtered or unexported fields
}
Meter is a telemetry.Meter backed by slog.
type Option ¶
type Option func(*config)
Option configures the adapter.
func WithClock ¶
WithClock sets the time source used for span durations. The default is time.Now. Injecting a clock makes emitted durations deterministic in tests.
func WithIDFn ¶
WithIDFn sets the monotonic span-id generator. The default is an internal atomic counter starting at 1. Injecting it makes emitted span ids deterministic in tests.
func WithLogger ¶
WithLogger sets the slog.Logger the adapter emits to. The default discards all records (slog.New(slog.DiscardHandler)), keeping the adapter silent until a real logger is supplied.