Golang HTTP Performance and Errors Monitoring
You can monitor HTTP performance and errors using distributed tracing and metrics by OpenTelemetry.
OpenTelemetry 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 bunrouterotel 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 context and pass it to other functions:
ctx := req.Context()
op1(ctx)
op2(ctx)
Uptrace
Uptrace is an OpenTelemetry distributed tracing tool that monitors performance and helps finding performance bottlenecks.
As expected, BunRouter instrumentation creates spans for processed requests and records any errors as they occur. Here is how the collected information is displayed at Uptrace:
You can find a runnable example on GitHub.