Skip to content

Commit

Permalink
Cleanup external-otlp examples
Browse files Browse the repository at this point in the history
* Fix comment header to use simpler language
* Fix comment header to refer to correct binary
* Coalesce use statements
* Update env_logger to 0.9.0
* ensure that both examples are formatted the same
  • Loading branch information
DavidS committed Sep 4, 2022
1 parent 0e6e67e commit da29067
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
2 changes: 1 addition & 1 deletion examples/external-otlp-grpcio-async-std/Cargo.toml
Expand Up @@ -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",
Expand Down
28 changes: 14 additions & 14 deletions 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,
};
Expand All @@ -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
Expand All @@ -40,8 +38,8 @@ fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
)
});
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)| {
Expand Down Expand Up @@ -71,6 +69,7 @@ fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
)
.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");

Expand All @@ -82,7 +81,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
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();
Expand All @@ -103,6 +102,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
// 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(())
}
9 changes: 5 additions & 4 deletions examples/external-otlp-tonic-tokio/Cargo.toml
Expand Up @@ -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"
25 changes: 15 additions & 10 deletions 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::{
Expand All @@ -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
Expand All @@ -41,6 +41,7 @@ fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
});
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))
Expand Down Expand Up @@ -79,9 +80,13 @@ const ANOTHER_KEY: Key = Key::from_static_str("ex.com/another");

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
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();
Expand Down

0 comments on commit da29067

Please sign in to comment.