Skip to content

Commit b0d7ae2

Browse files
committed
Formatting
Signed-off-by: Federico Torres <[email protected]>
1 parent 8749a9c commit b0d7ae2

File tree

3 files changed

+150
-46
lines changed

3 files changed

+150
-46
lines changed

examples/axum-utf-8.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ use axum::http::{HeaderMap, StatusCode};
55
use axum::response::{IntoResponse, Response};
66
use axum::routing::get;
77
use axum::Router;
8+
use prometheus_client::encoding::negotiate_escaping_scheme;
89
use prometheus_client::encoding::text::encode;
10+
use prometheus_client::encoding::EscapingScheme::UnderscoreEscaping;
11+
use prometheus_client::encoding::ValidationScheme::UTF8Validation;
912
use prometheus_client::metrics::counter::Counter;
1013
use prometheus_client::metrics::family::Family;
1114
use prometheus_client::registry::{Registry, RegistryBuilder};
1215
use std::sync::Arc;
1316
use tokio::sync::Mutex;
14-
use prometheus_client::encoding::EscapingScheme::UnderscoreEscaping;
15-
use prometheus_client::encoding::negotiate_escaping_scheme;
16-
use prometheus_client::encoding::ValidationScheme::UTF8Validation;
1717

1818
#[derive(Debug)]
1919
pub struct Metrics {
@@ -22,7 +22,9 @@ pub struct Metrics {
2222

2323
impl Metrics {
2424
pub fn inc_requests(&self, method: String) {
25-
self.requests.get_or_create(&vec![("method.label".to_owned(), method)]).inc();
25+
self.requests
26+
.get_or_create(&vec![("method.label".to_owned(), method)])
27+
.inc();
2628
}
2729
}
2830

@@ -31,14 +33,15 @@ pub struct AppState {
3133
pub registry: Registry,
3234
}
3335

34-
pub async fn metrics_handler(State(state): State<Arc<Mutex<AppState>>>, headers: HeaderMap) -> impl IntoResponse {
36+
pub async fn metrics_handler(
37+
State(state): State<Arc<Mutex<AppState>>>,
38+
headers: HeaderMap,
39+
) -> impl IntoResponse {
3540
let mut state = state.lock().await;
3641
let mut buffer = String::new();
3742
if let Some(accept) = headers.get("Accept") {
38-
let escaping_scheme = negotiate_escaping_scheme(
39-
accept.to_str().unwrap(),
40-
state.registry.escaping_scheme()
41-
);
43+
let escaping_scheme =
44+
negotiate_escaping_scheme(accept.to_str().unwrap(), state.registry.escaping_scheme());
4245
state.registry.set_escaping_scheme(escaping_scheme);
4346
}
4447
encode(&mut buffer, &state.registry).unwrap();
@@ -47,7 +50,8 @@ pub async fn metrics_handler(State(state): State<Arc<Mutex<AppState>>>, headers:
4750
.status(StatusCode::OK)
4851
.header(
4952
CONTENT_TYPE,
50-
"application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=".to_owned() + state.registry.escaping_scheme().as_str(),
53+
"application/openmetrics-text; version=1.0.0; charset=utf-8; escaping=".to_owned()
54+
+ state.registry.escaping_scheme().as_str(),
5155
)
5256
.body(Body::from(buffer))
5357
.unwrap()
@@ -69,9 +73,11 @@ async fn main() {
6973
.with_escaping_scheme(UnderscoreEscaping)
7074
.build(),
7175
};
72-
state
73-
.registry
74-
.register("requests.count", "Count of requests", metrics.requests.clone());
76+
state.registry.register(
77+
"requests.count",
78+
"Count of requests",
79+
metrics.requests.clone(),
80+
);
7581
let metrics = Arc::new(Mutex::new(metrics));
7682
let state = Arc::new(Mutex::new(state));
7783

src/encoding.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -797,16 +797,25 @@ fn is_valid_legacy_prefix(prefix: Option<&Prefix>) -> bool {
797797
}
798798
}
799799

800-
fn is_quoted_metric_name(name: &str, prefix: Option<&Prefix>, validation_scheme: &ValidationScheme) -> bool {
801-
*validation_scheme == ValidationScheme::UTF8Validation && (!is_valid_legacy_metric_name(name) || !is_valid_legacy_prefix(prefix))
800+
fn is_quoted_metric_name(
801+
name: &str,
802+
prefix: Option<&Prefix>,
803+
validation_scheme: &ValidationScheme,
804+
) -> bool {
805+
*validation_scheme == ValidationScheme::UTF8Validation
806+
&& (!is_valid_legacy_metric_name(name) || !is_valid_legacy_prefix(prefix))
802807
}
803808

804809
fn is_valid_legacy_label_name(label_name: &str) -> bool {
805810
if label_name.is_empty() {
806811
return false;
807812
}
808813
for (i, b) in label_name.chars().enumerate() {
809-
if !((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || (b >= '0' && b <= '9' && i > 0)) {
814+
if !((b >= 'a' && b <= 'z')
815+
|| (b >= 'A' && b <= 'Z')
816+
|| b == '_'
817+
|| (b >= '0' && b <= '9' && i > 0))
818+
{
810819
return false;
811820
}
812821
}
@@ -901,7 +910,10 @@ fn escape_name(name: &str, scheme: &EscapingScheme) -> String {
901910
}
902911

903912
/// Returns the escaping scheme to use based on the given header.
904-
pub fn negotiate_escaping_scheme(header: &str, default_escaping_scheme: EscapingScheme) -> EscapingScheme {
913+
pub fn negotiate_escaping_scheme(
914+
header: &str,
915+
default_escaping_scheme: EscapingScheme,
916+
) -> EscapingScheme {
905917
if header.contains("underscores") {
906918
return EscapingScheme::UnderscoreEscaping;
907919
}

0 commit comments

Comments
 (0)