aws_smithy_observability/unit_of_work.rs
1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Records what happened during one call to a generated client.
7//!
8//! A *handle* is a value that records one call. You keep the handle, add it to a [`Capture`] with
9//! [`Capture::with`], and attach the `Capture` to the call as an interceptor. The `Capture` holds
10//! a clone of the handle, and clones share one record, so the handle you kept reads what the call
11//! reported. In this release the only handle is [`ChecksumCapture`], which records whether the
12//! response body was validated against its checksum.
13//!
14//! ```
15//! use aws_smithy_observability::unit_of_work::checksums::{ChecksumCapture, ChecksumOutcome};
16//! use aws_smithy_observability::unit_of_work::Capture;
17//!
18//! let checksum = ChecksumCapture::new();
19//! let capture = Capture::new().with(&checksum);
20//! // Attach `capture` to one call with `.customize().interceptor(capture)`, send the call, and
21//! // read the response body to its end. Then:
22//! match checksum.try_outcome() {
23//! Some(ChecksumOutcome::Validated { algorithm, .. }) => {
24//! println!("validated with {}", algorithm.as_str())
25//! }
26//! Some(ChecksumOutcome::Mismatch { .. }) => println!("the body does not match its checksum"),
27//! Some(other) => println!("not validated: {other:?}"),
28//! None => println!("the call or its body has not ended"),
29//! }
30//! ```
31
32pub mod checksums;
33
34use aws_smithy_runtime_api::box_error::BoxError;
35use aws_smithy_runtime_api::client::interceptors::context::{
36 BeforeSerializationInterceptorContextRef, BeforeTransmitInterceptorContextRef,
37 FinalizerInterceptorContextRef,
38};
39use aws_smithy_runtime_api::client::interceptors::{dyn_dispatch_hint, Intercept};
40use aws_smithy_runtime_api::client::runtime_components::RuntimeComponents;
41use aws_smithy_types::config_bag::ConfigBag;
42use checksums::ChecksumCapture;
43
44/// Carries handles into one call to a generated client.
45///
46/// Attach it to a call with `.customize().interceptor(..)`, which consumes it. A handle records
47/// one call: attached to a second call, it reads
48/// [`NotValidatedReason::Reused`](checksums::NotValidatedReason::Reused) instead.
49#[derive(Debug, Default)]
50pub struct Capture {
51 checksums: Vec<ChecksumCapture>,
52}
53
54impl Capture {
55 /// Creates a `Capture` that carries no handles.
56 pub fn new() -> Self {
57 Self::default()
58 }
59
60 /// Adds a handle, which records the call this `Capture` is attached to.
61 pub fn with(mut self, handle: &impl AsHandle) -> Self {
62 handle.add_to(&mut self);
63 self
64 }
65}
66
67impl Drop for Capture {
68 fn drop(&mut self) {
69 for handle in &self.checksums {
70 handle.capture_dropped();
71 }
72 }
73}
74
75#[dyn_dispatch_hint]
76impl Intercept for Capture {
77 fn name(&self) -> &'static str {
78 "Capture"
79 }
80
81 fn read_before_execution(
82 &self,
83 _context: &BeforeSerializationInterceptorContextRef<'_>,
84 cfg: &mut ConfigBag,
85 ) -> Result<(), BoxError> {
86 for handle in &self.checksums {
87 handle.bind(cfg);
88 }
89 Ok(())
90 }
91
92 fn read_before_attempt(
93 &self,
94 _context: &BeforeTransmitInterceptorContextRef<'_>,
95 _runtime_components: &RuntimeComponents,
96 _cfg: &mut ConfigBag,
97 ) -> Result<(), BoxError> {
98 for handle in &self.checksums {
99 handle.attempt_started();
100 }
101 Ok(())
102 }
103
104 fn read_after_execution(
105 &self,
106 context: &FinalizerInterceptorContextRef<'_>,
107 _runtime_components: &RuntimeComponents,
108 _cfg: &mut ConfigBag,
109 ) -> Result<(), BoxError> {
110 let succeeded = matches!(context.output_or_error(), Some(Ok(_)));
111 for handle in &self.checksums {
112 handle.call_ended(succeeded);
113 }
114 Ok(())
115 }
116}
117
118/// A handle that a [`Capture`] can carry into a call.
119///
120/// This trait is sealed. [`ChecksumCapture`] is the only handle in this release.
121pub trait AsHandle: sealed::Sealed {}
122
123mod sealed {
124 /// Keeps [`AsHandle`](super::AsHandle) implemented only by this crate's handles.
125 pub trait Sealed {
126 /// Adds this handle to `capture`.
127 fn add_to(&self, capture: &mut super::Capture);
128 }
129}