|
| 1 | +//! This should show how to connect to a third party collector like |
| 2 | +//! honeycomb or lightstep using tonic with tls and using tokio as reactor. |
| 3 | +//! To run this you have to specify a few environment variables like in the example: |
| 4 | +//! ```shell |
| 5 | +//! OTLP_GRPCIO_ENDPOINT=https://api.honeycomb.io:443 \ |
| 6 | +//! OTLP_GRPCIO_X_HONEYCOMB_TEAM=token \ |
| 7 | +//! OTLP_GRPCIO_X_HONEYCOMB_DATASET=dataset \ |
| 8 | +//! cargo run --bin external-otlp-tonic-tokio |
| 9 | +//! ``` |
| 10 | +use async_std::task::sleep; |
| 11 | +use opentelemetry::trace::TraceError; |
| 12 | +use opentelemetry::{global, sdk::trace as sdktrace}; |
| 13 | +use opentelemetry::{ |
| 14 | + trace::{TraceContextExt, Tracer}, |
| 15 | + Key, |
| 16 | +}; |
| 17 | +use url::Url; |
| 18 | + |
| 19 | +use std::{ |
| 20 | + collections::HashMap, |
| 21 | + env::{set_var, vars}, |
| 22 | + time::Duration, |
| 23 | +}; |
| 24 | +use std::{ |
| 25 | + env::{remove_var, var}, |
| 26 | + error::Error, |
| 27 | +}; |
| 28 | + |
| 29 | +// Use the variables to try and export the example to any external collector that accepts otlp |
| 30 | +// like: oltp itself, honeycomb or lightstep |
| 31 | +const ENDPOINT: &str = "OTLP_GRPCIO_ENDPOINT"; |
| 32 | +const HEADER_PREFIX: &str = "OTLP_GRPCIO_"; |
| 33 | + |
| 34 | +fn init_tracer() -> Result<(sdktrace::Tracer, opentelemetry_otlp::Uninstall), TraceError> { |
| 35 | + let endpoint = var(ENDPOINT).unwrap_or_else(|_| { |
| 36 | + panic!( |
| 37 | + "You must specify and endpoint to connect to with the variable {:?}.", |
| 38 | + ENDPOINT |
| 39 | + ) |
| 40 | + }); |
| 41 | + let endpoint = Url::parse(&endpoint).expect("endpoint is not a valid url"); |
| 42 | + |
| 43 | + remove_var(ENDPOINT); |
| 44 | + let headers: HashMap<_, _> = vars() |
| 45 | + .filter(|(name, _)| name.starts_with(HEADER_PREFIX)) |
| 46 | + .map(|(name, value)| { |
| 47 | + let header_name = name |
| 48 | + .strip_prefix(HEADER_PREFIX) |
| 49 | + .unwrap() |
| 50 | + .replace("_", "-") |
| 51 | + .to_ascii_lowercase(); |
| 52 | + (header_name, value) |
| 53 | + }) |
| 54 | + .collect(); |
| 55 | + |
| 56 | + let grpcio_endpoint = format!( |
| 57 | + "{}:{}", |
| 58 | + endpoint.host_str().unwrap(), |
| 59 | + endpoint.port_or_known_default().unwrap() |
| 60 | + ); |
| 61 | + |
| 62 | + opentelemetry_otlp::new_pipeline() |
| 63 | + .with_endpoint(grpcio_endpoint) |
| 64 | + .with_headers(headers) |
| 65 | + .with_tls(true) |
| 66 | + .install() |
| 67 | +} |
| 68 | +const LEMONS_KEY: Key = Key::from_static_str("ex.com/lemons"); |
| 69 | +const ANOTHER_KEY: Key = Key::from_static_str("ex.com/another"); |
| 70 | + |
| 71 | +#[async_std::main] |
| 72 | +async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { |
| 73 | + match var("RUST_LOG") { |
| 74 | + Err(std::env::VarError::NotPresent) => set_var("RUST_LOG", "trace"), |
| 75 | + _ => {} |
| 76 | + }; |
| 77 | + env_logger::init(); |
| 78 | + let _guard = init_tracer()?; |
| 79 | + |
| 80 | + let tracer = global::tracer("ex.com/basic"); |
| 81 | + |
| 82 | + tracer.in_span("operation", |cx| { |
| 83 | + let span = cx.span(); |
| 84 | + span.add_event( |
| 85 | + "Nice operation!".to_string(), |
| 86 | + vec![Key::new("bogons").i64(100)], |
| 87 | + ); |
| 88 | + span.set_attribute(ANOTHER_KEY.string("yes")); |
| 89 | + |
| 90 | + tracer.in_span("Sub operation...", |cx| { |
| 91 | + let span = cx.span(); |
| 92 | + span.set_attribute(LEMONS_KEY.string("five")); |
| 93 | + |
| 94 | + span.add_event("Sub span event".to_string(), vec![]); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + // wait for 1 minutes so that we could see metrics being pushed via OTLP every 10 seconds. |
| 99 | + sleep(Duration::from_secs(60)).await; |
| 100 | + |
| 101 | + Ok(()) |
| 102 | +} |
0 commit comments