traefik/pkg/tracing/jaeger/jaeger.go

122 lines
5.2 KiB
Go
Raw Normal View History

2018-01-10 16:48:04 +00:00
package jaeger
import (
2018-08-01 11:52:03 +00:00
"fmt"
2018-01-10 16:48:04 +00:00
"io"
"github.com/opentracing/opentracing-go"
"github.com/traefik/traefik/v2/pkg/log"
jaegercli "github.com/uber/jaeger-client-go"
2018-01-10 16:48:04 +00:00
jaegercfg "github.com/uber/jaeger-client-go/config"
2018-08-01 11:52:03 +00:00
"github.com/uber/jaeger-client-go/zipkin"
2018-01-10 16:48:04 +00:00
jaegermet "github.com/uber/jaeger-lib/metrics"
)
2019-07-15 12:52:04 +00:00
// Name sets the name of this tracer.
2018-01-10 16:48:04 +00:00
const Name = "jaeger"
2019-07-15 12:52:04 +00:00
// Config provides configuration settings for a jaeger tracer.
2018-01-10 16:48:04 +00:00
type Config struct {
SamplingServerURL string `description:"Sets the sampling server URL." json:"samplingServerURL,omitempty" toml:"samplingServerURL,omitempty" yaml:"samplingServerURL,omitempty"`
SamplingType string `description:"Sets the sampling type." json:"samplingType,omitempty" toml:"samplingType,omitempty" yaml:"samplingType,omitempty" export:"true"`
SamplingParam float64 `description:"Sets the sampling parameter." json:"samplingParam,omitempty" toml:"samplingParam,omitempty" yaml:"samplingParam,omitempty" export:"true"`
LocalAgentHostPort string `description:"Sets the Jaeger Agent host:port." json:"localAgentHostPort,omitempty" toml:"localAgentHostPort,omitempty" yaml:"localAgentHostPort,omitempty"`
Gen128Bit bool `description:"Generates 128 bits span IDs." json:"gen128Bit,omitempty" toml:"gen128Bit,omitempty" yaml:"gen128Bit,omitempty" export:"true"`
Propagation string `description:"Sets the propagation format (jaeger/b3)." json:"propagation,omitempty" toml:"propagation,omitempty" yaml:"propagation,omitempty" export:"true"`
TraceContextHeaderName string `description:"Sets the header name used to store the trace ID." json:"traceContextHeaderName,omitempty" toml:"traceContextHeaderName,omitempty" yaml:"traceContextHeaderName,omitempty" export:"true"`
Collector *Collector `description:"Defines the collector information." json:"collector,omitempty" toml:"collector,omitempty" yaml:"collector,omitempty" export:"true"`
DisableAttemptReconnecting bool `description:"Disables the periodic re-resolution of the agent's hostname and reconnection if there was a change." json:"disableAttemptReconnecting,omitempty" toml:"disableAttemptReconnecting,omitempty" yaml:"disableAttemptReconnecting,omitempty" export:"true"`
}
// SetDefaults sets the default values.
func (c *Config) SetDefaults() {
c.SamplingServerURL = "http://localhost:5778/sampling"
c.SamplingType = "const"
c.SamplingParam = 1.0
c.LocalAgentHostPort = "127.0.0.1:6831"
c.Propagation = "jaeger"
c.Gen128Bit = false
c.TraceContextHeaderName = jaegercli.TraceContextHeaderName
c.DisableAttemptReconnecting = true
2018-01-10 16:48:04 +00:00
}
2019-07-15 12:52:04 +00:00
// Collector provides configuration settings for jaeger collector.
type Collector struct {
2020-10-30 11:44:05 +00:00
Endpoint string `description:"Instructs reporter to send spans to jaeger-collector at this URL." json:"endpoint,omitempty" toml:"endpoint,omitempty" yaml:"endpoint,omitempty"`
User string `description:"User for basic http authentication when sending spans to jaeger-collector." json:"user,omitempty" toml:"user,omitempty" yaml:"user,omitempty" loggable:"false"`
Password string `description:"Password for basic http authentication when sending spans to jaeger-collector." json:"password,omitempty" toml:"password,omitempty" yaml:"password,omitempty" loggable:"false"`
2019-07-15 12:52:04 +00:00
}
// SetDefaults sets the default values.
func (c *Collector) SetDefaults() {
c.Endpoint = ""
c.User = ""
c.Password = ""
}
2020-05-11 10:06:07 +00:00
// Setup sets up the tracer.
2018-01-10 16:48:04 +00:00
func (c *Config) Setup(componentName string) (opentracing.Tracer, io.Closer, error) {
2019-07-15 12:52:04 +00:00
reporter := &jaegercfg.ReporterConfig{
LogSpans: true,
LocalAgentHostPort: c.LocalAgentHostPort,
DisableAttemptReconnecting: c.DisableAttemptReconnecting,
2019-07-15 12:52:04 +00:00
}
if c.Collector != nil {
reporter.CollectorEndpoint = c.Collector.Endpoint
reporter.User = c.Collector.User
reporter.Password = c.Collector.Password
}
jcfg := &jaegercfg.Configuration{
2018-01-10 16:48:04 +00:00
Sampler: &jaegercfg.SamplerConfig{
SamplingServerURL: c.SamplingServerURL,
Type: c.SamplingType,
Param: c.SamplingParam,
},
2019-07-15 12:52:04 +00:00
Reporter: reporter,
2019-12-12 16:48:05 +00:00
Headers: &jaegercli.HeadersConfig{
TraceContextHeaderName: c.TraceContextHeaderName,
},
2018-01-10 16:48:04 +00:00
}
// Overrides existing tracer's Configuration with environment variables.
_, err := jcfg.FromEnv()
if err != nil {
return nil, nil, err
}
2018-01-10 16:48:04 +00:00
jMetricsFactory := jaegermet.NullFactory
2018-08-01 11:52:03 +00:00
opts := []jaegercfg.Option{
2018-11-14 09:18:03 +00:00
jaegercfg.Logger(newJaegerLogger()),
2018-08-01 11:52:03 +00:00
jaegercfg.Metrics(jMetricsFactory),
jaegercfg.Gen128Bit(c.Gen128Bit),
}
switch c.Propagation {
case "b3":
p := zipkin.NewZipkinB3HTTPHeaderPropagator()
opts = append(opts,
jaegercfg.Injector(opentracing.HTTPHeaders, p),
jaegercfg.Extractor(opentracing.HTTPHeaders, p),
)
case "jaeger", "":
default:
return nil, nil, fmt.Errorf("unknown propagation format: %s", c.Propagation)
}
2018-01-10 16:48:04 +00:00
// Initialize tracer with a logger and a metrics factory
closer, err := jcfg.InitGlobalTracer(
componentName,
2018-08-01 11:52:03 +00:00
opts...,
2018-01-10 16:48:04 +00:00
)
if err != nil {
log.WithoutContext().Warnf("Could not initialize jaeger tracer: %v", err)
2018-01-10 16:48:04 +00:00
return nil, nil, err
}
2018-11-14 09:18:03 +00:00
log.WithoutContext().Debug("Jaeger tracer configured")
2018-01-10 16:48:04 +00:00
return opentracing.GlobalTracer(), closer, nil
}