
To set up ddtrace for GoLang, you'll need to install the ddtrace Go agent. This agent can be installed using the following command: `go get -u github.com/ddtrace/dd-trace-go`.
The ddtrace Go agent requires a service name, which can be set using the `service_name` option. This option is used to identify the service in the ddtrace UI.
To enable tracing, you'll need to initialize the tracer. This can be done using the `NewTracer` function, which returns a new tracer instance. The tracer instance can then be used to create a span for the current function.
The tracer instance can be initialized with a service name, which is used to identify the service in the ddtrace UI. This service name can be set using the `service_name` option.
A fresh viewpoint: Golang vs Go
Getting Started
To get started with ddtrace golang, you'll need to have the Agent installed and configured. This is a crucial step before you can begin instrumenting your Go application.
There are two ways to instrument your Go application: compile-time instrumentation and manual instrumentation. The former is a more straightforward approach, while the latter allows for more flexibility and customization.
To choose the right approach for your project, refer to the instructions in the section corresponding to your preference. This will guide you through the process of setting up and configuring ddtrace for your Go application.
Here's a summary of the two instrumentation methods:
Install Sample Go Application
To get started with this tutorial, you'll need to install the sample containerized Go application. This can be done by cloning the GitHub repository at github.com/DataDog/apm-tutorial-golang.git.
The repository contains a multi-service Go application pre-configured to be run within Docker containers. It includes a basic notes app and a calendar app, each with a REST API to add and change data.
The docker-compose YAML files are located in the docker directory. The all-docker-compose.yaml file builds containers for both the notes and calendar applications and the Datadog Agent.
You can find more implementation examples in the main.go file. For an extensive list of libraries, see Go Compatibility Requirements.
Intriguing read: Golang Go
Start Config
To start using a tracer, you'll need to configure the StartSpanConfig, which holds the configuration for starting a new span. This configuration is usually shaped by one or more StartSpanOption functions.
You can start the tracer with the given set of options using the Start function, which will stop and replace any running tracer. Calling Start multiple times will restart the tracer with a new instance.
Before starting a new span, you'll need to configure the StartSpanConfig. If the tracer is not started, calling StartSpan will be a no-op.
You can configure the tracing library to add tags to the telemetry it sends to Datadog by specifying environment variables when running the application. This is done by setting environment variables like DD_ENV, DD_SERVICE, and DD_VERSION in the project Makefile.
Here's an interesting read: How to Update a Github Using Golang
Documentation
Documentation is a crucial part of any project, and in this section, we'll walk you through the process of setting up an opentracing.Tracer using Datadog's tracer.
The example illustrates how to set up an opentracing.Tracer using Datadog's tracer. This is a straightforward process that requires minimal configuration.
You can use the below example as a reference to get started with setting up your tracer. Just copy and paste the code into your project, and you're good to go.
The example code shows how to create a tracer using Datadog's tracer. This is a fundamental step in setting up your project for monitoring and logging.
Additional reading: Golang Source
Overview
The Datadog tracing library is a powerful tool that helps you monitor and understand your application's performance. It's a package called ddtrace that contains the interfaces for various implementations.
To get started, you'll want to visit the documentation for the packages you're interested in. You can find the subdirectories of the ddtrace package here: https://godoc.org/gopkg.in/DataDog/dd-trace-go.v1/ddtrace#pkg-subdirectories.
The ddtrace package includes a native implementation called "tracer", which is a Datadog APM tracing client integration. It's also worth noting that the package has a mock tracer for testing purposes.
You can use the "tracer" package to get started with Datadog's APM tracing. For a simple use case, you can check out the example on the godoc page for the "tracer" package.
Check this out: Create a Package in Golang
Configuration

You can configure the tracing library to add tags to the telemetry it sends to Datadog by specifying environment variables when running the application. The project Makefile includes environment variables like DD_ENV, DD_SERVICE, and DD_VERSION, which are set to enable Unified Service Tagging.
To add tags, you can use environment variables such as DD_ENV, DD_SERVICE, and DD_VERSION. These variables are used to set Unified Service Tagging for your traces.
You can set Unified Service Tagging by setting the corresponding environment variable in your application's runtime environment. The following table shows the Unified Tag and its corresponding environment variable:
This allows you to set Unified Service Tagging for your traces and view them in a meaningful way in dashboards and graphs.
Tracing Configuration
To configure tracing, you can specify environment variables when running your application. This is done by setting environment variables like DD_ENV, DD_SERVICE, and DD_VERSION, which are included in the project Makefile.
These environment variables enable Unified Service Tagging, which helps group, filter, and display data meaningfully in dashboards and graphs. You can find more information on available configuration options in the Configuring the Go Tracing Library documentation.
To add tags to your telemetry, specify environment variables when running your application. The project Makefile includes the environment variables DD_ENV, DD_SERVICE, and DD_VERSION, which are set to enable Unified Service Tagging.
You can also configure tracing by setting the DD_AGENT_HOST environment variable. This variable is used to set up the Agent host and Unified Service Tags for your app and Docker.
Here are the environment variables you can set to enable Unified Service Tagging:
Source Files
Source Files are the backbone of any configuration, and they deserve our attention. They are the files that contain the actual configuration settings, such as the ones we saw in the "Basic Configuration" section.
These files are often plain text files, like the ones we discussed in the "File Formats" section. They can be easily edited with a text editor, making it simple to make changes to the configuration.
The location of these files can vary depending on the system and application, as we learned in the "Configuration Locations" section. But in general, they are stored in a specific directory, such as the one we saw in the example.
For instance, in the "Example Configuration" section, we saw a file named "config.txt" that contained all the necessary settings. This file was stored in the root directory of the application.
A different take: Golang Programs
Instrumentation
Instrumentation is key to getting the most out of ddtrace golang. You can create custom trace spans by annotating functions with the //dd:span directive comment.
To take it to the next level, you can use automatic tracing libraries like go-chi, sql, and http. These libraries are fully supported by Datadog and can be aliased to their Datadog counterparts, such as chitrace, sqltrace, and httptrace.
Initializing these libraries with the WithService option ensures that all elements traced by the library fall under a specific service name. For example, using chitrace.WithService("notes") ensures that all elements traced by the chitrace library fall under the service name "notes".
Automatic Tracing Libraries
Using automatic tracing libraries can simplify the process of instrumenting your application.
Datadog has several fully supported libraries for Go that allow for automatic tracing when implemented in the code.
In the cmd/notes/main.go file, you can see the go-chi, sql, and http libraries being aliased to the corresponding Datadog libraries: chitrace, sqltrace, and httptrace respectively.
Initializing the Datadog libraries with the WithService option ensures that all elements traced by the library fall under a specific service name.
For example, the chitrace library is initialized as follows: chitrace.WithService("notes").
This means that all elements traced by the chitrace library will be categorized under the service name "notes".
Custom Tracing with Context
Custom tracing with context allows you to create spans manually when code doesn't fall under a supported library. You can use the SpanFromContext function to return the span contained in the given context.
To create custom tracing with context, you need to remove comments around certain lines of code in the notes/notesController.go file, specifically the makeSpanMiddleware function. This function generates middleware that wraps a request in a span with the supplied name.
Explore further: Custom Controller to Monitor All Crd Golang
The doLongRunningProcess function in notes/notesHelper.go creates child spans from a parent context. This is a key concept in custom tracing with context.
You can also create a completely separate service from a context using the privateMethod1 function. This function demonstrates how to create a separate service while still propagating spans.
To enable tracing in the calendar application, you need to remove comments around certain lines of code. This will allow you to create spans manually and use custom tracing with context.
Explore further: Golang Reflect to Call Function in Package
TracerMetadata in 1.73.0
In version 1.73.0, TracerMetadata was added to represent the configuration of the tracer.
TracerMetadata is a crucial part of instrumentation, as it allows you to configure the tracer to suit your needs.
With the addition of TracerMetadata, you can now customize the tracer's behavior to better suit your application's requirements.
TracerMetadata represents the configuration of the tracer, making it easier to understand and manage the tracer's settings.
This is a significant improvement, as it makes it easier to troubleshoot and optimize your application's performance.
The tracer's configuration is now more transparent, thanks to TracerMetadata, which represents the configuration of the tracer.
Error Handling
Error handling is a crucial aspect of any application, and ddtrace golang has got you covered.
If the annotated function returns an error result, any error returned by the function will be automatically attached to the corresponding trace span. This ensures that errors are properly tracked and analyzed.
The WithError function is a powerful tool for marking spans as having had an error. It uses the information from the error to set tags such as the error message, error type, and stack trace. If the error is nil, it has no effect, so be sure to pass a valid error object.
Error Results
If the annotated function returns an error result, any error returned by the function will be automatically attached to the corresponding trace span.
When an error occurs, the function will return an error result, and this result will be attached to the trace span. This means you can easily track and diagnose issues in your code.

The error result will include the error message, error type, and stack trace, making it easier to identify and fix the problem.
In fact, the error result will be automatically attached to the corresponding trace span, so you don't need to worry about manually handling the error.
The WithError function can also be used to mark a span as having had an error, using the information from the error to set relevant tags.
Recommended read: Golang Create Error
Troubleshooting
Troubleshooting is a crucial part of error handling. If you're not receiving traces as expected, set up debug mode for the Go tracer.
Sometimes, things don't go as planned, and that's where debugging comes in. Read Enable debug mode to find out more on how to do it.
In some cases, the issue might be more complex and require a closer look. For that, setting up debug mode is a great first step.
Expand your knowledge: Golang Run Debug Mode
Integration
To integrate ddtrace golang, you need to activate Go integrations to create spans.
Datadog has pluggable packages that support instrumenting libraries and frameworks.
These packages are listed on the Compatibility Requirements page.
Import the packages into your application and follow the configuration instructions.
Datadog Go Client
The Datadog Go Client is a set of packages for the client-side components of the Datadog product suite for Application Performance Monitoring, Continuous Profiling and Application Security Monitoring of Go applications.
These packages are built to support Go applications, and they're designed to work seamlessly with the Datadog Agent and API.
The Datadog Go Client supports the two latest releases of Go, which matches the official Go policy.
This library only officially supports first-class ports of Go, so make sure you're using a supported version to get the most out of it.
The Datadog Go Client is a powerful tool for monitoring and profiling Go applications, and it's a great choice for anyone looking to improve their app's performance and security.
Consider reading: Read a Custom Resource Using Cynamic Client Golang
Advanced Topics
In ddtrace golang, you can use the `WithServiceName` method to set the service name for a tracer. This is useful for distinguishing between different services in your application.
The `WithServiceName` method takes a string argument, which is the name of the service. For example, `tracer.WithServiceName("my_service")` would set the service name to "my_service".
Check this out: Golang Set Env Variable
ddtrace golang also supports distributed tracing, which allows you to see how different services interact with each other. This is particularly useful for complex applications with many interconnected services.
To enable distributed tracing, you need to configure the tracer to use a tracing backend, such as Jaeger or Zipkin. This can be done using the `WithBackend` method, for example: `tracer.WithBackend("jaeger")`.
Logger and Propagator
Logger implementations can log messages output by the tracer or profiler.
The tracer or profiler might output messages that the logger can log.
Propagator implementations should be able to inject and extract SpanContexts into an implementation specific carrier.
To determine the inject and extract propagators, environment variables are used with the following order of precedence:
- DD_TRACE_PROPAGATION_STYLE_INJECT
- DD_PROPAGATION_STYLE_INJECT (deprecated)
- DD_TRACE_PROPAGATION_STYLE (applies to both inject and extract)
- If none of the above, use default values
Logger in 1.15.0
Logger in 1.15.0 is a significant update that brings new functionality to the table.
Logger implementations are able to log given messages that the tracer or profiler might output.
With the introduction of WithLogger, you can set a logger as the tracer's error printer.
Diagnostic and startup tracer logs are prefixed to simplify the search within logs.
If you need JSON logging format, you can wrap tracer logs using an existing JSON logger with the WithLogger function.
Additional reading: Convert a Map to Json Golang
NewPropagator
The NewPropagator function returns a new propagator that uses TextMap to inject and extract values. It propagates trace and span IDs and baggage.
To use the defaults, nil may be provided in place of the config. This is a convenient option for those who don't need to customize their propagator settings.
The NewPropagator function uses environment variables to determine the inject and extract propagators. The order of precedence is determined by the following environment variables.
- DD_TRACE_PROPAGATION_STYLE_INJECT
- DD_PROPAGATION_STYLE_INJECT (deprecated)
- DD_TRACE_PROPAGATION_STYLE (applies to both inject and extract)
- If none of the above, use default values
This means that if you need to customize your propagator settings, you can do so by setting these environment variables. If you don't need to customize, you can simply use the defaults.
Event in 1.73.0
In the latest update of ddtrace golang, you can now examine a trace with ease. On the Traces page, click on a POST /notes trace to see a flame graph that shows how long each span took and what other spans occurred before a span completed.

The flame graph is a visual representation of the trace, with the width of a bar indicating how long it took to complete. A bar at a lower depth represents a span that completes during the lifetime of a bar at a higher depth.
You can also use WithSpanEventAttributes to set the given attributes for the span event, which is a new feature added in version 1.73.0. This feature allows you to customize the span event to better suit your needs.
The flame graph for a POST trace looks something like this: a visual representation of the trace with bars indicating the duration of each span. The bar at the top of the graph is the span you selected on the previous screen.
Related reading: Golang Progressbar
Featured Images: pexels.com

