New Relic integration for tracing
This crate provides a layer for collecting trace data from tracing and sending them to New Relic.
tracing::Span will be tried as Trace Span, and tracing::Event as Logs.
tracing::Attribute and tracing::Metadata wil be tried as Custom Attributes.
use std::thread::sleep;
use std::time::Duration;
use tracing_subscriber::{layer::SubscriberExt, Registry};
#[tracing::instrument(name = "fibonacci()")]
fn fibonacci(n: u32) -> u32 {
let ms = 100 * n as u64;
tracing::info!(n = n, "sleep {}ms", ms);
sleep(Duration::from_millis(ms));
match n {
0 | 1 => 1,
_ => fibonacci(n - 1) + fibonacci(n - 2),
}
}
fn main() {
env_logger::init();
let newrelic = tracing_newrelic::layer("YOUR-API-KEY");
let fmt = tracing_subscriber::fmt::layer();
let subscriber = Registry::default().with(newrelic).with(fmt);
tracing::subscriber::with_default(subscriber, || {
let span = tracing::info_span!(
"calculating fibonacci(3)",
service.name = "tracing-newrelic-demo"
);
let _enter = span.enter();
fibonacci(3);
});
}-
Replace
YOUR-API-KEYabove with your api key and run it. -
Open New Relic One, navigate to
Entity explorerand search fortracing-newrelic-demo. -
You should see a entry span named
calculating fibonacci(3)and click it for more details:
- Click
See logsto view all events inside this span:
And I strongly recommend include these attributes in your spans:
-
span.kindNew Relic creates throught and response time dashboards for spans with
span.kindset toserverandconsumer.
-
otel.status_code&otel.status_descriptionNew Relic creates error rate dashboard for spans with
otel.status_codeset toERROR.
-
service.nameNew Relic group entity by their
service.namefield.
-
nameNew Relic group trnsations by their
namefield.
MIT

