Golang HTTP Performance and Errors Monitoring

You can monitor HTTP performance and errors using distributed tracingopen in new window and metricsopen in new window by 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.

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 OpenTelemetry distributed tracing toolopen in new window that monitors performance and helps finding performance bottlenecks.

As expected, BunRouter instrumentation creates spansopen in new window for processed requests and records any errors as they occur. Here is how the collected information is displayed at Uptraceopen in new window:

Bunrouter trace

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

See also