-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add InstrumentLayer and InstrumentPlugin
- Loading branch information
Harry Barber
committed
Sep 20, 2022
1 parent
4b26e49
commit 46ffa8f
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
use tower::Layer; | ||
|
||
use super::{InstrumentOperation, MakeIdentity}; | ||
|
||
/// A [`Layer`] used to apply [`InstrumentOperation`]. | ||
#[derive(Debug)] | ||
pub struct InstrumentLayer<RequestMakeFmt = MakeIdentity, ResponseMakeFmt = MakeIdentity> { | ||
operation_name: &'static str, | ||
make_request: RequestMakeFmt, | ||
make_response: ResponseMakeFmt, | ||
} | ||
|
||
impl InstrumentLayer { | ||
/// Constructs a new [`InstrumentLayer`] with no data redacted. | ||
pub fn new(operation_name: &'static str) -> Self { | ||
Self { | ||
operation_name, | ||
make_request: MakeIdentity, | ||
make_response: MakeIdentity, | ||
} | ||
} | ||
} | ||
|
||
impl<RequestMakeFmt, ResponseMakeFmt> InstrumentLayer<RequestMakeFmt, ResponseMakeFmt> { | ||
/// Configures the request format. | ||
/// | ||
/// The argument is typically [`RequestFmt`](super::sensitivity::RequestFmt). | ||
pub fn request_fmt<R>(self, make_request: R) -> InstrumentLayer<R, ResponseMakeFmt> { | ||
InstrumentLayer { | ||
operation_name: self.operation_name, | ||
make_request, | ||
make_response: self.make_response, | ||
} | ||
} | ||
|
||
/// Configures the response format. | ||
/// | ||
/// The argument is typically [`ResponseFmt`](super::sensitivity::ResponseFmt). | ||
pub fn response_fmt<R>(self, make_response: R) -> InstrumentLayer<RequestMakeFmt, R> { | ||
InstrumentLayer { | ||
operation_name: self.operation_name, | ||
make_request: self.make_request, | ||
make_response, | ||
} | ||
} | ||
} | ||
|
||
impl<S, RequestMakeFmt, ResponseMakeFmt> Layer<S> for InstrumentLayer<RequestMakeFmt, ResponseMakeFmt> | ||
where | ||
RequestMakeFmt: Clone, | ||
ResponseMakeFmt: Clone, | ||
{ | ||
type Service = InstrumentOperation<S, RequestMakeFmt, ResponseMakeFmt>; | ||
|
||
fn layer(&self, service: S) -> Self::Service { | ||
InstrumentOperation::new(service, self.operation_name) | ||
.request_fmt(self.make_request.clone()) | ||
.response_fmt(self.make_response.clone()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
use tower::layer::util::Stack; | ||
|
||
use crate::{ | ||
operation::{Operation, OperationShape}, | ||
plugin::{Pluggable, Plugin}, | ||
}; | ||
|
||
use super::{layer::InstrumentLayer, sensitivity::Sensitivity}; | ||
|
||
/// An [`Plugin`] which applies [`InstrumentLayer`] to all operations in the builder. | ||
#[derive(Debug)] | ||
pub struct InstrumentPlugin; | ||
|
||
impl<P, Op, S, L> Plugin<P, Op, S, L> for InstrumentPlugin | ||
where | ||
Op: OperationShape, | ||
Op: Sensitivity, | ||
{ | ||
type Service = S; | ||
type Layer = Stack<L, InstrumentLayer<Op::RequestFmt, Op::ResponseFmt>>; | ||
|
||
fn map(&self, operation: Operation<S, L>) -> Operation<Self::Service, Self::Layer> { | ||
let layer = InstrumentLayer::new(Op::NAME) | ||
.request_fmt(Op::request_fmt()) | ||
.response_fmt(Op::response_fmt()); | ||
operation.layer(layer) | ||
} | ||
} | ||
|
||
/// An extension trait for applying [`InstrumentLayer`] to all operations. | ||
pub trait InstrumentExt: Pluggable<InstrumentPlugin> { | ||
/// Applies [`InstrumentLayer`] to all operations. See [`InstrumentOperation`](super::InstrumentOperation) for more | ||
/// information. | ||
fn instrument(self) -> Self::Output | ||
where | ||
Self: Sized, | ||
{ | ||
self.apply(InstrumentPlugin) | ||
} | ||
} | ||
|
||
impl<Builder> InstrumentExt for Builder where Builder: Pluggable<InstrumentPlugin> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters