mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-27 06:38:46 +00:00
feat(logging): add streams (#11435)
# Which Problems Are Solved Streams allow differentiating logs produced by different components of Zitadel. # How the Problems Are Solved The `backend/v3/instrumentation/logging` package now exposes convenience function for setting and getting a logger from the context. As well as high-level functions to emit log records at various levels. When constructing a new logger a "stream" needs to be specified: - **runtime**: General runtime logs, such as startup and shutdown messages. Default for logs that do not belong to the other categories. - **request**: Logs for incoming API and HTTP requests. - **event_handler**: Logs for event handling in projections. - **queue**: Logs for the job queue processing. - **event_pusher**: Logs for event pushing to the database. Disabled by default, contains sensitive information. Each line from the returned logger contains a `stream` field as well as a `version` field with the current Zitadel version. ## Runtime config Streams can be enabled by passing an array of stream names in the runtime config. Because some log streams may contain sensitive data (especially events), it is now also possible to mask values by their key. # Additional Changes - Wrap `slogctx` in the `logging` package. (Except API error converter packages, because of import cycle) - Add some docs to `logging` package so other devs understand how to add logging to Zitadel - Add `logging.OnError` and `logging.WithError` helper functions with `Panic()` and `Fatal()` methods, to preserve current calls in the `cmd` packages. - Add instance context extractor. - Only output request details in the request info log. Request ID remains propagated through context. - Moved middleware functionality into protocol specific packages. - Removed setting of URI to context in metric middleware. There were ony setters and no getters. (Unused value) - Reuse a single statusWriter in the middleware package for middlewares that need to know the response status. # Additional Context - Closes #11333 - Closes #11331 - Partly #11330 --------- Co-authored-by: Silvan <[email protected]>
This commit is contained in:
+116
-102
@@ -3,22 +3,20 @@ package instrumentation
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"slices"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"connectrpc.com/connect"
|
||||
"github.com/rs/xid"
|
||||
slogmulti "github.com/samber/slog-multi"
|
||||
slogctx "github.com/veqryn/slog-context"
|
||||
slogotel "github.com/veqryn/slog-context/otel"
|
||||
old_logging "github.com/zitadel/logging" //nolint:staticcheck
|
||||
"github.com/zitadel/sloggcp"
|
||||
"go.opentelemetry.io/contrib/bridges/otelslog"
|
||||
"go.opentelemetry.io/otel/sdk/log"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
http_util "github.com/zitadel/zitadel/internal/api/http"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
@@ -27,7 +25,7 @@ type LogFormat int
|
||||
//go:generate enumer -type=LogFormat -trimprefix=LogFormat -text -linecomment -transform=snake
|
||||
const (
|
||||
// Empty line comment sets empty string of unspecified value
|
||||
LogFormatUndefined LogFormat = iota //
|
||||
LogFormatUnspecified LogFormat = iota //
|
||||
LogFormatDisabled
|
||||
LogFormatText
|
||||
LogFormatJSON
|
||||
@@ -36,19 +34,74 @@ const (
|
||||
LogFormatGCPErrorReporting
|
||||
)
|
||||
|
||||
func (f LogFormat) isDisabled() bool {
|
||||
return f == LogFormatUnspecified || f == LogFormatDisabled
|
||||
}
|
||||
|
||||
type LogConfig struct {
|
||||
Level slog.Level
|
||||
Streams []Stream
|
||||
Mask MaskConfig
|
||||
Format LogFormat
|
||||
AddSource bool
|
||||
Errors ErrorConfig
|
||||
Exporter ExporterConfig
|
||||
}
|
||||
|
||||
func (c *LogConfig) SetLegacyConfig(lc *old_logging.Config) {
|
||||
if lc == nil || !c.Format.isDisabled() {
|
||||
return
|
||||
}
|
||||
err := c.Level.UnmarshalText([]byte(lc.Level))
|
||||
if err != nil {
|
||||
c.Level = slog.LevelInfo
|
||||
}
|
||||
c.Format, err = LogFormatString(lc.Formatter.Format)
|
||||
if err != nil {
|
||||
c.Format = LogFormatText
|
||||
}
|
||||
c.AddSource = lc.AddSource
|
||||
}
|
||||
|
||||
func (c LogConfig) replacer() replacer {
|
||||
var replacers []replacer
|
||||
if len(c.Mask.Keys) > 0 {
|
||||
replacers = append(replacers, c.Mask.replacer())
|
||||
}
|
||||
if c.Format == LogFormatGCP {
|
||||
replacers = append(replacers, sloggcp.ReplaceAttr)
|
||||
}
|
||||
if c.Format == LogFormatGCPErrorReporting {
|
||||
replacers = append(replacers, errReplacer())
|
||||
}
|
||||
return chainReplacers(replacers...)
|
||||
}
|
||||
|
||||
type MaskConfig struct {
|
||||
Keys []string
|
||||
Value string
|
||||
}
|
||||
|
||||
func (c MaskConfig) replacer() replacer {
|
||||
return func(_ []string, a slog.Attr) slog.Attr {
|
||||
if slices.Contains(c.Keys, a.Key) {
|
||||
a.Value = slog.StringValue(c.Value)
|
||||
}
|
||||
return a
|
||||
}
|
||||
}
|
||||
|
||||
type ErrorConfig struct {
|
||||
ReportLocation bool
|
||||
StackTrace bool
|
||||
}
|
||||
|
||||
var addSourceEnabled atomic.Bool
|
||||
|
||||
func IsAddSourceEnabled() bool {
|
||||
return addSourceEnabled.Load()
|
||||
}
|
||||
|
||||
// setLogger configures the global slog logger.
|
||||
// Logs are sent to [os.Stderr] and/or the [log.LoggerProvider].
|
||||
//
|
||||
@@ -60,12 +113,15 @@ type ErrorConfig struct {
|
||||
// - Request details, such as method and path.
|
||||
func setLogger(provider *log.LoggerProvider, cfg LogConfig) {
|
||||
options := &slog.HandlerOptions{
|
||||
AddSource: cfg.AddSource,
|
||||
Level: cfg.Level,
|
||||
AddSource: cfg.AddSource,
|
||||
Level: cfg.Level,
|
||||
ReplaceAttr: cfg.replacer(),
|
||||
}
|
||||
addSourceEnabled.Store(cfg.AddSource)
|
||||
|
||||
var stdErrHandler slog.Handler
|
||||
switch cfg.Format {
|
||||
case LogFormatUndefined:
|
||||
case LogFormatUnspecified:
|
||||
return // uses default slog logger
|
||||
case LogFormatDisabled:
|
||||
stdErrHandler = slog.DiscardHandler
|
||||
@@ -74,14 +130,13 @@ func setLogger(provider *log.LoggerProvider, cfg LogConfig) {
|
||||
case LogFormatJSON:
|
||||
stdErrHandler = slog.NewJSONHandler(os.Stderr, options)
|
||||
case LogFormatGCP:
|
||||
options.ReplaceAttr = sloggcp.ReplaceAttr
|
||||
stdErrHandler = slog.NewJSONHandler(os.Stderr, options)
|
||||
case LogFormatGCPErrorReporting:
|
||||
zerrors.GCPErrorReportingEnabled(true)
|
||||
options.ReplaceAttr = replaceErrAttr
|
||||
stdErrHandler = sloggcp.NewErrorReportingHandler(os.Stderr, options)
|
||||
}
|
||||
|
||||
EnableStreams(cfg.Streams...)
|
||||
zerrors.EnableReportLocation(cfg.Errors.ReportLocation)
|
||||
zerrors.EnableStackTrace(cfg.Errors.StackTrace)
|
||||
|
||||
@@ -89,8 +144,8 @@ func setLogger(provider *log.LoggerProvider, cfg LogConfig) {
|
||||
stdErrHandler,
|
||||
&slogctx.HandlerOptions{
|
||||
Prependers: []slogctx.AttrExtractor{
|
||||
domainExtractor,
|
||||
requestExtractor,
|
||||
instanceExtractor,
|
||||
requestIDExtractor,
|
||||
slogotel.ExtractTraceSpanID,
|
||||
slogctx.ExtractPrepended,
|
||||
},
|
||||
@@ -113,106 +168,65 @@ const (
|
||||
ProtocolGrpc = "grpc"
|
||||
)
|
||||
|
||||
// SetHttpRequestDetails adds static details to each context aware log entry.
|
||||
func SetHttpRequestDetails(ctx context.Context, service string, request *http.Request) context.Context {
|
||||
now := time.Now()
|
||||
return context.WithValue(ctx, ctxKey{}, &requestDetails{
|
||||
protocol: ProtocolHttp,
|
||||
service: service,
|
||||
http_method: request.Method,
|
||||
path: request.URL.Path,
|
||||
requestID: xid.NewWithTime(now),
|
||||
start: now,
|
||||
})
|
||||
// Instance is a minimal interface for logging the instance ID.
|
||||
type Instance interface {
|
||||
InstanceID() string
|
||||
}
|
||||
|
||||
// SetConnectRequestDetails adds static details to each context aware log entry.
|
||||
func SetConnectRequestDetails(ctx context.Context, request connect.AnyRequest) context.Context {
|
||||
now := time.Now()
|
||||
spec := request.Spec()
|
||||
return context.WithValue(ctx, ctxKey{}, &requestDetails{
|
||||
protocol: ProtocolConnect,
|
||||
service: serviceFromRPCMethod(spec.Procedure),
|
||||
http_method: request.HTTPMethod(),
|
||||
path: spec.Procedure,
|
||||
requestID: xid.NewWithTime(now),
|
||||
start: now,
|
||||
})
|
||||
// SetInstance adds the instance to the context for logging.
|
||||
func SetInstance(ctx context.Context, instance Instance) context.Context {
|
||||
return context.WithValue(ctx, ctxKeyInstance, instance)
|
||||
}
|
||||
|
||||
func SetGrpcRequestDetails(ctx context.Context, info *grpc.UnaryServerInfo) context.Context {
|
||||
now := time.Now()
|
||||
return context.WithValue(ctx, ctxKey{}, &requestDetails{
|
||||
protocol: ProtocolGrpc,
|
||||
service: serviceFromRPCMethod(info.FullMethod),
|
||||
http_method: http.MethodPost, // gRPC always uses POST
|
||||
path: info.FullMethod,
|
||||
requestID: xid.NewWithTime(now),
|
||||
start: now,
|
||||
})
|
||||
// SetRequest generates a new [xid.ID] based on the passed request timestamp
|
||||
// and adds it to the context.
|
||||
func SetRequestID(ctx context.Context, ts time.Time) context.Context {
|
||||
return context.WithValue(ctx, ctxKeyRequestID, xid.NewWithTime(ts))
|
||||
}
|
||||
|
||||
type ctxKey struct{}
|
||||
type ctxKeyType int
|
||||
|
||||
type requestDetails struct {
|
||||
protocol string
|
||||
service string
|
||||
http_method string
|
||||
path string
|
||||
requestID xid.ID
|
||||
start time.Time
|
||||
}
|
||||
const (
|
||||
ctxKeyRequestID ctxKeyType = iota
|
||||
ctxKeyInstance
|
||||
)
|
||||
|
||||
func (r *requestDetails) attrs() []slog.Attr {
|
||||
attrs := make([]slog.Attr, 0, 6)
|
||||
if r.protocol != "" {
|
||||
attrs = append(attrs, slog.String("protocol", r.protocol))
|
||||
}
|
||||
if r.service != "" {
|
||||
attrs = append(attrs, slog.String("service", r.service))
|
||||
}
|
||||
if r.http_method != "" {
|
||||
attrs = append(attrs, slog.String("http_method", r.http_method))
|
||||
}
|
||||
if r.path != "" {
|
||||
attrs = append(attrs, slog.String("path", r.path))
|
||||
}
|
||||
if !r.requestID.IsZero() {
|
||||
attrs = append(attrs, slog.String("request_id", r.requestID.String()))
|
||||
}
|
||||
if !r.start.IsZero() {
|
||||
attrs = append(attrs, slog.Duration("duration", time.Since(r.start)))
|
||||
}
|
||||
return attrs
|
||||
}
|
||||
|
||||
func serviceFromRPCMethod(fullMethod string) string {
|
||||
parts := strings.Split(fullMethod, "/")
|
||||
if len(parts) >= 2 {
|
||||
return parts[1]
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// domainExtractor sets the sanitized request hosts from [http_util.DomainCtx] to a log entry.
|
||||
func domainExtractor(ctx context.Context, _ time.Time, _ slog.Level, _ string) []slog.Attr {
|
||||
return []slog.Attr{
|
||||
slog.Any("domain", http_util.DomainContext(ctx)),
|
||||
}
|
||||
}
|
||||
|
||||
// requestExtractor sets the request details from [requestDetails] to a log entry.
|
||||
func requestExtractor(ctx context.Context, _ time.Time, _ slog.Level, _ string) []slog.Attr {
|
||||
if r, ok := ctx.Value(ctxKey{}).(*requestDetails); ok {
|
||||
return r.attrs()
|
||||
// instanceExtractor sets the instance ID from [Instance] to a log entry.
|
||||
func instanceExtractor(ctx context.Context, _ time.Time, _ slog.Level, _ string) []slog.Attr {
|
||||
if instance, ok := ctx.Value(ctxKeyInstance).(Instance); ok {
|
||||
return []slog.Attr{
|
||||
slog.String("instance", instance.InstanceID()),
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// replaceErrAttr renames the "err" attribute to the Google Cloud Platform compatible error key.
|
||||
func replaceErrAttr(groups []string, a slog.Attr) slog.Attr {
|
||||
if len(groups) == 0 && a.Key == "err" {
|
||||
a.Key = sloggcp.ErrorKey
|
||||
// requestIDExtractor sets the request XID to a log entry.
|
||||
func requestIDExtractor(ctx context.Context, _ time.Time, _ slog.Level, _ string) []slog.Attr {
|
||||
if r, ok := ctx.Value(ctxKeyRequestID).(xid.ID); ok {
|
||||
return []slog.Attr{
|
||||
slog.String("request_id", r.String()),
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type replacer func(groups []string, a slog.Attr) slog.Attr
|
||||
|
||||
func chainReplacers(replacers ...replacer) replacer {
|
||||
return func(groups []string, a slog.Attr) slog.Attr {
|
||||
for _, r := range replacers {
|
||||
a = r(groups, a)
|
||||
}
|
||||
return a
|
||||
}
|
||||
}
|
||||
|
||||
func errReplacer() replacer {
|
||||
return func(groups []string, a slog.Attr) slog.Attr {
|
||||
if len(groups) == 0 && a.Key == "err" {
|
||||
a.Key = sloggcp.ErrorKey
|
||||
}
|
||||
return a
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user