Skip to content

feat: export the telemetry of the benchmarks over OTLP - #151

Open
lvaroqui wants to merge 1 commit into
masterfrom
cod-4148-featpython-export-the-benchmark-telemetry-from-pytest
Open

lvaroqui wants to merge 1 commit into
masterfrom
cod-4148-featpython-export-the-benchmark-telemetry-from-pytest

Conversation

@lvaroqui

@lvaroqui lvaroqui commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Export the spans and logs emitted by the benchmarked code to the CodSpeed runner, so that they can be shown next to each benchmark.

Add a telemetry mode, selected when the runner sets CODSPEED_RUNNER_MODE=telemetry. It runs each benchmark once, like the other analysis modes, and skips the perf map warm-up, which would emit telemetry outside the benchmark.

When CODSPEED_OTEL_ENDPOINT is set, the plugin exports over OTLP/HTTP, with batch processors flushed when pytest exits:

  • the spans of the global tracer provider. If the benchmarked code already installed an SDK TracerProvider, the export is added to it;
  • the stdlib logging records, filtered by CODSPEED_TELEMETRY_FILTER (info by default, e.g. info,my_package=debug). The root logger level is lowered for the run so that the selected records reach the handler. The records of the exporter's own loggers (opentelemetry, urllib3, requests) are never exported.

Spans get codspeed.monotonic_start_ns / codspeed.monotonic_end_ns and log records codspeed.monotonic_time_ns, read from the instrument-hooks clock that the benchmark markers use.

The OpenTelemetry SDK and exporter come with a new telemetry extra (>=1.28.0). Without the extra, the plugin warns and exports nothing.

Round markers in all analysis modes

The analysis instrument now sends the start and end of each round (add_benchmark_timestamps) in every mode, not only in telemetry mode. The sample markers sent by start_benchmark / stop_benchmark reach the runner tens of microseconds away from the benchmarked code, so the rounds bound it more tightly. The walltime instrument already sends them.

  • Simulation: no change. Under valgrind, add_marker does nothing.
  • Memory: the markers reach the runner, and CodSpeed counts the allocations within the round instead of the whole sample. This leaves out the allocations of the plugin code between the sample and round markers, so benchmarks that allocate little can report less memory (see Verification).
  • Telemetry: CodSpeed uses the rounds to find the benchmark of each span and log.

Things worth a look

  • Trace ids. The plugin calls random.seed(0) before each benchmark, and the SDK's default id generator draws from the global random. Every benchmark got the same trace and span ids, and the SDK's draws changed the random numbers the benchmarks get. The ids now come from a generator with its own random.Random(). If the benchmarked code installed its own TracerProvider, the plugin patches its id generator in place, so that tracers created before the plugin starts also use it.
  • No global LoggerProvider. Only logging records are exported, through a logging.Filter on the SDK LoggingHandler. The SDK log processor API changed between 1.28 and 1.45, so stamping records emitted through the OpenTelemetry logs API would need version-specific code. LoggingHandler is deprecated in recent SDK versions; its warning is silenced.
  • service.name is the name of the pytest rootdir.

Example and CI

tests/benchmarks/test_bench_telemetry.py adds benchmarks that emit spans and logs. The benchmarks workflow installs the telemetry extra only in the new telemetry matrix entry, so the simulation, walltime and memory jobs run without it and check that the plugin works without the extra. Without the extra, these example benchmarks are skipped.

The telemetry jobs currently fail at upload (400 Invalid profile metadata): the CodSpeed API doesn't accept telemetry runs yet.

Verification

Checked end to end with a local build of the runner (codspeed run -m telemetry --skip-upload): it collects 3 spans and 3 logs from the example, the debug record is filtered out, each span and log falls within the round markers of its benchmark, and each benchmark has its own trace id.

Round markers in memory mode: ran 73 benchmarks in memory mode with runner v5.8.0, with and without the markers, twice each, and parsed the profiles with the CodSpeed parser.

  • 33 of the 41 memory flamegraphs are identical: same frames, call paths and allocated bytes.
  • The 8 others (test_multiprocessing_map, test_threadpool_map) also differ between two runs of the same code, by the same amount.
  • The plugin's own allocations, outside the root frame, are no longer counted. On CodSpeed, test_iterative_fibo_10 goes from 135 B to 0 B. In the local runs, test_sum_squares_fast no longer fails with a missing root frame: these allocations were what caused it.

Closes COD-4148

@lvaroqui
lvaroqui marked this pull request as ready for review November 10, 2026 16:04
@greptile-apps

greptile-apps Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues were found.

Summary

Adds a telemetry mode that exports benchmark spans and Python logs over OTLP/HTTP.

  • Pytest sends benchmark spans and logs to the CodSpeed runner.
  • The runner results file is written only for walltime runs.

The previous trace-ID finding is fixed. Exporter logger names are also rejected before user filter directives are applied.

Diagram

sequenceDiagram
  participant Pytest
  participant Hooks as Instrument hooks
  participant Benchmark
  participant Export as OTLP exporters
  participant Runner
  Pytest->>Hooks: Start benchmark and read round start
  Pytest->>Benchmark: Run once
  Benchmark->>Export: Queue spans and filtered logs
  Pytest->>Hooks: Send round timestamps and stop benchmark
  Hooks->>Runner: Benchmark markers
  Pytest->>Export: Shutdown and flush
  Export->>Runner: POST traces and logs
Loading

Reviews (3) · Last reviewed commit: "fix: write the results file only in wall..." · Reviewed by Greptile

Comment thread src/pytest_codspeed/telemetry.py Resolved
@codspeed

codspeed Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 11.23%

2 warnings, 2 notes

⚠️ 5 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 3 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

ℹ️ This run is excluded from the branch history

This commit was force-pushed, so this run no longer appears in the branch's performance history.

ℹ️ 23 benchmarks have no flamegraph

The flamegraph wasn't generated: the symbols of the profiling trace could not be resolved, because no information about the language frames was collected.

See affected benchmarks

⚡ 3 improved benchmarks
✅ 246 untouched benchmarks
🆕 9 new benchmarks
⏩ 28 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ Walltime test_solve_power_sum[13-2] 8.1 µs 7 µs +16.18%
⚡ Walltime test_generate_sum_of_subsets_soln[nums0-9] 28.5 µs 25.6 µs +11.04%
⚡ Walltime test_hostname_resolution[google.com] 411.3 µs 385.6 µs +6.66%
🆕 Walltime test_no_telemetry N/A 35.4 µs N/A
🆕 Walltime test_log_without_span N/A 36.8 µs N/A
🆕 Walltime test_nested_spans_with_logs N/A 272.5 µs N/A
🆕 CPU Simulation test_no_telemetry N/A 188.9 µs N/A
🆕 CPU Simulation test_nested_spans_with_logs N/A 885.9 µs N/A
🆕 CPU Simulation test_log_without_span N/A 203.4 µs N/A
🆕 Memory test_nested_spans_with_logs N/A 0 B N/A
🆕 Memory test_no_telemetry N/A 0 B N/A
🆕 Memory test_log_without_span N/A 0 B N/A

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing cod-4148-featpython-export-the-benchmark-telemetry-from-pytest (7b40c83) with master (15d46b4)

Open in CodSpeed

Footnotes

  1. 28 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@lvaroqui
lvaroqui force-pushed the cod-4148-featpython-export-the-benchmark-telemetry-from-pytest branch from 17e57f4 to e7f93a7 Compare November 10, 2026 17:15
Comment thread src/pytest_codspeed/telemetry.py Resolved
@lvaroqui
lvaroqui force-pushed the cod-4148-featpython-export-the-benchmark-telemetry-from-pytest branch 2 times, most recently from f9c9294 to 153d42a Compare November 10, 2026 17:37

@GuillaumeLagrange GuillaumeLagrange left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

olgtm

Comment thread .github/workflows/codspeed.yml Resolved
Comment thread src/pytest_codspeed/plugin.py Resolved
@lvaroqui
lvaroqui force-pushed the cod-4148-featpython-export-the-benchmark-telemetry-from-pytest branch from 153d42a to 7b40c83 Compare November 24, 2026 14:18
Add a `telemetry` mode, which the runner selects with
`CODSPEED_RUNNER_MODE=telemetry`. It runs each benchmark once, sends
the start and end of each round to the runner, and skips the perf map
warm-up.

When `CODSPEED_OTEL_ENDPOINT` is set, the plugin exports the spans of
the global tracer provider and the `logging` records over OTLP/HTTP,
and flushes them when pytest exits. `CODSPEED_TELEMETRY_FILTER` selects
the records (`info` by default). Spans and records also get their times
on the clock of the benchmark markers, so that the runner can find the
benchmark of each one. The trace and span ids come from a random
generator of their own: the plugin reseeds the global one before each
benchmark.

The OpenTelemetry SDK and exporter come with a new `telemetry` extra.
Add an example benchmark that emits telemetry, and install the extra in
the benchmarks workflow.

Closes COD-4148
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lvaroqui
lvaroqui force-pushed the cod-4148-featpython-export-the-benchmark-telemetry-from-pytest branch from 7b40c83 to 54e8003 Compare November 24, 2026 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants