Skip to content
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
116 changes: 66 additions & 50 deletions linkerd/app/outbound/src/http/logical/policy/route/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ use linkerd_app_core::{
use linkerd_http_prom::{
body_data::request::{BodyDataMetrics, NewRecordBodyData, RequestBodyFamilies},
record_response,
stream_label::{LabelSet, StreamLabel},
stream_label::{
error::LabelError,
status::{LabelGrpcStatus, LabelHttpStatus},
LabelSet, StreamLabel,
},
};

pub use linkerd_http_prom::stream_label::MkStreamLabel;
Expand Down Expand Up @@ -46,18 +50,21 @@ pub type GrpcRouteMetrics = RouteMetrics<LabelGrpcRouteRsp, LabelGrpcRouteBacken
#[derive(Clone, Debug)]
pub struct LabelHttpRsp<L> {
parent: L,
status: Option<http::StatusCode>,
error: Option<labels::Error>,
status: LabelHttpStatus,
error: LabelHttpError,
}

/// Tracks gRPC streams to produce response labels.
#[derive(Clone, Debug)]
pub struct LabelGrpcRsp<L> {
parent: L,
status: Option<tonic::Code>,
error: Option<labels::Error>,
status: LabelGrpcStatus,
error: LabelGrpcError,
}

type LabelHttpError = LabelError<labels::HttpRsp>;
type LabelGrpcError = LabelError<labels::GrpcRsp>;

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

Expand Down Expand Up @@ -256,8 +263,8 @@ impl<P> From<P> for LabelHttpRsp<P> {
fn from(parent: P) -> Self {
Self {
parent,
status: None,
error: None,
status: Default::default(),
error: Default::default(),
}
}
}
Expand All @@ -270,30 +277,37 @@ where
type DurationLabels = P;

fn init_response<B>(&mut self, rsp: &http::Response<B>) {
self.status = Some(rsp.status());
let Self {
parent: _,
status,
error,
} = self;
status.init_response(rsp);
error.init_response(rsp);
}

fn end_response(&mut self, res: Result<Option<&http::HeaderMap>, &linkerd_app_core::Error>) {
if let Err(e) = res {
match labels::Error::new_or_status(e) {
Ok(l) => self.error = Some(l),
Err(code) => match http::StatusCode::from_u16(code) {
Ok(s) => self.status = Some(s),
// This is kind of pathological, so mark it as an unkown error.
Err(_) => self.error = Some(labels::Error::Unknown),
},
}
}
let Self {
parent: _,
status,
error,
} = self;
status.end_response(res);
error.end_response(res);
}

fn status_labels(&self) -> Self::StatusLabels {
labels::Rsp(
self.parent.clone(),
labels::HttpRsp {
status: self.status,
error: self.error,
},
)
let Self {
parent,
status,
error,
} = self;

let error = error.status_labels();
let status = status.status_labels().map(labels::HttpRsp::status);
let rsp = labels::HttpRsp::default().apply(status).apply(error);

labels::Rsp(parent.clone(), rsp)
}

fn duration_labels(&self) -> Self::DurationLabels {
Expand All @@ -307,8 +321,8 @@ impl<P> From<P> for LabelGrpcRsp<P> {
fn from(parent: P) -> Self {
Self {
parent,
status: None,
error: None,
status: Default::default(),
error: Default::default(),
}
}
}
Expand All @@ -321,35 +335,37 @@ where
type DurationLabels = P;

fn init_response<B>(&mut self, rsp: &http::Response<B>) {
self.status = rsp
.headers()
.get("grpc-status")
.map(|v| tonic::Code::from_bytes(v.as_bytes()));
let Self {
parent: _,
status,
error,
} = self;
status.init_response(rsp);
error.init_response(rsp);
}

fn end_response(&mut self, res: Result<Option<&http::HeaderMap>, &linkerd_app_core::Error>) {
match res {
Ok(Some(trailers)) => {
self.status = trailers
.get("grpc-status")
.map(|v| tonic::Code::from_bytes(v.as_bytes()));
}
Ok(None) => {}
Err(e) => match labels::Error::new_or_status(e) {
Ok(l) => self.error = Some(l),
Err(code) => self.status = Some(tonic::Code::from_i32(i32::from(code))),
},
}
let Self {
parent: _,
status,
error,
} = self;
status.end_response(res);
error.end_response(res);
}

fn status_labels(&self) -> Self::StatusLabels {
labels::Rsp(
self.parent.clone(),
labels::GrpcRsp {
status: self.status,
error: self.error,
},
)
let Self {
parent,
status,
error,
} = self;

let error = error.status_labels();
let status = status.status_labels().map(labels::GrpcRsp::status);
let rsp = labels::GrpcRsp::default().apply(status).apply(error);

labels::Rsp(parent.clone(), rsp)
}

fn duration_labels(&self) -> Self::DurationLabels {
Expand Down
102 changes: 100 additions & 2 deletions linkerd/app/outbound/src/http/logical/policy/route/metrics/labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ pub type RouteBackendRsp<L> = Rsp<RouteBackend, L>;
pub type HttpRouteBackendRsp = RouteBackendRsp<HttpRsp>;
pub type GrpcRouteBackendRsp = RouteBackendRsp<GrpcRsp>;

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
#[derive(Clone, Debug, Default, Hash, PartialEq, Eq)]
pub struct HttpRsp {
pub status: Option<http::StatusCode>,
pub error: Option<Error>,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
#[derive(Clone, Debug, Default, Hash, PartialEq, Eq)]
pub struct GrpcRsp {
pub status: Option<tonic::Code>,
pub error: Option<Error>,
Expand Down Expand Up @@ -150,6 +150,43 @@ impl<P: EncodeLabelSetMut, L: EncodeLabelSetMut> EncodeLabelSet for Rsp<P, L> {

// === impl HttpRsp ===

impl HttpRsp {
pub fn status(status: http::StatusCode) -> Self {
Self {
status: Some(status),
error: None,
}
}

pub fn error(error: Error) -> Self {
Self {
status: None,
error: Some(error),
}
}

pub fn unknown() -> Self {
Self {
status: None,
error: Some(Error::Unknown),
}
}

/// Merges the two collections of label values.
///
/// NB: Values from the left-hand side take precedent.
pub fn apply(self, rhs: Option<Self>) -> Self {
let Some(Self { status, error }) = rhs else {
return self;
};

Self {
status: self.status.or(status),
error: self.error.or(error),
}
}
}

impl EncodeLabelSetMut for HttpRsp {
fn encode_label_set(&self, enc: &mut LabelSetEncoder<'_>) -> std::fmt::Result {
let Self { status, error } = self;
Expand All @@ -167,8 +204,57 @@ impl EncodeLabelSet for HttpRsp {
}
}

impl From<&linkerd_app_core::Error> for HttpRsp {
fn from(error: &linkerd_app_core::Error) -> Self {
match Error::new_or_status(error) {
Ok(error) => Self::error(error),
Err(code) => http::StatusCode::from_u16(code)
.map(Self::status)
// This is kind of pathological, so mark it as an unkown error.
.unwrap_or_else(|_| Self::unknown()),
}
}
}

// === impl GrpcRsp ===

impl GrpcRsp {
pub fn status(status: tonic::Code) -> Self {
Self {
status: Some(status),
error: None,
}
}

pub fn error(error: Error) -> Self {
Self {
status: None,
error: Some(error),
}
}

pub fn unknown() -> Self {
Self {
status: None,
error: Some(Error::Unknown),
}
}

/// Merges the two collections of label values.
///
/// NB: Values from the left-hand side take precedent.
pub fn apply(self, rhs: Option<Self>) -> Self {
let Some(Self { status, error }) = rhs else {
return self;
};

Self {
status: self.status.or(status),
error: self.error.or(error),
}
}
}

impl EncodeLabelSetMut for GrpcRsp {
fn encode_label_set(&self, enc: &mut LabelSetEncoder<'_>) -> std::fmt::Result {
let Self { status, error } = self;
Expand Down Expand Up @@ -209,6 +295,18 @@ impl EncodeLabelSet for GrpcRsp {
}
}

impl From<&linkerd_app_core::Error> for GrpcRsp {
fn from(error: &linkerd_app_core::Error) -> Self {
match Error::new_or_status(error)
.map_err(i32::from)
.map_err(tonic::Code::from_i32)
{
Ok(error) => Self::error(error),
Err(code) => Self::status(code),
}
}
}

// === impl Error ===

impl Error {
Expand Down
Loading