OpenTelemetry C++ Documentation
repository·main·Indexed 23 days ago
https://github.com/open-telemetry/opentelemetry-cppThe official C++ client implementation for OpenTelemetry, providing instrumentation capabilities for Logs, Metrics, and Traces. This documentation includes guides on building the library via Docker, implementing distributed tracing for HTTP and gRPC, configuring the Metrics SDK pipeline with Counters and Histograms, and managing runtime logger severity using LoggerConfigurator.
What's inside OpenTelemetry C++
- The OpenTelemetry C++ SDK ETW exporter allows applications instrumented with the OpenTelemetry API to forward events to an out-of-process Event Tracing for Windows (ETW) listener. This is useful for real-time event consumption, debugging, or performance analysis using Windows' kernel-level tracing facility. Events can be recorded to a log file or consumed in real time by an ETW listener.
What is the OpenTracing Shim and when to use it
mainThe OpenTracing shim is a bridge layer that allows you to use OpenTelemetry as an implementation for the OpenTracing API. It takes an OpenTelemetryTracerand exposes it as an OpenTracing-compatible Tracer. This is useful for migrating legacy codebases that rely on OpenTracing to OpenTelemetry without rewriting all tracing calls immediately.Extending SDK classes
mainYou can extend existing SDK classes by inheriting from them (e.g.,
class MyExporter : public OtlpGrpcExporter).Warning: Extending SDK classes creates a strong dependency on the SDK's internal implementation. Because the implementation details are exposed in public headers to allow this pattern, using this approach with shared libraries is not recommended, as any change to the base class implementation will break the ABI and cause your extension to fail.
Avoid breaking the One Definition Rule in API headers
mainEntities defined in ABI-stable API headers—such as inline functions, templates, and
inline/constexprvariables—must not change their definition or observable behavior within a stable ABI version.If different libraries are compiled against different versions of the API, they may each carry a different definition of the same inline entity. This violates the C++ One Definition Rule and can lead to unpredictable behavior or linker errors. Any change impacting observable behavior must be guarded by the
OPENTELEMETRY_ABI_VERSION_NOmacro.class Logger { public: template <class... ArgumentType> void EmitLogRecord(ArgumentType &&...args) { #if OPENTELEMETRY_ABI_VERSION_NO >= 2 // Behavior change added to the experimental ABI const Severity arg_severity = detail::FindSeverityInArgs(args...); if (arg_severity != Severity::kInvalid && !Enabled(arg_severity)) { return; } nostd::unique_ptr<LogRecord> log_record = CreateLogRecord(/* context_or_span */); #else // Stable ABI behavior nostd::unique_ptr<LogRecord> log_record = CreateLogRecord(); #endif ... } };How ABI versioning works in opentelemetry-cpp
mainTo ensure that instrumented applications do not break when updating the library,
opentelemetry-cppuses Application Binary Interface (ABI) versioning. This allows the project to deliver breaking changes (like adding virtual methods to a class) while maintaining compatibility for older binaries.Key Concepts:
- API Compatibility: Ensures that source code does not require changes to adopt a newer release.
- ABI Compatibility: Ensures that a binary compiled against an older version of the API headers can be linked against a newer version of the SDK library without recompilation.
- Inline Namespaces: The project uses C++ inline namespaces (e.g.,
opentelemetry::v1::...) to isolate different versions of the same symbols. This allows multiple versions of the same class to coexist in the same library. - Scope: An ABI version applies to the entire
opentelemetry-cppproject. You cannot mix and match different ABI versions for different signals (traces, metrics, etc.) because of their internal interdependencies.
How clang-format configuration precedence works
mainUsing
clang-formatis an optional way to ensure code conforms to the project style guide.For modules that require an alternate coding style, you can place a
.clang-formatsettings file within the local directory. A local.clang-formatfile takes precedence over the top-level project configuration file.How to update logger severity and state at runtime using LoggerConfigurator
mainYou can control the minimum severity level and the enabled/disabled state of specific loggers at runtime without restarting your application or recreating logger instances.
To do this, you must:
- Set a
LoggerConfiguratoron theLoggerProviderduring its construction. - Use
LoggerProvider::UpdateLoggerConfiguratorto apply new configurations. This method is thread-safe and automatically updates theLoggerConfigon all existing loggers.
This is useful for dynamic debugging workflows, such as enabling
Debuglevel logs for a specific library or application component only when an issue is reported, and then reverting to aWarnbaseline once the issue is resolved.- Set a
Enable SSL/TLS for OTLP gRPC Exporter
mainTo enable TLS authentication for the
OtlpGrpcExporter, useSslCredentials. You can provide the client certificate viaOtlpGrpcExporterOptionsby specifying either:- The path to a
.pemclient certificate file. - A string containing the certificate itself.
In the provided examples, the path to the
.pemfile can be passed as a command-line argument along with the collector endpoint.- The path to a
Deprecation of the plugin namespace
mainThe
opentelemetry::pluginnamespace and its associated framework for loading code from shared libraries are deprecated and will be removed after October 1st, 2026. This code was unused and is being removed to reduce maintenance costs.There is no replacement for this API. If your code relies on
opentelemetry/plugin/*.horopentelemetry/plugin/detail/*.h, you must remove these dependencies.Supported C++ standards and platforms
mainOpenTelemetry C++ is designed to be portable across various platforms and supports multiple C++ standards.
Supported C++ Standards
- C++14 (ISO/IEC 14882:2014)
- C++17 (ISO/IEC 14882:2017)
- C++20 (ISO/IEC 14882:2020)
- C++23 (ISO/IEC 14882:2024)
Note: Supporting the C programming language is not a goal of this project.
Supported Platforms
The project is built and tested on:
- Ubuntu (22.04, 24.04) on x86-64 using CMake and Bazel.
- macOS (14, 15) on arm64 using CMake and Bazel.
- Windows Server (2022, 2025) on x86-64 using CMake and Bazel.
Understand the OpenTelemetry C++ architectural requirements
mainOpenTelemetry C++ is designed around several core architectural principles to ensure ease of integration and flexibility:
- Zero-dependency API: The API is header-only and has zero dependencies. This allows you to vendor the API directly into your source tree without modifying your build system or adding external dependencies.
- Minimal-dependency SDK: The SDK is designed to have minimal dependencies to keep the footprint small.
- Flexible Exporters: While the SDK is minimal, exporters are permitted to include transport-specific dependencies (e.g., the Stackdriver exporter depends on gRPC).
- Linking Support: The project supports both static linking and dynamic loading. Dynamic loading is supported to allow vendors to provide their own SDK implementations and to enable low-dependency builds.
- ABI Stability: To support dynamic loading, the project aims for a stable C++ ABI. This implies that STL types are not used in the public interface to prevent ABI breakage.
- Threading Model: OpenTelemetry C++ does not require a dedicated background thread for exporters or general background work, giving users control over their application's threading model.
How to track ongoing deprecations and removals
mainTo avoid surprises when upgrading
opentelemetry-cpp, you can monitor ongoing changes through two primary artifacts:- The
DEPRECATEDfile: This file contains a list of all currently active deprecations. It is organized into sections such asPlatforms,Compilers,Third party dependencies,Build tools,Build scripts,opentelemetry-cpp API,opentelemetry-cpp SDK,opentelemetry-cpp Exporter, andDocumentation. You should check this file to see if any APIs you use have been marked for future removal. - GitHub Issues:
- Ongoing deprecations are tagged with the
Deprecatedlabel. - Planned removals are tagged with the
Removallabel.
- Ongoing deprecations are tagged with the
Note that the deprecation process primarily applies to stable parts of the code. Code marked as
feature previeworexperimentalmay change more rapidly and with little to no notice.- The