1
0
Fork 0

Elastic APM tracer implementation

This commit is contained in:
Amine Benseddik 2019-11-27 16:00:07 +01:00 committed by Traefiker Bot
parent c4a38de007
commit fe8b090911
11 changed files with 223 additions and 0 deletions

View file

@ -19,6 +19,7 @@ import (
"github.com/containous/traefik/v2/pkg/provider/rest"
"github.com/containous/traefik/v2/pkg/tls"
"github.com/containous/traefik/v2/pkg/tracing/datadog"
"github.com/containous/traefik/v2/pkg/tracing/elastic"
"github.com/containous/traefik/v2/pkg/tracing/haystack"
"github.com/containous/traefik/v2/pkg/tracing/instana"
"github.com/containous/traefik/v2/pkg/tracing/jaeger"
@ -144,6 +145,7 @@ type Tracing struct {
Datadog *datadog.Config `description:"Settings for Datadog." json:"datadog,omitempty" toml:"datadog,omitempty" yaml:"datadog,omitempty" export:"true" label:"allowEmpty"`
Instana *instana.Config `description:"Settings for Instana." json:"instana,omitempty" toml:"instana,omitempty" yaml:"instana,omitempty" export:"true" label:"allowEmpty"`
Haystack *haystack.Config `description:"Settings for Haystack." json:"haystack,omitempty" toml:"haystack,omitempty" yaml:"haystack,omitempty" export:"true" label:"allowEmpty"`
Elastic *elastic.Config `description:"Settings for Elastic." json:"elastic,omitempty" toml:"elastic,omitempty" yaml:"elastic,omitempty" export:"true" label:"allowEmpty"`
}
// SetDefaults sets the default values.

View file

@ -108,6 +108,14 @@ func setupTracing(conf *static.Tracing) *tracing.Tracing {
}
}
if conf.Elastic != nil {
if backend != nil {
log.WithoutContext().Error("Multiple tracing backend are not supported: cannot create Elastic backend.")
} else {
backend = conf.Elastic
}
}
if backend == nil {
log.WithoutContext().Debug("Could not initialize tracing, using Jaeger by default")
defaultBackend := &jaeger.Config{}

View file

@ -0,0 +1,64 @@
package elastic
import (
"io"
"net/url"
"github.com/containous/traefik/v2/pkg/log"
"github.com/containous/traefik/v2/pkg/version"
"github.com/opentracing/opentracing-go"
"go.elastic.co/apm"
"go.elastic.co/apm/module/apmot"
"go.elastic.co/apm/transport"
)
// Name sets the name of this tracer.
const Name = "elastic"
// Config provides configuration settings for a elastic.co tracer.
type Config struct {
ServerURL string `description:"Set the URL of the Elastic APM server." json:"serverURL,omitempty" toml:"serverURL,omitempty" yaml:"serverURL,omitempty"`
SecretToken string `description:"Set the token used to connect to Elastic APM Server." json:"secretToken,omitempty" toml:"secretToken,omitempty" yaml:"secretToken,omitempty"`
ServiceEnvironment string `description:"Set the name of the environment Traefik is deployed in, e.g. 'production' or 'staging'." json:"serviceEnvironment,omitempty" toml:"serviceEnvironment,omitempty" yaml:"serviceEnvironment,omitempty"`
}
// Setup sets up the tracer.
func (c *Config) Setup(serviceName string) (opentracing.Tracer, io.Closer, error) {
// Create default transport.
tr, err := transport.NewHTTPTransport()
if err != nil {
return nil, nil, err
}
if c.ServerURL != "" {
serverURL, err := url.Parse(c.ServerURL)
if err != nil {
return nil, nil, err
}
tr.SetServerURL(serverURL)
}
if c.SecretToken != "" {
tr.SetSecretToken(c.SecretToken)
}
tracer, err := apm.NewTracerOptions(apm.TracerOptions{
ServiceName: serviceName,
ServiceVersion: version.Version,
ServiceEnvironment: c.ServiceEnvironment,
Transport: tr,
})
if err != nil {
return nil, nil, err
}
tracer.SetLogger(log.WithoutContext())
otTracer := apmot.New(apmot.WithTracer(tracer))
// Without this, child spans are getting the NOOP tracer
opentracing.SetGlobalTracer(otTracer)
log.WithoutContext().Debug("Elastic tracer configured")
return otTracer, nil, nil
}