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