|
| 1 | +//! [`MkStreamLabel`] and [`StreamLabel`] implementations for status codes. |
| 2 | +//! |
| 3 | +//! This submodule provides [`MkLabelGrpcStatus`] and [`MkLabelHttpStatus`] |
| 4 | +
|
| 5 | +use crate::stream_label::{MkStreamLabel, StreamLabel}; |
| 6 | +use http::{HeaderMap, HeaderValue, Response, StatusCode}; |
| 7 | +use linkerd_error::Error; |
| 8 | +use tonic::Code; |
| 9 | + |
| 10 | +/// A [`MkStreamLabel`] implementation for gRPC traffic. |
| 11 | +/// |
| 12 | +/// This generates [`LabelGrpcStatus`] labelers. |
| 13 | +#[derive(Debug)] |
| 14 | +pub struct MkLabelGrpcStatus; |
| 15 | + |
| 16 | +/// A [`StreamLabel`] implementation for gRPC traffic. |
| 17 | +/// |
| 18 | +/// This will inspect response headers and trailers for a [`Code`]. |
| 19 | +#[derive(Clone, Debug, Default)] |
| 20 | +pub struct LabelGrpcStatus { |
| 21 | + code: Option<Code>, |
| 22 | +} |
| 23 | + |
| 24 | +/// A [`MkStreamLabel`] implementation for HTTP traffic. |
| 25 | +/// |
| 26 | +/// This generates [`LabelHttpStatus`] labelers. |
| 27 | +#[derive(Debug)] |
| 28 | +pub struct MkLabelHttpStatus; |
| 29 | + |
| 30 | +/// A [`StreamLabel`] implementation for HTTP traffic. |
| 31 | +/// |
| 32 | +/// This will inspect the response headers for a [`StatusCode`]. |
| 33 | +#[derive(Clone, Debug, Default)] |
| 34 | +pub struct LabelHttpStatus { |
| 35 | + status: Option<StatusCode>, |
| 36 | +} |
| 37 | + |
| 38 | +// === impl MkLabelGrpcStatus === |
| 39 | + |
| 40 | +impl MkStreamLabel for MkLabelGrpcStatus { |
| 41 | + type DurationLabels = <Self::StreamLabel as StreamLabel>::DurationLabels; |
| 42 | + type StatusLabels = <Self::StreamLabel as StreamLabel>::StatusLabels; |
| 43 | + type StreamLabel = LabelGrpcStatus; |
| 44 | + |
| 45 | + fn mk_stream_labeler<B>(&self, _: &http::Request<B>) -> Option<Self::StreamLabel> { |
| 46 | + Some(LabelGrpcStatus::default()) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// === LabelGrpcStatus === |
| 51 | + |
| 52 | +impl StreamLabel for LabelGrpcStatus { |
| 53 | + type DurationLabels = (); |
| 54 | + type StatusLabels = Option<Code>; |
| 55 | + |
| 56 | + fn init_response<B>(&mut self, rsp: &Response<B>) { |
| 57 | + let headers = rsp.headers(); |
| 58 | + self.code = Self::get_grpc_status(headers); |
| 59 | + } |
| 60 | + |
| 61 | + fn end_response(&mut self, trailers: Result<Option<&HeaderMap>, &Error>) { |
| 62 | + let Ok(Some(trailers)) = trailers else { return }; |
| 63 | + self.code = Self::get_grpc_status(trailers); |
| 64 | + } |
| 65 | + |
| 66 | + fn status_labels(&self) -> Self::StatusLabels { |
| 67 | + self.code |
| 68 | + } |
| 69 | + |
| 70 | + fn duration_labels(&self) -> Self::DurationLabels {} |
| 71 | +} |
| 72 | + |
| 73 | +impl LabelGrpcStatus { |
| 74 | + fn get_grpc_status(headers: &HeaderMap) -> Option<Code> { |
| 75 | + headers |
| 76 | + .get("grpc-status") |
| 77 | + .map(HeaderValue::as_bytes) |
| 78 | + .map(tonic::Code::from_bytes) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// === impl MkLabelHttpStatus === |
| 83 | + |
| 84 | +impl MkStreamLabel for MkLabelHttpStatus { |
| 85 | + type DurationLabels = <Self::StreamLabel as StreamLabel>::DurationLabels; |
| 86 | + type StatusLabels = <Self::StreamLabel as StreamLabel>::StatusLabels; |
| 87 | + type StreamLabel = LabelHttpStatus; |
| 88 | + |
| 89 | + fn mk_stream_labeler<B>(&self, _: &http::Request<B>) -> Option<Self::StreamLabel> { |
| 90 | + Some(LabelHttpStatus::default()) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// === impl LabelHttpStatus === |
| 95 | + |
| 96 | +impl StreamLabel for LabelHttpStatus { |
| 97 | + type DurationLabels = (); |
| 98 | + type StatusLabels = Option<StatusCode>; |
| 99 | + |
| 100 | + fn init_response<B>(&mut self, rsp: &http::Response<B>) { |
| 101 | + self.status = Some(rsp.status()); |
| 102 | + } |
| 103 | + |
| 104 | + fn end_response(&mut self, _: Result<Option<&http::HeaderMap>, &linkerd_error::Error>) {} |
| 105 | + |
| 106 | + fn status_labels(&self) -> Self::StatusLabels { |
| 107 | + self.status |
| 108 | + } |
| 109 | + |
| 110 | + fn duration_labels(&self) -> Self::DurationLabels {} |
| 111 | +} |
0 commit comments