traefik/pkg/tracing/jaeger/jaeger.go

91 lines
2.9 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"
2019-03-15 08:42:03 +00:00
"github.com/containous/traefik/pkg/log"
2018-01-10 16:48:04 +00:00
"github.com/opentracing/opentracing-go"
jaeger "github.com/uber/jaeger-client-go"
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"
)
// Name sets the name of this tracer
const Name = "jaeger"
// Config provides configuration settings for a jaeger tracer
type Config struct {
SamplingServerURL string `description:"Set the sampling server url." export:"false"`
SamplingType string `description:"Set the sampling type." export:"true"`
SamplingParam float64 `description:"Set the sampling parameter." export:"true"`
LocalAgentHostPort string `description:"Set jaeger-agent's host:port that the reporter will used." export:"false"`
Gen128Bit bool `description:"Generate 128 bit span IDs." export:"true"`
Propagation string `description:"Which propgation format to use (jaeger/b3)." export:"true"`
TraceContextHeaderName string `description:"Set the header to use for the trace-id." 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
2018-01-10 16:48:04 +00:00
}
// Setup sets up the tracer
func (c *Config) Setup(componentName string) (opentracing.Tracer, io.Closer, error) {
jcfg := jaegercfg.Configuration{
Sampler: &jaegercfg.SamplerConfig{
SamplingServerURL: c.SamplingServerURL,
Type: c.SamplingType,
Param: c.SamplingParam,
},
Reporter: &jaegercfg.ReporterConfig{
LogSpans: true,
LocalAgentHostPort: c.LocalAgentHostPort,
2018-01-10 16:48:04 +00:00
},
Headers: &jaeger.HeadersConfig{
TraceContextHeaderName: c.TraceContextHeaderName,
},
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 {
2018-11-14 09:18:03 +00:00
log.WithoutContext().Warnf("Could not initialize jaeger tracer: %s", err.Error())
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
}