github.com/thanos-io/thanos@v0.32.5/pkg/tracing/jaeger/jaeger.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package jaeger 5 6 import ( 7 "context" 8 9 "github.com/thanos-io/thanos/pkg/tracing/migration" 10 11 "github.com/go-kit/log" 12 "github.com/go-kit/log/level" 13 "go.opentelemetry.io/otel/attribute" 14 otel_jaeger "go.opentelemetry.io/otel/exporters/jaeger" 15 "go.opentelemetry.io/otel/sdk/resource" 16 tracesdk "go.opentelemetry.io/otel/sdk/trace" 17 semconv "go.opentelemetry.io/otel/semconv/v1.7.0" 18 "gopkg.in/yaml.v2" 19 ) 20 21 // NewTracerProvider returns a new instance of an OpenTelemetry tracer provider. 22 func NewTracerProvider(ctx context.Context, logger log.Logger, conf []byte) (*tracesdk.TracerProvider, error) { 23 config := Config{} 24 if err := yaml.Unmarshal(conf, &config); err != nil { 25 return nil, err 26 } 27 28 printDeprecationWarnings(config, logger) 29 30 var exporter *otel_jaeger.Exporter 31 var err error 32 33 if config.Endpoint != "" { 34 collectorOptions := getCollectorEndpoints(config) 35 36 exporter, err = otel_jaeger.New(otel_jaeger.WithCollectorEndpoint(collectorOptions...)) 37 if err != nil { 38 return nil, err 39 } 40 } else if config.AgentHost != "" && config.AgentPort != 0 { 41 jaegerAgentEndpointOptions := getAgentEndpointOptions(config) 42 43 exporter, err = otel_jaeger.New(otel_jaeger.WithAgentEndpoint(jaegerAgentEndpointOptions...)) 44 if err != nil { 45 return nil, err 46 } 47 } else { 48 exporter, err = otel_jaeger.New(otel_jaeger.WithAgentEndpoint()) 49 if err != nil { 50 return nil, err 51 } 52 } 53 54 var tags []attribute.KeyValue 55 if config.Tags != "" { 56 tags = getAttributesFromTags(config) 57 } 58 59 sampler := getSampler(config) 60 var processorOptions []tracesdk.BatchSpanProcessorOption 61 var processor tracesdk.SpanProcessor 62 if config.ReporterMaxQueueSize != 0 { 63 processorOptions = append(processorOptions, tracesdk.WithMaxQueueSize(config.ReporterMaxQueueSize)) 64 } 65 66 //Ref: https://epsagon.com/observability/opentelemetry-best-practices-overview-part-2-2/ . 67 if config.ReporterFlushInterval != 0 { 68 processorOptions = append(processorOptions, tracesdk.WithBatchTimeout(config.ReporterFlushInterval)) 69 } 70 71 processor = tracesdk.NewBatchSpanProcessor(exporter, processorOptions...) 72 73 tp := newTraceProvider(ctx, config.ServiceName, logger, processor, sampler, tags) 74 75 return tp, nil 76 } 77 78 // getAttributesFromTags returns tags as OTel attributes. 79 func getAttributesFromTags(config Config) []attribute.KeyValue { 80 return parseTags(config.Tags) 81 } 82 83 func newTraceProvider(ctx context.Context, serviceName string, logger log.Logger, processor tracesdk.SpanProcessor, 84 sampler tracesdk.Sampler, tags []attribute.KeyValue) *tracesdk.TracerProvider { 85 86 resource, err := resource.New( 87 ctx, 88 resource.WithAttributes(semconv.ServiceNameKey.String(serviceName)), 89 resource.WithAttributes(tags...), 90 ) 91 if err != nil { 92 level.Warn(logger).Log("msg", "jaeger: detecting resources for tracing provider failed", "err", err) 93 } 94 95 tp := tracesdk.NewTracerProvider( 96 tracesdk.WithSpanProcessor(processor), 97 tracesdk.WithSampler( 98 migration.SamplerWithOverride( 99 sampler, migration.ForceTracingAttributeKey, 100 ), 101 ), 102 tracesdk.WithResource(resource), 103 ) 104 105 return tp 106 }