Files

51 lines
1.2 KiB
Go

package log
import (
"context"
kitlog "github.com/go-kit/log"
"github.com/grafana/dskit/tracing"
"github.com/grafana/dskit/tenant"
)
// WithUserID returns a Logger that has information about the current user in
// its details.
func WithUserID(userID string, l kitlog.Logger) kitlog.Logger {
// See note in WithContext.
return kitlog.With(l, "org_id", userID)
}
// WithTraceID returns a Logger that has information about the traceID in
// its details.
func WithTraceID(traceID string, l kitlog.Logger) kitlog.Logger {
// See note in WithContext.
return kitlog.With(l, "traceID", traceID)
}
// WithContext returns a Logger that has information about the current user in
// its details.
//
// e.g.
// log := util.WithContext(ctx)
// log.Errorf("Could not chunk chunks: %v", err)
func WithContext(ctx context.Context, l kitlog.Logger) kitlog.Logger {
userID, err := tenant.TenantID(ctx)
if err == nil {
l = WithUserID(userID, l)
}
traceID, ok := tracing.ExtractSampledTraceID(ctx)
if !ok {
return l
}
return WithTraceID(traceID, l)
}
// WithSourceIPs returns a Logger that has information about the source IPs in
// its details.
func WithSourceIPs(sourceIPs string, l kitlog.Logger) kitlog.Logger {
return kitlog.With(l, "sourceIPs", sourceIPs)
}