diff --git a/examples/external-otlp-grpcio-async-std/Cargo.toml b/examples/external-otlp-grpcio-async-std/Cargo.toml index f7a42c6c37..6febb78d56 100644 --- a/examples/external-otlp-grpcio-async-std/Cargo.toml +++ b/examples/external-otlp-grpcio-async-std/Cargo.toml @@ -6,7 +6,7 @@ publish = false [dependencies] async-std = { version = "= 1.10.0", features = ["attributes"] } -env_logger = "0.8.2" +env_logger = "0.9.0" opentelemetry = { path = "../../opentelemetry", features = ["rt-async-std"] } opentelemetry-otlp = { path = "../../opentelemetry-otlp", features = [ "grpc-sys", diff --git a/examples/external-otlp-grpcio-async-std/src/main.rs b/examples/external-otlp-grpcio-async-std/src/main.rs index 3881dc0f4b..6ddcc7ed32 100644 --- a/examples/external-otlp-grpcio-async-std/src/main.rs +++ b/examples/external-otlp-grpcio-async-std/src/main.rs @@ -1,16 +1,17 @@ -//! This should show how to connect to a third party collector like -//! honeycomb or lightstep using tonic with tls and using tokio as reactor. -//! To run this you have to specify a few environment variables like in the example: +//! This shows how to connect to a third party collector like +//! honeycomb or lightstep using grpcio with tls and using async-std as reactor. +//! To run this specify a few environment variables like in the example: //! ```shell //! OTLP_GRPCIO_ENDPOINT=https://api.honeycomb.io:443 \ //! OTLP_GRPCIO_X_HONEYCOMB_TEAM=token \ //! OTLP_GRPCIO_X_HONEYCOMB_DATASET=dataset \ -//! cargo run --bin external-otlp-tonic-tokio +//! cargo run --bin external-otlp-grpcio-async-std //! ``` use async_std::task::sleep; -use opentelemetry::trace::TraceError; -use opentelemetry::{global, sdk::trace as sdktrace}; use opentelemetry::{ + global::{shutdown_tracer_provider, tracer}, + sdk::trace as sdktrace, + trace::TraceError, trace::{TraceContextExt, Tracer}, Key, }; @@ -19,12 +20,9 @@ use url::Url; use std::{ collections::HashMap, - env::{set_var, vars}, - time::Duration, -}; -use std::{ - env::{remove_var, var}, + env::{remove_var, set_var, var, vars}, error::Error, + time::Duration, }; // Use the variables to try and export the example to any external collector that accepts otlp @@ -40,8 +38,8 @@ fn init_tracer() -> Result { ) }); let endpoint = Url::parse(&endpoint).expect("endpoint is not a valid url"); - remove_var(ENDPOINT); + let headers: HashMap<_, _> = vars() .filter(|(name, _)| name.starts_with(HEADER_PREFIX)) .map(|(name, value)| { @@ -71,6 +69,7 @@ fn init_tracer() -> Result { ) .install_batch(opentelemetry::runtime::AsyncStd) } + const LEMONS_KEY: Key = Key::from_static_str("ex.com/lemons"); const ANOTHER_KEY: Key = Key::from_static_str("ex.com/another"); @@ -82,7 +81,7 @@ async fn main() -> Result<(), Box> { env_logger::init(); let _ = init_tracer()?; - let tracer = global::tracer("ex.com/basic"); + let tracer = tracer("ex.com/basic"); tracer.in_span("operation", |cx| { let span = cx.span(); @@ -103,6 +102,7 @@ async fn main() -> Result<(), Box> { // wait for 1 minutes so that we could see metrics being pushed via OTLP every 10 seconds. sleep(Duration::from_secs(60)).await; - global::shutdown_tracer_provider(); + shutdown_tracer_provider(); + Ok(()) } diff --git a/examples/external-otlp-tonic-tokio/Cargo.toml b/examples/external-otlp-tonic-tokio/Cargo.toml index d2e9b9f94c..a0a644e53b 100644 --- a/examples/external-otlp-tonic-tokio/Cargo.toml +++ b/examples/external-otlp-tonic-tokio/Cargo.toml @@ -5,9 +5,10 @@ edition = "2021" publish = false [dependencies] -opentelemetry = { path = "../../opentelemetry", features = ["rt-tokio", "metrics"] } -opentelemetry-otlp = { path = "../../opentelemetry-otlp", features = ["tonic", "tls", "tls-roots"] } +env_logger = "0.9.0" +opentelemetry = {path = "../../opentelemetry", features = ["rt-tokio", "metrics"]} +opentelemetry-otlp = {path = "../../opentelemetry-otlp", features = ["tonic", "tls", "tls-roots"]} serde_json = "1.0" -tokio = { version = "1.0", features = ["full"] } -tonic = { version = "0.8.0", features = ["tls"] } +tokio = {version = "1.0", features = ["full"]} +tonic = {version = "0.8.0", features = ["tls"]} url = "2.2.0" diff --git a/examples/external-otlp-tonic-tokio/src/main.rs b/examples/external-otlp-tonic-tokio/src/main.rs index 28e819c6a8..01a1b1c9b3 100644 --- a/examples/external-otlp-tonic-tokio/src/main.rs +++ b/examples/external-otlp-tonic-tokio/src/main.rs @@ -1,16 +1,16 @@ -//! This should show how to connect to a third party collector like +//! This shows how to connect to a third party collector like //! honeycomb or lightstep using tonic with tls and using tokio as reactor. -//! To run this you have to specify a few environment variables like in the example: +//! To run this specify a few environment variables like in the example: //! ```shell //! OTLP_TONIC_ENDPOINT=https://api.honeycomb.io:443 \ //! OTLP_TONIC_X_HONEYCOMB_TEAM=token \ -//! OTLP_TONIC_X_HONEYCOMB_DATASET=dataset \' +//! OTLP_TONIC_X_HONEYCOMB_DATASET=dataset \ //! cargo run --bin external-otlp-tonic-tokio //! ``` -use opentelemetry::trace::TraceError; -use opentelemetry::{global, sdk::trace as sdktrace}; use opentelemetry::{ - trace::{TraceContextExt, Tracer}, + global::{shutdown_tracer_provider, tracer}, + sdk::trace as sdktrace, + trace::{TraceContextExt, TraceError, Tracer}, Key, }; use tonic::{ @@ -19,12 +19,12 @@ use tonic::{ }; use url::Url; -use opentelemetry::global::shutdown_tracer_provider; use opentelemetry_otlp::WithExportConfig; -use std::{env::vars, str::FromStr, time::Duration}; use std::{ - env::{remove_var, var}, + env::{remove_var, set_var, var, vars}, error::Error, + str::FromStr, + time::Duration, }; // Use the variables to try and export the example to any external collector that accepts otlp @@ -41,6 +41,7 @@ fn init_tracer() -> Result { }); let endpoint = Url::parse(&endpoint).expect("endpoint is not a valid url"); remove_var(ENDPOINT); + let mut metadata = MetadataMap::new(); for (key, value) in vars() .filter(|(name, _)| name.starts_with(HEADER_PREFIX)) @@ -79,9 +80,13 @@ const ANOTHER_KEY: Key = Key::from_static_str("ex.com/another"); #[tokio::main] async fn main() -> Result<(), Box> { + if let Err(std::env::VarError::NotPresent) = var("RUST_LOG") { + set_var("RUST_LOG", "debug") + }; + env_logger::init(); let _ = init_tracer()?; - let tracer = global::tracer("ex.com/basic"); + let tracer = tracer("ex.com/basic"); tracer.in_span("operation", |cx| { let span = cx.span();