From c5bffdd95f2f977b0e99083e62a1a4d4682cb370 Mon Sep 17 00:00:00 2001 From: "zhongyang.wu" Date: Mon, 19 Apr 2021 21:57:35 -0400 Subject: [PATCH] fix: allow users to use custom export kind selector. Follow up on #497, which allows users to bring their own aggregator selector. Also updated the name of examples. --- examples/basic-otlp-with-selector/README.md | 7 +++++-- examples/basic-otlp-with-selector/src/main.rs | 11 +++++++++++ examples/basic-otlp/README.md | 4 +++- opentelemetry-otlp/src/metric.rs | 18 ++++++++++++++++-- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/examples/basic-otlp-with-selector/README.md b/examples/basic-otlp-with-selector/README.md index db067418cb..977096586a 100644 --- a/examples/basic-otlp-with-selector/README.md +++ b/examples/basic-otlp-with-selector/README.md @@ -1,4 +1,7 @@ -# Basic OpenTelemetry Example +# Basic OTLP exporter Example -This example shows basic span and metric usage, and exports to the [OpenTelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector) via OTLP with a custom metric aggregator selector. +This example shows how to configure OTLP metrics exporter to use custom aggregator selectors and custom export kind selectors. + +## Prerequisite +You should first start a `opentelemetry-collector` on localhost using the default configuration. diff --git a/examples/basic-otlp-with-selector/src/main.rs b/examples/basic-otlp-with-selector/src/main.rs index 7cbc46d176..179232b458 100644 --- a/examples/basic-otlp-with-selector/src/main.rs +++ b/examples/basic-otlp-with-selector/src/main.rs @@ -1,6 +1,7 @@ use futures::stream::Stream; use futures::StreamExt; use opentelemetry::global::shutdown_tracer_provider; +use opentelemetry::sdk::export::metrics::{ExportKind, ExportKindFor}; use opentelemetry::sdk::{ export::metrics::{Aggregator, AggregatorSelector}, metrics::{aggregators, PushController}, @@ -50,6 +51,15 @@ impl AggregatorSelector for CustomAggregator { } } +#[derive(Debug, Clone)] +struct CustomExportKindFor(); + +impl ExportKindFor for CustomExportKindFor { + fn export_kind_for(&self, _descriptor: &Descriptor) -> ExportKind { + ExportKind::Delta + } +} + fn init_meter() -> metrics::Result { let export_config = ExporterConfig { endpoint: "http://localhost:4317".to_string(), @@ -58,6 +68,7 @@ fn init_meter() -> metrics::Result { }; opentelemetry_otlp::new_metrics_pipeline(tokio::spawn, delayed_interval) .with_export_config(export_config) + .with_export_kind(CustomExportKindFor()) .with_aggregator_selector(CustomAggregator()) .build() } diff --git a/examples/basic-otlp/README.md b/examples/basic-otlp/README.md index 8e93a5d197..4d90ae8c77 100644 --- a/examples/basic-otlp/README.md +++ b/examples/basic-otlp/README.md @@ -1,4 +1,6 @@ -# Basic OpenTelemetry Example +# Basic OTLP exporter Example This example shows basic span and metric usage, and exports to the [OpenTelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector) via OTLP. +## Prerequisite +You should first start a `opentelemetry-collector` on localhost using the default configuration. \ No newline at end of file diff --git a/opentelemetry-otlp/src/metric.rs b/opentelemetry-otlp/src/metric.rs index 3f784fc510..d8baf1d994 100644 --- a/opentelemetry-otlp/src/metric.rs +++ b/opentelemetry-otlp/src/metric.rs @@ -169,10 +169,24 @@ where } /// Build with export kind selector - pub fn with_export_kind(self, export_selector: ES) -> Self { + pub fn with_export_kind( + self, + export_selector: E, + ) -> OtlpMetricPipelineBuilder + where + E: ExportKindFor + Send + Sync + Clone + 'static, + { OtlpMetricPipelineBuilder { + aggregator_selector: self.aggregator_selector, export_selector, - ..self + spawn: self.spawn, + interval: self.interval, + export_config: self.export_config, + tonic_config: self.tonic_config, + resource: self.resource, + stateful: self.stateful, + period: self.period, + timeout: self.timeout, } }