Stay organized with collections Save and categorize content based on your preferences.
This document describes how to add observability code to your application by using OpenTelemetry. OpenTelemetry provides instrumentation libraries that generate telemetry for popular frameworks. You can augment the library-generated telemetry by adding custom instrumentation that measures your application-specific behavior.
The principles and concepts described in this document can be applied to apps written in all languages supported by OpenTelemetry. To learn more about instrumentation, see the following documents:
The sample code, which is the same Go app that is described in Go instrumentation sample, is available in GitHub. To view the full sample, click more_vert More, and then select View on GitHub.
Before you beginEnable the Cloud Logging, Cloud Monitoring, and Cloud Trace APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin
), which contains the serviceusage.services.enable
permission. Learn how to grant roles.
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:
The tracer name, which is represented by scopeName
, identifies the instrumentation scope of the generated traces.
Use the tracer
instance to create spans. In the following code sample, the computeSubrequests
function generates a span whenever it is called:
In the previous code sample, the span generated from the computeSubrequests
function represents the work done by the entire function. This is because the first step of the function is to start a new span using tracer.Start
and the defer
keyword before the span.End()
ensures that the span is ended right before the function exits.
End()
to complete the span. OpenTelemetry only exports completed spans.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 meter
instance to create instruments which can record metrics. For example, in the following code, we use the meter
to create an OpenTelemetry Histogram:
This previous code generates a histogram named sleepHistogram
.
Use the sleepHistogram
instance to record the sleep time, which is determined when the function randomSleep
is invoked:
The recorded metrics from these instruments are exported from your application based on your OpenTelemetry exporter configuration.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-10-09 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-10-09 UTC."],[],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.5