improvement: remove origin label from connections metric

The origin header is controlled by clients, and there is no expiration
of any values that appear.  We would need to whitelist a set of known
origins in order to track this without giving someone the ability to
exhaust memory.
This commit is contained in:
Greg Heartsfield 2023-01-28 15:46:26 -06:00
parent bdd4e43df4
commit 0859e535ed

View File

@ -13,7 +13,7 @@ use crate::info::RelayInfo;
use crate::nip05; use crate::nip05;
use crate::notice::Notice; use crate::notice::Notice;
use crate::subscription::Subscription; use crate::subscription::Subscription;
use prometheus::{CounterVec, Encoder, Histogram, HistogramOpts, Opts, Registry, TextEncoder}; use prometheus::{Encoder, Histogram, IntCounter, HistogramOpts, Opts, Registry, TextEncoder};
use futures::SinkExt; use futures::SinkExt;
use futures::StreamExt; use futures::StreamExt;
use governor::{Jitter, Quota, RateLimiter}; use governor::{Jitter, Quota, RateLimiter};
@ -316,18 +316,15 @@ pub fn start_server(settings: &Settings, shutdown_rx: MpscReceiver<()>) -> Resul
let query_sub = Histogram::with_opts(HistogramOpts::new( let query_sub = Histogram::with_opts(HistogramOpts::new(
"query_sub", "query_sub",
"Subscription response times", "Subscription response times",
)) )).unwrap();
.unwrap();
let write_events = Histogram::with_opts(HistogramOpts::new( let write_events = Histogram::with_opts(HistogramOpts::new(
"write_event", "write_event",
"Event writing response times", "Event writing response times",
)) )).unwrap();
.unwrap(); let connections = IntCounter::with_opts(Opts::new(
let connections = CounterVec::new( "connections",
Opts::new("connections", "New connections"), "New connections"
vec!["origin"].as_slice(), )).unwrap();
)
.unwrap();
registry.register(Box::new(query_sub.clone())).unwrap(); registry.register(Box::new(query_sub.clone())).unwrap();
registry.register(Box::new(write_events.clone())).unwrap(); registry.register(Box::new(write_events.clone())).unwrap();
registry.register(Box::new(connections.clone())).unwrap(); registry.register(Box::new(connections.clone())).unwrap();
@ -559,15 +556,8 @@ async fn nostr_server(
cid, origin, user_agent cid, origin, user_agent
); );
// Measure connections per origin // Measure connections
let mut metric_map: HashMap<&str, &str> = HashMap::new(); metrics.connections.inc();
metric_map.insert("origin", origin.as_str());
metrics
.connections
.get_metric_with(&metric_map)
.unwrap()
.inc();
loop { loop {
tokio::select! { tokio::select! {
@ -787,5 +777,5 @@ async fn nostr_server(
pub struct NostrMetrics { pub struct NostrMetrics {
pub query_sub: Histogram, pub query_sub: Histogram,
pub write_events: Histogram, pub write_events: Histogram,
pub connections: CounterVec, pub connections: IntCounter,
} }