Skip to content

Commit 7512650

Browse files
authored
test(gax-internal): Add tracing test for connection error (#3582)
This PR adds an integration test to verify OpenTelemetry span emission when an HTTP client connection error occurs. Specifically, this test ensures that: - A single span named `http_request` is emitted. - The `error.type` attribute is correctly set to `CLIENT_CONNECTION_ERROR`. - The `http.response.status_code` attribute is not present, as no response was received. This test case helps ensure that network-level connection failures are properly instrumented.
1 parent 500b072 commit 7512650

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/gax-internal/tests/http_client_errors.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,63 @@ mod tests {
5353

5454
Ok(())
5555
}
56+
57+
#[cfg(all(test, google_cloud_unstable_tracing, feature = "_internal-http-client"))]
58+
mod tracing_tests {
59+
use super::*;
60+
use google_cloud_gax_internal::http::ReqwestClient;
61+
use google_cloud_gax_internal::observability::attributes::error_type_values::CLIENT_CONNECTION_ERROR;
62+
use google_cloud_gax_internal::options::ClientConfig;
63+
use google_cloud_test_utils::test_layer::TestLayer;
64+
use opentelemetry_semantic_conventions::trace as semconv;
65+
66+
#[tokio::test]
67+
async fn test_connection_error_with_tracing_on() -> Result<()> {
68+
use serde_json::Value;
69+
let endpoint = "http://localhost:1"; // Non-existent port
70+
let mut config = ClientConfig::default();
71+
config.tracing = true;
72+
config.cred = Some(test_credentials());
73+
let client = ReqwestClient::new(config, endpoint).await?;
74+
75+
let guard = TestLayer::initialize();
76+
77+
let builder = client.builder(reqwest::Method::GET, "/".into());
78+
let result = client
79+
.execute::<Value, Value>(builder, Option::<Value>::None, RequestOptions::default())
80+
.await;
81+
82+
assert!(result.is_err(), "Expected connection error");
83+
84+
let spans = TestLayer::capture(&guard);
85+
assert_eq!(spans.len(), 1, "Expected 1 span, got: {:?}", spans);
86+
87+
let span = &spans[0];
88+
let attributes = &span.attributes;
89+
assert_eq!(
90+
span.name, "http_request",
91+
"Span name mismatch: {:?}, all attributes: {:?}",
92+
span, attributes
93+
);
94+
95+
let expected_error_type = CLIENT_CONNECTION_ERROR.to_string();
96+
assert_eq!(
97+
attributes.get(semconv::ERROR_TYPE),
98+
Some(&expected_error_type),
99+
"Span 0: '{}' mismatch, expected: {:?}, got: {:?}, all attributes: {:?}",
100+
semconv::ERROR_TYPE,
101+
Some(&expected_error_type),
102+
attributes.get(semconv::ERROR_TYPE),
103+
attributes
104+
);
105+
assert!(
106+
!attributes.contains_key(semconv::HTTP_RESPONSE_STATUS_CODE),
107+
"Span 0: '{}' should not be present on connection error, all attributes: {:?}",
108+
semconv::HTTP_RESPONSE_STATUS_CODE,
109+
attributes
110+
);
111+
112+
Ok(())
113+
}
114+
}
56115
}

0 commit comments

Comments
 (0)