Nouvelles annonces sur les technologies sans serveur et réseau ainsi que sur le RUM (Real-User Monitoring) dévoilées à la conférence Dash ! Nouvelles annonces dévoilées à la conférence Dash !

Tracing Go Applications

Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

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
TwirpFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/twitchtv/twirp
VaultFully Supportedgopkg.in/DataDog/dd-trace-go.v1/contrib/hashicorp/vault

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 (
    "log"
    "net/http"
    "strings"

    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) {
    msg: = "Hello " + strings.TrimPrefix(r.URL.Path, "/")
    w.Write([] byte(msg))
}

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.

B3 Headers Extraction and Injection

The Datadog APM tracer supports B3 headers extraction and injection for distributed tracing.

Distributed headers injection and extraction is controlled by configuring injection/extraction styles. Two styles are supported: Datadog and B3.

Configure injection styles using the environment variable DD_PROPAGATION_STYLE_INJECT=Datadog,B3

Configure extraction styles using the environment variable DD_PROPAGATION_STYLE_EXTRACT=Datadog,B3

The values of these environment variables are comma separated lists of header styles that are enabled for injection or extraction. By default only the Datadog extraction style is enabled.

If multiple extraction styles are enabled, extraction attempts are made in the order that those styles are specified. The first successfully extracted value is used.

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