Skip to content

Send telemetry to the terminal or a file

Set Settings.Exporter to otelcore.Slog and pass it a logger. Each metric data point, each ended span and each log record becomes one slog record, written wherever that logger writes:

logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
s := otelcore.Settings{Enabled: true, Exporter: otelcore.Slog(logger)}

mp, err := metrics.NewProvider(ctx, res, s, metrics.WithInterval(5*time.Second))

Nothing else needs to be running. A cache with two hits and a miss prints:

time=2026-11-11T09:06:53.327Z level=INFO msg=metric name=cache.operations unit={operation} kind=sum value=2 monotonic=true attrs.cache.name=orders attrs.cache.result=hit
time=2026-11-11T09:06:53.327Z level=INFO msg=metric name=cache.operations unit={operation} kind=sum value=1 monotonic=true attrs.cache.name=orders attrs.cache.result=miss

Every field is listed in Slog record fields.

Expect metrics once a minute, or at shutdown

Metrics are exported on the provider's interval, which is metrics.DefaultInterval, 60 seconds, whatever the exporter. Nothing prints before the first export, so a demo that wants to see lines straight away sets a shorter one with metrics.WithInterval. A short-lived program prints its metrics when it calls the provider's Shutdown, which always runs a final export.

Each line is the running total for its series, not the change since the last line, so read the latest one.

Write to a file instead

Any io.Writer behind the handler works. For lines a program can parse, use a JSON handler:

f, err := os.OpenFile("metrics.jsonl", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
if err != nil {
    return err
}

logger := slog.New(slog.NewJSONHandler(f, nil))

The exporter never closes the file. Close it after the provider's Shutdown returns, or the final export has nowhere to go.

A full disk or a closed file comes back as an export error and reaches the OpenTelemetry error handler (otel.SetErrorHandler), because the exporter writes through the logger's handler rather than its methods, which would discard it.

Send all three signals

The same Settings works for every provider:

tp, err := tracing.NewProvider(ctx, res, s, tracing.WithSampling(1.0))
lp, err := logs.NewProvider(ctx, res, s)

A span prints when it ends, and each of its events prints as a line of its own straight after it. An error recorded with spanerr.RecordFailure looks like this:

time=2026-11-11T09:07:05.889Z level=INFO msg=span name=get_order kind=internal status=Error status_message="no such order" duration=10.84µs trace_id=604f6fa2f85e996ef4886598964b6778 span_id=0ddccefa35e2a348
time=2026-11-11T09:07:05.889Z level=INFO msg=span.event name=exception trace_id=604f6fa2f85e996ef4886598964b6778 span_id=0ddccefa35e2a348 attrs.exception.type=order_not_found attrs.exception.message="no such order"

Don't give the logs signal a logger that feeds logs.Handler. Each record would be exported back into the same provider, for ever. Give it a logger with a plain text or JSON handler of its own.

Write OTLP JSON for another tool to read

The slog lines are for people. For a file a program reads back, use otelcore.Writer, which writes the OTLP File Exporter format: one line of OTLP JSON per export. The collector's otlpjsonfile receiver reads it, and so does anything that reads OTLP JSON.

f, err := os.OpenFile("metrics.otlp.jsonl", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
if err != nil {
    return err
}

mp, err := metrics.NewProvider(ctx, res, otelcore.Settings{Enabled: true, Exporter: otelcore.Writer(f)})

Give each signal its own file. The format allows one kind of data per file, so metrics, traces and logs each need a Writer of their own. Nothing stops you passing the same file to all three, and the result is a file no reader accepts.

Each line holds a whole export, so a line can run to kilobytes, and the line carries the resource and scope that the slog lines leave out. The OTEL_EXPORTER_OTLP_* variables have no effect on it, except the temporality preference, which still applies.

Add the service name to every line

Resource attributes such as service.name aren't repeated on each line. Add the ones you want to the logger once:

logger = logger.With("service", "checkout")

Lower the level floor if you filter

Metric and span lines are logged at Info. A handler whose level is set above Info drops them, just as it would any other Info record.