OpenTracing API for Go
Clone or download
Latest commit be550b0 Oct 12, 2018
Failed to load latest commit information.
.github Adds issue template. (#183) Apr 12, 2018
ext Add PeerHostIPv4.SetString() (#155) Aug 6, 2017
harness Port of Python opentracing/harness/ to Go (#146) Feb 3, 2018
log Add a Noop log field type to log to allow for optional fields (#150) Jul 15, 2017
mocktracer Fix race condition in MockSpan.Context() (#170) Jan 31, 2018
.gitignore Use real import in the example Mar 2, 2016
.travis.yml Test with PR code, not with master (#157) Jun 6, 2018 Deprecate InitGlobalTracer in favor of SetGlobalTracer (#128) Dec 2, 2016
LICENSE update license to Apache 2.0 (#181) Apr 6, 2018
Makefile Fix race condition in MockSpan.Context() (#170) Jan 31, 2018 Fix link to specification Oct 12, 2018
globaltracer.go Deprecate InitGlobalTracer in favor of SetGlobalTracer (#128) Dec 2, 2016
gocontext.go Reduce unnecessary complexity (#158) Apr 9, 2018
gocontext_test.go Replace '' with 'context' (#176) Apr 6, 2018
noop.go Deprecate InitGlobalTracer in favor of SetGlobalTracer (#128) Dec 2, 2016
options_test.go Replace the Referee terminology Jul 25, 2016
propagation.go Use Set() instead of Add() in HTTPHeadersCarrier (#191) Sep 8, 2018
propagation_test.go Fix HTTPHeadersCarrier type name in the documentation (#104) Jul 30, 2016
span.go Span: add return type documentation (#162) Jun 6, 2018
testtracer_test.go Flesh out the LogFields and LogKV changes Sep 26, 2016
tracer.go Fix some usage examples (#145) May 6, 2017

Gitter chat Build Status GoDoc Sourcegraph Badge

OpenTracing API for Go

This package is a Go platform API for OpenTracing.

Required Reading

In order to understand the Go platform API, one must first be familiar with the OpenTracing project and terminology more specifically.

API overview for those adding instrumentation

Everyday consumers of this opentracing package really only need to worry about a couple of key abstractions: the StartSpan function, the Span interface, and binding a Tracer at main()-time. Here are code snippets demonstrating some important use cases.

Singleton initialization

The simplest starting point is ./default_tracer.go. As early as possible, call

    import ""
    import ".../some_tracing_impl"

    func main() {
            // tracing impl specific:

Non-Singleton initialization

If you prefer direct control to singletons, manage ownership of the opentracing.Tracer implementation explicitly.

Creating a Span given an existing Go context.Context

If you use context.Context in your application, OpenTracing's Go library will happily rely on it for Span propagation. To start a new (blocking child) Span, you can use StartSpanFromContext.

    func xyz(ctx context.Context, ...) {
        span, ctx := opentracing.StartSpanFromContext(ctx, "operation_name")
        defer span.Finish()
            log.String("event", "soft error"),
            log.String("type", "cache timeout"),
            log.Int("waited.millis", 1500))

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(
        defer sp.Finish()

Serializing to the wire

    func makeSomeRequest(ctx context.Context) ... {
        if span := opentracing.SpanFromContext(ctx); span != nil {
            httpClient := &http.Client{}
            httpReq, _ := http.NewRequest("GET", "http://myservice/", nil)

            // Transmit the span's TraceContext as HTTP headers on our
            // outbound request.

            resp, err := httpClient.Do(httpReq)

Deserializing from the wire

    http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
        var serverSpan opentracing.Span
        appSpecificOperationName := ...
        wireContext, err := opentracing.GlobalTracer().Extract(
        if err != nil {
            // Optionally record something about err here

        // Create the span referring to the RPC client if available.
        // If wireContext == nil, a root span will be created.
        serverSpan = opentracing.StartSpan(

        defer serverSpan.Finish()

        ctx := opentracing.ContextWithSpan(context.Background(), serverSpan)

Conditionally capture a field using log.Noop

In some situations, you may want to dynamically decide whether or not to log a field. For example, you may want to capture additional data, such as a customer ID, in non-production environments:

    func Customer(order *Order) log.Field {
        if os.Getenv("ENVIRONMENT") == "dev" {
            return log.String("customer", order.Customer.ID)
        return log.Noop()


The entire public API is goroutine-safe and does not require external synchronization.

API pointers for those implementing a tracing system

Tracing system implementors may be able to reuse or copy-paste-modify the basictracer package, found here. In particular, see basictracer.New(...).

API compatibility

For the time being, "mild" backwards-incompatible changes may be made without changing the major version number. As OpenTracing and opentracing-go mature, backwards compatibility will become more of a priority.

Tracer test suite

A test suite is available in the harness package that can assist Tracer implementors to assert that their Tracer is working correctly.


Apache 2.0 License.