github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.opentelemetry.io/otel/trace/context.go (about)

     1  // Copyright The OpenTelemetry Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package trace // import "go.opentelemetry.io/otel/trace"
    16  
    17  import "context"
    18  
    19  type traceContextKeyType int
    20  
    21  const currentSpanKey traceContextKeyType = iota
    22  
    23  // ContextWithSpan returns a copy of parent with span set as the current Span.
    24  func ContextWithSpan(parent context.Context, span Span) context.Context {
    25  	return context.WithValue(parent, currentSpanKey, span)
    26  }
    27  
    28  // ContextWithSpanContext returns a copy of parent with sc as the current
    29  // Span. The Span implementation that wraps sc is non-recording and performs
    30  // no operations other than to return sc as the SpanContext from the
    31  // SpanContext method.
    32  func ContextWithSpanContext(parent context.Context, sc SpanContext) context.Context {
    33  	return ContextWithSpan(parent, nonRecordingSpan{sc: sc})
    34  }
    35  
    36  // ContextWithRemoteSpanContext returns a copy of parent with rsc set explicly
    37  // as a remote SpanContext and as the current Span. The Span implementation
    38  // that wraps rsc is non-recording and performs no operations other than to
    39  // return rsc as the SpanContext from the SpanContext method.
    40  func ContextWithRemoteSpanContext(parent context.Context, rsc SpanContext) context.Context {
    41  	return ContextWithSpanContext(parent, rsc.WithRemote(true))
    42  }
    43  
    44  // SpanFromContext returns the current Span from ctx.
    45  //
    46  // If no Span is currently set in ctx an implementation of a Span that
    47  // performs no operations is returned.
    48  func SpanFromContext(ctx context.Context) Span {
    49  	if ctx == nil {
    50  		return noopSpan{}
    51  	}
    52  	if span, ok := ctx.Value(currentSpanKey).(Span); ok {
    53  		return span
    54  	}
    55  	return noopSpan{}
    56  }
    57  
    58  // SpanContextFromContext returns the current Span's SpanContext.
    59  func SpanContextFromContext(ctx context.Context) SpanContext {
    60  	return SpanFromContext(ctx).SpanContext()
    61  }