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::{
|
8 - | sdk_feature::AwsSdkFeature, user_agent::test_util::assert_ua_contains_metric_values,
|
9 - | };
|
10 - | use aws_sdk_s3::{
|
11 - | config::{Intercept, IntoShared},
|
12 - | primitives::ByteStream,
|
13 - | Client, Config,
|
14 - | };
|
15 - | use aws_smithy_http_client::test_util::capture_request;
|
16 - |
|
17 - | #[derive(Debug)]
|
18 - | struct TransferManagerFeatureInterceptor;
|
19 - |
|
20 - | impl Intercept for TransferManagerFeatureInterceptor {
|
21 - | fn name(&self) -> &'static str {
|
22 - | "TransferManagerFeature"
|
23 - | }
|
24 - |
|
25 - | fn read_before_execution(
|
26 - | &self,
|
27 - | _ctx: &aws_sdk_s3::config::interceptors::BeforeSerializationInterceptorContextRef<'_>,
|
28 - | cfg: &mut aws_sdk_s3::config::ConfigBag,
|
29 - | ) -> Result<(), aws_sdk_s3::error::BoxError> {
|
30 - | cfg.interceptor_state()
|
31 - | .store_append(AwsSdkFeature::S3Transfer);
|
32 - | Ok(())
|
33 - | }
|
34 - | }
|
35 - |
|
36 - | #[tokio::test]
|
37 - | async fn test_track_metric_for_s3_transfer_manager() {
|
38 - | let (http_client, captured_request) = capture_request(None);
|
39 - | let mut conf_builder = Config::builder()
|
40 - | .region(Region::new("us-east-1"))
|
41 - | .http_client(http_client.clone())
|
42 - | .with_test_defaults();
|
43 - | // The S3 Transfer Manager uses a passed-in S3 client SDK for operations.
|
44 - | // By configuring an interceptor at the client level to track metrics,
|
45 - | // all operations executed by the client will automatically include the metric.
|
46 - | // This eliminates the need to apply `.config_override` on individual operations
|
47 - | // to insert the `TransferManagerFeatureInterceptor`.
|
48 - | conf_builder.push_interceptor(TransferManagerFeatureInterceptor.into_shared());
|
49 - | let client = Client::from_conf(conf_builder.build());
|
50 - |
|
51 - | let _ = client
|
52 - | .put_object()
|
53 - | .bucket("doesnotmatter")
|
54 - | .key("doesnotmatter")
|
55 - | .body(ByteStream::from_static("Hello, world".as_bytes()))
|
56 - | .send()
|
57 - | .await
|
58 - | .unwrap();
|
59 - |
|
60 - | let expected_req = captured_request.expect_request();
|
61 - | let user_agent = expected_req.headers().get("x-amz-user-agent").unwrap();
|
62 - | assert_ua_contains_metric_values(user_agent, &["G"]);
|
63 - | }
|