Created
April 26, 2022 02:39
-
-
Save stonecobra/b7a25e5f1ad1fe81fa9f520f8653eec3 to your computer and use it in GitHub Desktop.
Rust opentelemetry-otlp and tracing in a program as of 2022-04-25
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [package] | |
| name = "otel" | |
| version = "0.1.0" | |
| edition = "2021" | |
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
| [dependencies] | |
| opentelemetry = {version="0.17", features=["rt-tokio"]} | |
| opentelemetry-otlp = {version="0.10", features = ["grpc-sys"]} | |
| tokio = {version = "1.17.0", features = ["full"]} | |
| tonic = {version="0.6.2", features=["transport"]} | |
| tracing = "0.1.34" | |
| tracing-opentelemetry = "0.17.2" | |
| tracing-subscriber = { version = "0.3.11", features = ["env-filter","json"] } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use opentelemetry::sdk::trace::{self, IdGenerator, Sampler}; | |
| use opentelemetry_otlp::WithExportConfig; //gives us .with_endpoint() | |
| use opentelemetry::global; | |
| use opentelemetry::sdk::propagation::TraceContextPropagator; | |
| use std::collections::HashMap; | |
| use std::error::Error; | |
| use tracing; | |
| use tracing::instrument; | |
| use tracing_subscriber::filter::EnvFilter; | |
| use tracing_subscriber::prelude::__tracing_subscriber_SubscriberExt; //get us the .with() method | |
| use tracing_subscriber::util::SubscriberInitExt; | |
| fn main() -> Result<(), Box<dyn Error>> { | |
| global::set_text_map_propagator(TraceContextPropagator::new()); | |
| let mut headers: HashMap<String, String> = HashMap::new(); | |
| headers.insert( | |
| "x-honeycomb-team".parse().unwrap(), | |
| "YOUR_API_KEY_HERE".parse().unwrap(), | |
| ); | |
| headers.insert( | |
| "x-honeycomb-dataset".parse().unwrap(), | |
| "YOUR_DATASET_NAME_HERE".parse().unwrap(), | |
| ); | |
| let tracer = opentelemetry_otlp::new_pipeline() | |
| .tracing() | |
| .with_exporter( | |
| opentelemetry_otlp::new_exporter() | |
| .grpcio() | |
| .with_endpoint("api.honeycomb.io") | |
| .with_headers(headers) | |
| .with_tls(true), | |
| ) | |
| .with_trace_config( | |
| trace::config() | |
| .with_sampler(Sampler::AlwaysOn) | |
| .with_id_generator(IdGenerator::default()) | |
| .with_max_events_per_span(64) | |
| .with_max_attributes_per_span(16) | |
| .with_max_events_per_span(16), // .with_resource(Resource::new(vec![KeyValue::new("service.name", "otel-trial")])), //If you don't want to use env var | |
| ) | |
| .install_simple()?; | |
| tracing_subscriber::registry::Registry::default() | |
| .with(EnvFilter::from_default_env()) | |
| .with(tracing_subscriber::fmt::Layer::default() | |
| .with_ansi(false) //no terminal coloring for us (use in AWS Lambda for me) | |
| .with_file(true) | |
| .with_line_number(true) | |
| .with_target(true) | |
| .with_thread_ids(true) | |
| .json() | |
| ) // log to stdout | |
| .with(tracing_opentelemetry::layer().with_tracer(tracer)) | |
| .try_init()?; | |
| tracing::info!("registered global subscriber"); | |
| call1(); | |
| opentelemetry::global::shutdown_tracer_provider(); //only way to guarantee ALL the events get sent | |
| // opentelemetry::global::force_flush_tracer_provider(); //does NOT work | |
| Ok(()) | |
| } | |
| #[instrument] | |
| fn call1() { | |
| tracing::info!("call1 log"); | |
| call2(); | |
| } | |
| #[instrument] | |
| fn call2() { | |
| tracing::info!("call2 log"); | |
| call3(); | |
| } | |
| #[instrument] | |
| fn call3() { | |
| tracing::info!("call3 log"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # NOTE: the commented out variables didn't do anything to make a successful run of the program, only service name works as of 2022-04-24 | |
| # OTEL_EXPORTER_OTLP_ENDPOINT="api.honeycomb.io:443" \ | |
| # OTEL_TRACES_EXPORTER="oltp" \ | |
| # OTEL_EXPORTER_OTLP_HEADERS="x-honeycomb-team=YOUR_API_KEY_HERE" \ | |
| # OTEL_EXPORTER_OTLP_TRACES_HEADERS="x-honeycomb-dataset=YOUR_DATASET_HERE" \ | |
| RUST_LOG=debug \ | |
| OTEL_SERVICE_NAME="otel-trial" \ | |
| cargo run |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, I accidentally published my API key, and yes, I rotated it immediately afterwards