slog

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 30, 2026 License: Apache-2.0 Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

crucible/telemetry/slog

A standard-library log/slog adapter for crucible/telemetry. Emits spans and metric instruments as structured log records, with zero external dependencies.

Import path: github.com/stablekernel/crucible/telemetry/slog

What it is

This adapter implements the telemetry.Tracer and telemetry.Meter interfaces on top of Go's log/slog. It is the reference adapter — it proves the telemetry seam end to end without pulling in any vendor SDK — and is useful for development, tests, and environments where structured logs are the only observability sink.

Because telemetry.Attr is an alias for slog.Attr, the adapter is conversion-free: attributes pass straight to the slog handler with no re-boxing, so the zero-allocation scalar attributes stay zero-allocation through emission.

For production tracing/metrics backends, use (or write) an otel or datadog adapter against the same interfaces.

Usage

The package name is slog, which collides with the standard library's log/slog. Import it under an alias (e.g. crucibleslog) in any file that also imports log/slog:

import (
    "log/slog"

    "github.com/stablekernel/crucible/telemetry"
    crucibleslog "github.com/stablekernel/crucible/telemetry/slog"
)

logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
    Level: slog.LevelDebug, // span/metric records are emitted at DEBUG
}))

tel := telemetry.Nop().Apply(
    telemetry.WithTracer(crucibleslog.NewTracer(crucibleslog.WithLogger(logger))),
    telemetry.WithMeter(crucibleslog.NewMeter(crucibleslog.WithLogger(logger))),
)

Emission shape

Surface Record (msg) Level Key fields
Tracer.Start span.start DEBUG span.name, span.id, span.parent, span.attrs.*
Span.SetAttributes span.attributes DEBUG span.name, span.id, span.attrs.*
Span.RecordError span.error ERROR span.name, span.id, span.error
Span.End span.end DEBUG span.name, span.id, span.status, span.elapsed, span.status_msg
Counter/Histogram/Gauge metric DEBUG metric.name, metric.kind, metric.value, metric.unit, metric.attrs.*

Tracer.Start carries the current span id in the returned context, so a nested span logs its parent's id — reproducing span parentage in the logs.

Options

Option Default Purpose
WithLogger(l) slog.New(slog.DiscardHandler) the slog logger to emit to (silent until set)
WithClock(now) time.Now time source for span durations (inject for deterministic tests)
WithIDFn(next) internal atomic counter monotonic span-id generator (inject for deterministic tests)

Stability

Stability label: experimental (pre-v1).

License

Apache 2.0 — see LICENSE and NOTICE.

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.

func NewMeter

func NewMeter(opts ...Option) *Meter

NewMeter returns a slog-backed Meter.

func (*Meter) Counter

func (m *Meter) Counter(name string, opts ...telemetry.InstrumentOption) telemetry.Counter

Counter returns a counter instrument that logs a "metric" record per Add.

func (*Meter) Gauge

func (m *Meter) Gauge(name string, opts ...telemetry.InstrumentOption) telemetry.Gauge

Gauge returns a gauge instrument that logs a "metric" record per Record.

func (*Meter) Histogram

func (m *Meter) Histogram(name string, opts ...telemetry.InstrumentOption) telemetry.Histogram

Histogram returns a histogram instrument that logs a "metric" record per Record.

type Option

type Option func(*config)

Option configures the adapter.

func WithClock

func WithClock(now func() time.Time) Option

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

func WithIDFn(next func() uint64) Option

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

func WithLogger(l *sl.Logger) Option

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.

type Tracer

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

Tracer is a telemetry.Tracer backed by slog.

func NewTracer

func NewTracer(opts ...Option) *Tracer

NewTracer returns a slog-backed Tracer.

func (*Tracer) Start

func (t *Tracer) Start(ctx context.Context, name string, attrs ...telemetry.Attr) (context.Context, telemetry.Span)

Start logs "span.start" and returns a context carrying the new span's id.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL