|
| 1 | +# OpenTelemetry C++ Starter for DOCA |
| 2 | + |
| 3 | +## Prerequisites |
| 4 | + |
| 5 | +Ensure you have the following installed: |
| 6 | + |
| 7 | +- **Git** |
| 8 | +- **C++ Compiler** |
| 9 | +- **Make** |
| 10 | +- **CMake** |
| 11 | +- **Docker** (for running the OTLP collector and Jeager) |
| 12 | + |
| 13 | +### Create Project Directory |
| 14 | + |
| 15 | +Create a folder named otel-cpp-starter. |
| 16 | + |
| 17 | +move into the newly created folder. This will serve as your working directory. |
| 18 | + |
| 19 | +```bash |
| 20 | +mkdir otel-cpp-starter |
| 21 | +cd otel-cpp-starter |
| 22 | +``` |
| 23 | + |
| 24 | +Next, install and build OpenTelemetry C++ locally using CMake, following these steps: |
| 25 | + |
| 26 | +In your terminal, navigate back to the otel-cpp-starter directory. Then, clone the OpenTelemetry C++ GitHub repository to your local machine. |
| 27 | + |
| 28 | +```bash |
| 29 | +git clone https://github.com/open-telemetry/opentelemetry-cpp.git |
| 30 | +``` |
| 31 | + |
| 32 | +Change your working directory to the OpenTelemetry C++ SDK directory. |
| 33 | + |
| 34 | +```bash |
| 35 | +cd opentelemetry-cpp |
| 36 | +``` |
| 37 | + |
| 38 | +Create a build directory and navigate into it. |
| 39 | + |
| 40 | +```bash |
| 41 | +mkdir build |
| 42 | +cd build |
| 43 | +``` |
| 44 | + |
| 45 | +In the build directory run CMake, to configure and generate the build system without enabling tests: |
| 46 | + |
| 47 | +```bash |
| 48 | +cmake -DBUILD_TESTING=OFF .. |
| 49 | +Or, if the cmake --build fails, you can also try: |
| 50 | + |
| 51 | +cmake -DBUILD_TESTING=OFF -DWITH_ABSEIL=ON .. |
| 52 | +Execute the build process: |
| 53 | + |
| 54 | +cmake --build . |
| 55 | +Install OpenTelemetry C++ in otel-cpp-starter/otel-cpp: |
| 56 | + |
| 57 | +cmake --install . --prefix ../../otel-cpp |
| 58 | +``` |
| 59 | +
|
| 60 | +## Traces |
| 61 | +
|
| 62 | +### Initialize tracing |
| 63 | +
|
| 64 | +```bash |
| 65 | +auto provider = opentelemetry::trace::Provider::GetTracerProvider(); |
| 66 | +auto tracer = provider->GetTracer("foo_library", "1.0.0"); |
| 67 | +``` |
| 68 | +
|
| 69 | +The TracerProvider acquired in the first step is a singleton object that is usually provided by the OpenTelemetry C++ SDK. It is used to provide specific implementations for API interfaces. In case no SDK is used, the API provides a default no-op implementation of a TracerProvider. |
| 70 | +
|
| 71 | +The Tracer acquired in the second step is needed to create and start Spans. |
| 72 | +
|
| 73 | +#### Start a span |
| 74 | +
|
| 75 | +```bash |
| 76 | +auto span = tracer->StartSpan("HandleRequest"); |
| 77 | +``` |
| 78 | +
|
| 79 | +This creates a span, sets its name to "HandleRequest", and sets its start time to the current time. Refer to the API documentation for other operations that are available to enrich spans with additional data. |
| 80 | +
|
| 81 | +#### Mark a span as active |
| 82 | +
|
| 83 | +```bash |
| 84 | +auto scope = tracer->WithActiveSpan(span); |
| 85 | +``` |
| 86 | +
|
| 87 | +This marks a span as active and returns a Scope object. The scope object controls how long a span is active. The span remains active for the lifetime of the scope object. |
| 88 | +
|
| 89 | +The concept of an active span is important, as any span that is created without explicitly specifying a parent is parented to the currently active span. A span without a parent is called root span. |
| 90 | +
|
| 91 | +## Exporters |
| 92 | +
|
| 93 | +### Available exporters |
| 94 | +
|
| 95 | +The registry contains a list of exporters for C++. |
| 96 | +
|
| 97 | +Among exporters, OpenTelemetry Protocol (OTLP) exporters are designed with the OpenTelemetry data model in mind, emitting OTel data without any loss of information. Furthermore, many tools that operate on telemetry data support OTLP (such as Prometheus, Jaeger, and most vendors), providing you with a high degree of flexibility when you need it. To learn more about OTLP, see OTLP Specification. |
| 98 | +
|
| 99 | +This page covers the main OpenTelemetry C++ exporters and how to set them up. |
| 100 | +
|
| 101 | +## OTLP Exporter |
| 102 | +
|
| 103 | +### Collector Setup |
| 104 | +
|
| 105 | +In an empty directory, create a file called collector-config.yaml with the following content: |
| 106 | +
|
| 107 | +```yaml |
| 108 | +receivers: |
| 109 | + otlp: |
| 110 | + protocols: |
| 111 | + grpc: |
| 112 | + endpoint: 0.0.0.0:4317 |
| 113 | + http: |
| 114 | + endpoint: 0.0.0.0:4318 |
| 115 | +exporters: |
| 116 | + debug: |
| 117 | + verbosity: detailed |
| 118 | +service: |
| 119 | + pipelines: |
| 120 | + traces: |
| 121 | + receivers: [otlp] |
| 122 | + exporters: [debug] |
| 123 | + metrics: |
| 124 | + receivers: [otlp] |
| 125 | + exporters: [debug] |
| 126 | + logs: |
| 127 | + receivers: [otlp] |
| 128 | + exporters: [debug] |
| 129 | +``` |
| 130 | +
|
| 131 | +Now run the collector in a docker container: |
| 132 | +
|
| 133 | +```bash |
| 134 | +docker run -p 4317:4317 -p 4318:4318 --rm -v $(pwd)/collector-config.yaml:/etc/otelcol/config.yaml otel/opentelemetry-collector |
| 135 | +``` |
| 136 | +
|
| 137 | +This collector is now able to accept telemetry via OTLP. Later you may want to configure the collector to send your telemetry to your observability backend. |
| 138 | +
|
| 139 | +#### Dependencies |
| 140 | +
|
| 141 | +If you want to send telemetry data to an OTLP endpoint (like the OpenTelemetry Collector, Jaeger or Prometheus), you can choose between two different protocols to transport your data: |
| 142 | +
|
| 143 | +HTTP/protobuf |
| 144 | +gRPC |
| 145 | +Make sure that you have set the right cmake build variables while building OpenTelemetry C++ from source: |
| 146 | +
|
| 147 | +```bash |
| 148 | +-DWITH_OTLP_GRPC=ON: To enable building OTLP gRPC exporter. |
| 149 | +-DWITH_OTLP_HTTP=ON: To enable building OTLP HTTP exporter. |
| 150 | +``` |
| 151 | +
|
| 152 | +In this tutorial, we use HTTP endpoint. |
| 153 | +
|
| 154 | +## Usage |
| 155 | +
|
| 156 | +```bash |
| 157 | +#include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h" |
| 158 | +#include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" |
| 159 | +#include "opentelemetry/sdk/trace/processor.h" |
| 160 | +#include "opentelemetry/sdk/trace/batch_span_processor_factory.h" |
| 161 | +#include "opentelemetry/sdk/trace/batch_span_processor_options.h" |
| 162 | +#include "opentelemetry/sdk/trace/tracer_provider_factory.h" |
| 163 | +#include "opentelemetry/trace/provider.h" |
| 164 | +#include "opentelemetry/sdk/trace/tracer_provider.h" |
| 165 | +
|
| 166 | +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h" |
| 167 | +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h" |
| 168 | +#include "opentelemetry/metrics/provider.h" |
| 169 | +#include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" |
| 170 | +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h" |
| 171 | +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h" |
| 172 | +#include "opentelemetry/sdk/metrics/meter_context_factory.h" |
| 173 | +#include "opentelemetry/sdk/metrics/meter_provider.h" |
| 174 | +#include "opentelemetry/sdk/metrics/meter_provider_factory.h" |
| 175 | +
|
| 176 | +#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h" |
| 177 | +#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h" |
| 178 | +#include "opentelemetry/logs/provider.h" |
| 179 | +#include "opentelemetry/sdk/logs/logger_provider_factory.h" |
| 180 | +#include "opentelemetry/sdk/logs/processor.h" |
| 181 | +#include "opentelemetry/sdk/logs/simple_log_record_processor_factory.h" |
| 182 | +
|
| 183 | +namespace trace_api = opentelemetry::trace; |
| 184 | +namespace trace_sdk = opentelemetry::sdk::trace; |
| 185 | +
|
| 186 | +namespace metric_sdk = opentelemetry::sdk::metrics; |
| 187 | +namespace metrics_api = opentelemetry::metrics; |
| 188 | +
|
| 189 | +namespace otlp = opentelemetry::exporter::otlp; |
| 190 | +
|
| 191 | +namespace logs_api = opentelemetry::logs; |
| 192 | +namespace logs_sdk = opentelemetry::sdk::logs; |
| 193 | +
|
| 194 | +
|
| 195 | +
|
| 196 | +void InitTracer() |
| 197 | + { |
| 198 | + // Create an OpenTelemetry Protocol (OTLP) exporter. |
| 199 | + auto exporter = otlp::OtlpHttpExporterFactory::Create(opts); |
| 200 | + auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); |
| 201 | +
|
| 202 | + resource::ResourceAttributes attributes = { |
| 203 | + {resource::SemanticConventions::kServiceName, "DOCA NVIDIA DEMO"}, // The application name. |
| 204 | + {resource::SemanticConventions::kHostName, "$"} |
| 205 | + }; |
| 206 | + auto resource = opentelemetry::sdk::resource::Resource::Create(attributes); |
| 207 | +
|
| 208 | + std::shared_ptr<opentelemetry::trace::TracerProvider> provider = |
| 209 | + trace_sdk::TracerProviderFactory::Create(std::move(processor), std::move(resource)); |
| 210 | +
|
| 211 | + // Set the trace provider. |
| 212 | + trace::Provider::SetTracerProvider(provider); |
| 213 | + } |
| 214 | +void CleanupTracer() |
| 215 | +{ |
| 216 | + std::shared_ptr<opentelemetry::trace::TracerProvider> none; |
| 217 | + trace::Provider::SetTracerProvider(none); |
| 218 | +} |
| 219 | +``` |
| 220 | +
|
| 221 | +and then we define our function traces and we pass the service name and operation name on to it. |
| 222 | +
|
| 223 | +```bash |
| 224 | +void traces(std::string serviceName, std::string operationName) |
| 225 | +{ |
| 226 | + auto span = getTracer(serviceName)->StartSpan(operationName); |
| 227 | +
|
| 228 | + span->SetAttribute("service.name", serviceName); |
| 229 | +
|
| 230 | + span->End(); |
| 231 | +} |
| 232 | +``` |
| 233 | +
|
| 234 | +we call it by using the traces and can pass the service and operation names respectively |
| 235 | +
|
| 236 | +```bash |
| 237 | + traces("doca", "doca prepare"); |
| 238 | +``` |
| 239 | +
|
| 240 | +The traces are called and it send the traces to otlp endpoint. |
| 241 | +
|
| 242 | +After that, by using otlp the traces is sent to the jeager and it can be accessed on http... |
| 243 | +
|
| 244 | +The Build steps are as follows, |
| 245 | +
|
| 246 | +```bash |
| 247 | +meson build |
| 248 | +cd build |
| 249 | +./doca_telemetry_export |
| 250 | +``` |
| 251 | +
|
| 252 | +make sure to run the docker containers for otel and jeager before executing above commands. |
| 253 | +
|
| 254 | +the will display on browser on [Jeager](https://localhost:16686) |
| 255 | +
|
| 256 | + |
0 commit comments