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

wip(prom): Simplify metrics traits #3242

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

cratelyn
Copy link
Collaborator

work in progress... 🚧

this is still in progress, but i'll start checking in incremental snapshots of progress here. 📸

`MkStreamLabel` is, in short, a generic
`(&Request) -> Option<StreamLabel>` function. we use it to inspect a
request, and potentially provide the caller with an object that can
provide relevant labels.

the `StreamLabel` interface includes associated types for the labels
used for metrics related to request/response duration, and counting
status codes.

we do not however, actually need to separately define these associated
types in the `MkStreamLabel` contract. instead, we can return a generic
`StreamLabel` of some sort, and leave the responsibility of the
(admittedly baroque) associated type access to our type aliases
like `RecordResponseDuration` and `RecordRequestDuration`.

this change has a pleasant knock-on effect of leaving a number of the
labels submodule's type aliases unused. this commit accordingly removes
aliases like `HttpRouteRsp`, `GrpcRouteRsp`, `HttpRouteBackendRsp`, and
`GrpcRouteBackendRsp`.

this is a small initial step towards simplifying code that must interact
with the `MkStreamLabel` interface.

Signed-off-by: katelyn martin <[email protected]>
our north star is making our metrics middleware easier to work with.

we want `StreamLabel` to be boxable. this will let us avoid needing to
thread baroque generics throughout our layer / service code, and instead
rely on dynamic dispatch instead of static `L: StreamLabel` bounds.

in order to move in that direction, we must take a small step back first.

these traits must be object-safe in order to do that. for a trait to be
object safe, per the Rust Reference, we may not use any function-level
generics. accordingly, this moves the `<B>` bounds up to the top-level
traits.

this makes some of our type aliases icky, and introduces new generic
parameters to various types/interfaces. we will be able to walk those
back in subsequent commits, now that these traits can be placed in e.g.
`Box<dyn StreamLabel>` values.

NB: as this is an "Zwischenzug"¹ commit, we don't update the app crates.
this commit will fail to compile if the entire workspace is built.

Signed-off-by: katelyn martin <[email protected]>

1 - https://en.wikipedia.org/wiki/Zwischenzug
this commit updates the signature of `mk_stream_labeler()` and
`init_response()` to instead examine the `http::request::Parts` and
`http::response::Parts`, respectively.

this is another step to simplify our label traits.

no implementation of `MkStreamLabel` examines the body of the request.
moreover, none even uses the `req` parameter at all!

the intent of this signature seems reasonable however, we may want to
look at certain requests and opt to ignore them. one could imagine an
`l5d-*` header to refrain from installing telemetry on the response
body, for example.

however, as the last commit showed, we are subject to plenty of pain
related to body generics. let's remove the need for them.
now that we have switched over to inspect the `Parts` head material of
the requests and responses, we can remove these type parameters, whilst
remaining object-safe.

nice!
@cratelyn cratelyn self-assigned this Sep 27, 2024
Comment on lines -79 to +61
fn init_response<B>(&mut self, rsp: &http::Response<B>);
fn init_response(&mut self, rsp: &http::response::Parts);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is a core insight i want to highlight.

we don't need or want to see the response body in our implementations of this hook. we do want to e.g. record the start time, but for now, using Parts gives us most of the same information without having to lug around that darn B type parameter.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ditto for MkStreamLabel. inspecting Parts means that we can have object-safe versions of these traits, crucial if we want to lean on type erasure and box these up over in our application code.

@cratelyn
Copy link
Collaborator Author

nb: this isn't expected to compile right now, i'm refraining from updating app code until the linkerd-http-prom interfaces are more situated.

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.

1 participant