otel golang example: Monitoring and Tracing Made Easy

Author

Reads 894

Black and white image of a Mediterranean hotel balcony with ornate railings.
Credit: pexels.com, Black and white image of a Mediterranean hotel balcony with ornate railings.

Monitoring and tracing are crucial aspects of any application, and OpenTelemetry (OTel) makes it easy to implement them in Go.

In a Go application, OTel allows you to export metrics and traces using the same API, making it a unified and efficient way to monitor your application.

To get started with OTel in Go, you need to create an OTel provider and a tracer. This is done using the `otelinit` function, which initializes the OTel provider and returns a tracer.

With OTel, you can easily add tracing and monitoring to your Go application, without having to worry about the underlying complexities.

Prerequisites

Before we dive into the example, make sure you have Go 1.23 or greater installed locally.

You can check your Go version by running the command `go version` in your terminal. If you're running an older version, you can update it using the official Go installation instructions.

To confirm, ensure you have Go 1.23 or greater installed, and you're ready to move forward with the example.

Curious to learn more? Check out: Golang Go

Instrumenting Your Application

Credit: youtube.com, GopherCon Europe 2021: Ricardo Ferreira - OpenTelemetry for Dummies: Instrumenting Go Apps

Instrumenting your application is a crucial step in getting the most out of OpenTelemetry. To do this, you'll need to add OpenTelemetry instrumentation to your sample app, or modify your own application's code to include instrumentation.

You can start by instrumenting the HTTP server, which is a key part of most applications. This involves modifying your main.go file to include code that sets up OpenTelemetry SDK and instruments the HTTP server using the otelhttp instrumentation library.

If you're using a Golang application, you can follow the steps outlined in the OpenTelemetry documentation to instrument your app correctly. This involves getting a sample Golang app from GitHub, installing dependencies, declaring environment variables, and initializing the OpenTelemetry SDK.

To instrument your Go application, you'll need to add the following code to your main.go file:

  • Declare environment variables for configuring OpenTelemetry
  • Initialize the OpenTelemetry SDK
  • Instrument the HTTP server using the otelhttp instrumentation library
  • Add the OpenTelemetry Gin middleware
  • Set environment variables and run your Go Gin application

Here's a summary of the steps you'll need to take:

By following these steps, you'll be able to instrument your application and start collecting telemetry data. Remember to set the environment variables correctly, as this will determine where your data is sent and how it's processed.

In addition to instrumenting your HTTP server, you may also want to add custom instrumentation to capture more detailed information about your application's performance. This can be done using the OpenTelemetry API, which provides a range of tools and libraries for instrumenting your application.

Intriguing read: Golang Programs

Instrumenting HTTP Server

Cables Connected on Server
Credit: pexels.com, Cables Connected on Server

Instrumenting the HTTP server is a crucial step in getting OpenTelemetry up and running in your Golang application. This involves using the otelhttp instrumentation library to capture telemetry at the edges of your system.

To instrument the HTTP server, you'll need to modify your main.go file to include code that sets up the OpenTelemetry SDK and instruments the HTTP server. This involves using the otelhttp instrumentation library.

The otelhttp instrumentation library allows you to capture telemetry at the edges of your system, including inbound and outbound HTTP requests. By using this library, you can get a clear picture of how your application is performing.

Here are the steps to instrument the HTTP server:

1. Modify main.go to include code that sets up OpenTelemetry SDK and instruments the HTTP server using the otelhttp instrumentation library.

By following these steps, you'll be able to instrument your HTTP server and start capturing telemetry data. This will give you a better understanding of how your application is performing and help you identify areas for improvement.

Broaden your view: Golang Test Main

Open Telemetry

Credit: youtube.com, Implementing Distributed Tracing in Golang with OpenTelemetry

Open Telemetry is a versatile, open-source, and vendor-agnostic framework empowering developers and DevOps specialists with comprehensive tools for observing and monitoring applications. It covers logging, metrics, and traces to gain insights into application performance.

Logging is a familiar concept, but metrics and traces may require clarification. Observability is the effective propagation of context, which we'll explore later.

Open Telemetry provides a robust foundation for application observability and monitoring capabilities. The core idea behind observability is the effective propagation of context.

Open Telemetry Intro

Open Telemetry is an open-source framework that's vendor-agnostic, meaning it works with any technology stack.

Developers and DevOps specialists use Open Telemetry to observe and monitor applications, which is crucial for understanding how they perform.

Open Telemetry provides tools for logging, metrics, and traces, giving you a comprehensive view of your application's behavior.

Logging is a familiar concept, but metrics and traces might need some explanation.

Metrics measure the performance of your application, such as how long it takes to complete tasks or how many users are interacting with it.

Traces help you understand the flow of data through your application, identifying bottlenecks and areas for improvement.

Open Telemetry gives you the tools to gain insights into your application's performance, making it easier to identify and fix issues.

OTel Best Practices

Credit: youtube.com, OpenTelemetry in 180 seconds

To get the most out of Open Telemetry, it's essential to engage in hands-on activities. Setting up a Trace Provider, working with Spans, Span Events, and Span Records, and understanding the nuances of baggage usage contribute to a comprehensive and practical understanding of Open Telemetry in action.

Effective instrumentation requires a thorough understanding of observability, which is the effective propagation of context. This allows for seamless data gathering at different points throughout the lifespan of a request.

To define resources in Golang, you can pull attributes from environment variables, configure detectors for various types of information, and even incorporate custom external detectors. This flexibility is showcased in the Otel's Golang documentation.

Resource attributes can be specified directly for a more tailored approach, allowing you to maximize the capabilities of Open Telemetry in your application.

Monitoring and Tracing

Monitoring and tracing are essential components of any distributed system, and OpenTelemetry makes it easy to implement in a Go application. With OpenTelemetry, you can monitor application metrics and tracing data to understand how user requests perform across services.

Credit: youtube.com, OpenTelemetry Golang Tutorial (Tracing in Grafana & Kubernetes & Tempo)

You can monitor application metrics like application latency, requests per second, error percentage, etc. with the Metrics tab of SigNoz. The Traces tab of SigNoz allows you to analyze tracing data using filters based on tags, status codes, service names, operations, etc.

To set up tracing, you need to initialize the trace provider and exporter, instrument the application by creating spans for each operation, and wrap your HTTP handler to enable automatic instrumentation with OpenTelemetry. The tracer provider is crucial as it manages the creation of tracers, which are used to generate the telemetry data, including spans.

Metrics: Unveiling Insights

Monitoring your application's performance is crucial to ensure it's running smoothly. With OpenTelemetry, you can collect and export metrics such as application latency, requests per second, and error percentage.

These metrics can be visualized using services like SigNoz, where you can see your application's performance in real-time. In SigNoz, you can monitor application metrics like application latency and error percentage with the Metrics tab.

If this caught your attention, see: Golang Create Error

Credit: youtube.com, Metrics, Logs and Traces: What To Observe and Why

You can also observe various aspects of your application's performance, such as resource utilization and garbage collection metrics. Some frameworks offer default metrics, but they still need to be gathered and exported through a collector for monitoring.

This process allows you to gain insights into your application's performance, enabling you to identify bottlenecks and areas for improvement. By analyzing metrics, you can make data-driven decisions to optimize your application's performance.

Distributed Tracing

Distributed tracing enables request tracking across multiple services. This allows you to visualize how user requests perform across services in a multi-service application.

To use OpenTelemetry's context propagation for distributed tracing and span correlation in a Go application, you need to follow these steps. These steps include initializing the trace provider and exporter, instrumenting the application, wrapping your HTTP handler, and setting up the OpenTelemetry middleware.

A trace is a representation of a request as it moves through the various services of an application. Each trace is made up of several spans, where a span represents a single operation or a unit of work performed within a service.

Credit: youtube.com, Distributed Tracing Explained: Understanding Microservice Observability

Here's an example of how a user request can trigger a series of operations across different services:

  • Span 1: Web server processes the request.
  • Span 2: Authentication service verifies user identity.
  • Span 3: Database service retrieves the order details.

Each span includes timing data, helping to identify how long each service took to complete its part of the request. This helps in pinpointing where delays occur if the request takes longer than expected.

A tracer provider is crucial as it manages the creation of tracers, which are used to generate the telemetry data. The tracer provider includes resource attributes that append metadata, such as the service name to each trace.

Implementation Guide

To implement OpenTelemetry in your Go application, you'll need to ensure your version of Go is supported by OTel, which is 1.16 or newer.

First, create a Dockerfile with the following content to get started with the installation.

Once the installation is complete, you can build the application by running the following command in the terminal: Build the application by running the following command in the terminal.

Consider reading: Golang Test Command

Customization and Integration

Credit: youtube.com, Sick of Digging Through Logs? Find Errors FAST with OpenTelemetry and Go!

You can add custom instrumentation to your application using the OpenTelemetry API, which captures what's going on inside your system.

This allows you to track specific events and metrics that aren't captured by instrumentation libraries.

To do this, you'll need to modify your code, such as in the rolldice.go file, to include custom instrumentation.

If you're only using tracing or metrics, you can omit the corresponding code that instruments the other telemetry type.

This means you can tailor your instrumentation to your specific needs and focus on the types of data that matter most to you.

Take a look at this: Golang Source Code

Jaeger and SigNoz

Jaeger and SigNoz are two popular tools used for monitoring and observability in distributed systems. Jaeger is an open-source distributed tracing system.

Jaeger can help identify performance bottlenecks in complex systems by providing a detailed view of how requests flow through the system. This is particularly useful for Go applications, as they often involve multiple services and microservices.

Credit: youtube.com, Go instrumentation - OpenTelemetry in Go applications : Complete Tutorial with SigNoz

Jaeger is designed to work with many different programming languages, including Go. In the context of otel golang example, Jaeger can be used to collect and visualize traces of Go applications.

SigNoz is another tool that can be used for monitoring and observability. It's a open-source APM (Application Performance Monitoring) tool designed specifically for Go applications.

REST API and Exporter

Instrumenting a Go REST API is where the magic happens, and it's where you get to implement tracing and track requests within your application. This gives you a clear picture of its performance.

You'll first need to install the necessary packages that enable OpenTelemetry, including the OpenTelemetry SDKs to collect telemetry data.

An exporter is responsible for sending telemetry data to one or more backends or a collector. The data flow typically looks like this:

  • Application Instrumentation: The application uses OpenTelemetry SDKs to collect telemetry data.
  • Exporter: Configured within the application, the exporter sends this data to a collector.
  • Collector: Receives the data and can perform various functions such as aggregation, transformation, or enrichment before exporting it to the final backend system.

To initialise an exporter, you define the HTTP headers that the exporter will use when sending data. The content-type header is set to application/json, which tells the receiver to expect the data in JSON format.

From above crop faceless male developer in black hoodie writing software code on netbook while working in light studio
Credit: pexels.com, From above crop faceless male developer in black hoodie writing software code on netbook while working in light studio

The core of the function involves setting up an OTLP (OpenTelemetry Protocol) HTTP exporter, which can be configured to send data to a local collector running on port 4318.

To configure your application to send data, you'll need a function to initialize OpenTelemetry. This function should be added to your main.go file after the variable declaration block.

You'll also need to initialise the tracer in your main function, at the very beginning. This will enable your application to send telemetry data to a collector.

The exporter can be configured to use an insecure connection, which is not recommended for production environments but is common in local testing environments.

For another approach, see: Golang Function Type

Setup

To set up an otel golang example, start by creating a new directory and adding a go.mod file to it. This will help you manage dependencies for your project.

Next, create a file called main.go and add some code to it. You'll also need to create another file called rolldice.go and add code to that file as well.

For more insights, see: Golang Add to Map

Credit: youtube.com, OpenTelemetry Deep Dive: Golang

In the same directory, you can also create files like jaeger-ui.json, otel-collector-config.yml, prometheus.yml, and docker-compose.yml to set up the necessary containers. Don't forget to create a Makefile to help you easily set up and clean things.

To begin with, create a new directory and add a go.mod file to it. This will help you manage dependencies for your project.

Cory Hayashi

Writer

Cory Hayashi is a writer with a passion for technology and innovation. He started his career as a software developer and quickly became interested in the intersection of tech and society. His writing explores how emerging technologies impact our lives, from the way we work to the way we communicate.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.