traefik/pkg/tracing/haystack/haystack.go

70 lines
3 KiB
Go
Raw Normal View History

2019-05-08 22:14:04 +00:00
package haystack
import (
"io"
"strings"
"time"
"github.com/ExpediaDotCom/haystack-client-go"
"github.com/opentracing/opentracing-go"
"github.com/traefik/traefik/v2/pkg/log"
2019-05-08 22:14:04 +00:00
)
2020-05-11 10:06:07 +00:00
// Name sets the name of this tracer.
2019-05-08 22:14:04 +00:00
const Name = "haystack"
2020-05-11 10:06:07 +00:00
// Config provides configuration settings for a haystack tracer.
2019-05-08 22:14:04 +00:00
type Config struct {
LocalAgentHost string `description:"Sets the Haystack Agent host." json:"localAgentHost,omitempty" toml:"localAgentHost,omitempty" yaml:"localAgentHost,omitempty"`
LocalAgentPort int `description:"Sets the Haystack Agent port." json:"localAgentPort,omitempty" toml:"localAgentPort,omitempty" yaml:"localAgentPort,omitempty"`
GlobalTag string `description:"Sets a key:value tag on all spans." json:"globalTag,omitempty" toml:"globalTag,omitempty" yaml:"globalTag,omitempty" export:"true"`
TraceIDHeaderName string `description:"Sets the header name used to store the trace ID." json:"traceIDHeaderName,omitempty" toml:"traceIDHeaderName,omitempty" yaml:"traceIDHeaderName,omitempty" export:"true"`
ParentIDHeaderName string `description:"Sets the header name used to store the parent ID." json:"parentIDHeaderName,omitempty" toml:"parentIDHeaderName,omitempty" yaml:"parentIDHeaderName,omitempty" export:"true"`
SpanIDHeaderName string `description:"Sets the header name used to store the span ID." json:"spanIDHeaderName,omitempty" toml:"spanIDHeaderName,omitempty" yaml:"spanIDHeaderName,omitempty" export:"true"`
BaggagePrefixHeaderName string `description:"Sets the header name prefix used to store baggage items in a map." json:"baggagePrefixHeaderName,omitempty" toml:"baggagePrefixHeaderName,omitempty" yaml:"baggagePrefixHeaderName,omitempty" export:"true"`
2019-05-08 22:14:04 +00:00
}
// SetDefaults sets the default values.
func (c *Config) SetDefaults() {
c.LocalAgentHost = "127.0.0.1"
c.LocalAgentPort = 35000
}
2020-05-11 10:06:07 +00:00
// Setup sets up the tracer.
2019-05-08 22:14:04 +00:00
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]
}
host := "localhost"
port := 35000
if len(c.LocalAgentHost) > 0 {
host = c.LocalAgentHost
}
if c.LocalAgentPort > 0 {
port = c.LocalAgentPort
}
tracer, closer := haystack.NewTracer(serviceName, haystack.NewAgentDispatcher(host, port, 3*time.Second, 1000),
haystack.TracerOptionsFactory.Tag(tag[0], value),
haystack.TracerOptionsFactory.Propagator(opentracing.HTTPHeaders,
haystack.NewTextMapPropagator(haystack.PropagatorOpts{
TraceIDKEYName: c.TraceIDHeaderName,
ParentSpanIDKEYName: c.ParentIDHeaderName,
SpanIDKEYName: c.SpanIDHeaderName,
BaggagePrefixKEYName: c.BaggagePrefixHeaderName,
}, haystack.DefaultCodex{})),
haystack.TracerOptionsFactory.Logger(&haystackLogger{logger: log.WithoutContext()}),
)
// Without this, child spans are getting the NOOP tracer
opentracing.SetGlobalTracer(tracer)
2019-09-02 10:18:04 +00:00
log.WithoutContext().Debug("haystack tracer configured")
2019-05-08 22:14:04 +00:00
return tracer, closer, nil
}