Public documentation
Documentation indexRead as MarkdownC++
Languages · Shared configuration · Verification
This example builds OpenTelemetry C++ 1.29.0 with the OTLP/HTTP exporter. The upstream C++ project lists traces, metrics, and logs as Stable. Only traces are configured here. Build options and API signatures were checked against that release on 2026-09-28.
Prepare the build
Use a C++17 compiler, Git, CMake 3.20 or later, and development packages for libcurl, Protobuf (including protoc), and nlohmann-json. Recent Protobuf installations also need a compatible Abseil installation. Keep the Protobuf compiler and libraries consistent.
In a new working directory, fetch the pinned SDK and its submodules:
mkdir corridora-cpp
cd corridora-cpp
git clone --branch v1.29.0 --depth 1 --recurse-submodules \
https://github.com/open-telemetry/opentelemetry-cpp.git
Save as CMakeLists.txt beside the cloned directory:
cmake_minimum_required(VERSION 3.20)
project(corridora_example LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(OTELCPP_WITH_OTLP_HTTP ON CACHE BOOL "" FORCE)
set(OTELCPP_BUILD_TESTING OFF CACHE BOOL "" FORCE)
set(OTELCPP_WITH_EXAMPLES OFF CACHE BOOL "" FORCE)
set(OTELCPP_WITH_FUNC_TESTS OFF CACHE BOOL "" FORCE)
add_subdirectory(opentelemetry-cpp)
add_executable(corridora_example main.cpp)
target_link_libraries(corridora_example PRIVATE
opentelemetry_trace opentelemetry_exporter_otlp_http)
These are the OTELCPP_ option names for this release. Building only the header API does not provide a functioning exporter.
Initialize and emit
Save as main.cpp:
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include <utility>
#include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h"
#include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h"
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/sdk/trace/batch_span_processor_factory.h"
#include "opentelemetry/sdk/trace/tracer_provider_factory.h"
#include "opentelemetry/trace/scope.h"
namespace otlp = opentelemetry::exporter::otlp;
namespace sdktrace = opentelemetry::sdk::trace;
int main() {
otlp::OtlpHttpExporterOptions options;
auto exporter = otlp::OtlpHttpExporterFactory::Create(options);
sdktrace::BatchSpanProcessorOptions batch_options;
auto processor = sdktrace::BatchSpanProcessorFactory::Create(
std::move(exporter), batch_options);
auto resource = opentelemetry::sdk::resource::Resource::Create({
{"service.name", "corridora-cpp"},
{"service.version", "example-1"},
{"deployment.environment.name", "development"}});
auto provider = sdktrace::TracerProviderFactory::Create(
std::move(processor), resource);
auto tracer = provider->GetTracer("corridora.examples.cpp");
auto span = tracer->StartSpan("example.calculate");
{
opentelemetry::trace::Scope scope(span);
span->SetAttribute("example.language", "cpp");
std::this_thread::sleep_for(std::chrono::milliseconds(50));
char trace_id[32];
span->GetContext().trace_id().ToLowerBase16(trace_id);
std::cout << "trace_id=" << std::string(trace_id, sizeof(trace_id)) << '\n';
}
span->End();
return provider->Shutdown(std::chrono::seconds(10)) ? 0 : 1;
}
Configure the exporter and build. OtlpHttpExporterOptions reads the OTLP environment settings in this release:
export OTEL_EXPORTER_OTLP_ENDPOINT='http://127.0.0.1:4318'
unset OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
unset OTEL_EXPORTER_OTLP_HEADERS OTEL_EXPORTER_OTLP_TRACES_HEADERS
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --target corridora_example --parallel 2
./build/corridora_example
The local Collector must be running. If CMake cannot find a dependency, install its development package or supply its installation prefix through CMAKE_PREFIX_PATH; changing the telemetry endpoint cannot fix a link or build error.
Application instrumentation and context
Keep one provider alive until application work stops. Create tracers from that provider, or explicitly register a global provider if your instrumentation uses the global API. This standalone sample does not register a global provider.
The scope makes this span current for nested work on the same execution context. End the span separately. For threads, queues, and custom HTTP clients, use the context/propagator APIs or a supported instrumentation integration. Do not assume a new thread inherits the caller's active span.
C++ does not provide one universal automatic instrumentation agent for every framework and binary. Library integrations and platform agents have separate compatibility requirements. Use manual spans around operations that are not covered by an installed integration.
Additional signals and direct export
Metrics require a meter provider, reader, and OTLP metric exporter; logs require a logger provider, processor, and OTLP log exporter. Build and initialize those components explicitly. std::cout is not automatically an OTLP log, and the trace exporter does not export metric instruments. Correlated logs need the active span's context in their log records.
For direct Corridora export, use the HTTPS base endpoint and OTEL_EXPORTER_OTLP_HEADERS="X-CORRIDORA-KEY=${CORRIDORA_INGEST_KEY}" from shared configuration. The trace-specific endpoint variable, if used, needs /v1/traces. Assigning options.url in code overrides endpoint discovery and requires the complete URL.
Verify
Find service corridora-cpp and the printed trace ID in Corridora. Expect one example.calculate span. Check the process exit status and SDK stderr, then the Collector's accepted/sent counters. A missing exporter target is a build configuration problem; a valid local ID with no stored span is a delivery or lifecycle problem. Continue with verification.