Exporters

In order to visualize and analyze your telemetry, you will need to export your data to an OpenTelemetry Collector or a backend such as Jaeger, Zipkin, Prometheus or a vendor-specific one.

As part of OpenTelemetry C++ you will find many exporters being available. Among them, the OpenTelemetry Protocol (OTLP) exporters provide the best experience for you as an end-user, since it is a general-purpose telemetry data delivery protocol designed in the scope of the OpenTelemetry project.

To learn more about the OTLP protocol, you can read the OTLP Specification.

Below you will find some introductions on how to set up exporters for OTLP and other common protocols in your code.

追踪导出器

OStream导出器

OStream导出器适用于开发和调试任务,并且是最简单的设置。

#include "opentelemetry/exporters/ostream/span_exporter_factory.h"

namespace trace_exporter = opentelemetry::exporter::trace;

auto exporter = trace_exporter::OStreamSpanExporterFactory::Create();

OTLP端点

要将追踪数据发送到OTLP端点(例如收集器或Jaeger),您需要配置一个OTLP导出器发送到您的端点。

OTLP HTTP导出器

#include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h"
#include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h"

namespace otlp = opentelemetry::exporter::otlp;

otlp::OtlpHttpExporterOptions opts;
opts.url = "http://localhost:4318/v1/traces";

auto exporter = otlp::OtlpHttpExporterFactory::Create(opts);

OTLP gRPC导出器

#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h"
#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h"

namespace otlp = opentelemetry::exporter::otlp;

otlp::OtlpGrpcExporterOptions opts;
opts.endpoint = "localhost:4317";
opts.use_ssl_credentials = true;
opts.ssl_credentials_cacert_as_string = "ssl-certificate";

auto exporter = otlp::OtlpGrpcExporterFactory::Create(opts);

您可以在此处找到如何使用OTLP导出器的示例here

Jaeger

要尝试OTLP导出器,您可以将Jaeger作为OTLP端点运行,并在docker容器中进行追踪可视化:

docker run -d --name jaeger \
  -e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
  -e COLLECTOR_OTLP_ENABLED=true \
  -p 6831:6831/udp \
  -p 6832:6832/udp \
  -p 5778:5778 \
  -p 16686:16686 \
  -p 4317:4317 \
  -p 4318:4318 \
  -p 14250:14250 \
  -p 14268:14268 \
  -p 14269:14269 \
  -p 9411:9411 \
  jaegertracing/all-in-one:latest

Zipkin

要将追踪数据发送到Zipkin端点,您需要配置一个Zipkin导出器发送到您的端点。

#include "opentelemetry/exporters/zipkin/zipkin_exporter_factory.h"
#include "opentelemetry/exporters/zipkin/zipkin_exporter_options.h"

namespace zipkin = opentelemetry::exporter::zipkin;

zipkin::ZipkinExporterOptions opts;
opts.endpoint = "http://localhost:9411/api/v2/spans" ; // or export OTEL_EXPORTER_ZIPKIN_ENDPOINT="..."
opts.service_name = "default_service" ;

auto exporter = zipkin::ZipkinExporterFactory::Create(opts);

追踪处理器

简单Span处理器

简单处理器将逐个将Span发送到导出器。

#include "opentelemetry/sdk/trace/simple_processor_factory.h"

namespace trace_sdk = opentelemetry::sdk::trace;

auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));

批量Span处理器

批量Span处理器将多个Span组合在一起,然后发送到导出器。

#include "opentelemetry/sdk/trace/batch_span_processor_factory.h"
#include "opentelemetry/sdk/trace/batch_span_processor_options.h"

namespace trace_sdk = opentelemetry::sdk::trace;

trace_sdk::BatchSpanProcessorOptions opts;
opts.max_queue_size = 2048;
opts.max_export_batch_size = 512;

auto processor = trace_sdk::BatchSpanProcessorFactory::Create(std::move(exporter), opts);

追踪器提供者

#include "opentelemetry/sdk/trace/tracer_provider_factory.h"

namespace trace_api = opentelemetry::trace;
namespace trace_sdk = opentelemetry::sdk::trace;

auto provider = trace_sdk::TracerProviderFactory::Create(std::move(processor));

trace_api::Provider::SetTracerProvider(provider);

指标导出器

OTLP HTTP导出器

opentelemetry::exporter::otlp::OtlpHttpExporterOptions otlpOptions;
otlpOptions.url = "http://localhost:4318/v1/metrics"; // or "http://localhost:4318/
otlpOptions.aggregation_temporality = opentelemetry::sdk::metrics::AggregationTemporality::kCumulative; // or kDelta
auto exporter = opentelemetry::exporter::otlp::OtlpHttpMetricExporterFactory::Create(otlpOptions);
// Initialize and set the periodic metrics reader
opentelemetry::sdk::metrics::PeriodicExportingMetricReaderOptions options;
options.export_interval_millis = std::chrono::milliseconds(1000);
options.export_timeout_millis  = std::chrono::milliseconds(500);
std::unique_ptr<opentelemetry::sdk::metrics::MetricReader> reader{
    new opentelemetry::sdk::metrics::PeriodicExportingMetricReader(std::move(exporter), options)};

OTLP gRPC导出器

opentelemetry::exporter::otlp::OtlpGrpcMetricExporterOptions otlpOptions;
otlpOptions.endpoint = "localhost:4317/v1/metrics";  // or "localhost:4317
otlpOptions.aggregation_temporality = opentelemetry::sdk::metrics::AggregationTemporality::kDelta; // or kCumulative
auto exporter = opentelemetry::exporter::otlp::OtlpGrpcMetricExporterFactory::Create(otlpOptions);
// Initialize and set the periodic metrics reader
opentelemetry::sdk::metrics::PeriodicExportingMetricReaderOptions options;
options.export_interval_millis = std::chrono::milliseconds(1000);
options.export_timeout_millis  = std::chrono::milliseconds(500);
std::unique_ptr<opentelemetry::sdk::metrics::MetricReader> reader{
    new opentelemetry::sdk::metrics::PeriodicExportingMetricReader(std::move(exporter), options)};

Prometheus

要将指标发送到Prometheus端点,您需要配置一个Prometheus导出器。

opentelemetry::sdk::metrics::PeriodicExportingMetricReaderOptions options;
options.export_interval_millis = std::chrono::milliseconds(1000); //optional, to override default values
options.export_timeout_millis  = std::chrono::milliseconds(500); // optional, to override default values
opentelemetry::exporter::metrics::PrometheusExporterOptions prometheusOptions;
prometheusOptions.url = "localhost:8080";
std::unique_ptr<opentelemetry::sdk::metrics::MetricExporter> exporter{new opentelemetry::exporter::metrics::PrometheusExporter(prometheusOptions)};
std::unique_ptr<opentelemetry::sdk::metrics::MetricReader> reader{
    new opentelemetry::sdk::metrics::PeriodicExportingMetricReader(std::move(exporter), options)};

要了解更多关于如何使用Prometheus导出器的信息,请尝试Prometheus示例

下一步

通过手动仪表化,将您的代码库与自定义的可观察性数据融合在一起。

最后修改 December 10, 2023: translate (a4350d6e)