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 FetchValueContext(ctx context.Context, p HeadfieldPair) string
- func HeadfieldPairMiddleware(pairs []HeadfieldPair) func(http.Handler) http.Handler
- func NewExportHandler(export func(context.Context, []slog.Attr), opts ...ExporterOption) slog.Handler
- func NewMiddleware(logger *slog.Logger) func(http.Handler) http.Handler
- func NewMiddlewareWithConfig(logger *slog.Logger, config MiddlewareConfig) func(http.Handler) http.Handler
- func NewMiddlewareWithFilters(logger *slog.Logger, filters ...Filter) func(http.Handler) http.Handler
- func RecoverPanicMiddleware(logger *slog.Logger) func(http.Handler) http.Handler
- func SetLogger(lg *slog.Logger)
- func SetNoopLogger()
- func UniqAttrs(attrs []slog.Attr) []slog.Attr
- 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 Filter
- func Accept(filter Filter) Filter
- func AcceptHost(hosts ...string) Filter
- func AcceptHostMatch(regs ...regexp.Regexp) Filter
- func AcceptMethod(methods ...string) Filter
- func AcceptPath(urls ...string) Filter
- func AcceptPathContains(parts ...string) Filter
- func AcceptPathMatch(regs ...regexp.Regexp) Filter
- func AcceptPathPrefix(prefixs ...string) Filter
- func AcceptPathSuffix(prefixs ...string) Filter
- func AcceptStatus(statuses ...int) Filter
- func Ignore(filter Filter) Filter
- func IgnoreHost(hosts ...string) Filter
- func IgnoreHostMatch(regs ...regexp.Regexp) Filter
- func IgnoreMethod(methods ...string) Filter
- func IgnorePath(urls ...string) Filter
- func IgnorePathContains(parts ...string) Filter
- func IgnorePathMatch(regs ...regexp.Regexp) Filter
- func IgnorePathPrefix(prefixs ...string) Filter
- func IgnorePathSuffix(suffixs ...string) Filter
- func IgnoreStatus(statuses ...int) Filter
- type HeadfieldPair
- type HttpRequestDoer
- type MiddlewareConfig
- type MultiCallback
- type MultiHandler
- type NoopHandler
- type Span
- type SpanID
- type TraceID
- type TracingDoer
- type TracingDoerConfig
- type WrapResponseWriter
Constants ¶
const ( 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
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 FetchValueContext ¶
func FetchValueContext(ctx context.Context, p HeadfieldPair) string
func HeadfieldPairMiddleware ¶
func HeadfieldPairMiddleware(pairs []HeadfieldPair) func(http.Handler) http.Handler
HeadfieldPairMiddleware is a middleware that extracts values from headers and stores them in the context.
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 NewMiddleware ¶
NewMiddleware returns a `func(http.Handler) http.Handler` (middleware) that logs requests using slog.
Requests with errors are logged using slog.Error(). Requests without errors are logged using slog.Info().
func NewMiddlewareWithConfig ¶
func NewMiddlewareWithConfig(logger *slog.Logger, config MiddlewareConfig) func(http.Handler) http.Handler
NewMiddlewareWithConfig returns a `func(http.Handler) http.Handler` (middleware) that logs requests using slog.
func NewMiddlewareWithFilters ¶
func NewMiddlewareWithFilters(logger *slog.Logger, filters ...Filter) func(http.Handler) http.Handler
NewMiddlewareWithFilters returns a `func(http.Handler) http.Handler` (middleware) that logs requests using slog.
Requests with errors are logged using slog.Error(). Requests without errors are logged using slog.Info().
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 SetNoopLogger ¶
func SetNoopLogger()
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 Filter ¶
type Filter func(w WrapResponseWriter, r *http.Request) bool
Filter is a function that determines whether a request should be logged or not.
func AcceptHost ¶
AcceptHost returns a filter that accepts requests with the given hosts.
func AcceptHostMatch ¶
AcceptHostContains returns a filter that accepts requests with hosts that contain the given regular expression.
func AcceptMethod ¶
AcceptMethod returns a filter that accepts requests with the given methods.
func AcceptPath ¶
AcceptPath returns a filter that accepts requests with the given paths.
func AcceptPathContains ¶
AcceptPathContains returns a filter that accepts requests with paths that contain the given parts.
func AcceptPathMatch ¶
AcceptPathMatch returns a filter that accepts requests with paths that match the given regular expressions.
func AcceptPathPrefix ¶
AcceptPathPrefix returns a filter that accepts requests with paths that have the given prefixes.
func AcceptPathSuffix ¶
AcceptPathSuffix returns a filter that accepts requests with paths that have the given suffixes.
func AcceptStatus ¶
AcceptStatus returns a filter that accepts requests with the given status codes.
func IgnoreHost ¶
IgnoreHost returns a filter that ignores requests with the given hosts.
func IgnoreHostMatch ¶
IgnoreHostContains returns a filter that ignores requests with hosts that contain the given regular expression.
func IgnoreMethod ¶
IgnoreMethod returns a filter that ignores requests with the given methods.
func IgnorePath ¶
IgnorePath returns a filter that ignores requests with the given paths.
func IgnorePathContains ¶
IgnorePathContains returns a filter that ignores requests with paths that contain the given parts.
func IgnorePathMatch ¶
IgnorePathMatch returns a filter that ignores requests with paths that match the given regular expressions.
func IgnorePathPrefix ¶
IgnorePathPrefix returns a filter that ignores requests with paths that have the given prefixes.
func IgnorePathSuffix ¶
IgnorePathSuffix returns a filter that ignores requests with paths that have the given suffixes.
func IgnoreStatus ¶
IgnoreStatus returns a filter that ignores requests with the given status codes.
type HeadfieldPair ¶
HeadfieldPair 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
// SpanName is the name of the span. Defaults to "http request".
SpanName string
// WithUserAgent enables logging of the User-Agent header. Defaults to false.
WithUserAgent bool
// WithRequestBody enables logging of the request body. Defaults to false.
WithRequestBody bool
// WithRequestHeader enables logging of the request headers. Defaults to false.
WithRequestHeader bool
// WithResponseBody enables logging of the response body. Defaults to false.
WithResponseBody bool
// WithResponseHeader enables logging of the response headers. Defaults to false.
WithResponseHeader bool
// WithSpanID enables logging of the span ID. Defaults to false.
WithTraceID bool
// WithSpanID enables logging of the span ID. Defaults to false.
WithSpanID bool
// Filters is a list of filters to apply before logging. Optional.
Filters []Filter
}
type MultiCallback ¶
func HeadfieldPairCallback ¶
func HeadfieldPairCallback(pairs []HeadfieldPair) MultiCallback
HeadfieldPairCallback 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 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 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