OpenTelemetry C++ SDK for Uptrace
This document explains how to configure the OpenTelemetry C++ SDK to export spans (traces), logs, and metrics to Uptrace using OTLP.
Prerequisites
- Git
- C++ compiler supporting C++ version >= 14
- Make
- CMake version >= 3.25
Quick Start Guide
Step 1: Create an Uptrace Project
Create an Uptrace project to obtain a DSN, for example, https://<secret>@api.uptrace.dev?grpc=4317.
Step 2: Install OpenTelemetry C++
git clone https://github.com/open-telemetry/opentelemetry-cpp.git
cd opentelemetry-cpp
mkdir build && cd build
cmake -DBUILD_TESTING=OFF -DWITH_OTLP_HTTP=ON ..
cmake --build .
sudo cmake --install .
Step 3: Configure and Create Traces
#include <cstdlib>
#include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h"
#include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h"
#include "opentelemetry/sdk/trace/batch_span_processor_factory.h"
#include "opentelemetry/sdk/trace/tracer_provider_factory.h"
#include "opentelemetry/sdk/trace/tracer_provider.h"
#include "opentelemetry/trace/provider.h"
namespace otlp = opentelemetry::exporter::otlp;
namespace trace_sdk = opentelemetry::sdk::trace;
namespace trace_api = opentelemetry::trace;
void InitTracer() {
otlp::OtlpHttpExporterOptions opts;
opts.url = "https://api.uptrace.dev/v1/traces";
const char* dsn = std::getenv("UPTRACE_DSN");
opts.http_headers = {{"uptrace-dsn", dsn ? dsn : ""}};
trace_sdk::BatchSpanProcessorOptions bsp_opts{};
auto exporter = otlp::OtlpHttpExporterFactory::Create(opts);
auto processor = trace_sdk::BatchSpanProcessorFactory::Create(
std::move(exporter), bsp_opts);
std::shared_ptr<trace_api::TracerProvider> provider =
trace_sdk::TracerProviderFactory::Create(std::move(processor));
trace_api::Provider::SetTracerProvider(provider);
}
void CleanupTracer() {
auto provider = trace_api::Provider::GetTracerProvider();
if (provider) {
static_cast<trace_sdk::TracerProvider*>(provider.get())->ForceFlush();
}
std::shared_ptr<trace_api::TracerProvider> none;
trace_api::Provider::SetTracerProvider(none);
}
int main() {
InitTracer();
auto tracer = trace_api::Provider::GetTracerProvider()
->GetTracer("myservice", "1.0.0");
auto span = tracer->StartSpan("main-operation");
span->SetAttribute("key", "value");
span->End();
CleanupTracer();
return 0;
}
Step 4: Build and Run
export UPTRACE_DSN="https://<secret>@api.uptrace.dev?grpc=4317"
mkdir build && cd build
cmake ..
cmake --build .
./myservice
Step 5: View Your Trace
Open the Uptrace dashboard to view your traces.
Configuration Options
Uptrace fully supports the OpenTelemetry Protocol (OTLP) over both gRPC and HTTP transports.
If you already have an OTLP exporter configured, you can continue using it with Uptrace by simply pointing it to the Uptrace OTLP endpoint.
Connecting to Uptrace
Choose an OTLP endpoint from the table below and pass your DSN via the uptrace-dsn header for authentication:
| Transport | Endpoint | Port |
|---|---|---|
| gRPC | https://api.uptrace.dev:4317 | 4317 |
| HTTP | https://api.uptrace.dev | 443 |
When using HTTP transport, you often need to specify the full URL for each signal type:
https://api.uptrace.dev/v1/traceshttps://api.uptrace.dev/v1/logshttps://api.uptrace.dev/v1/metrics
Note: Most OpenTelemetry SDKs support both transports. Use HTTP unless you're already familiar with gRPC.
Recommended Settings
For performance and reliability, we recommend:
- Use
BatchSpanProcessorandBatchLogProcessorfor batching spans and logs, reducing the number of export requests. - Enable
gzipcompression to reduce bandwidth usage. - Prefer
deltametrics temporality (Uptrace converts cumulative metrics automatically). - Use Protobuf encoding instead of JSON (Protobuf is more efficient and widely supported).
- Use HTTP transport for simplicity and fewer configuration issues (unless you're already familiar with gRPC).
- Optionally, use the AWS X-Ray ID generator to produce trace IDs compatible with AWS X-Ray.
Common Environment Variables
You can use environment variables to configure resource attributes and propagators::
| Variable | Description |
|---|---|
OTEL_RESOURCE_ATTRIBUTES | Comma-separated resource attributes, e.g., service.name=myservice,service.version=1.0.0. |
OTEL_SERVICE_NAME=myservice | Sets the service.name attribute (overrides OTEL_RESOURCE_ATTRIBUTES). |
OTEL_PROPAGATORS | Comma-separated list of context propagators (default: tracecontext,baggage). |
Most language SDKs allow configuring the OTLP exporter entirely via environment variables:
# Endpoint (choose HTTP or gRPC)
export OTEL_EXPORTER_OTLP_ENDPOINT="https://api.uptrace.dev" # HTTP
#export OTEL_EXPORTER_OTLP_ENDPOINT="https://api.uptrace.dev:4317" # gRPC
# Pass DSN for authentication
export OTEL_EXPORTER_OTLP_HEADERS="uptrace-dsn=<FIXME>"
# Performance optimizations
export OTEL_EXPORTER_OTLP_COMPRESSION=gzip
export OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION=BASE2_EXPONENTIAL_BUCKET_HISTOGRAM
export OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=DELTA
Configure BatchSpanProcessor to balance throughput and payload size:
export OTEL_BSP_EXPORT_TIMEOUT=10000 # Max export timeout (ms)
export OTEL_BSP_MAX_EXPORT_BATCH_SIZE=10000 # Avoid >32MB payloads
export OTEL_BSP_MAX_QUEUE_SIZE=30000 # Adjust for available memory
export OTEL_BSP_MAX_CONCURRENT_EXPORTS=2 # Parallel exports
What's Next?
| I want to... | Read this |
|---|---|
| Configure OTLP exporter in detail | OTLP Configuration |
| Instrument my code with spans | Tracing API |
| Collect application metrics | Metrics API |
| Send logs to Uptrace | Logs integration |
| Enable distributed tracing | Context propagation |
| Reduce costs in production | Sampling strategies |
| Add service metadata | Resource attributes |