|
1 | 1 | use futures::future::BoxFuture;
|
2 | 2 | use lazy_static::lazy_static;
|
| 3 | +use orchestrator::coordinator; |
3 | 4 | use prometheus::{
|
4 | 5 | self, register_histogram, register_histogram_vec, register_int_counter, register_int_gauge,
|
5 | 6 | Histogram, HistogramVec, IntCounter, IntGauge,
|
6 | 7 | };
|
7 | 8 | use regex::Regex;
|
8 |
| -use std::{future::Future, time::Instant}; |
| 9 | +use std::{ |
| 10 | + future::Future, |
| 11 | + time::{Duration, Instant}, |
| 12 | +}; |
9 | 13 |
|
10 | 14 | use crate::sandbox::{self, Channel, CompileTarget, CrateType, Edition, Mode};
|
11 | 15 |
|
@@ -62,6 +66,16 @@ pub(crate) enum Outcome {
|
62 | 66 | ErrorUserCode,
|
63 | 67 | }
|
64 | 68 |
|
| 69 | +pub(crate) struct LabelsCore { |
| 70 | + target: Option<CompileTarget>, |
| 71 | + channel: Option<Channel>, |
| 72 | + mode: Option<Mode>, |
| 73 | + edition: Option<Option<Edition>>, |
| 74 | + crate_type: Option<CrateType>, |
| 75 | + tests: Option<bool>, |
| 76 | + backtrace: Option<bool>, |
| 77 | +} |
| 78 | + |
65 | 79 | #[derive(Debug, Copy, Clone)]
|
66 | 80 | pub(crate) struct Labels {
|
67 | 81 | endpoint: Endpoint,
|
@@ -132,6 +146,29 @@ impl Labels {
|
132 | 146 | backtrace,
|
133 | 147 | ]
|
134 | 148 | }
|
| 149 | + |
| 150 | + pub(crate) fn complete(endpoint: Endpoint, labels_core: LabelsCore, outcome: Outcome) -> Self { |
| 151 | + let LabelsCore { |
| 152 | + target, |
| 153 | + channel, |
| 154 | + mode, |
| 155 | + edition, |
| 156 | + crate_type, |
| 157 | + tests, |
| 158 | + backtrace, |
| 159 | + } = labels_core; |
| 160 | + Self { |
| 161 | + endpoint, |
| 162 | + outcome, |
| 163 | + target, |
| 164 | + channel, |
| 165 | + mode, |
| 166 | + edition, |
| 167 | + crate_type, |
| 168 | + tests, |
| 169 | + backtrace, |
| 170 | + } |
| 171 | + } |
135 | 172 | }
|
136 | 173 |
|
137 | 174 | pub(crate) trait GenerateLabels {
|
@@ -406,11 +443,8 @@ where
|
406 | 443 | let outcome = SuccessDetails::for_sandbox_result(&response);
|
407 | 444 | let mut labels = request.generate_labels(outcome);
|
408 | 445 | f(&mut labels);
|
409 |
| - let values = &labels.as_values(); |
410 |
| - |
411 |
| - let histogram = REQUESTS.with_label_values(values); |
412 | 446 |
|
413 |
| - histogram.observe(elapsed.as_secs_f64()); |
| 447 | + record_metric_complete(labels, elapsed); |
414 | 448 |
|
415 | 449 | response
|
416 | 450 | }
|
@@ -443,10 +477,53 @@ where
|
443 | 477 | tests: None,
|
444 | 478 | backtrace: None,
|
445 | 479 | };
|
446 |
| - let values = &labels.as_values(); |
447 |
| - let histogram = REQUESTS.with_label_values(values); |
448 | 480 |
|
449 |
| - histogram.observe(elapsed.as_secs_f64()); |
| 481 | + record_metric_complete(labels, elapsed); |
450 | 482 |
|
451 | 483 | response
|
452 | 484 | }
|
| 485 | + |
| 486 | +pub(crate) trait HasLabelsCore { |
| 487 | + fn labels_core(&self) -> LabelsCore; |
| 488 | +} |
| 489 | + |
| 490 | +impl HasLabelsCore for coordinator::CompileRequest { |
| 491 | + fn labels_core(&self) -> LabelsCore { |
| 492 | + let Self { |
| 493 | + target, |
| 494 | + channel, |
| 495 | + crate_type, |
| 496 | + mode, |
| 497 | + edition, |
| 498 | + tests, |
| 499 | + backtrace, |
| 500 | + code: _, |
| 501 | + } = *self; |
| 502 | + |
| 503 | + LabelsCore { |
| 504 | + target: Some(target.into()), |
| 505 | + channel: Some(channel.into()), |
| 506 | + mode: Some(mode.into()), |
| 507 | + edition: Some(Some(edition.into())), |
| 508 | + crate_type: Some(crate_type.into()), |
| 509 | + tests: Some(tests), |
| 510 | + backtrace: Some(backtrace), |
| 511 | + } |
| 512 | + } |
| 513 | +} |
| 514 | + |
| 515 | +pub(crate) fn record_metric( |
| 516 | + endpoint: Endpoint, |
| 517 | + labels_core: LabelsCore, |
| 518 | + outcome: Outcome, |
| 519 | + elapsed: Duration, |
| 520 | +) { |
| 521 | + let labels = Labels::complete(endpoint, labels_core, outcome); |
| 522 | + record_metric_complete(labels, elapsed) |
| 523 | +} |
| 524 | + |
| 525 | +fn record_metric_complete(labels: Labels, elapsed: Duration) { |
| 526 | + let values = &labels.as_values(); |
| 527 | + let histogram = REQUESTS.with_label_values(values); |
| 528 | + histogram.observe(elapsed.as_secs_f64()); |
| 529 | +} |
0 commit comments