Prerequisites
- Go 1.19 or higher: Ensure you have Go version 1.19 or higher installed in your environment.
- Go app: Use your own app written in Go or start with the provided
main.gosample below. - Create an Axiom account.
- Create a dataset in Axiom where you send your data.
- Create an API token in Axiom with permissions to create, read, update, and delete datasets.
Installing Dependencies
First, run the following in your terminal to install the necessary Go packages:Initializing a Go module and managing dependencies
Before installing the OpenTelemetry dependencies, ensure your Go project is properly initialized as a module and all dependencies are correctly managed. This step is important for resolving import issues and managing your project’s dependencies effectively.Initialize a Go module
If your project is not already initialized as a Go module, run the following command in your project’s root directory. This step creates ago.mod file which tracks your project’s dependencies.
<module-name> with your project’s name or the GitHub repository path if you plan to push the code to GitHub. For example, go mod init github.com/yourusername/yourprojectname.
Manage dependencies
After initializing your Go module, tidy up your project’s dependencies. This ensures that yourgo.mod file accurately reflects the packages your project depends on, including the correct versions of the OpenTelemetry libraries you’ll be using.
Run the following command in your project’s root directory:
go.mod and go.sum files accordingly. It’s a good practice to run go mod tidy after adding new imports to your project or periodically to keep dependencies up to date.
HTTP server configuration (main.go)
main.go is the entry point of the app. It invokes InstallExportPipeline from exporter.go to set up the tracing exporter. It also sets up a basic HTTP server with OpenTelemetry instrumentation to demonstrate how telemetry data can be collected and exported in a simple web app context. It also demonstrates the usage of span links to establish relationships between spans across different traces.
Exporter configuration (exporter.go)
exporter.go is responsible for setting up the OpenTelemetry tracing exporter. It defines the resource attributes, initializes the tracer, and configures the OTLP (OpenTelemetry Protocol) exporter with appropriate endpoints and headers, allowing your app to send telemetry data to Axiom.
Run the app
To run the app, execute bothexporter.go and main.go. Use the command go run main.go exporter.go to start the app. Once your app is running, traces collected by your app are exported to Axiom. The server starts on the specified port, and you can interact with it by sending requests to the /rolldice endpoint.
For example, if you are using port 8080, your app will be accessible locally at http://localhost:8080/rolldice. This URL will direct your requests to the /rolldice endpoint of your server running on your local machine.
Observe the telemetry data in Axiom
After deploying your app, you can log into your Axiom account to view and analyze the telemetry data. As you interact with your app, traces will be collected and exported to Axiom, where you can monitor and analyze your app’s performance and behavior.
Observing the Telemetry Data in Axiom image
Dynamic OpenTelemetry traces dashboard
This data can then be further viewed and analyzed in Axiom’s dashboard, providing insights into the performance and behavior of your app.
Dynamic OpenTelemetry Traces Dashboard Go
Send data from an existing Golang project
Manual Instrumentation
Manual instrumentation in Go involves managing spans within your code to track operations and events. This method offers precise control over what is instrumented and how spans are configured.- Initialize the tracer:
- Create and manage spans:
- Annotate spans:
Automatic Instrumentation
Automatic instrumentation in Go uses libraries and integrations that automatically create spans for operations, simplifying the addition of observability to your app.- Instrumentation libraries:
OpenTelemetry-contrib libraries designed for automatic instrumentation of standard Go frameworks and libraries, such as net/http.
- Wrap handlers and clients:
otelhttp.NewHandler.
- Minimal code changes:
Reference
List of OpenTelemetry trace fields
List of imported libraries
OpenTelemetry Go SDK
go.opentelemetry.io/otel
This is the core SDK for OpenTelemetry in Go. It provides the necessary tools to create and manage telemetry data (traces, metrics, and logs).
OTLP Trace Exporter
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp
This package allows your app to export telemetry data over HTTP using the OpenTelemetry Protocol (OTLP). It’s important for sending data to Axiom or any other backend that supports OTLP.
Resource and Trace Packages
go.opentelemetry.io/otel/sdk/resource and go.opentelemetry.io/otel/sdk/trace
These packages help define the properties of your telemetry data, such as service name and version, and manage trace data within your app.
Semantic Conventions
go.opentelemetry.io/otel/semconv/v1.24.0
This package provides standardized schema URLs and attributes, ensuring consistency across different OpenTelemetry implementations.
Tracing API
go.opentelemetry.io/otel/trace
This package offers the API for tracing. It enables you to create spans, record events, and manage context propagation in your app.
HTTP Instrumentation
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp
Used for instrumenting HTTP clients and servers. It automatically records data about HTTP requests and responses, which is essential for web apps.
Propagators
go.opentelemetry.io/otel/propagation
This package provides the ability to propagate context and trace information across service boundaries.