github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/observability/tracing/tracing.go (about)

     1  // Copyright 2022 Gravitational, Inc
     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 tracing
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/gravitational/trace"
    21  	"go.opentelemetry.io/otel"
    22  	"go.opentelemetry.io/otel/codes"
    23  	"go.opentelemetry.io/otel/propagation"
    24  	oteltrace "go.opentelemetry.io/otel/trace"
    25  )
    26  
    27  // PropagationContext contains tracing information to be passed across service boundaries
    28  type PropagationContext map[string]string
    29  
    30  // TraceParent is the name of the header or query parameter that contains
    31  // tracing context across service boundaries.
    32  const TraceParent = "traceparent"
    33  
    34  // PropagationContextFromContext creates a PropagationContext from the given context.Context. If the context
    35  // does not contain any tracing information, the PropagationContext will be empty.
    36  func PropagationContextFromContext(ctx context.Context, opts ...Option) PropagationContext {
    37  	carrier := propagation.MapCarrier{}
    38  	NewConfig(opts).TextMapPropagator.Inject(ctx, &carrier)
    39  	return PropagationContext(carrier)
    40  }
    41  
    42  // WithPropagationContext injects any tracing information from the given PropagationContext into the
    43  // given context.Context.
    44  func WithPropagationContext(ctx context.Context, pc PropagationContext, opts ...Option) context.Context {
    45  	return NewConfig(opts).TextMapPropagator.Extract(ctx, propagation.MapCarrier(pc))
    46  }
    47  
    48  // DefaultProvider returns the global default TracerProvider.
    49  func DefaultProvider() oteltrace.TracerProvider {
    50  	return otel.GetTracerProvider()
    51  }
    52  
    53  // NewTracer creates a new [oteltrace.Tracer] from the global default
    54  // [oteltrace.TracerProvider] with the provided name.
    55  func NewTracer(name string) oteltrace.Tracer {
    56  	return DefaultProvider().Tracer(name)
    57  }
    58  
    59  // EndSpan ends the given span and if an error has occurred, set's the span's
    60  // status to error and additionally records the error.
    61  //
    62  // Example usage:
    63  //
    64  //	func myFunc() (err error) {
    65  //	  ctx, span := tracer.Start(ctx, "myFunc")
    66  //	  defer func() { tracing.EndSpan(span, err) }()
    67  //	  ...
    68  //	}
    69  func EndSpan(span oteltrace.Span, err error) {
    70  	if err != nil {
    71  		span.SetStatus(codes.Error, err.Error())
    72  		span.RecordError(trace.Unwrap(err))
    73  	}
    74  	span.End()
    75  }