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

Set OTEL_STATUS_CODE for connector spans #6133

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 @@ -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
18 changes: 18 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,
bnjjj marked this conversation as resolved.
Show resolved Hide resolved
);
get_text_map_propagator(|propagator| {
propagator.inject_context(
Expand Down Expand Up @@ -351,6 +358,17 @@ async fn do_fetch(
})
.await?
.into_parts();
let span = Span::current();
span.record(HTTP_RESPONSE_STATUS_CODE.as_str(), parts.status.as_u16());

// Some status codes intentionally do not result in the span being marked as success or error,
// in which case the OTEL status will just be "unset".
if parts.status.is_client_error() || parts.status.is_server_error() {
span.record(OTEL_STATUS_CODE, OTEL_STATUS_CODE_ERROR);
} else if parts.status.is_success() {
span.record(OTEL_STATUS_CODE, OTEL_STATUS_CODE_OK);
}

Ok(http::Response::from_parts(
parts,
RouterBody::wrap_stream(BodyStream { inner: body }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -841,15 +841,18 @@ resourceSpans:
kind: 3
startTimeUnixNano: "[start_time]"
endTimeUnixNano: "[end_time]"
attributes: []
attributes:
- key: http.response.status_code
value:
stringValue: "200"
droppedAttributesCount: 0
events: []
droppedEventsCount: 0
links: []
droppedLinksCount: 0
status:
message: ""
code: 0
code: 1
schemaUrl: ""
schemaUrl: ""
- resource:
Expand Down Expand Up @@ -1153,15 +1156,18 @@ resourceSpans:
kind: 3
startTimeUnixNano: "[start_time]"
endTimeUnixNano: "[end_time]"
attributes: []
attributes:
- key: http.response.status_code
value:
stringValue: "200"
droppedAttributesCount: 0
events: []
droppedEventsCount: 0
links: []
droppedLinksCount: 0
status:
message: ""
code: 0
code: 1
schemaUrl: ""
schemaUrl: ""
- resource:
Expand Down Expand Up @@ -1465,15 +1471,18 @@ resourceSpans:
kind: 3
startTimeUnixNano: "[start_time]"
endTimeUnixNano: "[end_time]"
attributes: []
attributes:
- key: http.response.status_code
value:
stringValue: "200"
droppedAttributesCount: 0
events: []
droppedEventsCount: 0
links: []
droppedLinksCount: 0
status:
message: ""
code: 0
code: 1
schemaUrl: ""
schemaUrl: ""
- resource:
Expand Down Expand Up @@ -2038,15 +2047,18 @@ resourceSpans:
kind: 3
startTimeUnixNano: "[start_time]"
endTimeUnixNano: "[end_time]"
attributes: []
attributes:
- key: http.response.status_code
value:
stringValue: "200"
droppedAttributesCount: 0
events: []
droppedEventsCount: 0
links: []
droppedLinksCount: 0
status:
message: ""
code: 0
code: 1
schemaUrl: ""
schemaUrl: ""
- resource:
Expand Down Expand Up @@ -2350,15 +2362,18 @@ resourceSpans:
kind: 3
startTimeUnixNano: "[start_time]"
endTimeUnixNano: "[end_time]"
attributes: []
attributes:
- key: http.response.status_code
value:
stringValue: "200"
droppedAttributesCount: 0
events: []
droppedEventsCount: 0
links: []
droppedLinksCount: 0
status:
message: ""
code: 0
code: 1
schemaUrl: ""
schemaUrl: ""
- resource:
Expand Down Expand Up @@ -2662,14 +2677,17 @@ resourceSpans:
kind: 3
startTimeUnixNano: "[start_time]"
endTimeUnixNano: "[end_time]"
attributes: []
attributes:
- key: http.response.status_code
value:
stringValue: "200"
droppedAttributesCount: 0
events: []
droppedEventsCount: 0
links: []
droppedLinksCount: 0
status:
message: ""
code: 0
code: 1
schemaUrl: ""
schemaUrl: ""
Original file line number Diff line number Diff line change
Expand Up @@ -841,15 +841,18 @@ resourceSpans:
kind: 3
startTimeUnixNano: "[start_time]"
endTimeUnixNano: "[end_time]"
attributes: []
attributes:
- key: http.response.status_code
value:
stringValue: "200"
droppedAttributesCount: 0
events: []
droppedEventsCount: 0
links: []
droppedLinksCount: 0
status:
message: ""
code: 0
code: 1
schemaUrl: ""
schemaUrl: ""
- resource:
Expand Down Expand Up @@ -1153,15 +1156,18 @@ resourceSpans:
kind: 3
startTimeUnixNano: "[start_time]"
endTimeUnixNano: "[end_time]"
attributes: []
attributes:
- key: http.response.status_code
value:
stringValue: "200"
droppedAttributesCount: 0
events: []
droppedEventsCount: 0
links: []
droppedLinksCount: 0
status:
message: ""
code: 0
code: 1
schemaUrl: ""
schemaUrl: ""
- resource:
Expand Down Expand Up @@ -1465,15 +1471,18 @@ resourceSpans:
kind: 3
startTimeUnixNano: "[start_time]"
endTimeUnixNano: "[end_time]"
attributes: []
attributes:
- key: http.response.status_code
value:
stringValue: "200"
droppedAttributesCount: 0
events: []
droppedEventsCount: 0
links: []
droppedLinksCount: 0
status:
message: ""
code: 0
code: 1
schemaUrl: ""
schemaUrl: ""
- resource:
Expand Down Expand Up @@ -2038,15 +2047,18 @@ resourceSpans:
kind: 3
startTimeUnixNano: "[start_time]"
endTimeUnixNano: "[end_time]"
attributes: []
attributes:
- key: http.response.status_code
value:
stringValue: "200"
droppedAttributesCount: 0
events: []
droppedEventsCount: 0
links: []
droppedLinksCount: 0
status:
message: ""
code: 0
code: 1
schemaUrl: ""
schemaUrl: ""
- resource:
Expand Down Expand Up @@ -2350,15 +2362,18 @@ resourceSpans:
kind: 3
startTimeUnixNano: "[start_time]"
endTimeUnixNano: "[end_time]"
attributes: []
attributes:
- key: http.response.status_code
value:
stringValue: "200"
droppedAttributesCount: 0
events: []
droppedEventsCount: 0
links: []
droppedLinksCount: 0
status:
message: ""
code: 0
code: 1
schemaUrl: ""
schemaUrl: ""
- resource:
Expand Down Expand Up @@ -2662,14 +2677,17 @@ resourceSpans:
kind: 3
startTimeUnixNano: "[start_time]"
endTimeUnixNano: "[end_time]"
attributes: []
attributes:
- key: http.response.status_code
value:
stringValue: "200"
droppedAttributesCount: 0
events: []
droppedEventsCount: 0
links: []
droppedLinksCount: 0
status:
message: ""
code: 0
code: 1
schemaUrl: ""
schemaUrl: ""
Loading