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
- func AddLogAttrs(ctx context.Context, attrs ...slog.Attr)
- func LogPanic(ctx context.Context, recovered any)
- func LogRequest(ctx context.Context, levels RequestLevels, request Request)
- func ParseLevel(level string) (slog.Level, error)
- func TextHandler(w io.Writer, minimum slog.Level) slog.Handler
- func WithLogAttrs(ctx context.Context, attrs ...slog.Attr) context.Context
- func WithRequest(ctx context.Context) context.Context
- type Request
- type RequestLevels
- type Shutdown
Constants ¶
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 ¶
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 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 ¶
ParseLevel reads a LOG_LEVEL value: debug, info, warn (or warning) or error, in any case. Empty means info.
func TextHandler ¶
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 ¶
WithLogAttrs returns ctx with attrs added to each record written with it, such as a job's id
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 ¶
Shutdown sends the logs still buffered. Call it last, before the process exits.
func Setup ¶
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 |