Skip to content

Commit 8b7611e

Browse files
authored
ref(iroh-metrics, iroh-relay): Remove the UsageStatsReporter (#2952)
## Description This was unused and a rather scary footgun that could easily lead to way to many http requests being made. ## Breaking Changes <!-- Optional, if there are any breaking changes document them, including how to migrate older code. --> ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [x] Tests if relevant. - [x] All breaking changes documented.
1 parent 73e7d44 commit 8b7611e

File tree

3 files changed

+7
-136
lines changed

3 files changed

+7
-136
lines changed

iroh-metrics/src/core.rs

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
1-
use anyhow::Error;
21
use erased_set::ErasedSyncSet;
32
use once_cell::sync::OnceCell;
43
#[cfg(feature = "metrics")]
54
use prometheus_client::{encoding::text::encode, registry::Registry};
6-
use serde::{Deserialize, Serialize};
7-
use time::OffsetDateTime;
8-
use tracing::trace;
95
#[cfg(not(feature = "metrics"))]
106
type Registry = ();
117

128
static CORE: OnceCell<Core> = OnceCell::new();
139

1410
/// Core is the base metrics struct.
11+
///
1512
/// It manages the mapping between the metrics name and the actual metrics.
1613
/// It also carries a single prometheus registry to be used by all metrics.
1714
#[derive(Debug, Default)]
1815
pub struct Core {
1916
#[cfg(feature = "metrics")]
2017
registry: Registry,
2118
metrics_map: ErasedSyncSet,
22-
#[cfg(feature = "metrics")]
23-
usage_reporter: UsageReporter,
2419
}
2520
/// Open Metrics [`Counter`] to measure discrete events.
2621
///
@@ -154,15 +149,10 @@ impl Core {
154149
let mut metrics_map = ErasedSyncSet::new();
155150
f(&mut registry, &mut metrics_map);
156151

157-
#[cfg(feature = "metrics")]
158-
let usage_reporter = UsageReporter::new();
159-
160152
CORE.set(Core {
161153
metrics_map,
162154
#[cfg(feature = "metrics")]
163155
registry,
164-
#[cfg(feature = "metrics")]
165-
usage_reporter,
166156
})
167157
.map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "already set"))
168158
}
@@ -191,11 +181,6 @@ impl Core {
191181
encode(&mut buf, &self.registry)?;
192182
Ok(buf)
193183
}
194-
195-
#[cfg(feature = "metrics")]
196-
pub(crate) fn usage_reporter(&self) -> &UsageReporter {
197-
&self.usage_reporter
198-
}
199184
}
200185

201186
/// Interface for all single value based metrics.
@@ -209,83 +194,3 @@ pub trait HistogramType {
209194
/// Returns the name of the metric
210195
fn name(&self) -> &'static str;
211196
}
212-
213-
/// Exposes a simple API to report usage statistics.
214-
#[derive(Debug, Default)]
215-
pub struct UsageReporter {
216-
pub(crate) report_endpoint: Option<String>,
217-
pub(crate) report_token: Option<String>,
218-
}
219-
220-
impl UsageReporter {
221-
/// Creates a new usage reporter.
222-
pub fn new() -> Self {
223-
let report_endpoint = std::env::var("IROH_METRICS_USAGE_STATS_ENDPOINT").ok();
224-
let report_token = std::env::var("IROH_METRICS_USAGE_STATS_TOKEN").ok();
225-
UsageReporter {
226-
report_endpoint,
227-
report_token,
228-
}
229-
}
230-
231-
/// Reports usage statistics to the configured endpoint.
232-
pub async fn report_usage_stats(&self, report: &UsageStatsReport) -> Result<(), Error> {
233-
if let Some(report_endpoint) = &self.report_endpoint {
234-
trace!("reporting usage stats to {}", report_endpoint);
235-
let mut client = reqwest::Client::new().post(report_endpoint);
236-
237-
if let Some(report_token) = &self.report_token {
238-
let mut headers = reqwest::header::HeaderMap::new();
239-
headers.insert(
240-
reqwest::header::COOKIE,
241-
format!("token={}", report_token).parse().unwrap(),
242-
);
243-
client = client.headers(headers);
244-
}
245-
let _ = client.json(report).send().await?;
246-
}
247-
Ok(())
248-
}
249-
}
250-
251-
/// Usage statistics report.
252-
#[derive(Clone, Debug, Serialize, Deserialize)]
253-
pub struct UsageStatsReport {
254-
/// The timestamp of the report.
255-
#[serde(with = "time::serde::rfc3339")]
256-
pub timestamp: OffsetDateTime,
257-
/// The resource being consumed.
258-
pub resource: String,
259-
/// Reference to the resource reporter.
260-
pub resource_ref: String,
261-
/// The value of the resource being consumed.
262-
pub value: i64,
263-
/// Identifier of the user consuming the resource.
264-
pub attribution_id: Option<String>,
265-
/// Public key of the user consuming the resource.
266-
pub attribution_key: Option<String>,
267-
}
268-
269-
/// Type alias for a metered resource.
270-
pub type UsageResource = String;
271-
272-
impl UsageStatsReport {
273-
/// Creates a new usage stats report.
274-
pub fn new(
275-
resource: UsageResource,
276-
resource_ref: String,
277-
value: i64,
278-
attribution_id: Option<String>,
279-
attribution_key: Option<String>,
280-
) -> Self {
281-
let timestamp = OffsetDateTime::now_utc();
282-
UsageStatsReport {
283-
timestamp,
284-
resource,
285-
resource_ref,
286-
value,
287-
attribution_id,
288-
attribution_key,
289-
}
290-
}
291-
}

iroh-metrics/src/lib.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ pub mod core;
1010
#[cfg(feature = "metrics")]
1111
mod service;
1212

13-
use core::UsageStatsReport;
1413
use std::collections::HashMap;
1514

1615
/// Reexport to make matching versions easier.
@@ -40,22 +39,6 @@ macro_rules! set {
4039
};
4140
}
4241

43-
/// Report usage statistics to the configured endpoint.
44-
#[allow(unused_variables)]
45-
pub async fn report_usage_stats(report: &UsageStatsReport) {
46-
#[cfg(feature = "metrics")]
47-
{
48-
if let Some(core) = core::Core::get() {
49-
core.usage_reporter()
50-
.report_usage_stats(report)
51-
.await
52-
.unwrap_or_else(|e| {
53-
tracing::error!("Failed to report usage stats: {}", e);
54-
});
55-
}
56-
}
57-
}
58-
5942
/// Parse Prometheus metrics from a string.
6043
pub fn parse_prometheus_metrics(data: &str) -> anyhow::Result<HashMap<String, f64>> {
6144
let mut metrics = HashMap::new();

iroh-relay/src/server/actor.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use std::{collections::HashMap, time::Duration};
77
use anyhow::{bail, Result};
88
use bytes::Bytes;
99
use iroh_base::key::NodeId;
10-
use iroh_metrics::{core::UsageStatsReport, inc, inc_by, report_usage_stats};
10+
use iroh_metrics::{inc, inc_by};
1111
use time::{Date, OffsetDateTime};
12-
use tokio::{sync::mpsc, task::JoinSet};
12+
use tokio::sync::mpsc;
1313
use tokio_util::{sync::CancellationToken, task::AbortOnDropHandle};
1414
use tracing::{info, info_span, trace, warn, Instrument};
1515

@@ -114,7 +114,6 @@ impl Actor {
114114
}
115115

116116
async fn run(mut self, done: CancellationToken) -> Result<()> {
117-
let mut tasks = JoinSet::new();
118117
loop {
119118
tokio::select! {
120119
biased;
@@ -126,16 +125,9 @@ impl Actor {
126125
self.clients.shutdown().await;
127126
return Ok(());
128127
}
129-
Some(res) = tasks.join_next(), if !tasks.is_empty() => {
130-
if let Err(err) = res {
131-
if err.is_panic() {
132-
panic!("Task panicked: {err:?}");
133-
}
134-
}
135-
}
136128
msg = self.receiver.recv() => match msg {
137129
Some(msg) => {
138-
self.handle_message(msg, &mut tasks).await;
130+
self.handle_message(msg).await;
139131
}
140132
None => {
141133
warn!("unexpected actor error: receiver gone, shutting down actor loop");
@@ -147,7 +139,7 @@ impl Actor {
147139
}
148140
}
149141

150-
async fn handle_message(&mut self, msg: Message, tasks: &mut JoinSet<()>) {
142+
async fn handle_message(&mut self, msg: Message) {
151143
match msg {
152144
Message::SendPacket { dst, data, src } => {
153145
trace!(?src, ?dst, len = data.len(), "send packet");
@@ -198,18 +190,9 @@ impl Actor {
198190
);
199191
let node_id = client_builder.node_id;
200192

201-
// build and register client, starting up read & write loops for the client connection
193+
// build and register client, starting up read & write loops for the client
194+
// connection
202195
self.clients.register(client_builder).await;
203-
tasks.spawn(async move {
204-
report_usage_stats(&UsageStatsReport::new(
205-
"relay_accepts".to_string(),
206-
"relay_server".to_string(), // TODO: other id?
207-
1,
208-
None, // TODO(arqu): attribute to user id; possibly with the re-introduction of request tokens or other auth
209-
Some(node_id.to_string()),
210-
))
211-
.await;
212-
});
213196
let nc = self.client_counter.update(node_id);
214197
inc_by!(Metrics, unique_client_keys, nc);
215198
}

0 commit comments

Comments
 (0)