Skip to content

Commit

Permalink
Set OTEL_STATUS_CODE for connector and HTTP client spans
Browse files Browse the repository at this point in the history
  • Loading branch information
pubmodmatt committed Oct 9, 2024
1 parent 9e3219c commit 3138f9b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions apollo-router/src/plugins/connectors/handle_responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use http_body::Body as HttpBody;
use parking_lot::Mutex;
use serde_json_bytes::ByteString;
use serde_json_bytes::Value;
use tracing::Span;

use crate::error::FetchError;
use crate::graphql;
Expand All @@ -14,6 +15,9 @@ use crate::plugins::connectors::make_requests::ResponseKey;
use crate::plugins::connectors::make_requests::ResponseTypeName;
use crate::plugins::connectors::plugin::ConnectorContext;
use crate::plugins::connectors::plugin::SelectionData;
use crate::plugins::telemetry::consts::OTEL_STATUS_CODE;
use crate::plugins::telemetry::consts::OTEL_STATUS_CODE_ERROR;
use crate::plugins::telemetry::consts::OTEL_STATUS_CODE_OK;
use crate::services::connect::Response;
use crate::services::fetch::AddSubgraphNameExt;

Expand Down Expand Up @@ -67,6 +71,7 @@ pub(crate) async fn handle_responses<T: HttpBody>(
.lock()
.push_invalid_response(debug_request, &parts, body);
}
Span::current().record(OTEL_STATUS_CODE, OTEL_STATUS_CODE_ERROR);
// TODO this stops processing all responses
return Err(InvalidResponseBody(format!(
"couldn't deserialize response body: {e}"
Expand Down Expand Up @@ -217,6 +222,15 @@ pub(crate) async fn handle_responses<T: HttpBody>(
Value::Object(data)
};

Span::current().record(
OTEL_STATUS_CODE,
if errors.is_empty() {
OTEL_STATUS_CODE_OK
} else {
OTEL_STATUS_CODE_ERROR
},
);

Ok(Response {
response: http::Response::builder()
.body(
Expand Down
1 change: 1 addition & 0 deletions apollo-router/src/services/connector_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl tower::Service<ConnectRequest> for ConnectorService {
"apollo.connector.source.name" = tracing::field::Empty,
"apollo.connector.source.detail" = tracing::field::Empty,
"apollo_private.sent_time_offset" = fetch_time_offset,
"otel.status_code" = tracing::field::Empty,
);
// TODO: apollo.connector.field.alias
// TODO: apollo.connector.field.return_type
Expand Down
17 changes: 17 additions & 0 deletions apollo-router/src/services/http/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use hyper_rustls::HttpsConnector;
#[cfg(unix)]
use hyperlocal::UnixConnector;
use opentelemetry::global;
use opentelemetry_semantic_conventions::trace::HTTP_RESPONSE_STATUS_CODE;
use pin_project_lite::pin_project;
use rustls::ClientConfig;
use rustls::RootCertStore;
Expand All @@ -30,6 +31,7 @@ use tower_http::decompression::Decompression;
use tower_http::decompression::DecompressionBody;
use tower_http::decompression::DecompressionLayer;
use tracing::Instrument;
use tracing::Span;

use super::HttpRequest;
use super::HttpResponse;
Expand All @@ -38,6 +40,9 @@ use crate::configuration::TlsClientAuth;
use crate::error::FetchError;
use crate::plugins::authentication::subgraph::SigningParamsConfig;
use crate::plugins::telemetry::consts::HTTP_REQUEST_SPAN_NAME;
use crate::plugins::telemetry::consts::OTEL_STATUS_CODE;
use crate::plugins::telemetry::consts::OTEL_STATUS_CODE_ERROR;
use crate::plugins::telemetry::consts::OTEL_STATUS_CODE_OK;
use crate::plugins::telemetry::otel::OpenTelemetrySpanExt;
use crate::plugins::telemetry::reload::prepare_context;
use crate::plugins::telemetry::LOGGING_DISPLAY_BODY;
Expand Down Expand Up @@ -267,6 +272,8 @@ impl tower::Service<HttpRequest> for HttpClientService {
"net.transport" = "ip_tcp",
//"apollo.subgraph.name" = %service_name,
//"graphql.operation.name" = %operation_name,
"otel.status_code" = tracing::field::Empty,
"http.response.status_code" = tracing::field::Empty,
);
get_text_map_propagator(|propagator| {
propagator.inject_context(
Expand Down Expand Up @@ -351,6 +358,16 @@ async fn do_fetch(
})
.await?
.into_parts();
let span = Span::current();
span.record(HTTP_RESPONSE_STATUS_CODE.as_str(), parts.status.as_u16());
span.record(
OTEL_STATUS_CODE,
if parts.status.is_client_error() || parts.status.is_server_error() {
OTEL_STATUS_CODE_ERROR
} else {
OTEL_STATUS_CODE_OK
},
);
Ok(http::Response::from_parts(
parts,
RouterBody::wrap_stream(BodyStream { inner: body }),
Expand Down

0 comments on commit 3138f9b

Please sign in to comment.