Configure the application for basic request tracing.
This function configures basic request tracing toCloud Trace. Thegoogle-cloud-cpplibraries useOpenTelemetryto provide observability into their operation at runtime.
You do not need to add OpenTelemetry instrumentation to your code. The C++ client libraries are already instrumented and all sampled RPCs will be sent to Cloud Trace. However, you may want to add instrumentation if multiple RPCs are performed as part of a single logical "operation" in your application.
OpenTelemetry traces, including those reported by the C++ client libraries start as soon as this function returns. Tracing stops when the object returned by this function is deleted.
OpenTelemetry is very configurable, supporting different sampling rates and filters, multiple "exporters" to send the collected data to different services, and multiple mechanisms to chain requests as they move from one program to the next. We do not expect this function will meet the needs of all applications. However, some applications will want a basic configuration that works with Gooogle Cloud Trace.
This function uses theOpenTelemetry C++ APIto change the global trace provider (opentelemetry::trace::Provider::#SetTraceProvider()). Do not use this function if your application needs fine control over OpenTelemetry settings.
Usage Example
Change your build scripts to also build and link the library that provides this function.
Change your application to call this function once, for example inmain()as follows:
#include <google/cloud/opentelemetry/configure_basic_tracing.h>
...
int main(...) {
...
auto tracing_project = std::string([TRACING PROJECT]);
auto tracing = google::cloud::opentelemetry::ConfigureBasicTracing(
google::cloud::Project(tracing_project));
}
Where[TRACING PROJECT]is the project id where you want to store the traces.
Permissions
The principal (user or service account) running your application will needcloud.traces.patchpermissions on the project where you send the traces. These permissions are typically granted as part of theroles/cloudtrace.agentrole. If the principal configured in yourApplication Default Credentialsdoes not have these permissions you will need to provide a different set of credentials:
auto credentials = google::cloud::MakeServiceAccountCredentials(...);
auto tracing = google::cloud::opentelemetry::ConfigureBasicTracing(
google::cloud::Project(tracing_project),
google::cloud::Options{}
.set<google::cloud::UnifiedCredentialsOption>(credentials));
Sampling Rate
By default this function configures the application to trace all requests. This is useful for troubleshooting, but it is excessive if you want to enable tracing by default and use the results to gather latency statistics. To reduce the sampling rate use@refBasicTracingRateOption. If desired, you can use an environment variable (or any other configuration source) to initialize its value.
namespace gc = ::google::cloud;
[](std::string project_id) {
auto project = gc::Project(std::move(project_id));
auto options = gc::Options{}.set<gc::otel::BasicTracingRateOption>(.001);
auto configuration = gc::otel::ConfigureBasicTracing(project, options);
MyApplicationCode();
}
[[["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-09-04 UTC."],[[["\u003cp\u003eThis webpage provides documentation for the \u003ccode\u003egoogle-cloud-cpp\u003c/code\u003e OpenTelemetry integration, with the latest release being version 2.37.0-rc and a current stable version being 2.14.0.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eConfigureBasicTracing\u003c/code\u003e function enables basic request tracing to Cloud Trace using the OpenTelemetry C++ API, allowing for observability into the operation of \u003ccode\u003egoogle-cloud-cpp\u003c/code\u003e libraries without requiring additional code instrumentation.\u003c/p\u003e\n"],["\u003cp\u003eThe tracing configuration supports options for setting the sampling rate, such as using the \u003ccode\u003eBasicTracingRateOption\u003c/code\u003e, to control the volume of traced requests, enabling both detailed troubleshooting and latency statistic gathering.\u003c/p\u003e\n"],["\u003cp\u003eUsers must ensure that the principal running the application has \u003ccode\u003ecloud.traces.patch\u003c/code\u003e permissions, typically granted via the \u003ccode\u003eroles/cloudtrace.agent\u003c/code\u003e role, to send traces to the specified project, along with potentially using a service account for credentials.\u003c/p\u003e\n"],["\u003cp\u003eThis page lists resources such as \u003ccode\u003eMakeResourceDetector\u003c/code\u003e to create a resource detector for the Google Cloud Platform, and \u003ccode\u003eMakeTraceExporter\u003c/code\u003e to create a trace exporter for Google Cloud Trace, enabling customization of trace data collection and reporting.\u003c/p\u003e\n"]]],[],null,["# Namespace google::cloud::otel (2.14.0)\n\nVersion 2.14.0keyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/opentelemetry/latest/namespacegoogle_1_1cloud_1_1otel)\n- [2.41.0](/cpp/docs/reference/opentelemetry/2.41.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.40.0](/cpp/docs/reference/opentelemetry/2.40.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.39.0](/cpp/docs/reference/opentelemetry/2.39.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.38.0](/cpp/docs/reference/opentelemetry/2.38.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.37.0](/cpp/docs/reference/opentelemetry/2.37.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.36.0](/cpp/docs/reference/opentelemetry/2.36.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.35.0](/cpp/docs/reference/opentelemetry/2.35.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.34.0](/cpp/docs/reference/opentelemetry/2.34.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.33.0](/cpp/docs/reference/opentelemetry/2.33.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.32.0](/cpp/docs/reference/opentelemetry/2.32.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.31.0](/cpp/docs/reference/opentelemetry/2.31.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.30.0](/cpp/docs/reference/opentelemetry/2.30.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.29.0](/cpp/docs/reference/opentelemetry/2.29.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.28.0](/cpp/docs/reference/opentelemetry/2.28.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.27.0](/cpp/docs/reference/opentelemetry/2.27.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.26.0](/cpp/docs/reference/opentelemetry/2.26.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.25.1](/cpp/docs/reference/opentelemetry/2.25.1/namespacegoogle_1_1cloud_1_1otel)\n- [2.24.0](/cpp/docs/reference/opentelemetry/2.24.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.23.0](/cpp/docs/reference/opentelemetry/2.23.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.22.1](/cpp/docs/reference/opentelemetry/2.22.1/namespacegoogle_1_1cloud_1_1otel)\n- [2.21.0](/cpp/docs/reference/opentelemetry/2.21.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.20.0](/cpp/docs/reference/opentelemetry/2.20.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.19.0](/cpp/docs/reference/opentelemetry/2.19.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.18.0](/cpp/docs/reference/opentelemetry/2.18.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.17.0](/cpp/docs/reference/opentelemetry/2.17.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.16.0](/cpp/docs/reference/opentelemetry/2.16.0/namespacegoogle_1_1cloud_1_1otel)\n- [2.15.1](/cpp/docs/reference/opentelemetry/2.15.1/namespacegoogle_1_1cloud_1_1otel)\n- [2.14.0](/cpp/docs/reference/opentelemetry/2.14.0/namespacegoogle_1_1cloud_1_1otel) \n\nClasses\n-------\n\n### [BasicTracingConfiguration](/cpp/docs/reference/opentelemetry/2.14.0/classgoogle_1_1cloud_1_1otel_1_1BasicTracingConfiguration)\n\nImplementation details for `ConfigureBasicTracing`.\n\nStructs\n-------\n\n### [BasicTracingRateOption](/cpp/docs/reference/opentelemetry/2.14.0/structgoogle_1_1cloud_1_1otel_1_1BasicTracingRateOption)\n\nConfigure the tracing rate for basic tracing.\n\nFunctions\n---------\n\n### ConfigureBasicTracing(Project, Options)\n\nConfigure the application for basic request tracing. \nThis function configures basic request tracing to [Cloud Trace](https://cloud.google.com/trace). The `google-cloud-cpp` libraries use [OpenTelemetry](https://opentelemetry.io) to provide observability into their operation at runtime.\n\nYou do not need to add OpenTelemetry instrumentation to your code. The C++ client libraries are already instrumented and all sampled RPCs will be sent to Cloud Trace. However, you may want to add instrumentation if multiple RPCs are performed as part of a single logical \"operation\" in your application.\n\nOpenTelemetry traces, including those reported by the C++ client libraries start as soon as this function returns. Tracing stops when the object returned by this function is deleted.\n\nOpenTelemetry is very configurable, supporting different sampling rates and filters, multiple \"exporters\" to send the collected data to different services, and multiple mechanisms to chain requests as they move from one program to the next. We do not expect this function will meet the needs of all applications. However, some applications will want a basic configuration that works with Gooogle Cloud Trace.\n\nThis function uses the [OpenTelemetry C++ API](https://opentelemetry-cpp.readthedocs.io/en/latest/) to change the global trace provider (`opentelemetry::trace::Provider::#SetTraceProvider()`). Do not use this function if your application needs fine control over OpenTelemetry settings.\n| **Note:** If you are using CMake as your build system, OpenTelemetry is not enabled by default in `google-cloud-cpp`. Please consult the build documentation to enable the additional libraries.\n\n###### Usage Example\n\nChange your build scripts to also build and link the library that provides this function.\n\nChange your application to call this function once, for example in `main()` as follows: \n\n #include \u003cgoogle/cloud/opentelemetry/configure_basic_tracing.h\u003e\n\n ...\n\n int main(...) {\n ...\n auto tracing_project = std::string([TRACING PROJECT]);\n auto tracing = google::cloud::opentelemetry::ConfigureBasicTracing(\n google::cloud::Project(tracing_project));\n }\n\nWhere `[TRACING PROJECT]` is the project id where you want to store the traces.\n\n###### Permissions\n\nThe principal (user or service account) running your application will need `cloud.traces.patch` permissions on the project where you send the traces. These permissions are typically granted as part of the `roles/cloudtrace.agent` role. If the principal configured in your [Application Default Credentials](https://cloud.google.com/docs/authentication#adc) does not have these permissions you will need to provide a different set of credentials: \n\n auto credentials = google::cloud::MakeServiceAccountCredentials(...);\n auto tracing = google::cloud::opentelemetry::ConfigureBasicTracing(\n google::cloud::Project(tracing_project),\n google::cloud::Options{}\n .set\u003cgoogle::cloud::UnifiedCredentialsOption\u003e(credentials));\n\n###### Sampling Rate\n\nBy default this function configures the application to trace all requests. This is useful for troubleshooting, but it is excessive if you want to enable tracing by default and use the results to gather latency statistics. To reduce the sampling rate use `@ref`[`BasicTracingRateOption`](/cpp/docs/reference/opentelemetry/2.14.0/structgoogle_1_1cloud_1_1otel_1_1BasicTracingRateOption). If desired, you can use an environment variable (or any other configuration source) to initialize its value. \n\n namespace gc = ::google::cloud;\n [](std::string project_id) {\n auto project = gc::Project(std::move(project_id));\n auto options = gc::Options{}.set\u003cgc::otel::BasicTracingRateOption\u003e(.001);\n auto configuration = gc::otel::ConfigureBasicTracing(project, options);\n\n MyApplicationCode();\n }\n\n###### See Also\n\n\u003chttps://cloud.google.com/trace/docs/iam\u003e for more information about IAM permissions for Cloud Trace.\n\n### MakeResourceDetector(Options)\n\nMake an OpenTelemetry Resource Detector for Google Cloud Platform. \n\n### MakeTraceExporter(Project, Options)\n\nMake an OpenTelemetry Trace Exporter for Google Cloud Trace."]]