1 + | /*
|
2 + | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
3 + | * SPDX-License-Identifier: Apache-2.0
|
4 + | */
|
5 + |
|
6 + | use aws_config::Region;
|
7 + | use aws_sdk_s3::config::{Credentials, SharedCredentialsProvider};
|
8 + | use aws_smithy_observability::TelemetryProvider;
|
9 + | use aws_smithy_runtime::client::http::test_util::{ReplayEvent, StaticReplayClient};
|
10 + | use aws_smithy_types::body::SdkBody;
|
11 + | use serial_test::serial;
|
12 + | use utils::init_metrics;
|
13 + |
|
14 + | mod utils;
|
15 + |
|
16 + | // Note: These tests are written with a multi-threaded runtime since OTel requires that to work
|
17 + | // and they are all run serially since they touch global state
|
18 + |
|
19 + | #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
20 + | #[serial]
|
21 + | async fn observability_otel_metrics_feature_tracked_in_user_agent() {
|
22 + | let (meter_provider, _exporter) = init_metrics();
|
23 + |
|
24 + | // Create a replay client to capture the actual HTTP request
|
25 + | let http_client = StaticReplayClient::new(vec![ReplayEvent::new(
|
26 + | http::Request::builder().body(SdkBody::empty()).unwrap(),
|
27 + | http::Response::builder().body(SdkBody::empty()).unwrap(),
|
28 + | )]);
|
29 + |
|
30 + | let config = aws_config::SdkConfig::builder()
|
31 + | .credentials_provider(SharedCredentialsProvider::new(Credentials::for_tests()))
|
32 + | .region(Region::new("us-east-1"))
|
33 + | .http_client(http_client.clone())
|
34 + | .build();
|
35 + |
|
36 + | let s3_client = aws_sdk_s3::Client::new(&config);
|
37 + | let _ = s3_client
|
38 + | .get_object()
|
39 + | .bucket("test-bucket")
|
40 + | .key("test.txt")
|
41 + | .send()
|
42 + | .await;
|
43 + |
|
44 + | // Get the actual HTTP request that was made
|
45 + | let requests = http_client.actual_requests();
|
46 + | let last_request = requests.last().expect("should have made a request");
|
47 + |
|
48 + | let user_agent = last_request
|
49 + | .headers()
|
50 + | .get("x-amz-user-agent")
|
51 + | .expect("should have user-agent header");
|
52 + |
|
53 + | // Should contain OBSERVABILITY_OTEL_METRICS metric (value "7")
|
54 + | // The metric appears in the m/ section, e.g. "m/E,b,7"
|
55 + | assert!(
|
56 + | user_agent.contains(",7") || user_agent.ends_with("/7") || user_agent.contains("7,"),
|
57 + | "User-Agent should contain observability OTel metrics feature (7), got: {}",
|
58 + | user_agent
|
59 + | );
|
60 + |
|
61 + | meter_provider.flush().unwrap();
|
62 + |
|
63 + | // Reset to noop for other tests
|
64 + | aws_smithy_observability::global::set_telemetry_provider(TelemetryProvider::noop()).unwrap();
|
65 + | }
|
66 + |
|
67 + | #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
68 + | #[serial]
|
69 + | async fn noop_provider_does_not_track_observability_metrics() {
|
70 + | // Reset to noop provider
|
71 + | aws_smithy_observability::global::set_telemetry_provider(TelemetryProvider::noop()).unwrap();
|
72 + |
|
73 + | // Create a replay client to capture the actual HTTP request
|
74 + | let http_client = StaticReplayClient::new(vec![ReplayEvent::new(
|
75 + | http::Request::builder().body(SdkBody::empty()).unwrap(),
|
76 + | http::Response::builder().body(SdkBody::empty()).unwrap(),
|
77 + | )]);
|
78 + |
|
79 + | let config = aws_config::SdkConfig::builder()
|
80 + | .credentials_provider(SharedCredentialsProvider::new(Credentials::for_tests()))
|
81 + | .region(Region::new("us-east-1"))
|
82 + | .http_client(http_client.clone())
|
83 + | .build();
|
84 + |
|
85 + | let s3_client = aws_sdk_s3::Client::new(&config);
|
86 + | let _ = s3_client
|
87 + | .get_object()
|
88 + | .bucket("test-bucket")
|
89 + | .key("test.txt")
|
90 + | .send()
|
91 + | .await;
|
92 + |
|
93 + | // Get the actual HTTP request that was made
|
94 + | let requests = http_client.actual_requests();
|
95 + | let last_request = requests.last().expect("should have made a request");
|
96 + |
|
97 + | let user_agent = last_request
|
98 + | .headers()
|
99 + | .get("x-amz-user-agent")
|
100 + | .expect("should have user-agent header");
|
101 + |
|
102 + | // Should NOT contain OBSERVABILITY_OTEL_METRICS metric when using noop provider
|
103 + | // The metric value is "7"
|
104 + | assert!(
|
105 + | !user_agent.contains(",7") && !user_agent.ends_with("/7") && !user_agent.contains("7,"),
|
106 + | "User-Agent should not contain observability OTel metrics feature (7) with noop provider, got: {}",
|
107 + | user_agent
|
108 + | );
|
109 + | }
|