Golang HTTP Performance and Errors Monitoring

You can monitor HTTP performance and errors using OpenTelemetry tracingopen in new window and OpenTelemetry metricsopen in new window.

What is OpenTelemetry?

OpenTelemetryopen in new window is a vendor-neutral API for distributed traces and metrics. It specifies how to collect and send telemetry data to backend platforms. It means that you can instrument your application once and then add or change vendors (backends) as required.

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

BunRouter OpenTelemetry instrumentation

BunRouter comes with an OpenTelemetry instrumentation called bunrouterotelopen in new window that is distributed as a separate module:

go get github.com/uptrace/bunrouter/extra/bunrouterotel

To instrument BunRouter, you need to add the middleware provided by bunrouterotel:

import (
	"github.com/uptrace/bunrouter/extra/bunrouterotel"
	"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

router := bunrouter.New(
	bunrouter.Use(bunrouterotel.NewMiddleware(
		bunrouterotel.WithClientIP(),
	)),
)
handler := otelhttp.NewHandler(router, "")

httpServer := &http.Server{
	Handler: handler,
}

You can then retrieve the active span contextopen in new window and pass it to other functions:

ctx := req.Context()
op1(ctx)
op2(ctx)

Uptrace

Uptrace is an open-source APMopen in new window that supports distributed tracing, metrics, and logs. You can use it to monitor applications and set up automatic alerts to receive notifications via email, Slack, Telegram, and more.

BunRouter instrumentation creates spans for processed requests and records any errors as they occur. Here is how the collected information is displayed at Uptrace:

Bunrouter trace

You can find a runnable example on GitHubopen in new window.

See also