From cb9975e567bf3c5b4674730ff13f470d8130d078 Mon Sep 17 00:00:00 2001 From: Ryan Butler Date: Wed, 18 Dec 2024 19:45:00 -0500 Subject: [PATCH] tracing experiment --- Cargo.lock | 176 +++++++++++++++++++++++++++++++++++++- telemetry/Cargo.toml | 11 +++ telemetry/examples/foo.rs | 103 ++++++++++++++++++++++ 3 files changed, 288 insertions(+), 2 deletions(-) create mode 100644 telemetry/examples/foo.rs diff --git a/Cargo.lock b/Cargo.lock index d33d90ed..ac80f514 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4626,6 +4626,131 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "opentelemetry" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab70038c28ed37b97d8ed414b6429d343a8bbf44c9f79ec854f3a643029ba6d7" +dependencies = [ + "futures-core", + "futures-sink", + "js-sys", + "pin-project-lite", + "thiserror 1.0.65", + "tracing", +] + +[[package]] +name = "opentelemetry-datadog" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf575cba36695d55ac2f7e4b58abdb262eba7ceeb48cc2ba47b4187e6eee5e91" +dependencies = [ + "ahash 0.8.11", + "futures-core", + "http 1.2.0", + "indexmap 2.7.0", + "itoa", + "once_cell", + "opentelemetry", + "opentelemetry-http", + "opentelemetry-semantic-conventions", + "opentelemetry_sdk", + "reqwest 0.12.9", + "rmp", + "ryu", + "thiserror 1.0.65", + "url", +] + +[[package]] +name = "opentelemetry-http" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a8a7f5f6ba7c1b286c2fbca0454eaba116f63bbe69ed250b642d36fbb04d80" +dependencies = [ + "async-trait", + "bytes", + "http 1.2.0", + "opentelemetry", + "reqwest 0.12.9", +] + +[[package]] +name = "opentelemetry-otlp" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91cf61a1868dacc576bf2b2a1c3e9ab150af7272909e80085c3173384fe11f76" +dependencies = [ + "async-trait", + "futures-core", + "http 1.2.0", + "opentelemetry", + "opentelemetry-proto", + "opentelemetry_sdk", + "prost 0.13.3", + "thiserror 1.0.65", + "tokio", + "tonic", + "tracing", +] + +[[package]] +name = "opentelemetry-proto" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6e05acbfada5ec79023c85368af14abd0b307c015e9064d249b2a950ef459a6" +dependencies = [ + "opentelemetry", + "opentelemetry_sdk", + "prost 0.13.3", + "tonic", +] + +[[package]] +name = "opentelemetry-semantic-conventions" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc1b6902ff63b32ef6c489e8048c5e253e2e4a803ea3ea7e783914536eb15c52" + +[[package]] +name = "opentelemetry-stdout" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc8a298402aa5c260be90d10dc54b5a7d4e1025c354848f8e2c976d761351049" +dependencies = [ + "async-trait", + "chrono", + "futures-util", + "opentelemetry", + "opentelemetry_sdk", + "ordered-float", + "serde", + "serde_json", + "thiserror 1.0.65", +] + +[[package]] +name = "opentelemetry_sdk" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "231e9d6ceef9b0b2546ddf52335785ce41252bc7474ee8ba05bfad277be13ab8" +dependencies = [ + "async-trait", + "futures-channel", + "futures-executor", + "futures-util", + "glob", + "opentelemetry", + "percent-encoding", + "rand", + "serde_json", + "thiserror 1.0.65", + "tokio", + "tokio-stream", + "tracing", +] + [[package]] name = "option-ext" version = "0.2.0" @@ -4638,7 +4763,7 @@ version = "0.2.10" dependencies = [ "color-eyre", "data-encoding", - "event-listener 2.5.3", + "event-listener 5.3.1", "eyre", "futures", "orb-attest-dbus", @@ -4966,9 +5091,18 @@ dependencies = [ name = "orb-telemetry" version = "0.0.0" dependencies = [ + "color-eyre", "console-subscriber", + "opentelemetry", + "opentelemetry-datadog", + "opentelemetry-otlp", + "opentelemetry-stdout", + "opentelemetry_sdk", + "reqwest 0.12.9", + "tokio", "tracing", "tracing-journald", + "tracing-opentelemetry", "tracing-subscriber", ] @@ -5181,6 +5315,15 @@ dependencies = [ "zenoh", ] +[[package]] +name = "ordered-float" +version = "4.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c65ee1f9701bf938026630b455d5315f490640234259037edb259798b3bcf85e" +dependencies = [ + "num-traits", +] + [[package]] name = "ordered-stream" version = "0.2.0" @@ -5752,7 +5895,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e9552f850d5f0964a4e4d0bf306459ac29323ddfbae05e35a7c0d35cb0803cc5" dependencies = [ "anyhow", - "itertools 0.12.1", + "itertools 0.13.0", "proc-macro2", "quote", "syn 2.0.90", @@ -6313,6 +6456,17 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3582f63211428f83597b51b2ddb88e2a91a9d52d12831f9d08f5e624e8977422" +[[package]] +name = "rmp" +version = "0.8.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "228ed7c16fa39782c3b3468e974aec2795e9089153cd08ee2e9aefb3613334c4" +dependencies = [ + "byteorder", + "num-traits", + "paste", +] + [[package]] name = "rodio" version = "0.17.3" @@ -7840,6 +7994,24 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-opentelemetry" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97a971f6058498b5c0f1affa23e7ea202057a7301dbff68e968b2d578bcbd053" +dependencies = [ + "js-sys", + "once_cell", + "opentelemetry", + "opentelemetry_sdk", + "smallvec", + "tracing", + "tracing-core", + "tracing-log", + "tracing-subscriber", + "web-time", +] + [[package]] name = "tracing-serde" version = "0.1.3" diff --git a/telemetry/Cargo.toml b/telemetry/Cargo.toml index 08104544..1249f3b7 100644 --- a/telemetry/Cargo.toml +++ b/telemetry/Cargo.toml @@ -21,3 +21,14 @@ console-subscriber.workspace = true [lints.rust.unexpected_cfgs] level = "warn" check-cfg = ['cfg(tokio_unstable)'] + +[dev-dependencies] +color-eyre.workspace = true +opentelemetry = { version = "0.27.1" } +opentelemetry-otlp = { version = "0.27.0" } +opentelemetry-datadog = { version = "0.15.0", features = ["reqwest-client"] } +opentelemetry-stdout = { version = "0.27.0", features = ["trace"] } +opentelemetry_sdk = { version = "0.27.1", features = ["rt-tokio"] } +reqwest.workspace = true +tokio.workspace = true +tracing-opentelemetry = "0.28.0" diff --git a/telemetry/examples/foo.rs b/telemetry/examples/foo.rs new file mode 100644 index 00000000..42f0a631 --- /dev/null +++ b/telemetry/examples/foo.rs @@ -0,0 +1,103 @@ +use std::time::Duration; + +use color_eyre::eyre::Context as _; +use color_eyre::Result; +use opentelemetry::trace::{TraceContextExt as _, Tracer as _, TracerProvider as _}; +use opentelemetry::KeyValue; +use tracing::{error, span}; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::util::SubscriberInitExt as _; + +#[tokio::main] +async fn main() -> Result<()> { + color_eyre::install()?; + // Create a new OpenTelemetry trace pipeline that prints to stdout + //let tracer = datadog_tracer()?; + let tracer = otlp_tracer()?; + + //test_traces_on_tracer_directly(&tracer); + + // Create a tracing layer with the configured tracer + let otel_layer = tracing_opentelemetry::layer() + .with_tracer(tracer) + .with_level(true); + + // Use the tracing subscriber `Registry`, or any other subscriber + // that impls `LookupSpan` + tracing_subscriber::registry() + .with(otel_layer) + .with(tracing_subscriber::fmt::layer()) + .init(); + + // Spans will be sent to the configured OpenTelemetry exporter + { + let root = span!(tracing::Level::ERROR, "app_start", work_units = 2); + let _enter = root.enter(); + + error!(foo = 2, "This event will be logged in the root span."); + tokio::time::sleep(Duration::from_millis(4000)).await; + error!("This event will be logged in the root span."); + } + tokio::time::sleep(Duration::from_millis(2000)).await; + opentelemetry::global::shutdown_tracer_provider(); + tokio::time::sleep(Duration::from_millis(5000)).await; + + Ok(()) +} + +fn datadog_tracer() -> Result { + opentelemetry_datadog::new_pipeline() + .with_service_name("ryan-test") + .with_http_client(reqwest::Client::new()) + .with_agent_endpoint("http://localhost:8126") + .with_trace_config( + opentelemetry_sdk::trace::Config::default() + .with_sampler(opentelemetry_sdk::trace::Sampler::AlwaysOn) + .with_id_generator( + opentelemetry_sdk::trace::RandomIdGenerator::default(), + ), + ) + .install_batch(opentelemetry_sdk::runtime::Tokio) + .wrap_err("failed to install batch") +} + +fn otlp_tracer() -> Result { + let trace_provider = opentelemetry_sdk::trace::TracerProvider::builder() + .with_resource(opentelemetry_sdk::Resource::new([ + KeyValue::new("service.name", "ryan-test"), + KeyValue::new("service", "ryan-test"), + ])) + .with_sampler(opentelemetry_sdk::trace::Sampler::AlwaysOn) + .with_id_generator(opentelemetry_sdk::trace::RandomIdGenerator::default()) + .with_batch_exporter( + opentelemetry_otlp::SpanExporter::builder() + .with_tonic() + .build()?, + opentelemetry_sdk::runtime::Tokio, + ) + .build(); + Ok(trace_provider.tracer("ryan-test")) +} + +fn test_traces_on_tracer_directly(tracer: &opentelemetry_sdk::trace::Tracer) { + tracer.in_span("foo", |cx| { + use opentelemetry::{Key, Value}; + let span = cx.span(); + span.set_attribute(KeyValue::new( + Key::new("span.type"), + Value::String("web".into()), + )); + span.set_attribute(KeyValue::new( + Key::new("http.url"), + Value::String("http://localhost:8080/foo".into()), + )); + span.set_attribute(KeyValue::new( + Key::new("http.method"), + Value::String("GET".into()), + )); + span.set_attribute(KeyValue::new( + Key::new("http.status_code"), + Value::I64(200), + )); + }); +}