Monitor Gin performance with OpenTelemetry

Gin is one of the fastest full-featured web frameworks for Go. Gin allows you to build web applications and microservices in Go.

In this article you will learn how to monitor and optimize Gin performance using OpenTelemetry observability framework.

What is OpenTelemetry?

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

Otel specifies how 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, many open source tracing toolsopen in new window already support OpenTelemetry.

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

Installation

To install Gin OpenTelemetry instrumentation:

go get go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin

Usage

You can instrument Gin router by installing OpenTelemetry middleware:

import (
	"github.com/gin-gonic/gin"
	"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
)

router := gin.Default()
router.Use(otelgin.Middleware("service-name"))

Instrumenting templates rendering

To instrument templates rendering, use otelgin.HTML helper:

import "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"

func exampleHandler(c *gin.Context) {
	otelgin.HTML(c, http.StatusOK, indexTmpl, gin.H{
		"foo": "bar",
	})
}

What is Uptrace?

Uptrace is an open source APMopen in new window for OpenTelemetry that helps developers pinpoint failures and find performance bottlenecks. Uptrace can process billions of spans on a single server and allows to monitor your software at 10x lower cost.

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:

See also:

Last Updated: