github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/observability/tracing/option.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 "go.opentelemetry.io/otel" 19 "go.opentelemetry.io/otel/propagation" 20 oteltrace "go.opentelemetry.io/otel/trace" 21 ) 22 23 // Option applies an option value for a Config. 24 type Option interface { 25 apply(*Config) 26 } 27 28 // Config stores tracing related properties to customize 29 // creating Tracers and extracting TraceContext 30 type Config struct { 31 TracerProvider oteltrace.TracerProvider 32 TextMapPropagator propagation.TextMapPropagator 33 } 34 35 // NewConfig returns a Config configured with all the passed Option. 36 func NewConfig(opts []Option) *Config { 37 c := &Config{ 38 TracerProvider: otel.GetTracerProvider(), 39 TextMapPropagator: otel.GetTextMapPropagator(), 40 } 41 for _, o := range opts { 42 o.apply(c) 43 } 44 return c 45 } 46 47 type tracerProviderOption struct{ tp oteltrace.TracerProvider } 48 49 func (o tracerProviderOption) apply(c *Config) { 50 if o.tp != nil { 51 c.TracerProvider = o.tp 52 } 53 } 54 55 // WithTracerProvider returns an Option to use the trace.TracerProvider when 56 // creating a trace.Tracer. 57 func WithTracerProvider(tp oteltrace.TracerProvider) Option { 58 return tracerProviderOption{tp: tp} 59 } 60 61 type propagatorOption struct{ p propagation.TextMapPropagator } 62 63 func (o propagatorOption) apply(c *Config) { 64 if o.p != nil { 65 c.TextMapPropagator = o.p 66 } 67 } 68 69 // WithTextMapPropagator returns an Option to use the propagation.TextMapPropagator when extracting 70 // and injecting trace context. 71 func WithTextMapPropagator(p propagation.TextMapPropagator) Option { 72 return propagatorOption{p: p} 73 }