Skip to content

Commit 3335cfa

Browse files
committed
use &'static str for metric labels
1 parent c24fe9f commit 3335cfa

File tree

4 files changed

+63
-90
lines changed

4 files changed

+63
-90
lines changed

crates/rproxy/src/server/metrics.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,29 +368,29 @@ impl Metrics {
368368

369369
#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, EncodeLabelSet)]
370370
pub(crate) struct LabelsProxy {
371-
pub(crate) proxy: String,
371+
pub(crate) proxy: &'static str,
372372
}
373373

374374
// LabelsProxyClientInfo -----------------------------------------------
375375

376376
#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, EncodeLabelSet)]
377377
pub(crate) struct LabelsProxyClientInfo {
378-
pub(crate) proxy: String,
378+
pub(crate) proxy: &'static str,
379379
pub(crate) user_agent: String,
380380
}
381381

382382
// LabelsProxyHttpJrpc -------------------------------------------------
383383

384384
#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, EncodeLabelSet)]
385385
pub(crate) struct LabelsProxyHttpJrpc {
386-
pub(crate) proxy: String,
386+
pub(crate) proxy: &'static str,
387387
pub(crate) jrpc_method: Cow<'static, str>,
388388
}
389389

390390
// LabelsProxyWs -------------------------------------------------------
391391

392392
#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, EncodeLabelSet)]
393393
pub(crate) struct LabelsProxyWs {
394-
pub(crate) proxy: String,
394+
pub(crate) proxy: &'static str,
395395
pub(crate) destination: &'static str,
396396
}

crates/rproxy/src/server/proxy.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ pub(crate) trait Proxy {
2525
fn on_connect(
2626
metrics: Arc<Metrics>,
2727
client_connections_count: Arc<AtomicI64>,
28-
proxy_name: String,
28+
proxy_name: &'static str,
2929
) -> impl Fn(&dyn Any, &mut Extensions) {
3030
move |connection, extensions| {
3131
{
3232
let val = client_connections_count.fetch_add(1, Ordering::Relaxed) + 1;
33-
let metric_labels = LabelsProxy { proxy: proxy_name.clone() };
33+
let metric_labels = LabelsProxy { proxy: proxy_name };
3434

3535
metrics.client_connections_active_count.get_or_create(&metric_labels).set(val);
3636
metrics.client_connections_established_count.get_or_create(&metric_labels).inc();
@@ -73,7 +73,7 @@ pub(crate) trait Proxy {
7373

7474
extensions.insert(ProxyConnectionGuard::new(
7575
id,
76-
&proxy_name,
76+
proxy_name,
7777
remote_addr,
7878
local_addr,
7979
&metrics,
@@ -91,15 +91,15 @@ pub struct ProxyConnectionGuard {
9191
pub remote_addr: Option<String>,
9292
pub local_addr: Option<String>,
9393

94-
proxy_name: String,
94+
proxy_name: &'static str,
9595
metrics: Arc<Metrics>,
9696
client_connections_count: Arc<AtomicI64>,
9797
}
9898

9999
impl ProxyConnectionGuard {
100100
fn new(
101101
id: Uuid,
102-
proxy_name: &str,
102+
proxy_name: &'static str,
103103
remote_addr: Option<String>,
104104
local_addr: Option<String>,
105105
metrics: &Arc<Metrics>,
@@ -109,7 +109,7 @@ impl ProxyConnectionGuard {
109109
id,
110110
remote_addr,
111111
local_addr,
112-
proxy_name: proxy_name.to_string(),
112+
proxy_name,
113113
metrics: metrics.clone(),
114114
client_connections_count,
115115
}
@@ -120,7 +120,7 @@ impl Drop for ProxyConnectionGuard {
120120
fn drop(&mut self) {
121121
let val = self.client_connections_count.fetch_sub(1, Ordering::Relaxed) - 1;
122122

123-
let metric_labels = LabelsProxy { proxy: self.proxy_name.clone() };
123+
let metric_labels = LabelsProxy { proxy: self.proxy_name };
124124

125125
self.metrics.client_connections_active_count.get_or_create(&metric_labels).set(val);
126126
self.metrics.client_connections_closed_count.get_or_create(&metric_labels).inc();

crates/rproxy/src/server/proxy/http/proxy.rs

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ where
9999
inner.clone(),
100100
id,
101101
shared.metrics.clone(),
102-
&shared.proxy_name,
102+
shared.proxy_name,
103103
config.backend_url(),
104104
connections_limit,
105105
config.backend_timeout(),
@@ -114,7 +114,7 @@ where
114114
shared.inner(),
115115
id,
116116
shared.metrics.clone(),
117-
&shared.proxy_name,
117+
shared.proxy_name,
118118
peer_url.to_owned(),
119119
config.backend_max_concurrent_requests(),
120120
config.backend_timeout(),
@@ -128,7 +128,7 @@ where
128128
worker_id: id,
129129
inner: inner.clone(),
130130
metrics: shared.metrics.clone(),
131-
proxy_name: shared.proxy_name.clone(),
131+
proxy_name: shared.proxy_name,
132132
mirroring_peers: peers.clone(),
133133
mirroring_peer_round_robin_index: AtomicUsize::new(0),
134134
}
@@ -141,7 +141,7 @@ where
141141
config: C,
142142
tls: ConfigTls,
143143
metrics: Arc<Metrics>,
144-
proxy_name: &str,
144+
proxy_name: &'static str,
145145
canceller: tokio_util::sync::CancellationToken,
146146
resetter: broadcast::Sender<()>,
147147
) -> Result<(), Box<dyn std::error::Error + Send>> {
@@ -193,11 +193,7 @@ where
193193
.wrap(NormalizePath::new(TrailingSlash::Trim))
194194
.default_service(web::route().to(Self::receive))
195195
})
196-
.on_connect(Self::on_connect(
197-
metrics.clone(),
198-
client_connections_count,
199-
proxy_name.to_string(),
200-
))
196+
.on_connect(Self::on_connect(metrics.clone(), client_connections_count, proxy_name))
201197
.shutdown_signal(canceller.cancelled_owned())
202198
.workers(workers_count);
203199

@@ -312,7 +308,7 @@ where
312308
.metrics
313309
.client_info
314310
.get_or_create(&LabelsProxyClientInfo {
315-
proxy: this.shared.proxy_name.clone(),
311+
proxy: this.shared.proxy_name,
316312
user_agent: user_agent.to_string(),
317313
})
318314
.inc();
@@ -340,7 +336,7 @@ where
340336
this.shared
341337
.metrics
342338
.http_proxy_failure_count
343-
.get_or_create(&LabelsProxy { proxy: this.shared.proxy_name.clone() })
339+
.get_or_create(&LabelsProxy { proxy: this.shared.proxy_name })
344340
.inc();
345341
return Ok(HttpResponse::BadGateway().body(format!("Backend error: {:?}", err)));
346342
}
@@ -401,7 +397,7 @@ where
401397
inner: Arc<P>,
402398
worker_id: Uuid,
403399
metrics: Arc<Metrics>,
404-
proxy_name: &str,
400+
proxy_name: &'static str,
405401
mirroring_peers: Arc<Vec<actix::Addr<ProxyHttpBackendEndpoint<C, P>>>>,
406402
mut mirroring_peer_round_robin_index: usize,
407403
) {
@@ -471,7 +467,7 @@ where
471467
mut mrr_res: ProxiedHttpResponse,
472468
inner: Arc<P>,
473469
metrics: Arc<Metrics>,
474-
proxy_name: &str,
470+
proxy_name: &'static str,
475471
worker_id: Uuid,
476472
) {
477473
if cli_req.decompressed_size < cli_req.size {
@@ -489,7 +485,7 @@ where
489485
metrics
490486
.http_mirror_success_count
491487
.get_or_create(&LabelsProxyHttpJrpc {
492-
proxy: proxy_name.to_string(),
488+
proxy: proxy_name,
493489
jrpc_method: cli_req.info.jrpc_method_enriched,
494490
})
495491
.inc();
@@ -731,18 +727,16 @@ where
731727
req: &ProxiedHttpRequest,
732728
res: &ProxiedHttpResponse,
733729
metrics: Arc<Metrics>,
734-
proxy_name: &str,
730+
proxy_name: &'static str,
735731
) {
736732
let metric_labels_jrpc = match jrpc {
737-
JrpcRequestMetaMaybeBatch::Single(jrpc) => LabelsProxyHttpJrpc {
738-
jrpc_method: jrpc.method_enriched(),
739-
proxy: proxy_name.to_string(),
740-
},
733+
JrpcRequestMetaMaybeBatch::Single(jrpc) => {
734+
LabelsProxyHttpJrpc { jrpc_method: jrpc.method_enriched(), proxy: proxy_name }
735+
}
741736

742-
JrpcRequestMetaMaybeBatch::Batch(_) => LabelsProxyHttpJrpc {
743-
jrpc_method: Cow::Borrowed("batch"),
744-
proxy: proxy_name.to_string(),
745-
},
737+
JrpcRequestMetaMaybeBatch::Batch(_) => {
738+
LabelsProxyHttpJrpc { jrpc_method: Cow::Borrowed("batch"), proxy: proxy_name }
739+
}
746740
};
747741

748742
let latency_backend = 1000000.0 * (res.start() - req.end()).as_seconds_f64();
@@ -776,7 +770,7 @@ where
776770
for jrpc in batch.iter() {
777771
let metric_labels_jrpc = LabelsProxyHttpJrpc {
778772
jrpc_method: jrpc.method_enriched(),
779-
proxy: proxy_name.to_string(),
773+
proxy: proxy_name,
780774
};
781775
metrics.http_proxy_success_count.get_or_create(&metric_labels_jrpc).inc();
782776
}
@@ -833,7 +827,7 @@ where
833827
{
834828
inner: Arc<P>,
835829
metrics: Arc<Metrics>,
836-
proxy_name: String,
830+
proxy_name: &'static str,
837831

838832
client_connections_count: Arc<AtomicI64>,
839833

@@ -845,11 +839,11 @@ where
845839
C: ConfigProxyHttp,
846840
P: ProxyHttpInner<C>,
847841
{
848-
fn new(config: C, metrics: &Arc<Metrics>, proxy_name: &str) -> Self {
842+
fn new(config: C, metrics: &Arc<Metrics>, proxy_name: &'static str) -> Self {
849843
Self {
850844
inner: Arc::new(P::new(config)),
851845
metrics: metrics.clone(),
852-
proxy_name: proxy_name.to_string(),
846+
proxy_name,
853847
client_connections_count: Arc::new(AtomicI64::new(0)),
854848
_config: PhantomData,
855849
}
@@ -876,7 +870,7 @@ where
876870
inner: Arc<P>,
877871
worker_id: Uuid,
878872
metrics: Arc<Metrics>,
879-
proxy_name: String,
873+
proxy_name: &'static str,
880874

881875
/// mirroring_peers is the vector of endpoints for mirroring peers.
882876
mirroring_peers: Arc<Vec<actix::Addr<ProxyHttpBackendEndpoint<C, P>>>>,
@@ -909,7 +903,7 @@ where
909903
fn handle(&mut self, msg: ProxiedHttpCombo, ctx: &mut Self::Context) -> Self::Result {
910904
let inner = self.inner.clone();
911905
let metrics = self.metrics.clone();
912-
let proxy_name = self.proxy_name.clone();
906+
let proxy_name = self.proxy_name;
913907
let worker_id = self.worker_id;
914908
let mirroring_peers = self.mirroring_peers.clone();
915909
let mut mirroring_peer_round_robin_index =
@@ -923,7 +917,7 @@ where
923917
inner,
924918
worker_id,
925919
metrics,
926-
&proxy_name,
920+
proxy_name,
927921
mirroring_peers,
928922
mirroring_peer_round_robin_index,
929923
);
@@ -950,7 +944,7 @@ where
950944
inner: Arc<P>,
951945
worker_id: Uuid,
952946
metrics: Arc<Metrics>,
953-
proxy_name: String,
947+
proxy_name: &'static str,
954948

955949
client: Client,
956950
url: Url,
@@ -967,7 +961,7 @@ where
967961
inner: Arc<P>,
968962
worker_id: Uuid,
969963
metrics: Arc<Metrics>,
970-
proxy_name: &str,
964+
proxy_name: &'static str,
971965
url: Url,
972966
connections_limit: usize,
973967
timeout: std::time::Duration,
@@ -983,15 +977,7 @@ where
983977
.timeout(timeout)
984978
.finish();
985979

986-
Self {
987-
inner,
988-
worker_id,
989-
metrics,
990-
proxy_name: proxy_name.to_string(),
991-
client,
992-
url,
993-
_config: PhantomData,
994-
}
980+
Self { inner, worker_id, metrics, proxy_name, client, url, _config: PhantomData }
995981
}
996982

997983
fn new_backend_request(&self, info: &ProxyHttpRequestInfo) -> ClientRequest {
@@ -1033,7 +1019,7 @@ where
10331019
let inner = self.inner.clone();
10341020
let worker_id = self.worker_id;
10351021
let metrics = self.metrics.clone();
1036-
let proxy_name = self.proxy_name.clone();
1022+
let proxy_name = self.proxy_name;
10371023

10381024
let mrr_req = self.new_backend_request(&cli_req.info);
10391025
let mrr_req_body = cli_req.body.clone();
@@ -1066,12 +1052,7 @@ where
10661052
end,
10671053
};
10681054
ProxyHttp::<C, P>::postprocess_mirrored_response(
1069-
cli_req,
1070-
mrr_res,
1071-
inner,
1072-
metrics,
1073-
&proxy_name,
1074-
worker_id,
1055+
cli_req, mrr_res, inner, metrics, proxy_name, worker_id,
10751056
);
10761057
}
10771058
Err(err) => {
@@ -1083,7 +1064,7 @@ where
10831064
);
10841065
metrics
10851066
.http_mirror_failure_count
1086-
.get_or_create(&LabelsProxy { proxy: proxy_name.clone() })
1067+
.get_or_create(&LabelsProxy { proxy: proxy_name })
10871068
.inc();
10881069
}
10891070
};
@@ -1098,7 +1079,7 @@ where
10981079
);
10991080
metrics
11001081
.http_mirror_failure_count
1101-
.get_or_create(&LabelsProxy { proxy: proxy_name.clone() })
1082+
.get_or_create(&LabelsProxy { proxy: proxy_name })
11021083
.inc();
11031084
}
11041085
}

0 commit comments

Comments
 (0)