GoVisual includes OpenTelemetry integration, allowing you to export telemetry data to your preferred backend.
To use OpenTelemetry with GoVisual, you need:
- An OTLP-compatible collector running (such as Jaeger with OTLP enabled)
- GoVisual v1.0.0 or later
The easiest way to get started is with Jaeger. Run it using Docker:
docker run -d --name jaeger \
-e COLLECTOR_OTLP_ENABLED=true \
-p 16686:16686 \
-p 4317:4317 \
-p 4318:4318 \
jaegertracing/all-in-one:latestpackage main
import (
"net/http"
"github.com/doganarif/govisual"
)
func main() {
mux := http.NewServeMux()
// Add your routes
mux.HandleFunc("/api/users", userHandler)
// Enable GoVisual with OpenTelemetry
handler := govisual.Wrap(
mux,
govisual.WithOpenTelemetry(true), // Enable OpenTelemetry
govisual.WithServiceName("my-service"), // Set service name
govisual.WithServiceVersion("1.0.0"), // Set service version
govisual.WithOTelEndpoint("localhost:4317"), // OTLP exporter endpoint
govisual.WithRequestBodyLogging(true), // Log request bodies
govisual.WithResponseBodyLogging(true), // Log response bodies
)
http.ListenAndServe(":8080", handler)
}You can run the included example in the repository:
cd cmd/examples/otel
docker-compose up -d
go run main.goThen visit:
- GoVisual dashboard: http://localhost:8080/\_\_viz
- Jaeger UI: http://localhost:16686
| Option | Description | Default |
|---|---|---|
WithOpenTelemetry(bool) |
Enable OpenTelemetry integration | false |
WithServiceName(string) |
Set the service name for OpenTelemetry | "govisual" |
WithServiceVersion(string) |
Set the service version | "dev" |
WithOTelEndpoint(string) |
Set the OTLP exporter endpoint | "localhost:4317" |
When OpenTelemetry is enabled:
- GoVisual initializes an OpenTelemetry tracer with the provided service name and version
- HTTP requests passing through GoVisual are automatically traced
- Trace data is exported to the configured OTLP endpoint
- The original GoVisual dashboard continues to work alongside OpenTelemetry
You can add custom spans within your request handlers:
func myHandler(w http.ResponseWriter, r *http.Request) {
// Get the context from the request (it contains the parent span from GoVisual)
ctx := r.Context()
// Start a new child span
ctx, span := otel.Tracer("my-service").Start(ctx, "my-operation")
defer span.End()
// Add attributes to the span
span.SetAttributes(attribute.String("user.id", "123"))
// Create nested spans for detailed operations
_, dbSpan := otel.Tracer("my-service").Start(ctx, "database.query")
// ... do database work
dbSpan.End()
// Respond to the client
w.Write([]byte("Hello, world!"))
}If you encounter issues:
- Check that your OpenTelemetry collector is running and accessible
- Ensure the endpoint in
WithOTelEndpoint()matches your collector configuration - Look for initialization errors in your application logs
To see a full working example, refer to the cmd/examples/otel directory in the repository.