Skip to main content

Go Net/HTTP

Prerequisites

Go tracer requires Go version 1.18 or higher and Datadog Agent version 5.21.1 or later.

Installation

  1. Install dependencies:

    go get github.com/DataDog/dd-trace-go/contrib/net/http/v2 \
    github.com/DataDog/dd-trace-go/v2/ddtrace/tracer
  2. Add the highlighted lines below to your project's main.go file:

    package main

    import ddhttp "github.com/DataDog/dd-trace-go/contrib/net/http/v2"
    import "github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"

    func main() {
    tracer.Start()
    defer tracer.Stop()

    handleFunc := func(pattern string, handlerFunc func(http.ResponseWriter, *http.Request)) {
    wrapped := ddhttp.WrapHandler(http.HandlerFunc(handlerFunc), "go-sample-app", pattern)
    mux.Handle(pattern, wrapped)
    }
    }
  3. Configure the tracer.

     DD_SERVICE=<app_name>
    # send traces to CubeAPM
    DD_TRACE_AGENT_URL=http://<ip_address_of_cubeapm_server>:3130

    # optional settings
    DD_ENV=myenv
    DD_VERSION=1.2.3
    DD_TAGS=mykey1:myvalue1,mykey2:myvalue2

Sample Application

A working example is available at https://github.com/cubeapm/sample_app_go_net_http/tree/datadog.

Troubleshooting

The following can be used for debugging:

# Print Datadog tracer startup logs on screen
DD_TRACE_STARTUP_LOGS=true
# Enable Datadog tracer debug logging if needed to see detailed logs
DD_TRACE_DEBUG=true