Configure the application for basic request tracing.
This function configures basic request tracing to Cloud Trace. The google-cloud-cpp libraries use OpenTelemetry to 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 the OpenTelemetry C++ API to 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, as described in this library's quickstart.
Change your application to call this function once, for example in main() 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 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 does 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();
}
Troubleshooting
By design, OpenTelemetry exporters fail silently. To troubleshoot problems, enable logging in the client library. Errors during the export are logged at a WARNING level.
Look through the logs for mentions of "BatchWriteSpans". These mentions are likely accompanied by actionable error messages.
If "BatchWriteSpans" is not mentioned in the logs, the client library did not attempt to export any traces. In this case, check that the project ID is not empty and that the sample rate is high enough. Also ensure that OpenTelemetry tracing is enabled in the library.
[[["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-08-14 UTC."],[[["\u003cp\u003eThe latest version of the OpenTelemetry C++ API documentation is 2.37.0-rc, with links provided for previous versions ranging from 2.36.0 down to 2.14.0.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eConfigureBasicTracing\u003c/code\u003e function enables basic request tracing to Cloud Trace using OpenTelemetry without needing additional instrumentation in user code, and it allows the user to set a sample rate using \u003ccode\u003eBasicTracingRateOption\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThis page details the usage of the \u003ccode\u003eConfigureBasicTracing\u003c/code\u003e function, including setting the tracing project, managing permissions, and adjusting the sampling rate for performance, in addition to troubleshooting tips for when traces are not being exported.\u003c/p\u003e\n"],["\u003cp\u003eThe document provides details on the classes, structs, and functions available, including \u003ccode\u003eBasicTracingConfiguration\u003c/code\u003e, \u003ccode\u003eBasicTracingRateOption\u003c/code\u003e, \u003ccode\u003eMakeMonitoringExporter\u003c/code\u003e, \u003ccode\u003eMakeResourceDetector\u003c/code\u003e, and \u003ccode\u003eMakeTraceExporter\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe documentation notes that OpenTelemetry is not enabled by default in \u003ccode\u003egoogle-cloud-cpp\u003c/code\u003e, and the user needs to consult the build documentation to enable the additional libraries.\u003c/p\u003e\n"]]],[],null,["# Namespace google::cloud::otel (2.42.0-rc)\n\nVersion latestkeyboard_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/latest/classgoogle_1_1cloud_1_1otel_1_1BasicTracingConfiguration)\n\nImplementation details for `ConfigureBasicTracing`.\n\nStructs\n-------\n\n### [BasicTracingRateOption](/cpp/docs/reference/opentelemetry/latest/structgoogle_1_1cloud_1_1otel_1_1BasicTracingRateOption)\n\nConfigure the tracing rate for basic tracing.\n\n### [MetricNameFormatterOption](/cpp/docs/reference/opentelemetry/latest/structgoogle_1_1cloud_1_1otel_1_1MetricNameFormatterOption)\n\nChange formatting for metric names.\n\n### [MonitoredResourceOption](/cpp/docs/reference/opentelemetry/latest/structgoogle_1_1cloud_1_1otel_1_1MonitoredResourceOption)\n\nOverride the monitored resource to tie metrics to.\n\n### [ServiceTimeSeriesOption](/cpp/docs/reference/opentelemetry/latest/structgoogle_1_1cloud_1_1otel_1_1ServiceTimeSeriesOption)\n\nExport Google-defined metrics.\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, as described in this library's [quickstart](https://github.com/googleapis/google-cloud-cpp/blob/main/google/cloud/opentelemetry/quickstart/README.md#opentelemetry-dependency).\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/latest/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###### Troubleshooting\n\nBy design, OpenTelemetry exporters fail silently. To troubleshoot problems, [enable logging](https://cloud.google.com/cpp/docs/reference/common/latest/logging#enabling-logs) in the client library. Errors during the export are logged at a WARNING level.\n\nLook through the logs for mentions of \"BatchWriteSpans\". These mentions are likely accompanied by actionable error messages.\n\nIf \"BatchWriteSpans\" is not mentioned in the logs, the client library did not attempt to export any traces. In this case, check that the project ID is not empty and that the sample rate is high enough. Also ensure that OpenTelemetry tracing is enabled in the library.\n\nSee also: \u003chttps://cloud.google.com/trace/docs/troubleshooting#no-data\u003e\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### MakeMonitoringExporter(Project, std::shared_ptr\\\u003c monitoring_v3::MetricServiceConnection \\\u003e, Options)\n\n### MakeResourceDetector()\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."]]