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

add utility function for retrieving a trace id #1663

Merged
merged 10 commits into from
Sep 2, 2022
11 changes: 11 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ The router will now return a response with the status code `406 Not Acceptable`
By [@Geal](https://github.com/Geal) in https://github.com/apollographql/router/pull/1652

## 🚀 Features

### router now provides TraceId ([PR #1663](https://github.com/apollographql/router/issues/1536))

If you need a reliable way to link together the various stages of pipeline processing, you can now use

```
apollo_router::tracer::TraceId
```

By [@garypen](https://github.com/garypen) in https://github.com/apollographql/router/pull/1663

## 🐛 Fixes

### Update our helm documentation to illustrate how to use our registry ([#1643](https://github.com/apollographql/router/issues/1643))
Expand Down
1 change: 1 addition & 0 deletions apollo-router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub mod services;
mod spec;
mod state_machine;
mod test_harness;
pub mod tracer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer tracing as I could see us adding other trace related things in future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sigh... If I rename to tracing it creates several hundred ambiguity errors with the tracing crate. Probably best to leave as tracer.


pub use crate::configuration::Configuration;
pub use crate::configuration::ListenAddr;
Expand Down
90 changes: 90 additions & 0 deletions apollo-router/src/tracer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//! Trace Ids for the router.

#![warn(unreachable_pub)]
#![warn(missing_docs)]
use std::fmt;

use opentelemetry::trace::TraceContextExt;
use tracing::Span;
use tracing_opentelemetry::OpenTelemetrySpanExt;

/// Trace ID
#[derive(Debug, PartialEq, Eq)]
pub struct TraceId([u8; 16]);

impl TraceId {
/// Invalid Trace ID
pub const INVALID: TraceId = TraceId([0; 16]);

/// Create a TraceId. If called from an invalid context
/// (e.g.: not in a span, or in a disabled span), then
/// the value of the TraceId is [`TraceId::INVALID`].
pub fn new() -> Self {
TraceId(
Span::current()
.context()
.span()
.span_context()
.trace_id()
.to_bytes(),
)
}

/// Convert the TraceId to bytes.
pub fn as_bytes(&self) -> &[u8; 16] {
&self.0
}

/// Convert the TraceId to u128.
pub fn to_u128(&self) -> u128 {
u128::from_be_bytes(self.0)
}
}

impl Default for TraceId {
fn default() -> Self {
Self::new()
}
}

impl fmt::Display for TraceId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_u128())
}
}

#[cfg(test)]
mod test {
use opentelemetry::sdk::export::trace::stdout;
use tracing::span;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::Registry;

use super::TraceId;

#[test]
fn it_returns_invalid_trace_id() {
let my_id = TraceId::new();
assert_eq!(my_id, TraceId::INVALID);
assert_eq!(my_id.as_bytes(), &[0; 16])
}

#[test]
fn it_returns_valid_trace_id() {
// Create a new OpenTelemetry pipeline
let tracer = stdout::new_pipeline().install_simple();
// Create a tracing layer with the configured tracer
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
// Use the tracing subscriber `Registry`, or any other subscriber
// that impls `LookupSpan`
let subscriber = Registry::default().with(telemetry);
// Trace executed code
tracing::subscriber::with_default(subscriber, || {
// Spans will be sent to the configured OpenTelemetry exporter
let root = span!(tracing::Level::TRACE, "trace test");
let _enter = root.enter();

assert!(TraceId::new() != TraceId::INVALID);
});
}
}