Skip to content

Commit a4e1030

Browse files
committed
Add a metric recording the overall time spent building each crate
1 parent ea7eb5c commit a4e1030

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

src/build_queue.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,7 @@ impl BuildQueue {
164164
.is_some())
165165
}
166166

167-
pub(crate) fn process_next_crate(
168-
&self,
169-
f: impl FnOnce(&QueuedCrate) -> Result<()>,
170-
) -> Result<()> {
167+
fn process_next_crate(&self, f: impl FnOnce(&QueuedCrate) -> Result<()>) -> Result<()> {
171168
let mut conn = self.db.get()?;
172169
let mut transaction = conn.transaction()?;
173170

@@ -198,11 +195,13 @@ impl BuildQueue {
198195
None => return Ok(()),
199196
};
200197

201-
let res = f(&to_process).with_context(|| {
202-
format!(
203-
"Failed to build package {}-{} from queue",
204-
to_process.name, to_process.version
205-
)
198+
let res = self.metrics.build_time.observe_closure_duration(|| {
199+
f(&to_process).with_context(|| {
200+
format!(
201+
"Failed to build package {}-{} from queue",
202+
to_process.name, to_process.version
203+
)
204+
})
206205
});
207206
self.metrics.total_builds.inc();
208207
if let Err(err) =
@@ -631,6 +630,7 @@ mod tests {
631630
let metrics = env.instance_metrics();
632631
assert_eq!(metrics.total_builds.get(), 9);
633632
assert_eq!(metrics.failed_builds.get(), 1);
633+
assert_eq!(metrics.build_time.get_sample_count(), 9);
634634

635635
// no invalidations were run since we don't have a distribution id configured
636636
assert!(cdn::queued_or_active_crate_invalidations(&mut *env.db().conn())?.is_empty());

src/metrics/macros.rs

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ macro_rules! metrics {
2222
pub(crate) recently_accessed_releases: RecentlyAccessedReleases,
2323
pub(crate) cdn_invalidation_time: prometheus::HistogramVec,
2424
pub(crate) cdn_queue_time: prometheus::HistogramVec,
25+
pub(crate) build_time: prometheus::Histogram,
2526
}
2627
impl $name {
2728
$vis fn new() -> Result<Self, prometheus::Error> {
@@ -61,11 +62,22 @@ macro_rules! metrics {
6162
)?;
6263
registry.register(Box::new(cdn_queue_time.clone()))?;
6364

65+
let build_time = prometheus::Histogram::with_opts(
66+
prometheus::HistogramOpts::new(
67+
"build_time",
68+
"time spent building crates",
69+
)
70+
.namespace($namespace)
71+
.buckets($crate::metrics::build_time_histogram_buckets()),
72+
)?;
73+
registry.register(Box::new(build_time.clone()))?;
74+
6475
Ok(Self {
6576
registry,
6677
recently_accessed_releases: RecentlyAccessedReleases::new(),
6778
cdn_invalidation_time,
6879
cdn_queue_time,
80+
build_time,
6981
$(
7082
$(#[$meta])*
7183
$metric,

src/metrics/mod.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,38 @@ pub const CDN_INVALIDATION_HISTOGRAM_BUCKETS: &[f64; 11] = &[
3030
1200.0, // 20
3131
1800.0, // 30
3232
2700.0, // 45
33-
6000.0, // 60
34-
12000.0, // 120
35-
24000.0, // 240
33+
6000.0, // 100
34+
12000.0, // 200
35+
24000.0, // 400
3636
];
3737

38+
/// the measured times of building crates will be put into these buckets
39+
pub fn build_time_histogram_buckets() -> Vec<f64> {
40+
vec![
41+
30.0, // 0.5
42+
60.0, // 1
43+
120.0, // 2
44+
180.0, // 3
45+
240.0, // 4
46+
300.0, // 5
47+
360.0, // 6
48+
420.0, // 7
49+
480.0, // 8
50+
540.0, // 9
51+
600.0, // 10
52+
660.0, // 11
53+
720.0, // 12
54+
780.0, // 13
55+
840.0, // 14
56+
900.0, // 15
57+
1200.0, // 20
58+
1800.0, // 30
59+
2400.0, // 40
60+
3000.0, // 50
61+
3600.0, // 60
62+
]
63+
}
64+
3865
metrics! {
3966
pub struct InstanceMetrics {
4067
/// The number of idle database connections

0 commit comments

Comments
 (0)