github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/experimental/tracer/ctx.go (about)

     1  // Copyright 2022 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 tracer
    14  
    15  import (
    16  	"context"
    17  
    18  	"github.com/facebookincubator/go-belt"
    19  )
    20  
    21  // StartChildSpanFromCtx creates a child span, given a context. The parent span is
    22  // extracted from the Belt of the context. If one is not set, then the function
    23  // returns a new root Span (a Span without a parent).
    24  func StartChildSpanFromCtx(ctx context.Context, name string, options ...SpanOption) (Span, context.Context) {
    25  	return FromCtx(ctx).StartChildWithCtx(ctx, name, options...)
    26  }
    27  
    28  // StartSpanFromCtx creates a new (root) span, given a context.
    29  func StartSpanFromCtx(ctx context.Context, name string, options ...SpanOption) (Span, context.Context) {
    30  	return FromCtx(ctx).StartWithCtx(ctx, name, options...)
    31  }
    32  
    33  // SpanFromCtx returns the current span, given a context. Returns a NoopSpan if
    34  // one is not set.
    35  func SpanFromCtx(ctx context.Context) Span {
    36  	return SpanFromBelt(belt.CtxBelt(ctx))
    37  }
    38  
    39  // CtxWithSpan returns a context derivative/clone with the specified Span.
    40  func CtxWithSpan(ctx context.Context, span Span) context.Context {
    41  	return belt.CtxWithBelt(ctx, BeltWithSpan(belt.CtxBelt(ctx), span))
    42  }
    43  
    44  // FromCtx returns a Tracer, given a context.
    45  func FromCtx(ctx context.Context) Tracer {
    46  	return FromBelt(belt.CtxBelt(ctx))
    47  }
    48  
    49  // CtxWithTracer returns a context derivative/clone with the specified Tracer.
    50  func CtxWithTracer(ctx context.Context, tracer Tracer) context.Context {
    51  	return belt.WithTool(ctx, ToolID, tracer)
    52  }