From 540cdb3613ba872bc73542fcedc835aa829c613c Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Tue, 10 Dec 2024 11:53:13 -0800 Subject: [PATCH] Remove self diagnostics example (#2404) --- examples/README.md | 2 +- examples/self-diagnostics/Cargo.toml | 15 ------ examples/self-diagnostics/README.md | 28 ---------- examples/self-diagnostics/src/main.rs | 73 --------------------------- 4 files changed, 1 insertion(+), 117 deletions(-) delete mode 100644 examples/self-diagnostics/Cargo.toml delete mode 100644 examples/self-diagnostics/README.md delete mode 100644 examples/self-diagnostics/src/main.rs diff --git a/examples/README.md b/examples/README.md index a4e3148c86..eac8a3a779 100644 --- a/examples/README.md +++ b/examples/README.md @@ -7,7 +7,7 @@ This directory contains some examples that should help you get start crates from This example uses following crates from this repo: - opentelemetry(log) -- opentelemetry-appender-log +- opentelemetry-appender-tracing - opentelemetry-stdout Check this example if you want to understand *how to instrument logs using opentelemetry*. diff --git a/examples/self-diagnostics/Cargo.toml b/examples/self-diagnostics/Cargo.toml deleted file mode 100644 index 0f32702fe5..0000000000 --- a/examples/self-diagnostics/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "self-diagnostics" -version = "0.1.0" -edition = "2021" -license = "Apache-2.0" -publish = false - -[dependencies] -opentelemetry = { path = "../../opentelemetry" } -opentelemetry_sdk = { path = "../../opentelemetry-sdk", features = ["rt-tokio"]} -opentelemetry-stdout = { path = "../../opentelemetry-stdout"} -tokio = { workspace = true, features = ["full"] } -tracing = { workspace = true, features = ["std"]} -tracing-core = { workspace = true } -tracing-subscriber = { workspace = true, features = ["env-filter","registry", "std", "fmt"]} diff --git a/examples/self-diagnostics/README.md b/examples/self-diagnostics/README.md deleted file mode 100644 index e40bc00702..0000000000 --- a/examples/self-diagnostics/README.md +++ /dev/null @@ -1,28 +0,0 @@ -# Basic OpenTelemetry metrics example with custom error handler: - -This example shows how to self-diagnose OpenTelemetry by enabling its internal -logs. OpenTelemetry crates publish internal logs when "internal-logs" feature is -enabled. This feature is enabled by default. Internal logs are published using -`tracing` events, and hence, a `tracing` subscriber must be configured without -which the logs are simply discarded. - -## Filtering logs from external dependencies of OTLP Exporter: - -The example configures a tracing `filter` to restrict logs from external crates -(`hyper`, `tonic`, and `reqwest` etc.) used by the OTLP Exporter to the `error` -level. This helps prevent an infinite loop of log generation when these crates -emit logs that are picked up by the tracing subscriber. This is only a -workaround until [the root -issue](https://github.com/open-telemetry/opentelemetry-rust/issues/761) is -resolved. - -## Filtering logs to be send to OpenTelemetry itself - -If you use [OpenTelemetry Tracing -Appender](../../opentelemetry-appender-tracing/README.md) to send `tracing` logs -to OpenTelemetry, then enabling OpenTelemetry internal logs can also cause -infinite, recursive logging. You can filter out all OpenTelemetry internal logs -from being sent to [OpenTelemetry Tracing -Appender](../../opentelemetry-appender-tracing/README.md) using a filter, like -"add_directive("opentelemetry=off".parse().unwrap())" being done for tracing's -`FmtSubscriber`. \ No newline at end of file diff --git a/examples/self-diagnostics/src/main.rs b/examples/self-diagnostics/src/main.rs deleted file mode 100644 index 2c38458d84..0000000000 --- a/examples/self-diagnostics/src/main.rs +++ /dev/null @@ -1,73 +0,0 @@ -use opentelemetry::global; -use opentelemetry::KeyValue; -use opentelemetry_sdk::metrics::PeriodicReader; -use opentelemetry_sdk::Resource; -use std::error::Error; -use tracing::info; -use tracing_subscriber::fmt; -use tracing_subscriber::prelude::*; -use tracing_subscriber::EnvFilter; - -fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider { - let exporter = opentelemetry_stdout::MetricExporterBuilder::default().build(); - - let reader = PeriodicReader::builder(exporter, opentelemetry_sdk::runtime::Tokio).build(); - - let provider = opentelemetry_sdk::metrics::SdkMeterProvider::builder() - .with_resource(Resource::new([KeyValue::new( - "service.name", - "self-diagnostics-example", - )])) - .with_reader(reader) - .build(); - - let cloned_provider = provider.clone(); - global::set_meter_provider(cloned_provider); - provider -} - -#[tokio::main] -async fn main() -> Result<(), Box> { - // OpenTelemetry uses `tracing` crate for its internal logging. Unless a - // tracing subscriber is set, the logs will be discarded. In this example, - // we configure a `tracing` subscriber to: - // 1. Print logs of level DEBUG or higher to stdout using tracing's fmt layer. - // 2. Filter logs from OpenTelemetry's dependencies (like tonic, hyper, - // reqwest etc. which are commonly used by the OTLP exporter) to only print - // ERROR-level logs. This filtering helps reduce repetitive log messages - // that could otherwise create an infinite loop of log output. This is a - // workaround until - // https://github.com/open-telemetry/opentelemetry-rust/issues/761 is - // resolved. - - // Target names used by all OpenTelemetry official crates always start with "opentelemetry". - // Hence, one may use "add_directive("opentelemetry=off".parse().unwrap())" - // to turn off all logs from OpenTelemetry. - - let filter = EnvFilter::new("debug") - .add_directive("hyper=error".parse().unwrap()) - .add_directive("tonic=error".parse().unwrap()) - .add_directive("h2=error".parse().unwrap()) - .add_directive("tower=error".parse().unwrap()) - .add_directive("reqwest=error".parse().unwrap()); - tracing_subscriber::registry() - .with(fmt::layer().with_thread_names(true).with_filter(filter)) - .init(); - - // Initialize the MeterProvider with the stdout Exporter. - let meter_provider = init_meter_provider(); - info!("Starting self-diagnostics example"); - - let meter = global::meter("example"); - let counter = meter.u64_counter("my_counter").build(); - counter.add(10, &[KeyValue::new("key", "value")]); - - let _observable_counter = meter - .u64_observable_counter("my_observable_counter") - .with_callback(|observer| observer.observe(10, &[KeyValue::new("key", "value")])) - .build(); - - meter_provider.shutdown()?; - info!("Shutdown complete. Bye!"); - Ok(()) -}