github.com/weaveworks/common@v0.0.0-20230728070032-dd9e68f319d5/tracing/tracing.go (about)

     1  package tracing
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  
     7  	"github.com/opentracing/opentracing-go"
     8  	"github.com/pkg/errors"
     9  	jaeger "github.com/uber/jaeger-client-go"
    10  	jaegercfg "github.com/uber/jaeger-client-go/config"
    11  	jaegerprom "github.com/uber/jaeger-lib/metrics/prometheus"
    12  )
    13  
    14  // ErrInvalidConfiguration is an error to notify client to provide valid trace report agent or config server
    15  var (
    16  	ErrBlankTraceConfiguration = errors.New("no trace report agent, config server, or collector endpoint specified")
    17  )
    18  
    19  // installJaeger registers Jaeger as the OpenTracing implementation.
    20  func installJaeger(serviceName string, cfg *jaegercfg.Configuration, options ...jaegercfg.Option) (io.Closer, error) {
    21  	metricsFactory := jaegerprom.New()
    22  
    23  	// put the metricsFactory earlier so provided options can override it
    24  	opts := append([]jaegercfg.Option{jaegercfg.Metrics(metricsFactory)}, options...)
    25  
    26  	closer, err := cfg.InitGlobalTracer(serviceName, opts...)
    27  	if err != nil {
    28  		return nil, errors.Wrap(err, "could not initialize jaeger tracer")
    29  	}
    30  	return closer, nil
    31  }
    32  
    33  // NewFromEnv is a convenience function to allow tracing configuration
    34  // via environment variables
    35  //
    36  // Tracing will be enabled if one (or more) of the following environment variables is used to configure trace reporting:
    37  // - JAEGER_AGENT_HOST
    38  // - JAEGER_SAMPLER_MANAGER_HOST_PORT
    39  func NewFromEnv(serviceName string, options ...jaegercfg.Option) (io.Closer, error) {
    40  	cfg, err := jaegercfg.FromEnv()
    41  	if err != nil {
    42  		return nil, errors.Wrap(err, "could not load jaeger tracer configuration")
    43  	}
    44  
    45  	if cfg.Sampler.SamplingServerURL == "" && cfg.Reporter.LocalAgentHostPort == "" && cfg.Reporter.CollectorEndpoint == "" {
    46  		return nil, ErrBlankTraceConfiguration
    47  	}
    48  
    49  	return installJaeger(serviceName, cfg, options...)
    50  }
    51  
    52  // ExtractTraceID extracts the trace id, if any from the context.
    53  func ExtractTraceID(ctx context.Context) (string, bool) {
    54  	sp := opentracing.SpanFromContext(ctx)
    55  	if sp == nil {
    56  		return "", false
    57  	}
    58  	sctx, ok := sp.Context().(jaeger.SpanContext)
    59  	if !ok {
    60  		return "", false
    61  	}
    62  
    63  	return sctx.TraceID().String(), true
    64  }
    65  
    66  // ExtractSampledTraceID works like ExtractTraceID but the returned bool is only
    67  // true if the returned trace id is sampled.
    68  func ExtractSampledTraceID(ctx context.Context) (string, bool) {
    69  	sp := opentracing.SpanFromContext(ctx)
    70  	if sp == nil {
    71  		return "", false
    72  	}
    73  	sctx, ok := sp.Context().(jaeger.SpanContext)
    74  	if !ok {
    75  		return "", false
    76  	}
    77  
    78  	return sctx.TraceID().String(), sctx.IsSampled()
    79  }