Adding custom OpenTelemetry instrumentation to your application code lets you record custom trace spans and metrics that measure application-specific behavior beyond what standard framework libraries collect. You can apply the concepts in this document to applications written in any language supported by OpenTelemetry; for background information, see Observability for application developers and Choose an instrumentation approach.
The examples in this document use the Go sample application described in Go instrumentation sample, which is available in GitHub.
Before you begin
Enable the Cloud Logging, Cloud Monitoring, and Cloud Trace APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the serviceusage.services.enable permission. If you
created the project, then you likely already have this permission through the
Owner role (roles/owner). Otherwise, you can get this permission through the
Service Usage Admin role (roles/serviceusage.serviceUsageAdmin).
Learn how to grant roles.
Create custom traces
To generate custom traces from your application, you add instrumentation code that creates OpenTelemetry spans. In OpenTelemetry, spans are the building blocks for traces.
To create a span, do the following:
Modify your app to acquire an OpenTelemetry
Tracer. In OpenTelemetry, a tracer is a creator of spans. You can acquire a tracer as demonstrated in the following code. To view the full sample, in the sample's toolbar, select the GitHub logo.The tracer name, which is represented by
scopeName, identifies the instrumentation scope of the generated traces.Use the
tracerinstance to create spans. In the following code sample, thecomputeSubrequestsfunction generates a span whenever it is called:In the previous code sample, the span generated from the
computeSubrequestsfunction represents the work done by the entire function. This is because the first step of the function is to start a new span usingtracer.Startand thedeferkeyword before thespan.End()ensures that the span is ended right before the function exits.
Create custom metrics
To generate metrics from your application, you add instrumentation code that records measurements taken during your app's execution.
To create metrics, do the following:
Modify your app to acquire an OpenTelemetry
Meter. In OpenTelemetry, a meter provides access to metric instruments for recording metrics. You can acquire a meter as demonstrated in the following code:The meter name, which is represented by
scopeName, identifies the instrumentation scope of the generated metrics.Use the
meterinstance to create instruments that can record metrics. For example, the following code uses themeterto create an OpenTelemetry Histogram:The previous code generates a histogram named
sleepHistogram.Use the
sleepHistograminstance to record the sleep time, which is determined when the functionrandomSleepis invoked:The recorded metrics from these instruments are exported from your application based on your OpenTelemetry exporter configuration.
What's next
- Correlate metrics and traces by using exemplars
- OpenTelemetry
- OpenTelemetry Instrumentation
- OpenTelemetry Metrics Data Model