2018-06-28 16:40:04 +00:00
package datadog
import (
"io"
"strings"
2019-03-15 08:42:03 +00:00
"github.com/containous/traefik/pkg/log"
2018-06-28 16:40:04 +00:00
"github.com/opentracing/opentracing-go"
ddtracer "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/opentracer"
datadog "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
// Name sets the name of this tracer
const Name = "datadog"
// Config provides configuration settings for a datadog tracer
type Config struct {
2019-06-17 09:48:05 +00:00
LocalAgentHostPort string ` description:"Set datadog-agent's host:port that the reporter will used." export:"false" `
2019-03-27 16:20:05 +00:00
GlobalTag string ` description:"Key:Value tag to be set on all the spans." export:"true" `
Debug bool ` description:"Enable DataDog debug." export:"true" `
PrioritySampling bool ` description:"Enable priority sampling. When using distributed tracing, this option must be enabled in order to get all the parts of a distributed trace sampled." `
TraceIDHeaderName string ` description:"Specifies the header name that will be used to store the trace ID." export:"true" `
ParentIDHeaderName string ` description:"Specifies the header name that will be used to store the parent ID." export:"true" `
SamplingPriorityHeaderName string ` description:"Specifies the header name that will be used to store the sampling priority." export:"true" `
2019-06-17 09:48:05 +00:00
BagagePrefixHeaderName string ` description:"Specifies the header name prefix that will be used to store baggage items in a map." export:"true" `
}
// SetDefaults sets the default values.
func ( c * Config ) SetDefaults ( ) {
c . LocalAgentHostPort = "localhost:8126"
c . GlobalTag = ""
c . Debug = false
c . PrioritySampling = false
2018-06-28 16:40:04 +00:00
}
// Setup sets up the tracer
func ( c * Config ) Setup ( serviceName string ) ( opentracing . Tracer , io . Closer , error ) {
tag := strings . SplitN ( c . GlobalTag , ":" , 2 )
value := ""
if len ( tag ) == 2 {
value = tag [ 1 ]
}
2019-01-16 16:08:06 +00:00
opts := [ ] datadog . StartOption {
2018-06-28 16:40:04 +00:00
datadog . WithAgentAddr ( c . LocalAgentHostPort ) ,
datadog . WithServiceName ( serviceName ) ,
datadog . WithGlobalTag ( tag [ 0 ] , value ) ,
datadog . WithDebugMode ( c . Debug ) ,
2019-03-27 16:20:05 +00:00
datadog . WithPropagator ( datadog . NewPropagator ( & datadog . PropagatorConfig {
TraceHeader : c . TraceIDHeaderName ,
ParentHeader : c . ParentIDHeaderName ,
PriorityHeader : c . SamplingPriorityHeaderName ,
BaggagePrefix : c . BagagePrefixHeaderName ,
} ) ) ,
2019-01-16 16:08:06 +00:00
}
if c . PrioritySampling {
opts = append ( opts , datadog . WithPrioritySampling ( ) )
}
tracer := ddtracer . New ( opts ... )
2018-06-28 16:40:04 +00:00
// Without this, child spans are getting the NOOP tracer
opentracing . SetGlobalTracer ( tracer )
2018-11-14 09:18:03 +00:00
log . WithoutContext ( ) . Debug ( "DataDog tracer configured" )
2018-06-28 16:40:04 +00:00
return tracer , nil , nil
}