github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/beltctx/context.go (about)

     1  // Copyright 2023 Meta Platforms, Inc. and affiliates.
     2  //
     3  // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     4  //
     5  // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
     6  //
     7  // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
     8  //
     9  // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    10  //
    11  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    12  
    13  package beltctx
    14  
    15  import (
    16  	"context"
    17  
    18  	"github.com/facebookincubator/go-belt"
    19  	"github.com/facebookincubator/go-belt/internal"
    20  	"github.com/facebookincubator/go-belt/pkg/field"
    21  )
    22  
    23  type observerContextKey = internal.BeltCtxKeyType
    24  
    25  var ctxKeyBelt = observerContextKey{}
    26  
    27  // WithBelt returns a context derivative which includes the Belt as a value.
    28  //
    29  // Deprecated: use belt.CtxWithBelt, instead.
    30  func WithBelt(ctx context.Context, belt *belt.Belt) context.Context {
    31  	return context.WithValue(ctx, ctxKeyBelt, belt)
    32  }
    33  
    34  // Belt returns the Belt from context values. Returns the default observer if one is not set in the context.
    35  //
    36  // Deprecated: use belt.CtxBelt, instead.
    37  func Belt(ctx context.Context) *belt.Belt {
    38  	observer := ctx.Value(ctxKeyBelt)
    39  	if observer == nil {
    40  		return belt.Default()
    41  	}
    42  	return observer.(*belt.Belt)
    43  }
    44  
    45  // WithField returns a context with a clone/derivative of the Belt which includes the passed value.
    46  //
    47  // The value is used by observability tooling. For example a Logger derived from the resulting
    48  // Belt may add this value to the structured fields of each log entry.
    49  //
    50  // Deprecated: use belt.WithField, instead.
    51  func WithField(ctx context.Context, key string, value interface{}, props ...field.Property) context.Context {
    52  	return context.WithValue(ctx, ctxKeyBelt, Belt(ctx).WithField(key, value, props...))
    53  }
    54  
    55  // WithFields is the same as WithField, but adds multiple Fields at the same time.
    56  //
    57  // It is more performance efficient than adding fields by one.
    58  //
    59  // Deprecated: use belt.WithFields, instead.
    60  func WithFields(ctx context.Context, fields field.AbstractFields) context.Context {
    61  	return context.WithValue(ctx, ctxKeyBelt, Belt(ctx).WithFields(fields))
    62  }
    63  
    64  // WithMap is just a sugar method, which provides logrus like way of adding fields.
    65  // Effectively the same as WithFields, just the argument are in another format.
    66  //
    67  // Deprecated: use belt.WithMap, instead.
    68  func WithMap(ctx context.Context, m map[string]interface{}, props ...field.Property) context.Context {
    69  	return context.WithValue(ctx, ctxKeyBelt, Belt(ctx).WithMap(m, props...))
    70  }
    71  
    72  // WithTool returns a context with an Belt clone/derivative, but the provided tool
    73  // added to the collection of tools.
    74  //
    75  // Special case: to remove a specific tool, just passed an untyped nil as `tool`.
    76  //
    77  // Deprecated: use belt.WithTool, instead.
    78  func WithTool(ctx context.Context, toolID belt.ToolID, tool belt.Tool) context.Context {
    79  	return context.WithValue(ctx, ctxKeyBelt, Belt(ctx).WithTool(toolID, tool))
    80  }
    81  
    82  // WithTraceID returns a context with an Belt clone/derivative with the passed traceIDs added to the set of TraceIDs.
    83  //
    84  // Deprecated: use belt.WithTraceID, instead.
    85  func WithTraceID(ctx context.Context, traceIDs ...belt.TraceID) context.Context {
    86  	return context.WithValue(ctx, ctxKeyBelt, Belt(ctx).WithTraceID(traceIDs...))
    87  }
    88  
    89  // WithArtifact returns a derivative of the context, but with the Artifact set.
    90  //
    91  // Deprecated: use belt.WithArtifact, instead.
    92  func WithArtifact(ctx context.Context, artifactID belt.ArtifactID, artifact belt.Artifact) context.Context {
    93  	return context.WithValue(ctx, ctxKeyBelt, Belt(ctx).WithArtifact(artifactID, artifact))
    94  }
    95  
    96  // Fields returns returns the set of fields set in the scope of this Belt.
    97  //
    98  // Do not modify the output of this function! It is for reading only.
    99  //
   100  // Deprecated: use belt.GetFields, instead.
   101  func Fields(ctx context.Context) field.AbstractFields {
   102  	return Belt(ctx).Fields()
   103  }
   104  
   105  // Artifacts returns the collection of Artifacts in the scope of the Belt.
   106  //
   107  // Do not modify the output of this function! It is for reading only.
   108  //
   109  // Deprecated: use belt.GetArtifacts, instead.
   110  func Artifacts(ctx context.Context) belt.Artifacts {
   111  	return Belt(ctx).Artifacts()
   112  }
   113  
   114  // TraceIDs returns the current set of TraceID-s.
   115  //
   116  // Do not modify the output of this function! It is for reading only.
   117  //
   118  // Deprecated: use belt.GetTraceIDs, instead.
   119  func TraceIDs(ctx context.Context) belt.TraceIDs {
   120  	return Belt(ctx).TraceIDs()
   121  }
   122  
   123  // Tools returns the current collection of Tools.
   124  //
   125  // Do not modify the output of this function! It is for reading only.
   126  //
   127  // Deprecated: use belt.GetTools, instead.
   128  func Tools(ctx context.Context) belt.Tools {
   129  	return Belt(ctx).Tools()
   130  }
   131  
   132  // Flush forces to flush all buffers of all the tools.
   133  //
   134  // Deprecated: use belt.Flush, instead.
   135  func Flush(ctx context.Context) {
   136  	Belt(ctx).Flush()
   137  }