OpenTelemetry Support
Using OpenTelemetry SDKs

OTel For Go

15min

In this guide, we will walk you through the process of setting up and using OpenTelemetry in Go. You will learn how to instrument a simple application to produce tracing telemetry with OpenTelemetry Go.

Step 1: Prerequisites

Before diving into OpenTelemetry, be sure to have Go 1.16 or newer installed on your local machine

Step 2: Example Application

For this tutorial, we will be using an application that computes Fibonacci numbers for users.

  1. Let’s start by creating a new directory named fib. This directory will contain the Fibonacci project.
Text


2. Create a file named fib.go in the fib directory and add the following code to it:

Text


This is the core logic around which we will create our application.

3. Create another file named app.go and add the following code to it:

Text


4. Create a new file named main.go, which will be used to run the application. Add the following code to it:

Text


5. Run the following command in your terminal in the fib directory to initialize it as a Go module:

Text


6. Run the application using the following command:

Text


Step 3: Trace Instrumentation

Now that our application code is ready, we will be using the OpenTelemetry Trace API from the go.opentelemetry.io/otel/trace package to instrument the application code.

  1. Run the following command in your working directory to install the necessary packages for the Trace API:
Text


2. Next, add imports to your application by adding the following code to the app.go file:

Text


3. The Trace API provides a Tracer to create traces. Add the following code line in app.go to uniquely identify your application the Tracer:

Text


4. Instrument the Run method in your app.go file by updating it with the following code:

Text


5. Instrument the Poll method in your app.go file by updating it with the following code:

Text


6. Instrument the Write method in your app.go file by updating it with the following code:

Text


Step 4: SDK Installation

The OpenTelemetry Go project provides go.opentelemetry.io/otel/sdk SDK package that implements the Trace API that we used in the previous step to instrument the application code.

  1. Run the following command in the fib directory to install the trace STDOUT exporter and the SDK:
Text


2. Next, add the necessary imports to the main.go file.

Text


Step 5: Create a Console Exporter

Exporter packages are required to send the telemetry data to the target system or KloudMate in our case. To create and initialize a console exporter, add the following function to your main.go file:

Text


The otlptracehttp exporter, along with the KloudMate API-Key as the Authorization header will send the data from the collector to KloudMate.

Step 6: Create a Resource

When Telemetry data is produced, it is important to be able to identify what service or service instance the data is coming from in order to solve issues with the service. To represent the entity producing the telemetry data, OpenTelemetry uses a Resource.

To create a Resource for your application, add the following function to the main.go file:

Text


Step 7: Install a Tracer Provider

The instrumented code will produce the telemetry data using Tracer. The Trace provider is required to send the telemetry data from these Tracers to the exporter.

To configure a TraceProvider, update your main function in the main.go file with the following:

Text


Step 8: Run the Instrumented App

You should now have a working application that produces trace telemetry data. Run the application using the following command:

Text


Conclusion

When you run the instrumented application, a new file named traces.txt will be created in your working directory. This file will contain all the traces created from running the application. To learn more, explore the official documentation of the OpenTelemetry Go Project.











Related Resources: