github.com/thanos-io/thanos@v0.32.5/internal/cortex/util/log/wrappers.go (about)

     1  // Copyright (c) The Cortex Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package log
     5  
     6  import (
     7  	"context"
     8  
     9  	kitlog "github.com/go-kit/log"
    10  	"github.com/weaveworks/common/tracing"
    11  
    12  	"github.com/thanos-io/thanos/internal/cortex/tenant"
    13  )
    14  
    15  // WithUserID returns a Logger that has information about the current user in
    16  // its details.
    17  func WithUserID(userID string, l kitlog.Logger) kitlog.Logger {
    18  	// See note in WithContext.
    19  	return kitlog.With(l, "org_id", userID)
    20  }
    21  
    22  // WithTraceID returns a Logger that has information about the traceID in
    23  // its details.
    24  func WithTraceID(traceID string, l kitlog.Logger) kitlog.Logger {
    25  	// See note in WithContext.
    26  	return kitlog.With(l, "traceID", traceID)
    27  }
    28  
    29  // WithContext returns a Logger that has information about the current user in
    30  // its details.
    31  //
    32  // e.g.
    33  //
    34  //	log := util.WithContext(ctx)
    35  //	log.Errorf("Could not chunk chunks: %v", err)
    36  func WithContext(ctx context.Context, l kitlog.Logger) kitlog.Logger {
    37  	// Weaveworks uses "orgs" and "orgID" to represent Cortex users,
    38  	// even though the code-base generally uses `userID` to refer to the same thing.
    39  	userID, err := tenant.TenantID(ctx)
    40  	if err == nil {
    41  		l = WithUserID(userID, l)
    42  	}
    43  
    44  	traceID, ok := tracing.ExtractSampledTraceID(ctx)
    45  	if !ok {
    46  		return l
    47  	}
    48  
    49  	return WithTraceID(traceID, l)
    50  }