Skip to content

Commit 2e709d9

Browse files
authored
feat: collect the OTLP data exported by the benchmarks (#589)
Add `codspeed run --mode telemetry`, an experimental mode that collects the OpenTelemetry spans and logs the benchmarks export and uploads them with the results. It runs as its own pass, each benchmark once, so the export never runs during measured runs. Combine it with a measuring mode, e.g. `--mode telemetry,walltime`. - Analysis executor: a generic executor that runs the benchmarks once without a profiler, with analysis instruments hooked onto the process. It saves the benchmark markers and uploads with `executor: "analysis"` and the enabled instruments. `CODSPEED_RUNNER_MODE` is `telemetry` in this mode. - Telemetry instrument: an OTLP/HTTP receiver on a free localhost port, passed to the benchmark in `CODSPEED_OTEL_ENDPOINT`. It accepts protobuf and JSON, plain or gzipped, and writes each trace and log request to disk before it acknowledges it. Metrics are dropped. When the process exits, the requests are decoded into `otel/spans.msgpack.zst` and `otel/logs.msgpack.zst`, one row per span or log. The `codspeed.monotonic_*` attributes move to row fields to match each row to its benchmark. - File format: `runner_shared::telemetry` defines the files the runner and the parsers share: versioned positional MessagePack records in a long-window zstd stream, with trace and span ids stored as integers. `TelemetryReader` decodes them as a stream. v1 fixtures keep old files decodable. Closes COD-3863
1 parent 8b62e4a commit 2e709d9

34 files changed

Lines changed: 2421 additions & 10 deletions

‎Cargo.lock‎

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

‎Cargo.toml‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ reqwest-retry = "0.9"
3333
serde = { workspace = true }
3434
serde_json = { workspace = true, features = ["preserve_order"] }
3535
url = "2.5.8"
36+
hyper = { version = "1.9", features = ["server", "http1"] }
37+
hyper-util = { version = "0.1.20", features = ["tokio"] }
38+
http-body-util = "0.1.3"
3639
sha256 = "1.6"
37-
tokio = { version = "1", features = ["macros", "rt", "process"] }
40+
tokio = { version = "1", features = ["fs", "macros", "net", "rt", "sync", "process"] }
3841
tokio-tar = { package = "astral-tokio-tar", version = "0.6.2" }
3942
tokio-util = "0.7.18"
4043
crc-fast = { version = "1.9.0", default-features = false, features = ["std"] }
@@ -79,6 +82,13 @@ which = "8.0.2"
7982
crc32fast = "1.5.0"
8083
samply = { path = "crates/samply-codspeed/samply" }
8184
bytes = "1"
85+
opentelemetry-proto = { version = "0.33", default-features = false, features = [
86+
"gen-tonic-messages",
87+
"trace",
88+
"logs",
89+
"with-serde",
90+
] }
91+
prost = "0.14"
8292

8393
# Memory profiling (memtrack) and the capability handling around it are Linux-only.
8494
[target.'cfg(target_os = "linux")'.dependencies]

‎crates/exec-harness/src/lib.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ pub fn execute_benchmarks(
7070

7171
match measurement_mode {
7272
MeasurementMode::Walltime => walltime::perform(commands),
73-
MeasurementMode::Memory | MeasurementMode::Simulation => analysis::perform(commands),
73+
MeasurementMode::Memory | MeasurementMode::Simulation | MeasurementMode::Telemetry => {
74+
analysis::perform(commands)
75+
}
7476
}
7577
}

‎crates/runner-shared/Cargo.toml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ harness = false
3939
[[bench]]
4040
name = "memtrack_reader"
4141
harness = false
42+
43+
[[bench]]
44+
name = "telemetry"
45+
harness = false

‎crates/runner-shared/benches/memtrack_reader.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ fn sizes() -> &'static [usize] {
3636
.ok()
3737
.and_then(|mode| MeasurementMode::from_str(&mode, true).ok());
3838
match mode {
39-
Some(MeasurementMode::Simulation | MeasurementMode::Memory) => ANALYSIS_MODE_SIZES,
39+
Some(
40+
MeasurementMode::Simulation | MeasurementMode::Memory | MeasurementMode::Telemetry,
41+
) => ANALYSIS_MODE_SIZES,
4042
Some(MeasurementMode::Walltime) | None => WALLTIME_MODE_SIZES,
4143
}
4244
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//! Writing and reading the telemetry artifacts, on the spans and logs of a telemetry-heavy benchmark:
2+
//! about 10k spans and 10k logs, from nested `tracing` spans with attributes and an event in each.
3+
4+
use std::io::Cursor;
5+
use std::path::Path;
6+
7+
use divan::Bencher;
8+
use divan::counter::{BytesCount, ItemsCount};
9+
use runner_shared::telemetry::{LogRow, SpanRow, TelemetryReader, TelemetryRow, TelemetryWriter};
10+
11+
#[global_allocator]
12+
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
13+
14+
fn main() {
15+
divan::main();
16+
}
17+
18+
const SPANS: &[u8] = include_bytes!("../testdata/telemetry_volume_spans.msgpack.zst");
19+
const LOGS: &[u8] = include_bytes!("../testdata/telemetry_volume_logs.msgpack.zst");
20+
21+
fn read<T: TelemetryRow>(artifact: &[u8]) -> Vec<T> {
22+
TelemetryReader::<T, _>::new(Cursor::new(artifact))
23+
.unwrap()
24+
.collect::<anyhow::Result<_>>()
25+
.unwrap()
26+
}
27+
28+
fn bench_read<T: TelemetryRow>(bencher: Bencher, artifact: &[u8]) {
29+
let rows = read::<T>(artifact).len();
30+
bencher
31+
.counter(ItemsCount::new(rows))
32+
.counter(BytesCount::new(artifact.len()))
33+
.bench_local(|| read::<T>(artifact).len());
34+
}
35+
36+
fn bench_write<T: TelemetryRow + Clone>(bencher: Bencher, artifact: &[u8]) {
37+
let rows = read::<T>(artifact);
38+
let dir = tempfile::tempdir_in(env!("CARGO_TARGET_TMPDIR")).unwrap();
39+
let path = dir.path().join("telemetry.msgpack.zst");
40+
let write = |rows: Vec<T>, path: &Path| {
41+
let mut writer = TelemetryWriter::<T>::create(path).unwrap();
42+
writer.write(rows).unwrap();
43+
writer.close().unwrap();
44+
};
45+
bencher
46+
.counter(ItemsCount::new(rows.len()))
47+
.with_inputs(|| rows.clone())
48+
.bench_local_values(|rows| write(rows, &path));
49+
}
50+
51+
#[divan::bench]
52+
fn read_spans(bencher: Bencher) {
53+
bench_read::<SpanRow>(bencher, SPANS);
54+
}
55+
56+
#[divan::bench]
57+
fn read_logs(bencher: Bencher) {
58+
bench_read::<LogRow>(bencher, LOGS);
59+
}
60+
61+
#[divan::bench]
62+
fn write_spans(bencher: Bencher) {
63+
bench_write::<SpanRow>(bencher, SPANS);
64+
}
65+
66+
#[divan::bench]
67+
fn write_logs(bencher: Bencher) {
68+
bench_write::<LogRow>(bencher, LOGS);
69+
}

‎crates/runner-shared/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ pub mod metadata;
66
pub mod module_symbols;
77
pub mod perf_event;
88
pub mod runtime_env;
9+
pub mod telemetry;
910
pub mod unwind_data;
1011
pub mod walltime_results;

‎crates/runner-shared/src/measurement_mode.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub enum MeasurementMode {
88
Memory,
99
#[value(alias = "instrumentation")]
1010
Simulation,
11+
Telemetry,
1112
}
1213

1314
impl MeasurementMode {
@@ -19,6 +20,7 @@ impl MeasurementMode {
1920
Self::Simulation => "instrumentation",
2021
Self::Walltime => "walltime",
2122
Self::Memory => "memory",
23+
Self::Telemetry => "telemetry",
2224
}
2325
}
2426
}

0 commit comments

Comments
 (0)