datadog opentracing implementation. For more information on opentracing and the terminology, take a look at the OpenTracing Project.
Requirements:
- Go 1.7
- docker-dd-agent
go get github.com/savaki/datadog
The simplest starting point is to register datadog as the default tracer.
package main
import (
"github.com/opentracing/opentracing-go"
"github.com/savaki/datadog"
)
func main() {
tracer, _ := datadog.New("service")
defer tracer.Close()
opentracing.SetGlobalTracer(tracer)
// ...
}
Alternately, you can manage the reference to Tracer explicitly.
By default, the datadog tracer will connect to the datadog agent at localhost:8126
func main() {
tracer, _ := datadog.New("service",
datadog.WithHost("10.0.0.1"),
datadog.WithPort("8200"),
)
defer tracer.Close()
}
func main() {
tracer, _ := datadog.New("service", datadog.WithECS())
defer tracer.Close()
}
Unlike the opentracing default, datadog has a notion of resource. Resource can be defined in two ways:
span := opentracing.StartSpan("operation_name", datadog.Resource("/"))
span := opentracing.StartSpan("operation_name")
span.SetTag(ext.Resource, "/foo")
Datadog has a number of default service, types: web, cache, db, and rpc. By default, the datadog tracer defaults the type to web. To override this you can do either of the following:
span := opentracing.StartSpan("operation_name", datadog.Type(datadog.TypeRPC))
span := opentracing.StartSpan("operation_name")
span.SetTag(ext.Type, "custom")
Errors can be propagated to datadog in a couple ways:
Via Tag:
span := opentracing.StartSpan("operation_name")
span.SetTag(ext.Error, error)
Via LogFields:
span := opentracing.StartSpan("operation_name")
span.LogFields(log.Error(err))
Via LogKV:
span := opentracing.StartSpan("operation_name")
span.LogKV("err", err)
It's always possible to create a "root" Span
with no parent or other causal
reference.
func xyz() {
...
sp := opentracing.StartSpan("operation_name")
defer sp.Finish()
...
}
func xyz(parentSpan opentracing.Span, ...) {
...
sp := opentracing.StartSpan(
"operation_name",
opentracing.ChildOf(parentSpan.Context()))
defer sp.Finish()
...
}
As a convenience, the datadog tracer provides a couple of convenience methods above the standard opentracing Inject/Extract.
Instrument an http client with the datadog tracer.
client := &http.Client{
Transport: datadog.WrapRoundTripper(http.DefaultRoundTripper, tracer),
}
Instrument an http server with the datadog tracer.
var h http.Handler = ....
h = datadog.WrapHandler(h, tracer)
By default, datadog logs to os.Stdout via the datadog.Stdout
logger.
You can use a custom logger by specifying WithLogger when you construct the datadog tracer.
For example:
func main() {
tracer, _ := datadog.New("service", datadog.WithLogger(datadog.Stderr))
defer tracer.Close()
}
- allow direct posts of traces to datadog e.g. agent-free tracer
- integration datadog log api
- allow direct posts of logs to datadog