HTTP in Go

Use the instructions below to instrument HTTP in your Go service.

Now that your Go tracer provider is all set, you may instrument your HTTP client and server to get end-to-end visibility and troubleshooting capabilities in Helios.

Package net/http

In order to instrument and track HTTP requests received by your net/http implemented server or client, wrap each handler with an OpenTelemetry middleware, defining also a span name corresponding to the handler's operation (install the dependency first with go get go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp):

import (
    "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
    "net/http"
)

//server side
http.Handle("/your-router", otelhttp.NewHandler(http.HandlerFunc(yourHandler), "your-span-name"))
// For example:
http.Handle("/account", otelhttp.NewHandler(http.HandlerFunc(getAccount), "GET account"))
//client side 
client := http.Client{Transport: otelhttp.NewTransport(http.DefaultTransport),}
// Make sure to pass the context to the request to avoid broken traces
req, err := http.NewRequestWithContext(context.Background(), "<GET/POST>", "<destinationRoute>", nil)
// For example
req, err := http.NewRequestWithContext(context.Background(), "GET", "localhost:8080/hello", nil)

πŸ‘

All set

After setup is complete and once the service is up and running, it will show up in the Helios application.


Did this page help you?