Documentation
¶
Overview ¶
A simple tracing library via log/slog.
Index ¶
- Constants
- Variables
- func AddSpanIDHeader(ctx context.Context, req *http.Request)
- func AddTraceIDHeader(ctx context.Context, req *http.Request)
- func EchoContextSetLogger(logger *slog.Logger) echo.MiddlewareFunc
- func EchoHeadersExtractor(pairs []HeaderField) echo.MiddlewareFunc
- func EchoRequestLogger(logger *slog.Logger, config MiddlewareConfig) echo.MiddlewareFunc
- func EchoTraceExtractor() echo.MiddlewareFunc
- func NewExportHandler(export func(context.Context, []slog.Attr), opts ...ExporterOption) slog.Handler
- func RecoverPanicMiddleware(logger *slog.Logger) func(http.Handler) http.Handler
- func SetLogger(logger *slog.Logger)
- func SetNoopLogger()
- func UniqAttrs(attrs []slog.Attr) []slog.Attr
- func WithSpan(ctx context.Context, span *Span) context.Context
- func WithSpanID(ctx context.Context, spanID SpanID) context.Context
- func WithTraceID(ctx context.Context, traceID TraceID) context.Context
- type DoerErr
- type ExportHandler
- type ExporterOption
- type HeaderField
- type HttpRequestDoer
- type MiddlewareConfig
- type MultiCallback
- type MultiHandler
- type NoopHandler
- type Span
- type SpanID
- type TraceID
- type Tracer
- type TracingDoer
- type TracingDoerConfig
Constants ¶
const ( TraceHTTPHeaderName = "X-Strc-Trace-ID" SpanHTTPHeaderName = "X-Strc-Span-ID" EmptyTraceID TraceID = TraceID("000000000000000") EmptySpanID SpanID = SpanID("0000000.0000000") )
Variables ¶
var ( TraceIDKey = "trace_id" SpanIDKey = "span_id" RequestBodyMaxSize = 64 * 1024 // 64KB ResponseBodyMaxSize = 64 * 1024 // 64KB HiddenRequestHeaders = map[string]struct{}{ "authorization": {}, "cookie": {}, "set-cookie": {}, "x-auth-token": {}, "x-csrf-token": {}, "x-xsrf-token": {}, } HiddenResponseHeaders = map[string]struct{}{ "set-cookie": {}, } )
var ( // TraceIDFieldKey is the key used to store the trace ID in the log record by MultiHandler. // Set to empty string to disable this feature. TraceIDFieldKey = "trace_id" // BuildIDFieldKey is the key used to store the git commit in the log record by MultiHandler. // Set to empty string to disable this feature. BuildIDFieldKey = "build_id" )
var Level slog.Level = slog.LevelDebug
Level is the log level used for trace logging.
var ParentIDName string = "parent"
ParentIDName is the key name used for trace ID.
var SkipSource bool
SkipSource is a flag that disables source logging.
var SpanGroupName string = "span"
SpanGroupName is the group name used for span attributes.
var SpanIDName string = "id"
SpanIDName is the key name used for trace ID.
var TraceIDName string = "trace"
TraceIDName is the key name used for trace ID.
Functions ¶
func AddSpanIDHeader ¶
AddSpanIDHeader adds span ID from context to a request header. If span ID is not found in the context or if the request already has a span ID header, it does nothing.
func AddTraceIDHeader ¶
AddTraceIDHeader adds trace ID from context to a request header. If trace ID is not found in the context or if the request already has a trace ID header, it does nothing.
func EchoContextSetLogger ¶ added in v0.0.8
func EchoContextSetLogger(logger *slog.Logger) echo.MiddlewareFunc
This sets the logger for each request to the specified logger. Anything processing the cecho.Context can just call echo.Context.Logger() to get the appropriate logger.
Meant to be chained after middlewares that add fields to the request context.
func EchoHeadersExtractor ¶ added in v0.0.8
func EchoHeadersExtractor(pairs []HeaderField) echo.MiddlewareFunc
EchoHeadersExtractor is a middleware that extracts values from headers and stores them in the context.
func EchoRequestLogger ¶ added in v0.0.8
func EchoRequestLogger(logger *slog.Logger, config MiddlewareConfig) echo.MiddlewareFunc
This generates exactly one log statement per request processed.
Meant to be chained after middlewares that add fields to the request context.
func EchoTraceExtractor ¶ added in v0.0.8
func EchoTraceExtractor() echo.MiddlewareFunc
EchoTraceExtractor extracts trace IDs and span IDs from HTTP headers and sets them in the request context.
Meant to be chained before any logging middleware.
func NewExportHandler ¶
func NewExportHandler(export func(context.Context, []slog.Attr), opts ...ExporterOption) slog.Handler
NewExportHandler creates a new Exporter with the given callback function and options.
func RecoverPanicMiddleware ¶
RecoverPanicMiddleware is a middleware that recovers from panics and logs them using slog as errors with status code 500. No body is returned.
func WithSpanID ¶
Start returns a new context with span ID.
Types ¶
type DoerErr ¶
type DoerErr struct {
Err error
}
DoerErr is a simple wrapped error without any message. Additional message would stack for each request as multiple doers are called leading to:
"error in doer1: error in doer2: error in doer3: something happened"
func NewDoerErr ¶
type ExportHandler ¶
type ExportHandler struct {
// contains filtered or unexported fields
}
ExportHandler is an slog.Handler which provides a callback function that allows for exporting attributes (slog.Attr) to a different system. This handler is not optimized for performance, it is recommended to write a dedicated slog.Handler. For more information on the topic, see https://github.com/golang/example/blob/master/slog-handler-guide/README.md
type ExporterOption ¶
type ExporterOption func(*ExportHandler)
func IncludeTime ¶
func IncludeTime() ExporterOption
IncludeTime is an ExporterOption that includes the time in the exported attributes.
type HeaderField ¶ added in v0.0.8
HeaderField is a pair of header name and field name.
type HttpRequestDoer ¶
type MiddlewareConfig ¶
type MiddlewareConfig struct {
// DefaultLevel is the default log level for requests. Defaults to Info.
DefaultLevel slog.Level
// ClientErrorLevel is the log level for requests with client errors (4xx). Defaults to Warn.
ClientErrorLevel slog.Level
// ServerErrorLevel is the log level for requests with server errors (5xx). Defaults to Error.
ServerErrorLevel slog.Level
}
type MultiCallback ¶
func HeadersCallback ¶ added in v0.0.8
func HeadersCallback(pairs []HeaderField) MultiCallback
HeadersCallback is a slog callback that extracts values from the context and adds them to the attributes.
type MultiHandler ¶
type MultiHandler struct {
// contains filtered or unexported fields
}
MultiHandler distributes records to multiple slog.Handler
func NewMultiHandler ¶
func NewMultiHandler(handlers ...slog.Handler) *MultiHandler
NewMultiHandler distributes records to multiple slog.Handler
func NewMultiHandlerCustom ¶
func NewMultiHandlerCustom(attrs []slog.Attr, callback MultiCallback, handlers ...slog.Handler) *MultiHandler
NewMultiHandlerCustom distributes records to multiple slog.Handler with custom attributes and callback. Pass static slice of attributes added to the every record, and a callback that can add dynamic attributes from the context. No custom fields are added to the "span" group.
type NoopHandler ¶
type NoopHandler struct {
}
NoopHandler does nothing.
type Span ¶
type Span struct {
// contains filtered or unexported fields
}
Span represents a span of a trace. It is used to log events and end the span. It is a lightweight object and can be passed around in contexts.
func SpanFromContext ¶ added in v0.0.4
Start returns span from a context or nil if it was not found.
func Start ¶
Start starts a new span with the given name and optional arguments. All arguments are present in subsequent Event and End calls. Must call End to finish the span.
span, ctx := strc.Start(ctx, "calculating something big") defer span.End()
Avoid adding complex arguments to spans as they are added to the context.
It immediately logs a message with the span name, span information in SpanGroupName and optional arguments.
Special argument named "started" of type time.Time can be used to set the start time of the span.
func (*Span) End ¶
End finishes the span and logs the duration of the span. Optional arguments can be provided to log additional information.
It immediately logs a message with the span name, span information in SpanGroupName and optional arguments.
Special argument named "finished" of type time.Time can be used to set the finish time of the span.
type SpanID ¶
type SpanID string
SpanID is a unique identifier for a trace.
func SpanIDFromContext ¶
Start returns span ID from a context. It returns EmptySpanID if span ID is not found. Use NewSpanID to generate a new span ID.
func SpanIDFromRequest ¶
SpanIDFromRequest returns span ID from a request. If span ID is not found, it returns EmptySpanID.
type TraceID ¶
type TraceID string
TraceID is a unique identifier for a trace.
func TraceIDFromContext ¶
Start returns trace ID from a context. It returns EmptyTraceID if trace ID is not found. Use NewTraceID to generate a new trace ID.
func TraceIDFromRequest ¶
TraceIDFromRequest returns trace ID from a request. If trace ID is not found, it returns EmptyTraceID.
type Tracer ¶ added in v0.0.4
type Tracer struct {
// contains filtered or unexported fields
}
Tracer is a wrapper for slog.Logger which logs into the initialized slog. Use strc.Start and End package functions to use slog.Default() logger.
type TracingDoer ¶
type TracingDoer struct {
// contains filtered or unexported fields
}
TracingDoer is a http client doer that adds tracing to the request and response.
func NewTracingDoer ¶
func NewTracingDoer(doer HttpRequestDoer) *TracingDoer
NewTracingDoer returns a new TracingDoer.
func NewTracingDoerWithConfig ¶
func NewTracingDoerWithConfig(doer HttpRequestDoer, config TracingDoerConfig) *TracingDoer