github.com/thanos-io/thanos@v0.32.5/pkg/tracing/client/factory.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package client
     5  
     6  import (
     7  	"context"
     8  	"io"
     9  	"strings"
    10  
    11  	"github.com/go-kit/log"
    12  	"github.com/go-kit/log/level"
    13  	"github.com/opentracing/opentracing-go"
    14  	"github.com/pkg/errors"
    15  	"github.com/prometheus/client_golang/prometheus"
    16  	"gopkg.in/yaml.v2"
    17  
    18  	"github.com/thanos-io/thanos/pkg/tracing/elasticapm"
    19  	"github.com/thanos-io/thanos/pkg/tracing/google_cloud"
    20  	"github.com/thanos-io/thanos/pkg/tracing/jaeger"
    21  	"github.com/thanos-io/thanos/pkg/tracing/lightstep"
    22  	"github.com/thanos-io/thanos/pkg/tracing/migration"
    23  	"github.com/thanos-io/thanos/pkg/tracing/otlp"
    24  )
    25  
    26  type TracingProvider string
    27  
    28  const (
    29  	Stackdriver           TracingProvider = "STACKDRIVER"
    30  	GoogleCloud           TracingProvider = "GOOGLE_CLOUD"
    31  	Jaeger                TracingProvider = "JAEGER"
    32  	ElasticAPM            TracingProvider = "ELASTIC_APM"
    33  	Lightstep             TracingProvider = "LIGHTSTEP"
    34  	OpenTelemetryProtocol TracingProvider = "OTLP"
    35  )
    36  
    37  type TracingConfig struct {
    38  	Type   TracingProvider `yaml:"type"`
    39  	Config interface{}     `yaml:"config"`
    40  }
    41  
    42  func NewTracer(ctx context.Context, logger log.Logger, metrics *prometheus.Registry, confContentYaml []byte) (opentracing.Tracer, io.Closer, error) {
    43  	level.Info(logger).Log("msg", "loading tracing configuration")
    44  	tracingConf := &TracingConfig{}
    45  
    46  	if err := yaml.UnmarshalStrict(confContentYaml, tracingConf); err != nil {
    47  		return nil, nil, errors.Wrap(err, "parsing config tracing YAML")
    48  	}
    49  
    50  	var config []byte
    51  	var err error
    52  	if tracingConf.Config != nil {
    53  		config, err = yaml.Marshal(tracingConf.Config)
    54  		if err != nil {
    55  			return nil, nil, errors.Wrap(err, "marshal content of tracing configuration")
    56  		}
    57  	}
    58  
    59  	switch strings.ToUpper(string(tracingConf.Type)) {
    60  	case string(Stackdriver), string(GoogleCloud):
    61  		tracerProvider, err := google_cloud.NewTracerProvider(ctx, logger, config)
    62  		if err != nil {
    63  			return nil, nil, err
    64  		}
    65  		tracer, closerFunc := migration.Bridge(tracerProvider, logger)
    66  		return tracer, closerFunc, nil
    67  	case string(Jaeger):
    68  		tracerProvider, err := jaeger.NewTracerProvider(ctx, logger, config)
    69  		if err != nil {
    70  			return nil, nil, errors.Wrap(err, "new tracer provider err")
    71  		}
    72  		tracer, closerFunc := migration.Bridge(tracerProvider, logger)
    73  		return tracer, closerFunc, nil
    74  	case string(ElasticAPM):
    75  		return elasticapm.NewTracer(config)
    76  	case string(Lightstep):
    77  		return lightstep.NewTracer(ctx, config)
    78  	case string(OpenTelemetryProtocol):
    79  		tracerProvider, err := otlp.NewTracerProvider(ctx, logger, config)
    80  		if err != nil {
    81  			return nil, nil, errors.Wrap(err, "new tracer provider err")
    82  		}
    83  		tracer, closerFunc := migration.Bridge(tracerProvider, logger)
    84  		return tracer, closerFunc, nil
    85  	default:
    86  		return nil, nil, errors.Errorf("tracing with type %s is not supported", tracingConf.Type)
    87  	}
    88  }
    89  
    90  func NoopTracer() opentracing.Tracer {
    91  	return &opentracing.NoopTracer{}
    92  }