2018-01-10 16:48:04 +00:00
package zipkin
import (
"io"
2018-11-27 16:42:04 +00:00
"time"
2018-01-10 16:48:04 +00:00
2019-08-03 01:58:23 +00:00
"github.com/containous/traefik/v2/pkg/log"
2018-07-03 08:02:03 +00:00
"github.com/opentracing/opentracing-go"
2019-09-03 09:52:04 +00:00
zipkinot "github.com/openzipkin-contrib/zipkin-go-opentracing"
"github.com/openzipkin/zipkin-go"
"github.com/openzipkin/zipkin-go/reporter/http"
2018-01-10 16:48:04 +00:00
)
2018-11-14 09:18:03 +00:00
// Name sets the name of this tracer.
2018-01-10 16:48:04 +00:00
const Name = "zipkin"
2018-11-14 09:18:03 +00:00
// Config provides configuration settings for a zipkin tracer.
2018-01-10 16:48:04 +00:00
type Config struct {
2019-07-01 09:30:05 +00:00
HTTPEndpoint string ` description:"HTTP Endpoint to report traces to." json:"httpEndpoint,omitempty" toml:"httpEndpoint,omitempty" yaml:"httpEndpoint,omitempty" `
SameSpan bool ` description:"Use Zipkin SameSpan RPC style traces." json:"sameSpan,omitempty" toml:"sameSpan,omitempty" yaml:"sameSpan,omitempty" export:"true" `
ID128Bit bool ` description:"Use Zipkin 128 bit root span IDs." json:"id128Bit,omitempty" toml:"id128Bit,omitempty" yaml:"id128Bit,omitempty" export:"true" `
SampleRate float64 ` description:"The rate between 0.0 and 1.0 of requests to trace." json:"sampleRate,omitempty" toml:"sampleRate,omitempty" yaml:"sampleRate,omitempty" export:"true" `
2018-01-10 16:48:04 +00:00
}
2019-06-17 09:48:05 +00:00
// SetDefaults sets the default values.
func ( c * Config ) SetDefaults ( ) {
2019-09-03 09:52:04 +00:00
c . HTTPEndpoint = "http://localhost:9411/api/v2/spans"
2019-06-17 09:48:05 +00:00
c . SameSpan = false
c . ID128Bit = true
c . SampleRate = 1.0
}
2018-01-10 16:48:04 +00:00
// Setup sets up the tracer
func ( c * Config ) Setup ( serviceName string ) ( opentracing . Tracer , io . Closer , error ) {
2019-09-03 09:52:04 +00:00
// create our local endpoint
endpoint , err := zipkin . NewEndpoint ( serviceName , "0.0.0.0:0" )
2018-02-15 09:28:03 +00:00
if err != nil {
return nil , nil , err
}
2018-11-14 09:18:03 +00:00
2019-09-03 09:52:04 +00:00
// create our sampler
sampler , err := zipkin . NewBoundarySampler ( c . SampleRate , time . Now ( ) . Unix ( ) )
if err != nil {
return nil , nil , err
}
2018-11-14 09:18:03 +00:00
2019-09-03 09:52:04 +00:00
// create the span reporter
reporter := http . NewReporter ( c . HTTPEndpoint )
// create the native Zipkin tracer
nativeTracer , err := zipkin . NewTracer (
reporter ,
zipkin . WithLocalEndpoint ( endpoint ) ,
zipkin . WithSharedSpans ( c . SameSpan ) ,
zipkin . WithTraceID128Bit ( c . ID128Bit ) ,
zipkin . WithSampler ( sampler ) ,
2018-01-10 16:48:04 +00:00
)
if err != nil {
return nil , nil , err
}
2019-09-03 09:52:04 +00:00
// wrap the Zipkin native tracer with the OpenTracing Bridge
tracer := zipkinot . Wrap ( nativeTracer )
2018-01-10 16:48: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 ( "Zipkin tracer configured" )
2018-04-16 15:42:03 +00:00
2019-09-03 09:52:04 +00:00
return tracer , reporter , nil
2018-01-10 16:48:04 +00:00
}