From 71cca7fc6e7a6c514bdaa3d4d7a968504c3d5eaf Mon Sep 17 00:00:00 2001 From: katelyn martin Date: Fri, 1 Nov 2024 00:00:00 +0000 Subject: [PATCH] refactor(app): Outline route label extraction this addresses the `TODO` comment, moving the `RouteLabelExtract` type out of the policy route layer function. this defines a method on `MatchedRoute` which may be called to retrieve a `RouteLabelExtract`. that type holds references to the contruction-time information about the route, and can be used to later inspect an outbound request at call-time, returning a `labels::Route` set of labels. this is a helpful piece of reusable glue that is broadly useful in instrumenting our route-level middleware. Signed-off-by: katelyn martin --- .../outbound/src/http/logical/policy/route.rs | 13 ++---------- .../logical/policy/route/metrics/labels.rs | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/linkerd/app/outbound/src/http/logical/policy/route.rs b/linkerd/app/outbound/src/http/logical/policy/route.rs index dca0e4b07c..fd6cbdebff 100644 --- a/linkerd/app/outbound/src/http/logical/policy/route.rs +++ b/linkerd/app/outbound/src/http/logical/policy/route.rs @@ -124,18 +124,9 @@ where .push_on_service(svc::LoadShed::layer()) .push(filters::NewApplyFilters::::layer()) .push({ - // TODO(kate): extracting route labels like this should ideally live somewhere - // else, like e.g. the `SetExtensions` middleware. - let mk_extract = |rt: &Self| { - let Route { - parent_ref, - route_ref, - .. - } = &rt.params; - metrics::labels::RouteLabelExtract(parent_ref.clone(), route_ref.clone()) - }; + let mk = Self::label_extractor; let metrics = metrics.retry.clone(); - retry::NewHttpRetry::layer_via_mk(mk_extract, metrics) + retry::NewHttpRetry::layer_via_mk(mk, metrics) }) .check_new::() .check_new_service::>() diff --git a/linkerd/app/outbound/src/http/logical/policy/route/metrics/labels.rs b/linkerd/app/outbound/src/http/logical/policy/route/metrics/labels.rs index f7f144d487..0378bf4a44 100644 --- a/linkerd/app/outbound/src/http/logical/policy/route/metrics/labels.rs +++ b/linkerd/app/outbound/src/http/logical/policy/route/metrics/labels.rs @@ -215,6 +215,26 @@ impl EncodeLabelSet for GrpcRsp { } } +// === impl MatchedRoute === + +impl super::super::MatchedRoute { + /// Returns a [`RouteLabelExtract`]. + /// + /// The extractor returned by this function provides a [`ExtractParam`] implementation + /// that can be used to acquire the route-level labels corresponding to a given outbound + /// request. + pub(crate) fn label_extractor(&self) -> RouteLabelExtract { + use super::super::Route; + let Route { + parent_ref, + route_ref, + .. + } = &self.params; + + RouteLabelExtract(parent_ref.clone(), route_ref.clone()) + } +} + // === impl RouteLabelExtract === impl ExtractParam> for RouteLabelExtract {