From 48112e0f44688bf41a7dec6e3def13c0fb0d6472 Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Tue, 14 May 2024 17:52:06 -0700 Subject: [PATCH] Fix periodic reader to trigger first exporter at the interval (#1766) --- .../examples/basic-otlp-http/README.md | 18 +++++++++++++++--- .../examples/basic-otlp/README.md | 18 +++++++++++++++--- .../examples/basic-otlp/src/main.rs | 1 - opentelemetry-sdk/CHANGELOG.md | 3 +++ .../src/metrics/periodic_reader.rs | 1 + 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/opentelemetry-otlp/examples/basic-otlp-http/README.md b/opentelemetry-otlp/examples/basic-otlp-http/README.md index 2448bae890..2608deafce 100644 --- a/opentelemetry-otlp/examples/basic-otlp-http/README.md +++ b/opentelemetry-otlp/examples/basic-otlp-http/README.md @@ -32,12 +32,24 @@ docker-compose down If you don't want to use `docker-compose`, you can manually run the `otel/opentelemetry-collector` container and inspect the logs to see traces being transferred. +On Unix based systems use: + ```shell # From the current directory, run `opentelemetry-collector` -$ docker run --rm -it -p 4318:4318 -v $(pwd):/cfg otel/opentelemetry-collector:latest --config=/cfg/otel-collector-config.yaml +docker run --rm -it -p 4318:4318 -v $(pwd):/cfg otel/opentelemetry-collector:latest --config=/cfg/otel-collector-config.yaml +``` + +On Windows use: -# Run the app which exports logs, metrics and traces via OTLP to the collector. -$ cargo run +```shell +# From the current directory, run `opentelemetry-collector` +docker run --rm -it -p 4318:4318 -v "%cd%":/cfg otel/opentelemetry-collector:latest --config=/cfg/otel-collector-config.yaml +``` + +Run the app which exports logs, metrics and traces via OTLP to the collector + +```shell +cargo run ``` ## View results diff --git a/opentelemetry-otlp/examples/basic-otlp/README.md b/opentelemetry-otlp/examples/basic-otlp/README.md index 1d9d81534d..e7e57bf815 100644 --- a/opentelemetry-otlp/examples/basic-otlp/README.md +++ b/opentelemetry-otlp/examples/basic-otlp/README.md @@ -32,12 +32,24 @@ docker-compose down If you don't want to use `docker-compose`, you can manually run the `otel/opentelemetry-collector` container and inspect the logs to see traces being transferred. +On Unix based systems use: + ```shell # From the current directory, run `opentelemetry-collector` -$ docker run --rm -it -p 4317:4317 -v $(pwd):/cfg otel/opentelemetry-collector:latest --config=/cfg/otel-collector-config.yaml +docker run --rm -it -p 4317:4317 -v $(pwd):/cfg otel/opentelemetry-collector:latest --config=/cfg/otel-collector-config.yaml +``` + +On Windows use: -# Run the app which exports logs, metrics and traces via OTLP to the collector. -$ cargo run +```shell +# From the current directory, run `opentelemetry-collector` +docker run --rm -it -p 4317:4317 -v "%cd%":/cfg otel/opentelemetry-collector:latest --config=/cfg/otel-collector-config.yaml +``` + +Run the app which exports logs, metrics and traces via OTLP to the collector + +```shell +cargo run ``` ## View results diff --git a/opentelemetry-otlp/examples/basic-otlp/src/main.rs b/opentelemetry-otlp/examples/basic-otlp/src/main.rs index 112a142696..2fc122b8d4 100644 --- a/opentelemetry-otlp/examples/basic-otlp/src/main.rs +++ b/opentelemetry-otlp/examples/basic-otlp/src/main.rs @@ -114,7 +114,6 @@ async fn main() -> Result<(), Box> { for _ in 0..10 { counter.add(1, &[KeyValue::new("test_key", "test_value")]); } - counter.add(1, &[KeyValue::new("test_key", "test_value")]); tracer.in_span("Main operation", |cx| { let span = cx.span(); diff --git a/opentelemetry-sdk/CHANGELOG.md b/opentelemetry-sdk/CHANGELOG.md index ed97748f50..0264343a00 100644 --- a/opentelemetry-sdk/CHANGELOG.md +++ b/opentelemetry-sdk/CHANGELOG.md @@ -7,6 +7,9 @@ [1612](https://github.com/open-telemetry/opentelemetry-rust/pull/1612/files) - [#1422](https://github.com/open-telemetry/opentelemetry-rust/pull/1422) Fix metrics aggregation bug when using Views to drop attributes. +- [#1766](https://github.com/open-telemetry/opentelemetry-rust/pull/1766) + Fix Metrics PeriodicReader to trigger first collect/export at the first interval + instead of doing it right away. - [#1623](https://github.com/open-telemetry/opentelemetry-rust/pull/1623) Add Drop implementation for SdkMeterProvider, which shuts down metricreaders, thereby allowing metrics still in memory to be flushed out. - **Breaking** [#1624](https://github.com/open-telemetry/opentelemetry-rust/pull/1624) Remove `OsResourceDetector` and diff --git a/opentelemetry-sdk/src/metrics/periodic_reader.rs b/opentelemetry-sdk/src/metrics/periodic_reader.rs index b0ea070513..3c7b9d66be 100644 --- a/opentelemetry-sdk/src/metrics/periodic_reader.rs +++ b/opentelemetry-sdk/src/metrics/periodic_reader.rs @@ -132,6 +132,7 @@ where let ticker = self .runtime .interval(self.interval) + .skip(1) // The ticker is fired immediately, so we should skip the first one to align with the interval. .map(|_| Message::Export); let messages = Box::pin(stream::select(message_receiver, ticker));