Skip to content

Commit 408ac64

Browse files
authored
Set OTEL_STATUS_CODE for connector spans (#6133)
1 parent ce71c17 commit 408ac64

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

apollo-router/src/plugins/connectors/handle_responses.rs

+14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use http_body::Body as HttpBody;
55
use parking_lot::Mutex;
66
use serde_json_bytes::ByteString;
77
use serde_json_bytes::Value;
8+
use tracing::Span;
89

910
use crate::error::FetchError;
1011
use crate::graphql;
@@ -14,6 +15,9 @@ use crate::plugins::connectors::make_requests::ResponseKey;
1415
use crate::plugins::connectors::make_requests::ResponseTypeName;
1516
use crate::plugins::connectors::plugin::ConnectorContext;
1617
use crate::plugins::connectors::plugin::SelectionData;
18+
use crate::plugins::telemetry::consts::OTEL_STATUS_CODE;
19+
use crate::plugins::telemetry::consts::OTEL_STATUS_CODE_ERROR;
20+
use crate::plugins::telemetry::consts::OTEL_STATUS_CODE_OK;
1721
use crate::services::connect::Response;
1822
use crate::services::fetch::AddSubgraphNameExt;
1923

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

225+
Span::current().record(
226+
OTEL_STATUS_CODE,
227+
if errors.is_empty() {
228+
OTEL_STATUS_CODE_OK
229+
} else {
230+
OTEL_STATUS_CODE_ERROR
231+
},
232+
);
233+
220234
Ok(Response {
221235
response: http::Response::builder()
222236
.body(

apollo-router/src/plugins/connectors/tests.rs

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use wiremock::ResponseTemplate;
2727

2828
use crate::json_ext::ValueExt;
2929
use crate::plugins::connectors::tracing::CONNECT_SPAN_NAME;
30+
use crate::plugins::telemetry::consts::OTEL_STATUS_CODE;
3031
use crate::router_factory::RouterSuperServiceFactory;
3132
use crate::router_factory::YamlRouterFactory;
3233
use crate::services::new_service::ServiceFactory;
@@ -722,6 +723,7 @@ async fn test_tracing_connect_span() {
722723
.fields()
723724
.field("apollo.connector.source.detail")
724725
.is_some());
726+
assert!(attributes.fields().field(OTEL_STATUS_CODE).is_some());
725727
Id::from_u64(1)
726728
} else {
727729
panic!("unexpected span: {}", attributes.metadata().name());

apollo-router/src/services/connector_service.rs

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ impl tower::Service<ConnectRequest> for ConnectorService {
123123
"apollo.connector.source.name" = tracing::field::Empty,
124124
"apollo.connector.source.detail" = tracing::field::Empty,
125125
"apollo_private.sent_time_offset" = fetch_time_offset,
126+
"otel.status_code" = tracing::field::Empty,
126127
);
127128
// TODO: apollo.connector.field.alias
128129
// TODO: apollo.connector.field.return_type

0 commit comments

Comments
 (0)