github.com/blend/go-sdk@v1.20220411.3/datadog/tracer.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package datadog 9 10 import ( 11 "context" 12 "strings" 13 14 "github.com/opentracing/opentracing-go" 15 16 "github.com/blend/go-sdk/env" 17 "github.com/blend/go-sdk/logger" 18 19 "gopkg.in/DataDog/dd-trace-go.v1/ddtrace" 20 ddopentracer "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/opentracer" 21 ddtracer "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" 22 ) 23 24 // NewTracer returns a new tracer. 25 // 26 // It defaults to the service environment as named by the `SERVICE_ENV` environment variable. 27 // It defaults the sample rate to the sample rate as returned by the configuration. 28 func NewTracer(opts ...TracerOption) opentracing.Tracer { 29 options := TracerOptions{ 30 ServiceName: env.Env().ServiceName(), 31 ServiceEnv: env.Env().ServiceEnv(), 32 Hostname: env.Env().Hostname(), 33 SampleRate: DefaultTraceSampleRate, 34 } 35 for _, opt := range opts { 36 opt(&options) 37 } 38 39 var startOptions []ddtracer.StartOption 40 if options.Addr != "" { 41 startOptions = append(startOptions, ddtracer.WithAgentAddr(options.Addr)) 42 } 43 if options.StatsAddr != "" { 44 startOptions = append(startOptions, ddtracer.WithAnalytics(true), ddtracer.WithAnalyticsRate(options.SampleRate)) 45 startOptions = append(startOptions, ddtracer.WithDogstatsdAddress(options.StatsAddr)) 46 } 47 if options.ServiceEnv != "" { 48 startOptions = append(startOptions, ddtracer.WithEnv(options.ServiceEnv)) 49 } 50 if options.ServiceName != "" { 51 startOptions = append(startOptions, ddtracer.WithService(options.ServiceName)) 52 } 53 if options.Version != "" { 54 startOptions = append(startOptions, ddtracer.WithServiceVersion(options.Version)) 55 } 56 if options.Hostname != "" { 57 startOptions = append(startOptions, ddtracer.WithGlobalTag(TagHostname, options.Hostname)) 58 } 59 for key, value := range options.Tags { 60 startOptions = append(startOptions, ddtracer.WithGlobalTag(key, value)) 61 } 62 startOptions = append(startOptions, ddtracer.WithSampler(RateSampler(options.SampleRate))) 63 64 if options.Log != nil { 65 startOptions = append(startOptions, ddtracer.WithLogger(traceLogShim{options.Log})) 66 } else { 67 startOptions = append(startOptions, ddtracer.WithLogger(traceLogShim{})) 68 } 69 return ddopentracer.New(startOptions...) 70 } 71 72 // OptTraceAgentAddr returns a dd tracer start option that sets the agent addr. 73 func OptTraceAgentAddr(addr string) TracerOption { 74 return func(to *TracerOptions) { to.Addr = addr } 75 } 76 77 // OptTraceServiceName returns a dd tracer start option that sets the service. 78 func OptTraceServiceName(serviceName string) TracerOption { 79 return func(to *TracerOptions) { to.ServiceName = serviceName } 80 } 81 82 // OptTraceServiceEnv returns a dd tracer start option that sets the service environment. 83 func OptTraceServiceEnv(serviceEnv string) TracerOption { 84 return func(to *TracerOptions) { to.ServiceEnv = serviceEnv } 85 } 86 87 // OptTraceVersion returns a dd tracer start option that sets the service version. 88 func OptTraceVersion(version string) TracerOption { 89 return func(to *TracerOptions) { to.Version = version } 90 } 91 92 // OptTraceHostname returns a dd tracer start option that sets the service hostname. 93 func OptTraceHostname(hostname string) TracerOption { 94 return func(to *TracerOptions) { to.Hostname = hostname } 95 } 96 97 // OptTraceSampleRate returns a dd tracer start option that sets trace sampler with a given rate. 98 func OptTraceSampleRate(rate float64) TracerOption { 99 return func(to *TracerOptions) { to.SampleRate = rate } 100 } 101 102 // OptTraceLog returns an option that sets the log output. 103 func OptTraceLog(log logger.Triggerable) TracerOption { 104 return func(to *TracerOptions) { to.Log = log } 105 } 106 107 // OptTraceConfig sets relevant fields from the datadog config. 108 func OptTraceConfig(cfg Config) TracerOption { 109 return func(to *TracerOptions) { 110 to.Addr = cfg.GetTraceAddress() 111 to.StatsAddr = cfg.GetAddress() 112 to.SampleRate = cfg.TraceSampleRateOrDefault() 113 } 114 } 115 116 // TracerOption mutates tracer options 117 type TracerOption func(*TracerOptions) 118 119 // TracerOptions are all the options we can set when creating a tracer. 120 type TracerOptions struct { 121 Addr string 122 StatsAddr string 123 ServiceName string 124 ServiceEnv string 125 Version string 126 Hostname string 127 SampleRate float64 128 Tags map[string]string 129 Log logger.Triggerable 130 } 131 132 var ( 133 _ ddtrace.Logger = (*traceLogShim)(nil) 134 ) 135 136 // traceLogShim is a shim between the sdk logger and the datadog logger. 137 type traceLogShim struct { 138 Logger logger.Triggerable 139 } 140 141 // Log implements ddtrace.Logger 142 func (tls traceLogShim) Log(msg string) { 143 if tls.Logger != nil { 144 tls.Logger.TriggerContext(context.Background(), logger.NewMessageEvent("datadog-tracer", strings.TrimSpace(msg))) 145 } 146 }