OpenTelemetry C++ Documentation

repository·main·Indexed 23 days ago

https://github.com/open-telemetry/opentelemetry-cpp

The 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.

Tokens
34K
Snippets
62
Records
144
Agent score
79%

What's inside OpenTelemetry C++

  1. Use the ETW exporter to forward events on Windows

    main
    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.
  2. What is the OpenTracing Shim and when to use it

    main
    The OpenTracing shim is a bridge layer that allows you to use OpenTelemetry as an implementation for the OpenTracing API. It takes an OpenTelemetry Tracer and 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.
  3. Extending SDK classes

    main

    You 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.

  4. Avoid breaking the One Definition Rule in API headers

    main

    Entities defined in ABI-stable API headers—such as inline functions, templates, and inline/constexpr variables—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_NO macro.

    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
    
        ...
      }
    };
  5. How ABI versioning works in opentelemetry-cpp

    main

    To ensure that instrumented applications do not break when updating the library, opentelemetry-cpp uses 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-cpp project. You cannot mix and match different ABI versions for different signals (traces, metrics, etc.) because of their internal interdependencies.
  6. How clang-format configuration precedence works

    main

    Using clang-format is 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-format settings file within the local directory. A local .clang-format file takes precedence over the top-level project configuration file.

  7. How to update logger severity and state at runtime using LoggerConfigurator

    main

    You 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:

    1. Set a LoggerConfigurator on the LoggerProvider during its construction.
    2. Use LoggerProvider::UpdateLoggerConfigurator to apply new configurations. This method is thread-safe and automatically updates the LoggerConfig on all existing loggers.

    This is useful for dynamic debugging workflows, such as enabling Debug level logs for a specific library or application component only when an issue is reported, and then reverting to a Warn baseline once the issue is resolved.

  8. Enable SSL/TLS for OTLP gRPC Exporter

    main

    To enable TLS authentication for the OtlpGrpcExporter, use SslCredentials. You can provide the client certificate via OtlpGrpcExporterOptions by specifying either:

    1. The path to a .pem client certificate file.
    2. A string containing the certificate itself.

    In the provided examples, the path to the .pem file can be passed as a command-line argument along with the collector endpoint.

  9. Deprecation of the plugin namespace

    main

    The opentelemetry::plugin namespace 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/*.h or opentelemetry/plugin/detail/*.h, you must remove these dependencies.

  10. Supported C++ standards and platforms

    main

    OpenTelemetry 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.
  11. Understand the OpenTelemetry C++ architectural requirements

    main

    OpenTelemetry 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.
  12. How to track ongoing deprecations and removals

    main

    To avoid surprises when upgrading opentelemetry-cpp, you can monitor ongoing changes through two primary artifacts:

    1. The DEPRECATED file: This file contains a list of all currently active deprecations. It is organized into sections such as Platforms, Compilers, Third party dependencies, Build tools, Build scripts, opentelemetry-cpp API, opentelemetry-cpp SDK, opentelemetry-cpp Exporter, and Documentation. You should check this file to see if any APIs you use have been marked for future removal.
    2. GitHub Issues:
      • Ongoing deprecations are tagged with the Deprecated label.
      • Planned removals are tagged with the Removal label.

    Note that the deprecation process primarily applies to stable parts of the code. Code marked as feature preview or experimental may change more rapidly and with little to no notice.