telemetry

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 22 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

go-telemetry

Logs for Update Prompt's Go services, through OpenTelemetry.

  • With an OTLP endpoint set, logs go to it as OpenTelemetry logs (OTLP over HTTP).
  • Without one, they go to stdout as text.
  • Every record written while handling a request carries the request's trace_id, span_id and the attributes its handlers added, such as request_id and user_id.
  • Email addresses are replaced with <email> in every record.
  • No spans are recorded or exported: the tracer only gives each request a trace id and passes it on to the services it calls.

Packages

Package What it does
github.com/Update-Prompt/go-telemetry (package telemetry) Setup makes slog's default logger write the service's logs. WithLogAttrs and AddLogAttrs add attributes to the records of a context.
.../go-telemetry/ginlog Middleware that traces and logs each request a gin server answers.
.../go-telemetry/httplog The same for a net/http handler.

Use

shutdown, err := telemetry.Setup(ctx, "usage-service", os.Getenv("LOG_LEVEL"))
if err != nil {
	return err
}
defer shutdown(context.Background()) // sends the logs still buffered

// gin
router := gin.New()
levels := telemetry.RequestLevels{Succeeded: slog.LevelDebug, Refused: slog.LevelInfo}
router.Use(ginlog.Middleware("usage-service", false, levels, onPanic)...)

// net/http
handler := httplog.Handler(mux, "user-management-service", httplog.Options{Levels: levels})

Each request is logged once it is answered, as Answered a request, at:

Answer Level
5xx, or the handler panicked error, with the panic and its stack
4xx RequestLevels.Refused
1xx to 3xx RequestLevels.Succeeded
GET /health not logged or traced

Set public (gin) or Options.Public (net/http) on services callers reach from the internet: a caller's traceparent is then ignored and each request starts its own trace.

Configuration

Variable Default What it does
LOG_LEVEL info Lowest level logged: debug, info, warn or error (the service passes it to Setup)
OTEL_EXPORTER_OTLP_ENDPOINT unset OTLP endpoint for the logs, such as http://localhost:4318; stdout when unset
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT unset The same, for logs only
OTEL_SERVICE_NAME, OTEL_RESOURCE_ATTRIBUTES unset Override the service name and add resource attributes

The other standard OTEL_EXPORTER_OTLP_* variables, such as headers and timeouts, work too.

Develop

go test ./...

Lint with .golangci.yml, a copy of the shared config in shared-actions (go-lint/golangci.yml); keep the two in step. Releases are tags (vX.Y.Z); services pin a version in their go.mod.

Documentation

Overview

Package telemetry sets up a service's logs. With an OTLP endpoint in the standard OTEL_EXPORTER_OTLP_* environment variables, they go to it as OpenTelemetry logs; without one, they go to stdout as text. Either way, a record written while handling a request carries the request's trace id and the attributes its handlers added, so its logs can be found together, also across services. Email addresses are replaced with <email> in every record.

The ginlog and httplog packages log each request a gin or net/http server answers.

Index

Constants

View Source
const HealthPath = "/health"

HealthPath is the health check's path, which the middleware don't trace or log

Variables

This section is empty.

Functions

func AddLogAttrs

func AddLogAttrs(ctx context.Context, attrs ...slog.Attr)

AddLogAttrs adds attrs, such as the request id or the signed-in user's id, to the records written with the request's context from now on, and to the line the request is logged with. Outside a request it does nothing.

func LogPanic

func LogPanic(ctx context.Context, recovered any)

LogPanic logs what a request's handler panicked with, and where

func LogRequest

func LogRequest(ctx context.Context, levels RequestLevels, request Request)

LogRequest writes the line for an answered request, at the level for how it ended, with OpenTelemetry's names for its attributes

func ParseLevel

func ParseLevel(level string) (slog.Level, error)

ParseLevel reads a LOG_LEVEL value: debug, info, warn (or warning) or error, in any case. Empty means info.

func TextHandler

func TextHandler(w io.Writer, minimum slog.Level) slog.Handler

TextHandler writes the records at minimum and above to w as text, with the trace and span ids, as Setup does without an OTLP endpoint. Tests can use it to read what a service logs.

func WithLogAttrs

func WithLogAttrs(ctx context.Context, attrs ...slog.Attr) context.Context

WithLogAttrs returns ctx with attrs added to each record written with it, such as a job's id

func WithRequest

func WithRequest(ctx context.Context) context.Context

WithRequest returns ctx with a place for the attributes AddLogAttrs adds. The ginlog and httplog middleware call it for each request.

Types

type Request

type Request struct {
	Method string
	// Path is the path as it may be logged, such as with an email address replaced
	Path string
	// Route is the route that matched, such as /v1/users/:id, when there is one
	Route    string
	Status   int
	Duration time.Duration
	Panicked bool
}

Request is how a request ended, for its log line

type RequestLevels

type RequestLevels struct {
	// Succeeded is for 1xx to 3xx
	Succeeded slog.Level
	// Refused is for 4xx
	Refused slog.Level
}

RequestLevels are the levels requests are logged at, by how they ended. Failed requests (5xx) and those whose handler panicked are always logged as errors.

type Shutdown

type Shutdown func(context.Context) error

Shutdown sends the logs still buffered. Call it last, before the process exits.

func Setup

func Setup(ctx context.Context, serviceName, level string) (Shutdown, error)

Setup makes slog's default logger write the service's logs, at level and above (debug, info, warn or error; info when empty). It also starts a tracer provider that records and exports no spans: it only gives each request a trace id for its logs, and passes it on to the services the request calls.

Directories

Path Synopsis
Package ginlog traces and logs the requests a gin server answers, with the logs set up by the telemetry package
Package ginlog traces and logs the requests a gin server answers, with the logs set up by the telemetry package
Package httplog traces and logs the requests a net/http server answers, with the logs set up by the telemetry package
Package httplog traces and logs the requests a net/http server answers, with the logs set up by the telemetry package
internal
testlogs
Package testlogs lets the tests read what is logged
Package testlogs lets the tests read what is logged

Jump to

Keyboard shortcuts

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