github.com/m3db/m3@v1.5.0/src/query/util/logging/log.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package logging
    22  
    23  import (
    24  	"context"
    25  
    26  	"github.com/pborman/uuid"
    27  	"go.uber.org/zap"
    28  	"go.uber.org/zap/zapcore"
    29  
    30  	"github.com/m3db/m3/src/x/instrument"
    31  )
    32  
    33  type loggerKeyType int
    34  
    35  const (
    36  	loggerKey loggerKeyType = iota
    37  	rqIDKey
    38  
    39  	undefinedID = "undefined"
    40  )
    41  
    42  // NewContext returns a context has a zap logger with the extra fields added.
    43  func NewContext(
    44  	ctx context.Context,
    45  	instrumentOpts instrument.Options,
    46  	fields ...zapcore.Field,
    47  ) context.Context {
    48  	return NewContextWithLogger(ctx, WithContext(ctx, instrumentOpts).With(fields...))
    49  }
    50  
    51  // NewContextWithLogger returns a context with the provided logger set as a context value.
    52  func NewContextWithLogger(ctx context.Context, l *zap.Logger) context.Context {
    53  	return context.WithValue(ctx, loggerKey, l)
    54  }
    55  
    56  // NewContextWithGeneratedID returns a context with a generated id with a zap
    57  // logger and an id field.
    58  func NewContextWithGeneratedID(
    59  	ctx context.Context,
    60  	instrumentOpts instrument.Options,
    61  ) context.Context {
    62  	// Attach a rqID with all logs so that its simple to trace the whole call stack
    63  	rqID := uuid.NewRandom()
    64  	return NewContextWithID(ctx, rqID.String(), instrumentOpts)
    65  }
    66  
    67  // NewContextWithID returns a context which has a zap logger and an id field.
    68  func NewContextWithID(
    69  	ctx context.Context,
    70  	id string,
    71  	instrumentOpts instrument.Options,
    72  ) context.Context {
    73  	ctxWithID := context.WithValue(ctx, rqIDKey, id)
    74  	return context.WithValue(ctxWithID, loggerKey,
    75  		WithContext(ctx, instrumentOpts).With(zap.String("rqID", id)))
    76  }
    77  
    78  // ReadContextID returns the context's id or "undefined".
    79  func ReadContextID(ctx context.Context) string {
    80  	if ctxID, ok := ctx.Value(rqIDKey).(string); ok {
    81  		return ctxID
    82  	}
    83  
    84  	return undefinedID
    85  }
    86  
    87  // WithContext returns a zap logger with as much context as possible.
    88  func WithContext(ctx context.Context, instrumentOpts instrument.Options) *zap.Logger {
    89  	if ctx == nil {
    90  		return instrumentOpts.Logger()
    91  	}
    92  	if ctxLogger, ok := ctx.Value(loggerKey).(*zap.Logger); ok {
    93  		return ctxLogger
    94  	}
    95  
    96  	return instrumentOpts.Logger()
    97  }