Monitor Gorilla Mux with OpenTelemetry

gorilla/mux implements a request router and dispatcher for matching incoming requests to their respective handler. It one of the oldest and most popular routers for Go.

This article explains how you can monitor and optimize Gorilla Mux performance using OpenTelemetry observability framework.

What is OpenTelemetry?

OpenTelemetryopen in new window is an open-source observability framework for distributed tracingopen in new window (including logs and errors) and metricsopen in new window.

Otel allows developers to collect and export telemetry data in a vendor agnostic way. With OpenTelemetry, you can instrumentopen in new window your application once and then add or change vendors without changing the instrumentation, for example, here is a list popular DataDog alternativesopen in new window that support OpenTelemetry.

OpenTelemetry is available for most programming languages and provides interoperability across different languages and environments.

Installation

To install Gorilla Mux OpenTelemetry instrumentation:

go get go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux

Usage

You can instrument Gorilla Mux router by installing OpenTelemetry middleware:

import (
	"github.com/gorilla/mux"
	"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
)

router := mux.NewRouter()
router.Use(otelmux.Middleware("service-name"))

What is Uptrace?

Need a backend to store telemetry data collected by OpenTelemetry instrumentations?

Uptrace is an open source APM for OpenTelemetry with an intuitive query builder, rich dashboards, automatic alerts, and integrations for most languages and frameworks.

You can get startedopen in new window with Uptrace by downloading a DEB/RPM package or a pre-compiled Go binary.

What's next?

Next, instrument more operations, for example, database queries, errors, and logs. You can also learn about OpenTelemetry Go Tracing API to create your own instrumentations.

Popular instrumentations:

Last Updated: