Skip to content

Commit af0464e

Browse files
authored
feat: metrics NG (#22)
1 parent 3d85923 commit af0464e

File tree

24 files changed

+2667
-893
lines changed

24 files changed

+2667
-893
lines changed

Cargo.lock

Lines changed: 657 additions & 146 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ collections = ["dep:collections"]
3030
future = ["dep:future"]
3131
geoblock = ["geoip/middleware"]
3232
geoip = ["dep:geoip"]
33-
metrics = ["dep:metrics", "future/metrics", "alloc/metrics"]
34-
future_metrics = ["dep:future_metrics"]
33+
metrics = ["dep:metrics", "alloc/metrics"]
3534
alloc_metrics = ["alloc/metrics"]
3635
profiler = ["alloc/profiler"]
3736
rate_limit = ["dep:rate_limit"]
@@ -45,8 +44,7 @@ analytics = { path = "./crates/analytics", optional = true }
4544
collections = { path = "./crates/collections", optional = true }
4645
future = { path = "./crates/future", optional = true }
4746
geoip = { path = "./crates/geoip", optional = true }
48-
metrics = { path = "./crates/metrics", optional = true }
49-
future_metrics = { path = "./crates/future_metrics", optional = true }
47+
metrics = { package = "wc_metrics", path = "./crates/metrics", optional = true }
5048
rate_limit = { path = "./crates/rate_limit", optional = true }
5149

5250
[dev-dependencies]
@@ -56,6 +54,7 @@ tokio = { version = "1", features = ["full"] }
5654
hyper = { version = "1.2.0", features = ["full"] }
5755
tower = { version = "0.4", features = ["util", "filter"] }
5856
axum = "0.7.5"
57+
metrics-exporter-prometheus = { version = "0.15", default-features = false }
5958

6059
[[example]]
6160
name = "alloc_profiler"
@@ -65,10 +64,6 @@ required-features = ["alloc", "profiler"]
6564
name = "alloc_stats"
6665
required-features = ["alloc", "metrics"]
6766

68-
[[example]]
69-
name = "metrics"
70-
required-features = ["metrics", "future"]
71-
7267
[[example]]
7368
name = "geoblock"
7469
required-features = ["geoblock"]

crates/alloc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ profiler = ["dep:dhat", "dep:tokio"]
1010
metrics = ["dep:metrics"]
1111

1212
[dependencies]
13-
metrics = { version = "0.23", optional = true }
13+
metrics = { package = "wc_metrics", path = "../metrics", optional = true }
1414
tikv-jemallocator = { version = "0.5", features = ["stats"] }
1515
tikv-jemalloc-ctl = { version = "0.5", features = ["use_std"] }
1616
serde = { version = "1", features = ["derive"] }

crates/alloc/src/stats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn collect_jemalloc_stats() -> Result<JemallocStats, Error> {
8787

8888
#[cfg(feature = "metrics")]
8989
pub fn update_jemalloc_metrics() -> Result<(), Error> {
90-
use metrics::gauge;
90+
use metrics::backend::gauge;
9191

9292
let stats = collect_jemalloc_stats()?;
9393
let total = &stats.total;

crates/future/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ edition = "2021"
55

66
[features]
77
default = []
8-
full = ["metrics"]
9-
metrics = ["dep:metrics"]
108

119
[dependencies]
12-
metrics = { path = "../metrics", optional = true }
1310
tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "time", "macros"] }
1411
tokio-util = { version = "0.7", default-features = false }
1512
pin-project = "1"

crates/future/src/lib.rs

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -202,34 +202,6 @@ pub trait FutureExt {
202202
self,
203203
token: CancellationToken,
204204
) -> CancellationFuture<Self::Future, Ready<()>>;
205-
206-
/// Consumes the future, returning a new future that records the metrics of
207-
/// the inner future's async task execution.
208-
///
209-
/// # Example
210-
///
211-
/// ```rust
212-
/// use {future::FutureExt, metrics::OtelTaskMetricsRecorder, std::time::Duration};
213-
///
214-
/// # async fn example() {
215-
/// let recorder = OtelTaskMetricsRecorder::new("custom_task").with_name("specific_task_name");
216-
///
217-
/// async {
218-
/// tokio::time::sleep(Duration::from_millis(500)).await;
219-
/// }
220-
/// .with_metrics(recorder)
221-
/// .await
222-
/// # }
223-
///
224-
/// # #[tokio::main]
225-
/// # async fn main() {
226-
/// # example().await;
227-
/// # }
228-
/// ```
229-
#[cfg(feature = "metrics")]
230-
fn with_metrics<R>(self, recorder: R) -> metrics::TaskMetricsFuture<Self::Future, R>
231-
where
232-
R: metrics::TaskMetricsRecorder;
233205
}
234206

235207
pub trait StaticFutureExt {
@@ -248,7 +220,7 @@ pub trait StaticFutureExt {
248220
/// tokio::time::sleep(Duration::from_millis(500)).await;
249221
/// 42
250222
/// }
251-
/// .spawn("");
223+
/// .spawn();
252224
///
253225
/// assert!(matches!(join_handle.await, Ok(42)));
254226
/// # }
@@ -258,15 +230,7 @@ pub trait StaticFutureExt {
258230
/// # example().await;
259231
/// # }
260232
/// ```
261-
#[cfg(feature = "metrics")]
262-
fn spawn(self, name: &'static str) -> JoinHandle<<Self::Future as Future>::Output>;
263-
264-
/// Same as [`StaticFutureExt::spawn`], but it won't monitor long running
265-
/// futures.
266-
///
267-
/// Use this only if your future is expected to be long running (ex.
268-
/// singleton).
269-
fn spawn_and_forget(self) -> JoinHandle<<Self::Future as Future>::Output>;
233+
fn spawn(self) -> JoinHandle<<Self::Future as Future>::Output>;
270234
}
271235

272236
impl<T> FutureExt for T
@@ -292,14 +256,6 @@ where
292256
on_cancel: ready(()),
293257
}
294258
}
295-
296-
#[cfg(feature = "metrics")]
297-
fn with_metrics<R>(self, recorder: R) -> metrics::TaskMetricsFuture<Self::Future, R>
298-
where
299-
R: metrics::TaskMetricsRecorder,
300-
{
301-
metrics::TaskMetricsFuture::new(self, recorder)
302-
}
303259
}
304260

305261
impl<T> StaticFutureExt for T
@@ -309,19 +265,12 @@ where
309265
{
310266
type Future = T;
311267

312-
#[cfg(feature = "metrics")]
313-
fn spawn(self, name: &'static str) -> JoinHandle<<Self::Future as Future>::Output> {
314-
static METRICS: metrics::TaskMetrics = metrics::TaskMetrics::new("spawned_task");
315-
316-
tokio::spawn(self.with_metrics(METRICS.with_name(name)))
317-
}
318-
319-
fn spawn_and_forget(self) -> JoinHandle<<Self::Future as Future>::Output> {
268+
fn spawn(self) -> JoinHandle<<Self::Future as Future>::Output> {
320269
tokio::spawn(self)
321270
}
322271
}
323272

324-
#[cfg(all(test, feature = "metrics"))]
273+
#[cfg(test)]
325274
mod test {
326275
use {
327276
super::*,
@@ -356,7 +305,7 @@ mod test {
356305
tokio::time::sleep(Duration::from_millis(100)).await;
357306
b.fetch_add(1, Ordering::SeqCst);
358307
})
359-
.spawn("")
308+
.spawn()
360309
};
361310

362311
tokio::time::sleep(Duration::from_millis(200)).await;
@@ -385,7 +334,7 @@ mod test {
385334
tokio::time::sleep(Duration::from_millis(100)).await;
386335
b.fetch_add(1, Ordering::Relaxed);
387336
})
388-
.spawn("")
337+
.spawn()
389338
};
390339

391340
tokio::time::sleep(Duration::from_millis(200)).await;
@@ -416,7 +365,7 @@ mod test {
416365
tokio::time::sleep(Duration::from_millis(100)).await;
417366
b.fetch_add(1, Ordering::Relaxed);
418367
})
419-
.spawn("")
368+
.spawn()
420369
};
421370

422371
assert_eq!(handle.await.unwrap(), Err(Error::Timeout));
@@ -441,7 +390,7 @@ mod test {
441390
tokio::time::sleep(Duration::from_millis(100)).await;
442391
b.fetch_add(1, Ordering::Relaxed);
443392
})
444-
.spawn("")
393+
.spawn()
445394
};
446395

447396
assert_eq!(handle.await.unwrap(), Ok(42));

crates/future_metrics/Cargo.toml

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)