From 0cd26138082ad242ebc8e0c9207dc451a41006dc Mon Sep 17 00:00:00 2001 From: "Cijo Thomas (from Dev Box)" Date: Tue, 14 May 2024 17:00:05 -0700 Subject: [PATCH 1/3] Fix periodic reader to trigger first exporter at the interval --- .../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 | 4 ++++ 5 files changed, 37 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..d31768a22a 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 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/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..27cbdf4419 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)); @@ -267,17 +268,20 @@ impl PeriodicReaderWorker { async fn process_message(&mut self, message: Message) -> bool { match message { Message::Export => { + println!("Export called"); if let Err(err) = self.collect_and_export().await { global::handle_error(err) } } Message::Flush(ch) => { + println!("Flush called"); let res = self.collect_and_export().await; if ch.send(res).is_err() { global::handle_error(MetricsError::Other("flush channel closed".into())) } } Message::Shutdown(ch) => { + println!("Shutdown called"); let res = self.collect_and_export().await; let _ = self.reader.exporter.shutdown(); if ch.send(res).is_err() { From b1bf6577980712658336b165a0f81bbd31ad998c Mon Sep 17 00:00:00 2001 From: "Cijo Thomas (from Dev Box)" Date: Tue, 14 May 2024 17:00:59 -0700 Subject: [PATCH 2/3] revert --- opentelemetry-sdk/src/metrics/periodic_reader.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/opentelemetry-sdk/src/metrics/periodic_reader.rs b/opentelemetry-sdk/src/metrics/periodic_reader.rs index 27cbdf4419..3c7b9d66be 100644 --- a/opentelemetry-sdk/src/metrics/periodic_reader.rs +++ b/opentelemetry-sdk/src/metrics/periodic_reader.rs @@ -268,20 +268,17 @@ impl PeriodicReaderWorker { async fn process_message(&mut self, message: Message) -> bool { match message { Message::Export => { - println!("Export called"); if let Err(err) = self.collect_and_export().await { global::handle_error(err) } } Message::Flush(ch) => { - println!("Flush called"); let res = self.collect_and_export().await; if ch.send(res).is_err() { global::handle_error(MetricsError::Other("flush channel closed".into())) } } Message::Shutdown(ch) => { - println!("Shutdown called"); let res = self.collect_and_export().await; let _ = self.reader.exporter.shutdown(); if ch.send(res).is_err() { From d7f8cd3352b19c796809c060e8e918f176546412 Mon Sep 17 00:00:00 2001 From: "Cijo Thomas (from Dev Box)" Date: Tue, 14 May 2024 17:10:44 -0700 Subject: [PATCH 3/3] use 4318 for http --- opentelemetry-otlp/examples/basic-otlp-http/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/opentelemetry-otlp/examples/basic-otlp-http/README.md b/opentelemetry-otlp/examples/basic-otlp-http/README.md index d31768a22a..2608deafce 100644 --- a/opentelemetry-otlp/examples/basic-otlp-http/README.md +++ b/opentelemetry-otlp/examples/basic-otlp-http/README.md @@ -36,14 +36,14 @@ 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 4318:4318 -v $(pwd):/cfg otel/opentelemetry-collector:latest --config=/cfg/otel-collector-config.yaml ``` On Windows use: ```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 +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