Using instrumentation libraries
Go does not support truly automatic instrumentation like other languages today.
Instead, you'll need to depend on
instrumentation libraries
that generate telemetry data for a particular instrumented library. For example,
the instrumentation library for net/http
will automatically create spans that
track inbound and outbound requests once you configure it in your code.
Setup¶
Each instrumentation library is a package. In general, this means you need to
go get
the appropriate package:
And then configure it in your code based on what the library requires to be activated.
Example with net/http
¶
As an example, here's how you can set up automatic instrumentation for inbound
HTTP requests for net/http
:
First, get the net/http
instrumentation library:
Next, use the library to wrap an HTTP handler in your code:
Assuming that you have a Tracer
and exporter configured, this
code will:
- Start an HTTP server on port
3030
- Automatically generate a span for each inbound HTTP request to
/hello-instrumented
- Create a child span of the automatically-generated one that tracks the work
done in
sleepy
Connecting manual instrumentation you write in your app with instrumentation generated from a library is essential to get good observability into your apps and services.
Available packages¶
A full list of instrumentation libraries available can be found in the OpenTelemetry registry.
Next steps¶
Instrumentation libraries can do things like generate telemetry data for inbound and outbound HTTP requests, but they don't instrument your actual application.
To get richer telemetry data, use manual instrumentation to enrich your telemetry data from instrumentation libraries with instrumentation from your running application.