# C++ Instrument C++ applications with OpenTelemetry # Integration Instrument your C++ applications to send traces, logs, and metrics to Kopai. ## Installation ### CMake (Recommended) ```cmake find_package(opentelemetry-cpp CONFIG REQUIRED) target_link_libraries(myapp opentelemetry-cpp::trace opentelemetry-cpp::logs opentelemetry-cpp::metrics opentelemetry-cpp::otlp_http_exporter ) ``` ### vcpkg ```bash vcpkg install opentelemetry-cpp[otlp-http] ``` ## Basic Setup ```cpp #include #include #include #include #include namespace otlp = opentelemetry::exporter::otlp; namespace trace_sdk = opentelemetry::sdk::trace; namespace trace_api = opentelemetry::trace; void initTracer() { // Get endpoint from environment const char* endpoint = std::getenv("OTEL_EXPORTER_OTLP_ENDPOINT"); if (!endpoint) endpoint = "http://localhost:4318"; otlp::OtlpHttpExporterOptions options; options.url = std::string(endpoint) + "/v1/traces"; auto exporter = otlp::OtlpHttpExporterFactory::Create(options); auto processor = trace_sdk::SimpleSpanProcessorFactory::Create( std::move(exporter) ); auto provider = trace_sdk::TracerProviderFactory::Create( std::move(processor) ); trace_api::Provider::SetTracerProvider(std::move(provider)); } ``` ## Creating Spans ```cpp auto tracer = trace_api::Provider::GetTracerProvider() ->GetTracer("my-app", "1.0.0"); auto span = tracer->StartSpan("my-operation"); auto scope = tracer->WithActiveSpan(span); // Your code here span->End(); ``` ## Running Your Application ```bash export OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp-http.kopai.app" export OTEL_SERVICE_NAME="my-service" ./myapp ``` | Variable | Description | |----------|-------------| | `OTEL_EXPORTER_OTLP_ENDPOINT` | Kopai OTLP endpoint | | `OTEL_SERVICE_NAME` | Name shown in Kopai UI | ## Build Notes Building opentelemetry-cpp requires: - CMake 3.12+ - C++14 or later - curl, gRPC, protobuf dependencies For a complete working example: [C++ Example](https://github.com/kopai-app/kopai-integration-examples/tree/main/cpp) ## Reference [OpenTelemetry C++](https://opentelemetry.io/docs/languages/cpp/)