Skip to content

Commit

Permalink
use Cow<'static, str>
Browse files Browse the repository at this point in the history
  • Loading branch information
flaneur2020 committed Jul 18, 2024
1 parent 59c8d02 commit 6dee3e6
Showing 1 changed file with 22 additions and 46 deletions.
68 changes: 22 additions & 46 deletions core/src/layers/prometheus_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use std::borrow::Cow;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::sync::Arc;
Expand All @@ -23,8 +24,6 @@ use std::time::Instant;

use bytes::Buf;
use futures::TryFutureExt;
use prometheus_client::encoding::EncodeLabel;
use prometheus_client::encoding::EncodeLabelSet;
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::metrics::histogram;
Expand Down Expand Up @@ -115,31 +114,8 @@ impl<A: Access> Layer<A> for PrometheusClientLayer {
}
}

type OperationLabels = [(&'static str, String); 4];
// type ErrorLabels = [(&'static str, String); 5];

#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct ErrorLabels {
scheme: &'static str,
op: &'static str,
namespace: String,
root: String,
err: &'static str,
}

impl<'a> EncodeLabelSet for ErrorLabels {
fn encode(
&self,
mut encoder: prometheus_client::encoding::LabelSetEncoder,
) -> std::result::Result<(), std::fmt::Error> {
("scheme", self.scheme).encode(encoder.encode_label())?;
("op", self.op).encode(encoder.encode_label())?;
("namespace", self.namespace.as_str()).encode(encoder.encode_label())?;
("root", self.root.as_str()).encode(encoder.encode_label())?;
("err", self.err).encode(encoder.encode_label())?;
Ok(())
}
}
type OperationLabels = [(&'static str, Cow<'static, str>); 4];
type ErrorLabels = [(&'static str, Cow<'static, str>); 5];

/// [`PrometheusClientMetricDefinitions`] provide the definition about RED(Rate/Error/Duration) metrics with the `prometheus-client` crate.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -213,32 +189,32 @@ impl PrometheusClientMetrics {
}

fn increment_errors_total(&self, op: &'static str, err: ErrorKind) {
let labels = ErrorLabels {
op: op,
err: err.into_static(),
scheme: self.scheme.into_static(),
root: self.root.clone(),
namespace: self.name.clone(),
};
let labels = [
("scheme", self.scheme.into_static().into()),
("op", op.into()),
("error", err.into_static().into()),
("namespace", self.name.clone().into()),
("root", self.root.clone().into()),
];
self.metrics.errors_total.get_or_create(&labels).inc();
}

fn increment_request_total(&self, scheme: Scheme, op: &'static str) {
let labels = [
("scheme", scheme.to_string()),
("op", op.to_string()),
("namespace", self.name.to_string()),
("root", self.root.to_string()),
("scheme", scheme.into_static().into()),
("op", op.into()),
("namespace", self.name.clone().into()),
("root", self.root.clone().into()),
];
self.metrics.requests_total.get_or_create(&labels).inc();
}

fn observe_bytes_total(&self, scheme: Scheme, op: &'static str, bytes: usize) {
let labels = [
("scheme", scheme.to_string()),
("op", op.to_string()),
("namespace", self.name.to_string()),
("root", self.root.to_string()),
("scheme", scheme.into_static().into()),
("op", op.into()),
("namespace", self.name.clone().into()),
("root", self.root.clone().into()),
];
self.metrics
.bytes_histogram
Expand All @@ -252,10 +228,10 @@ impl PrometheusClientMetrics {

fn observe_request_duration(&self, scheme: Scheme, op: &'static str, duration: Duration) {
let labels = [
("scheme", scheme.to_string()),
("op", op.to_string()),
("namespace", self.name.to_string()),
("root", self.root.to_string()),
("scheme", scheme.into_static().into()),
("op", op.into()),
("namespace", self.name.clone().into()),
("root", self.root.clone().into()),
];
self.metrics
.request_duration_seconds
Expand Down

0 comments on commit 6dee3e6

Please sign in to comment.