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(app): Add hostname label to http metrics #3258

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions linkerd/app/outbound/src/http/logical/policy/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub(crate) mod retry;

pub(crate) use self::backend::{Backend, MatchedBackend};
pub use self::filters::errors;
use self::metrics::labels::Route as RouteLabels;

pub use self::metrics::{GrpcRouteMetrics, HttpRouteMetrics};

Expand Down Expand Up @@ -117,16 +116,33 @@ where
.push(MatchedBackend::layer(metrics.backend.clone()))
.lift_new_with_target()
.push(NewDistribute::layer())
.check_new::<Self>()
.check_new_service::<Self, http::Request<http::BoxBody>>()
// The router does not take the backend's availability into
// consideration, so we must eagerly fail requests to prevent
// leaking tasks onto the runtime.
.push_on_service(svc::LoadShed::layer())
.push(filters::NewApplyFilters::<Self, _, _>::layer())
.push(retry::NewHttpRetry::layer(metrics.retry.clone()))
.push({
let mk_extract = |rt: &Self| {
let Route {
parent_ref,
route_ref,
..
} = &rt.params;
retry::RetryLabelExtract(parent_ref.clone(), route_ref.clone())
};
let metrics = metrics.retry.clone();
retry::NewHttpRetry::layer_via_mk(mk_extract, metrics)
})
.check_new::<Self>()
.check_new_service::<Self, http::Request<http::BoxBody>>()
// Set request extensions based on the route configuration
// AND/OR headers
.push(extensions::NewSetExtensions::layer())
.push(metrics::layer(&metrics.requests))
.check_new::<Self>()
.check_new_service::<Self, http::Request<http::BoxBody>>()
// Configure a classifier to use in the endpoint stack.
// TODO(ver) move this into NewSetExtensions?
.push(classify::NewClassify::layer())
Expand All @@ -149,15 +165,6 @@ impl<T: Clone, M, F, P> svc::Param<BackendDistribution<T, F>> for MatchedRoute<T
}
}

impl<T: Clone, M, F, P> svc::Param<RouteLabels> for MatchedRoute<T, M, F, P> {
fn param(&self) -> RouteLabels {
RouteLabels(
self.params.parent_ref.clone(),
self.params.route_ref.clone(),
)
}
}
cratelyn marked this conversation as resolved.
Show resolved Hide resolved

// === impl Http ===

impl<T> filters::Apply for Http<T> {
Expand All @@ -174,15 +181,15 @@ impl<T> filters::Apply for Http<T> {

impl<T> metrics::MkStreamLabel for Http<T> {
type StatusLabels = metrics::labels::HttpRouteRsp;
type DurationLabels = metrics::labels::Route;
type DurationLabels = metrics::labels::HttpRoute;
type StreamLabel = metrics::LabelHttpRouteRsp;

fn mk_stream_labeler<B>(&self, _: &::http::Request<B>) -> Option<Self::StreamLabel> {
fn mk_stream_labeler<B>(&self, req: &::http::Request<B>) -> Option<Self::StreamLabel> {
let parent = self.params.parent_ref.clone();
let route = self.params.route_ref.clone();
Some(metrics::LabelHttpRsp::from(metrics::labels::Route::from((
parent, route,
))))
Some(metrics::LabelHttpRsp::from(
metrics::labels::HttpRoute::new(parent, route, req.uri()),
))
}
}

Expand Down Expand Up @@ -228,15 +235,15 @@ impl<T> filters::Apply for Grpc<T> {

impl<T> metrics::MkStreamLabel for Grpc<T> {
type StatusLabels = metrics::labels::GrpcRouteRsp;
type DurationLabels = metrics::labels::Route;
type DurationLabels = metrics::labels::GrpcRoute;
type StreamLabel = metrics::LabelGrpcRouteRsp;

fn mk_stream_labeler<B>(&self, _: &::http::Request<B>) -> Option<Self::StreamLabel> {
let parent = self.params.parent_ref.clone();
let route = self.params.route_ref.clone();
Some(metrics::LabelGrpcRsp::from(metrics::labels::Route::from((
parent, route,
))))
Some(metrics::LabelGrpcRsp::from(
metrics::labels::GrpcRoute::from((parent, route)),
))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub struct LabelGrpcRsp<L> {
error: Option<labels::Error>,
}

pub type LabelHttpRouteRsp = LabelHttpRsp<labels::Route>;
pub type LabelGrpcRouteRsp = LabelGrpcRsp<labels::Route>;
pub type LabelHttpRouteRsp = LabelHttpRsp<labels::HttpRoute>;
pub type LabelGrpcRouteRsp = LabelGrpcRsp<labels::GrpcRoute>;

pub type LabelHttpRouteBackendRsp = LabelHttpRsp<labels::RouteBackend>;
pub type LabelGrpcRouteBackendRsp = LabelGrpcRsp<labels::RouteBackend>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
//! Prometheus label types.
use linkerd_app_core::{errors, metrics::prom::EncodeLabelSetMut, proxy::http, Error as BoxError};
use linkerd_app_core::{
dns, errors, metrics::prom::EncodeLabelSetMut, proxy::http, Error as BoxError,
};
use prometheus_client::encoding::*;

use crate::{BackendRef, ParentRef, RouteRef};

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct Route(pub ParentRef, pub RouteRef);
pub struct HttpRoute {
parent: ParentRef,
route: RouteRef,
name: Option<dns::Name>,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct GrpcRoute(pub ParentRef, pub RouteRef);

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct RouteBackend(pub ParentRef, pub RouteRef, pub BackendRef);

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct Rsp<P, L>(pub P, pub L);

pub type RouteRsp<L> = Rsp<Route, L>;
pub type HttpRouteRsp = RouteRsp<HttpRsp>;
pub type GrpcRouteRsp = RouteRsp<GrpcRsp>;
pub type HttpRouteRsp = Rsp<HttpRoute, HttpRsp>;
pub type GrpcRouteRsp = Rsp<GrpcRoute, GrpcRsp>;

pub type RouteBackendRsp<L> = Rsp<RouteBackend, L>;
pub type HttpRouteBackendRsp = RouteBackendRsp<HttpRsp>;
Expand Down Expand Up @@ -50,15 +58,68 @@ pub enum Error {
Unknown,
}

// === impl Route ===
// === impl HttpRoute ===

impl HttpRoute {
pub fn new(parent: ParentRef, route: RouteRef, uri: &http::uri::Uri) -> Self {
let name = uri
.host()
.map(str::as_bytes)
.map(dns::Name::try_from_ascii)
.and_then(Result::ok);

Self {
parent,
route,
name,
}
}

#[cfg(test)]
pub(super) fn new_with_name(
parent: ParentRef,
route: RouteRef,
name: Option<dns::Name>,
) -> Self {
Self {
parent,
route,
name,
}
}
}

impl EncodeLabelSetMut for HttpRoute {
fn encode_label_set(&self, enc: &mut LabelSetEncoder<'_>) -> std::fmt::Result {
let Self {
parent,
route,
name,
} = self;

parent.encode_label_set(enc)?;
route.encode_label_set(enc)?;
("hostname", name.as_deref()).encode(enc.encode_label())?;

Ok(())
}
}

impl EncodeLabelSet for HttpRoute {
fn encode(&self, mut enc: LabelSetEncoder<'_>) -> std::fmt::Result {
self.encode_label_set(&mut enc)
}
}

// === impl GrpcRoute ===

impl From<(ParentRef, RouteRef)> for Route {
impl From<(ParentRef, RouteRef)> for GrpcRoute {
fn from((parent, route): (ParentRef, RouteRef)) -> Self {
Self(parent, route)
}
}

impl EncodeLabelSetMut for Route {
impl EncodeLabelSetMut for GrpcRoute {
fn encode_label_set(&self, enc: &mut LabelSetEncoder<'_>) -> std::fmt::Result {
let Self(parent, route) = self;
parent.encode_label_set(enc)?;
Expand All @@ -67,7 +128,7 @@ impl EncodeLabelSetMut for Route {
}
}

impl EncodeLabelSet for Route {
impl EncodeLabelSet for GrpcRoute {
fn encode(&self, mut enc: LabelSetEncoder<'_>) -> std::fmt::Result {
self.encode_label_set(&mut enc)
}
Expand Down
Loading
Loading