New announcements for Serverless, Network, RUM, and more from Dash! New announcements from Dash!

Tracing Go Applications

Installation and Getting Started

For configuration instructions and details about using the API, see the Datadog API documentation. For manual instrumentation, use the integrations section below for Go libraries and frameworks supporting automatic instrumentation.

For a description of the terminology used in APM, see the Getting started with APM section. For details about contributing, check the official repository README.md.

Consult the migration document if you need to migrate from an older version of the tracer (e.g. v<0.6.x) to the newest version.

Installation

If you already have a Datadog account you can find step-by-step instructions in our in-app guides for host-based and container-based set ups.

First install and configure the Datadog Agent. See the additional documentation for tracing Docker applications or Kubernetes applications.

Next, install the Go tracer from its canonical import path:

go get gopkg.in/DataDog/dd-trace-go.v1/ddtrace

You are now ready to import the tracer and start instrumenting your code.

Automatic Instrumentation

Datadog has a series of pluggable packages which provide out-of-the-box support for instrumenting a series of libraries and frameworks. Find the list of supported integrations below.

Compatibility

To begin tracing your Go applications, your environment must first meet the following requirements:

  • Runing the Datadog Agent >= 5.21.1.
  • Using Go 1.9+

Integrations

Framework Compatibility

Integrate the Go tracer with the following list of web frameworks using one of the following helper packages.

FrameworkSupport TypeGoDoc Datadog Documentation
GinFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/gin-gonic/gin
Gorilla MuxFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/gorilla/mux
gRPCFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/google.golang.org/grpc
gRPC v1.2Fully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/google.golang.org/grpc.v12

Library Compatibility

The Go tracer includes support for the following data stores and libraries.

LibrarySupport TypeExamples and Documentation
AWS SDKFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/aws/aws-sdk-go/aws
ElasticsearchFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/olivere/elastic
CassandraFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/gocql/gocql
GraphQLFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/graph-gophers/graphql-go
HTTPFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/net/http
HTTP routerFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/julienschmidt/httprouter
Redis (go-redis)Fully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/go-redis/redis
Redis (redigo)Fully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/garyburd/redigo
SQLFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql
SQLxFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/jmoiron/sqlx
MongoDBFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/go.mongodb.org/mongo-driver/mongo
BuntDBFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/tidwall/buntdb
LevelDBFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/syndtr/goleveldb/leveldb
miekg/dnsFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/miekg/dns
Kafka (confluent)Fully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/confluentinc/confluent-kafka-go
Kafka (sarama)Fully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/Shopify/sarama
Google APIFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/google.golang.org/api
go-restfulFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/emicklei/go-restful

Note: The integrations documentation provides a detailed overview of the supported packages and their APIs, along with usage examples.

Packages must be imported, i.e.:

import "gopkg.in/DataDog/dd-trace-go.v1/contrib/<package_dir>/<package_name>"

Configuration

The tracer is configured with options parameters when the Start function is called. An example for generating a trace using the HTTP library:

package main

import (
    "net/http"
    "strings"
    "log"
    httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
    "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

func sayHello(w http.ResponseWriter, r *http.Request) {
  message := r.URL.Path
  message = strings.TrimPrefix(message, "/")
  message = "Hello " + message
  w.Write([]byte(message))
}

func main() {
    // start the tracer with zero or more options
    tracer.Start(tracer.WithServiceName("test-go"))
    defer tracer.Stop()

    mux := httptrace.NewServeMux() // init the http tracer
    mux.HandleFunc("/", sayHello) // use the tracer to handle the urls

    err := http.ListenAndServe(":9090", mux) // set listen port
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

For more tracer settings, see available options in the configuration documentation.

Change Agent Hostname

Configure your application level tracers to submit traces to a custom Agent hostname:

The Go Tracing Module automatically looks for and initializes with the ENV variables DD_AGENT_HOST and DD_TRACE_AGENT_PORT

package main

import (
    "net"
    "os"

    "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

func main() {
    addr := net.JoinHostPort(
        os.Getenv("DD_AGENT_HOST"),
        os.Getenv("DD_TRACE_AGENT_PORT"),
    )
    tracer.Start(tracer.WithAgentAddr(addr))
    defer tracer.Stop()
}

Further Reading