forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrappers.go
39 lines (32 loc) · 977 Bytes
/
wrappers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package log
import (
"context"
"github.com/go-kit/log"
"github.com/weaveworks/common/tracing"
"github.com/frelon/loki/v2/pkg/tenant"
)
// WithUserID returns a Logger that has information about the current user in
// its details.
func WithUserID(userID string, l log.Logger) log.Logger {
// See note in WithContext.
return log.With(l, "org_id", userID)
}
// WithContext returns a log.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 log.Logger) log.Logger {
// Weaveworks uses "orgs" and "orgID" to represent Cortex users,
// even though the code-base generally uses `userID` to refer to the same thing.
userID, err := tenant.TenantID(ctx)
if err == nil {
l = WithUserID(userID, l)
}
traceID, ok := tracing.ExtractSampledTraceID(ctx)
if !ok {
return l
}
return log.With(l, "traceID", traceID)
}