Skip to content

Commit

Permalink
Convert trace display names once. (hasura#576)
Browse files Browse the repository at this point in the history
There's no reason to go through `String`; we want an
`opentelemetry::Value`, and we shall get it.

Accepting anything that converts from `opentelemetry::Value` is also
more optimal in the case of a `&'static str`; when we have one, nothing
is cloned.

V3_GIT_ORIGIN_REV_ID: edbbf93666b15d714b95dedeb3adbcece93626fc
  • Loading branch information
SamirTalwar authored and hasura-bot committed May 14, 2024
1 parent d3f844b commit 83e4079
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 113 deletions.
2 changes: 1 addition & 1 deletion v3/crates/auth/dev-auth-webhook/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async fn graphql_request_tracing_middleware<B: Send>(
let traceable = global_tracer()
.in_span_async_with_parent_context(
"request",
"request".to_string(),
"request",
SpanVisibility::User,
&request.headers().clone(),
|| {
Expand Down
4 changes: 2 additions & 2 deletions v3/crates/auth/hasura-authn-jwt/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub async fn authenticate_request(
tracer
.in_span_async(
"jwt_authenticate_request",
"Authenticate request using JSON Web Token".to_string(),
"Authenticate request using JSON Web Token",
SpanVisibility::Internal,
|| {
Box::pin({
Expand All @@ -53,7 +53,7 @@ pub async fn authenticate_request(
let hasura_claims = tracer
.in_span_async(
"decode_and_parse_hasura_claims",
"Decode and parse Hasura claims".to_string(),
"Decode and parse Hasura claims",
SpanVisibility::Internal,
|| {
Box::pin(decode_and_parse_hasura_claims(
Expand Down
75 changes: 35 additions & 40 deletions v3/crates/auth/hasura-authn-jwt/src/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,47 +447,42 @@ async fn get_decoding_key_from_jwk_url(
) -> Result<(jwt::Algorithm, jwt::DecodingKey), Error> {
let tracer = tracing_util::global_tracer();
tracer
.in_span_async(
"fetch_jwk",
"Fetch JWK".to_string(),
SpanVisibility::Internal,
|| {
Box::pin(async {
let jwk_request = http_client
.get(jwk_url)
.headers(tracing_util::get_trace_headers())
.timeout(Duration::from_secs(60))
.build()
.map_err(InternalError::ReqwestError)?;

let jwk_response = http_client
.execute(jwk_request)
.in_span_async("fetch_jwk", "Fetch JWK", SpanVisibility::Internal, || {
Box::pin(async {
let jwk_request = http_client
.get(jwk_url)
.headers(tracing_util::get_trace_headers())
.timeout(Duration::from_secs(60))
.build()
.map_err(InternalError::ReqwestError)?;

let jwk_response = http_client
.execute(jwk_request)
.await
.map_err(InternalError::ErrorFetchingJWKSet)?;
if jwk_response.status().is_success() {
let jwk_set: jwt::jwk::JwkSet = jwk_response
.json()
.await
.map_err(InternalError::ErrorFetchingJWKSet)?;
if jwk_response.status().is_success() {
let jwk_set: jwt::jwk::JwkSet = jwk_response
.json()
.await
.map_err(InternalError::ReqwestError)?;
let decoded_header = decode_header(jwt_authorization_header)
.map_err(Error::ErrorDecodingAuthorizationHeader)?;
let kid = decoded_header.kid.ok_or(Error::KidHeaderNotFound)?;
let jwk = jwk_set
.find(kid.as_str())
.ok_or(InternalError::NoMatchingJWKFound { kid })?;
let decoding_key = jwt::DecodingKey::from_jwk(jwk)
.map_err(InternalError::JWTDecodingKeyError)?;
let algorithm = jwk
.common
.algorithm
.ok_or(InternalError::AlgorithmNotFoundInJWK)?;
Ok((algorithm, decoding_key))
} else {
Err(InternalError::UnsuccessfulJWKFetch(jwk_response.status()))?
}
})
},
)
.map_err(InternalError::ReqwestError)?;
let decoded_header = decode_header(jwt_authorization_header)
.map_err(Error::ErrorDecodingAuthorizationHeader)?;
let kid = decoded_header.kid.ok_or(Error::KidHeaderNotFound)?;
let jwk = jwk_set
.find(kid.as_str())
.ok_or(InternalError::NoMatchingJWKFound { kid })?;
let decoding_key = jwt::DecodingKey::from_jwk(jwk)
.map_err(InternalError::JWTDecodingKeyError)?;
let algorithm = jwk
.common
.algorithm
.ok_or(InternalError::AlgorithmNotFoundInJWK)?;
Ok((algorithm, decoding_key))
} else {
Err(InternalError::UnsuccessfulJWKFetch(jwk_response.status()))?
}
})
})
.await
}

Expand Down
4 changes: 2 additions & 2 deletions v3/crates/auth/hasura-authn-webhook/src/webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ async fn make_auth_hook_request(
let response = tracer
.in_span_async(
"request_to_webhook",
"Send request to webhook".to_string(),
"Send request to webhook",
SpanVisibility::Internal,
|| {
Box::pin(async {
Expand Down Expand Up @@ -292,7 +292,7 @@ pub async fn authenticate_request(
tracer
.in_span_async(
"webhook_authenticate_request",
"Webhook authenticate request".to_string(),
"Webhook authenticate request",
SpanVisibility::Internal,
|| {
Box::pin(make_auth_hook_request(
Expand Down
12 changes: 6 additions & 6 deletions v3/crates/engine/bin/engine/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async fn main() {
if let Err(e) = tracer
.in_span_async(
"app init",
"App initialization".to_string(),
"App initialization",
SpanVisibility::Internal,
|| Box::pin(start_engine(&server)),
)
Expand Down Expand Up @@ -267,7 +267,7 @@ async fn graphql_request_tracing_middleware<B: Send>(
tracer
.in_span_async_with_parent_context(
path,
path.to_string(),
path,
SpanVisibility::User,
&request.headers().clone(),
|| {
Expand Down Expand Up @@ -299,7 +299,7 @@ async fn explain_request_tracing_middleware<B: Send>(
tracer
.in_span_async_with_parent_context(
path,
path.to_string(),
path,
SpanVisibility::User,
&request.headers().clone(),
|| {
Expand Down Expand Up @@ -358,7 +358,7 @@ where
let resolved_identity = tracer
.in_span_async(
"authentication_middleware",
"Authentication middleware".to_string(),
"Authentication middleware",
SpanVisibility::Internal,
|| {
Box::pin(async {
Expand Down Expand Up @@ -408,7 +408,7 @@ async fn handle_request(
let response = tracer
.in_span_async(
"handle_request",
"Handle request".to_string(),
"Handle request",
SpanVisibility::User,
|| {
Box::pin(engine::execute::execute_query(
Expand Down Expand Up @@ -441,7 +441,7 @@ async fn handle_explain_request(
let response = tracer
.in_span_async(
"handle_explain_request",
"Handle explain request".to_string(),
"Handle explain request",
SpanVisibility::User,
|| {
Box::pin(engine::execute::explain::execute_explain(
Expand Down
42 changes: 21 additions & 21 deletions v3/crates/engine/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
pub mod error;
pub mod explain;
pub mod global_id;
pub mod ir;
pub mod model_tracking;
pub mod ndc;
pub mod plan;
pub mod process_response;
pub mod remote_joins;

use indexmap::IndexMap;
use thiserror::Error;

use gql::normalized_ast::Operation;
use hasura_authn_core::Session;
Expand All @@ -9,22 +20,11 @@ use lang_graphql::{
schema::Schema,
};
use schema::GDS;
use thiserror::Error;
use tracing_util::{
set_attribute_on_active_span, AttributeVisibility, ErrorVisibility, SpanVisibility, Traceable,
TraceableError,
};

pub mod error;
pub mod explain;
pub mod global_id;
pub mod ir;
pub mod model_tracking;
pub mod ndc;
pub mod plan;
pub mod process_response;
pub mod remote_joins;

/// Context for making HTTP requests
pub struct HttpContext {
/// The HTTP client to use for making requests
Expand Down Expand Up @@ -114,7 +114,7 @@ pub async fn execute_query_internal(
tracer
.in_span_async(
"execute_query",
"Execute query request".to_string(),
"Execute query request",
SpanVisibility::User,
|| {
tracing_util::set_attribute_on_active_span(
Expand All @@ -125,7 +125,7 @@ pub async fn execute_query_internal(
tracing_util::set_attribute_on_active_span(
AttributeVisibility::Default,
"request.graphql_query",
raw_request.query.to_string(),
raw_request.query.clone(),
);
Box::pin(async {
// parse the raw request into a GQL query
Expand All @@ -142,8 +142,8 @@ pub async fn execute_query_internal(
let request_plan = build_request_plan(&ir)?;

let display_name = match normalized_request.name {
Some(ref name) => format!("Execute {}", name),
None => "Execute request plan".to_string(),
Some(ref name) => std::borrow::Cow::Owned(format!("Execute {}", name)),
None => std::borrow::Cow::Borrowed("Execute request plan"),
};

// execute the query plan
Expand Down Expand Up @@ -199,7 +199,7 @@ pub async fn explain_query_internal(
tracer
.in_span_async(
"explain_query",
"Execute explain request".to_string(),
"Execute explain request",
SpanVisibility::User,
|| {
tracing_util::set_attribute_on_active_span(
Expand Down Expand Up @@ -230,7 +230,7 @@ pub async fn explain_query_internal(
let response = tracer
.in_span_async(
"explain",
"Explain request plan".to_string(),
"Explain request plan",
SpanVisibility::Internal,
|| {
Box::pin(async {
Expand Down Expand Up @@ -279,7 +279,7 @@ pub(crate) fn parse_query(
let query = tracer
.in_span(
"parse",
"Parse the raw request into a GraphQL query".into(),
"Parse the raw request into a GraphQL query",
SpanVisibility::Internal,
|| {
gql::parser::Parser::new(query)
Expand All @@ -302,7 +302,7 @@ pub(crate) fn normalize_request<'s>(
let normalized_request = tracer
.in_span(
"validate",
"Normalize the parsed GraphQL query".into(),
"Normalize the parsed GraphQL query",
SpanVisibility::Internal,
|| {
// add the operation name even if validation fails
Expand Down Expand Up @@ -336,7 +336,7 @@ pub(crate) fn build_ir<'n, 's>(
let tracer = tracing_util::global_tracer();
let ir = tracer.in_span(
"generate_ir",
"Generate IR for the request".into(),
"Generate IR for the request",
SpanVisibility::Internal,
|| generate_ir(schema, session, normalized_request),
)?;
Expand All @@ -350,7 +350,7 @@ pub(crate) fn build_request_plan<'n, 's, 'ir>(
let tracer = tracing_util::global_tracer();
let plan = tracer.in_span(
"plan",
"Construct a plan to execute the request".into(),
"Construct a plan to execute the request",
SpanVisibility::Internal,
|| plan::generate_request_plan(ir),
)?;
Expand Down
2 changes: 1 addition & 1 deletion v3/crates/engine/src/execute/ir/mutation_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn generate_ir<'n, 's>(
let tracer = tracing_util::global_tracer();
tracer.in_span(
"generate_ir",
"Generate IR for request".into(),
"Generate IR for request",
SpanVisibility::Internal,
|| {
let type_name = selection_set
Expand Down
4 changes: 2 additions & 2 deletions v3/crates/engine/src/execute/ndc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub(crate) async fn fetch_from_data_connector<'s>(
tracer
.in_span_async(
"fetch_from_data_connector",
"Fetch from data connector".to_string(),
"Fetch from data connector",
SpanVisibility::Internal,
|| {
Box::pin(async {
Expand Down Expand Up @@ -151,7 +151,7 @@ pub(crate) async fn execute_ndc_mutation<'n, 's, 'ir>(
// Post process the response to add the `__typename` fields
tracer.in_span(
"process_response",
"Process NDC response".into(),
"Process NDC response",
SpanVisibility::Internal,
|| {
// NOTE: NDC returns a `Vec<RowSet>` (to account for
Expand Down
Loading

0 comments on commit 83e4079

Please sign in to comment.