Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: observability layer #370

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

gregorydemay
Copy link
Member

@gregorydemay gregorydemay commented Feb 14, 2025

Follow-up on #364 to add a new observability layer to take care of logging and metrics. Unfortunately, tower_http::trace cannot be used in a canister environment because it measures call latency with Instant::now. The proposed layer, while inspired by tower_http::trace, is also simpler because it does not have to deal with streaming responses.

@@ -129,60 +125,6 @@ pub fn set_num_subnet_nodes(nodes: u32) {
});
}

pub fn http_client(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved this part to http.rs

) -> impl Service<CanisterHttpRequestArgument, Response = HttpResponse, Error = RpcError> {
ServiceBuilder::new()
.layer(
ObservabilityLayer::new()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the main starting point.

@gregorydemay gregorydemay marked this pull request as ready for review February 14, 2025 16:37
@gregorydemay gregorydemay requested a review from a team as a code owner February 14, 2025 16:37
Copy link
Contributor

@lpahlavi lpahlavi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @gregorydemay! A few nits regarding mostly the documentation, but otherwise looks good! Had to wrap my head a bit around the number of generics but the end result is quite cool.

impl<OnRequest, OnResponse, OnError> ObservabilityLayer<OnRequest, OnResponse, OnError> {
/// Customize what to do when a request is received.
///
/// `NewOnRequest` is expected to implement [`RequestObserver`].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understanding question: why do we not require NewOnRequest to implement RequestObserver here? What would be a use-case where NewOnRequest does not implement RequestObserver?

type Service = Observability<S, OnRequest, OnResponse, OnError>;

fn layer(&self, inner: S) -> Self::Service {
Self::Service {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I feel like using Observability here is clearer as this is what we are actually initializing.

Suggested change
Self::Service {
Observability {


/// Middleware that adds high level observability to a [`Service`].
///
/// See the [module docs](crate::observability) for an example.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the example here is missing.

impl<OnRequest, OnResponse, OnError> ObservabilityLayer<OnRequest, OnResponse, OnError> {
/// Customize what to do when a request is received.
///
/// `NewOnRequest` is expected to implement [`RequestObserver`].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it seems the link to RequestObserver in the doc is broken here
image
It looks exposing the type in lib.rs fixes the problem.

@@ -9,6 +9,8 @@ pub use client::{Client, IcError};
pub use cycles::{
CyclesAccounting, CyclesAccountingError, CyclesChargingPolicy, CyclesCostEstimator,
};
pub use observability::ObservabilityLayer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this fixes a few doc warnings:

Suggested change
pub use observability::ObservabilityLayer;
pub use observability::{Observability, ObservabilityLayer, RequestObserver, ResponseObserver};


/// Trait used to tell [`Observability`] what to do when a response is received.
pub trait ResponseObserver<RequestData, Result> {
/// Observe the response (typically an instance of [`std::Result`] and the request data produced by a [`RequestObserver`].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: unmatched bracket

Suggested change
/// Observe the response (typically an instance of [`std::Result`] and the request data produced by a [`RequestObserver`].
/// Observe the response (typically an instance of [`std::Result`]) and the request data produced by a [`RequestObserver`].

}

/// Trait used to tell [`Observability`] what to do when a response is received.
pub trait ResponseObserver<RequestData, Result> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I feel like this should be Response. ResponseObserver would then more closely mirror RequestObserver, and I feel it's more telling of what this actually is than just Request. I know that this is not always an HTTP response, and can be e.g. an error, but I feel like this is a type of response. WDYT?

Don't want to bikeshed the naming of generic arguments, but given that there's quite a handful here I do think it makes sense to try to make everything as readable as possible.

}
}

impl<F, ReqData, T> ResponseObserver<ReqData, T> for F
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think an example with a closure here could be pretty useful. Could also potentially be in the top-level docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants