Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] opentelemetry-aware event formatter #1304

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tracing-futures = { version = "0.3", path = "../tracing-futures", features = ["f
tracing-attributes = { path = "../tracing-attributes", version = "0.2"}
tracing-log = { path = "../tracing-log", version = "0.2", features = ["env_logger"] }
tracing-serde = { path = "../tracing-serde" }
tracing-opentelemetry = { path = "../tracing-opentelemetry" }
tracing-opentelemetry = { path = "../tracing-opentelemetry", features = ["fmt", "ansi"] }
tracing-appender = { path = "../tracing-appender" }
tracing-journald = { path = "../tracing-journald" }

Expand Down
83 changes: 83 additions & 0 deletions examples/examples/opentelemetry-fmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use opentelemetry::global;
use opentelemetry::sdk::propagation::TraceContextPropagator;
use std::collections::HashMap;
use std::{error::Error, thread, time::Duration};
use tracing::{debug, info, span, trace, warn};
use tracing_attributes::instrument;
use tracing_opentelemetry::OpenTelemetrySpanExt;
use tracing_subscriber::prelude::*;

#[instrument]
#[inline]
fn expensive_work() -> &'static str {
span!(tracing::Level::INFO, "expensive_step_1").in_scope(|| {
debug!("doing expensive step 1");
thread::sleep(Duration::from_millis(25));
});
span!(tracing::Level::INFO, "expensive_step_2").in_scope(|| {
debug!("doing expensive step 2");
thread::sleep(Duration::from_millis(25))
});

"success"
}

/// Does some work inside a span (supposedly) propagated from a remote trace (we
/// actually fake this for the purposes of the example).
#[instrument]
fn in_remote_span() {
// Extract context from request headers
let parent_context =
global::get_text_map_propagator(|propagator| propagator.extract(&build_example_carrier()));
tracing::Span::current().set_parent(parent_context);

info!("doing some work in a remote span...");
thread::sleep(Duration::from_millis(10));
info!("...done");
}

fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
// Set a format for propagating context. This MUST be provided, as the default is a no-op.
global::set_text_map_propagator(TraceContextPropagator::new());
let (tracer, _uninstall) = opentelemetry_jaeger::new_pipeline()
.with_service_name("report_example")
.install()?;
let opentelemetry = tracing_opentelemetry::subscriber().with_tracer(tracer);
let fmt = tracing_subscriber::fmt::subscriber().event_format(
tracing_opentelemetry::fmt()
// Display OpenTelemetry span IDs when logging events
.with_span_ids(true)
// Display any remote OpenTelemetry parent spans when formatting the
// event context.
.with_remote_parents(true)
// Like `tracing_subscriber`'s default formatters, we can enable and
// disable parts of the log line, like targets...
.with_target(false),
);
tracing_subscriber::registry()
.with(fmt)
.with(opentelemetry)
.try_init()?;

let root = span!(tracing::Level::INFO, "app_start", work_units = 2);
let _enter = root.enter();

let work_result = expensive_work();

in_remote_span();

warn!("About to exit!");
trace!("status: {}", work_result);

Ok(())
}

fn build_example_carrier() -> HashMap<String, String> {
let mut carrier = HashMap::new();
carrier.insert(
"traceparent".to_string(),
"00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01".to_string(),
);

carrier
}
5 changes: 4 additions & 1 deletion tracing-opentelemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ license = "MIT"
edition = "2018"

[features]
default = ["tracing-log"]
default = ["tracing-log", "fmt"]
fmt = ["tracing-subscriber/fmt"]
ansi = ["tracing-subscriber/ansi", "ansi_term"]

[dependencies]
opentelemetry = { version = "0.12", default-features = false, features = ["trace"] }
tracing = { path = "../tracing", version = "0.2", default-features = false, features = ["std"] }
tracing-core = { path = "../tracing-core", version = "0.2" }
tracing-subscriber = { path = "../tracing-subscriber", version = "0.3", default-features = false, features = ["registry"] }
tracing-log = { path = "../tracing-log", version = "0.2", default-features = false, optional = true }
ansi_term = { version = "0.12", optional = true }

[dev-dependencies]
async-trait = "0.1"
Expand Down
Loading