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

Implement trace propagation #49

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
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
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dev:
cargo watch \
-x 'run --bin ndc_hub_example \
-- serve --configuration <(echo 'null') \
--otlp-endpoint http://localhost:4317'
1 change: 1 addition & 0 deletions rust-connector-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ opentelemetry = { version = "^0.20", features = [
"trace",
], default-features = false }
opentelemetry_api = "^0.20.0"
opentelemetry-http = "^0.9.0"
opentelemetry_sdk = "^0.20.0"
opentelemetry-otlp = { version = "^0.13.0", features = ["reqwest-client"] }
opentelemetry-semantic-conventions = "^0.12.0"
Expand Down
27 changes: 18 additions & 9 deletions rust-connector-sdk/src/default_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::{
connector::{Connector, InvalidRange, SchemaError, UpdateConfigurationError},
routes,
};

use async_trait::async_trait;
use axum::{
body::Body,
Expand All @@ -15,7 +14,10 @@ use axum::{
routing::{get, post},
Json, Router,
};
use opentelemetry_http::HeaderExtractor;
use tower_http::trace::MakeSpan;
use tower_http::validate_request::ValidateRequestHeaderLayer;
use tracing::Span;

use clap::{Parser, Subcommand};
use ndc_client::models::{
Expand Down Expand Up @@ -331,10 +333,7 @@ where
});

router
.layer(
TraceLayer::new_for_http()
.make_span_with(DefaultMakeSpan::default().level(Level::INFO)),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removing this make_span_with means we default to the default TraceLayer implementation, which makes all error responses into error traces rather than always INFO, solving #43

Info: https://docs.rs/tower-http/latest/tower_http/trace/struct.TraceLayer.html#method.new_for_http

)
.layer(TraceLayer::new_for_http().make_span_with(PropagateParentTraces))
.layer(ValidateRequestHeaderLayer::custom(
move |request: &mut Request<Body>| {
// Validate the request
Expand All @@ -359,6 +358,19 @@ where
))
}

#[derive(Clone, Copy, Debug)]
pub struct PropagateParentTraces;

impl<B> MakeSpan<B> for PropagateParentTraces {
fn make_span(&mut self, req: &Request<B>) -> Span {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This grabs the W3 trace header (ie, traceheader and tracestate) and adds them to the context of any spans we create, meaning we can follow between services.

let parent_cx = global::get_text_map_propagator(|propagator| {
propagator.extract(&HeaderExtractor(req.headers()))
});
let _cx_guard = parent_cx.attach();

tracing::info_span!("Request")
}
}
pub fn create_v2_router<C: Connector + Clone + 'static>(
state: ServerState<C>,
service_token_secret: Option<String>,
Expand All @@ -374,10 +386,7 @@ where
// .route("/mutation", post(v2_compat::post_mutation::<C>))
// .route("/raw", post(v2_compat::post_raw::<C>))
.route("/explain", post(v2_compat::post_explain::<C>))
.layer(
TraceLayer::new_for_http()
.make_span_with(DefaultMakeSpan::default().level(Level::INFO)),
)
.layer(TraceLayer::new_for_http().make_span_with(PropagateParentTraces))
.layer(ValidateRequestHeaderLayer::custom(
move |request: &mut Request<Body>| {
let provided_service_token_secret = request
Expand Down
Loading