Skip to content
datadog opentracing implementation
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples/basic
ext
.gitignore
.travis.yml
LICENSE
README.md
encoder.go
encoder_test.go
errors_test.go
http.go
http_test.go
logger.go
logger_test.go
options_test.go
span.go
span_test.go
trace.go
trace_test.go
tracer.go
tracer_test.go
transporter.go
transporter_test.go
util.go

README.md

datadog

GoDoc Build Status

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

Installation

go get github.com/savaki/datadog

Singleton initialization

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

Specify the host and port of the datadog agent

func main() {
	tracer, _ := datadog.New("service", 
		datadog.WithHost("10.0.0.1"), 
		datadog.WithPort("8200"), 
	) 
	defer tracer.Close()
}

From within an ECS container, specify a docker-dd-agent running on the host

func main() {
	tracer, _ := datadog.New("service", datadog.WithECS())
	defer tracer.Close()
}

Specifying Datadog Resource

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")

Specifying Datadog service type

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")

Handling Errors

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)

Starting an empty trace by creating a "root span"

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()
        ...
    }

Creating a (child) Span given an existing (parent) Span

    func xyz(parentSpan opentracing.Span, ...) {
        ...
        sp := opentracing.StartSpan(
            "operation_name",
            opentracing.ChildOf(parentSpan.Context()))
        defer sp.Finish()
        ...
    }

Serializing over http

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)

Logging

By default, datadog logs to os.Stdout via the datadog.Stdout logger.

Using a custom 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()
}

Todo

  • allow direct posts of traces to datadog e.g. agent-free tracer
  • integration datadog log api
  • allow direct posts of logs to datadog
You can’t perform that action at this time.