Skip to content

Commit

Permalink
Set OTEL_STATUS_CODE for connector spans (#6133)
Browse files Browse the repository at this point in the history
  • Loading branch information
pubmodmatt authored Oct 15, 2024
1 parent ce71c17 commit 408ac64
Show file tree
Hide file tree
Showing 3 changed files with 17 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
2 changes: 2 additions & 0 deletions apollo-router/src/plugins/connectors/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use wiremock::ResponseTemplate;

use crate::json_ext::ValueExt;
use crate::plugins::connectors::tracing::CONNECT_SPAN_NAME;
use crate::plugins::telemetry::consts::OTEL_STATUS_CODE;
use crate::router_factory::RouterSuperServiceFactory;
use crate::router_factory::YamlRouterFactory;
use crate::services::new_service::ServiceFactory;
Expand Down Expand Up @@ -722,6 +723,7 @@ async fn test_tracing_connect_span() {
.fields()
.field("apollo.connector.source.detail")
.is_some());
assert!(attributes.fields().field(OTEL_STATUS_CODE).is_some());
Id::from_u64(1)
} else {
panic!("unexpected span: {}", attributes.metadata().name());
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 @@ -123,6 +123,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

0 comments on commit 408ac64

Please sign in to comment.