Repository navigation
feat: export the telemetry of the benchmarks over OTLP #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lvaroqui
wants to merge
3
commits into
main
Choose a base branch
from
cod-4149-featnode-export-the-benchmark-telemetry-from-the-codspeed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { withCodSpeed } from "@codspeed/benchmark.js-plugin"; | ||
| import Benchmark from "benchmark"; | ||
| import { | ||
| legacyLog, | ||
| logWithoutSpan, | ||
| noTelemetry, | ||
| parseAndSum, | ||
| } from "../src/workloads"; | ||
|
|
||
| const suite = withCodSpeed(new Benchmark.Suite()); | ||
|
|
||
| suite | ||
| .add("parse_and_sum", { | ||
| defer: true, | ||
| fn: (deferred: Benchmark.Deferred) => { | ||
| parseAndSum().then(() => deferred.resolve()); | ||
| }, | ||
| }) | ||
| .add("log_without_span", () => { | ||
| logWithoutSpan(); | ||
| }) | ||
| .add("legacy_log", () => { | ||
| legacyLog(); | ||
| }) | ||
| .add("no_telemetry", () => { | ||
| noTelemetry(); | ||
| }) | ||
| .on("cycle", (event: Benchmark.Event) => { | ||
| console.log(String(event.target)); | ||
| }); | ||
|
|
||
| await suite.run({ async: true }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { withCodSpeed } from "@codspeed/tinybench-plugin"; | ||
| import { Bench } from "tinybench"; | ||
| import { | ||
| legacyLog, | ||
| logWithoutSpan, | ||
| noTelemetry, | ||
| parseAndSum, | ||
| } from "../src/workloads"; | ||
|
|
||
| const bench = withCodSpeed(new Bench()); | ||
|
|
||
| bench | ||
| .add("parse_and_sum", async () => { | ||
| await parseAndSum(); | ||
| }) | ||
| .add("log_without_span", () => { | ||
| logWithoutSpan(); | ||
| }) | ||
| .add("legacy_log", () => { | ||
| legacyLog(); | ||
| }) | ||
| .add("no_telemetry", () => { | ||
| noTelemetry(); | ||
| }); | ||
|
|
||
| await bench.run(); | ||
| console.table(bench.table()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "name": "with-telemetry", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "bench-benchmark-js": "node --import tsx bench/benchmark.js.bench.ts", | ||
| "bench-tinybench": "node --import tsx bench/tinybench.bench.ts", | ||
| "bench-vitest": "vitest bench --run", | ||
| "typecheck": "tsc --noEmit --pretty" | ||
| }, | ||
| "dependencies": { | ||
| "@opentelemetry/api": "^1.10.0", | ||
| "@opentelemetry/api-logs": "^0.223.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@codspeed/benchmark.js-plugin": "workspace:*", | ||
| "@codspeed/tinybench-plugin": "workspace:*", | ||
| "@codspeed/vitest-plugin": "workspace:*", | ||
| "@types/benchmark": "^2.1.2", | ||
| "benchmark": "^2.1.4", | ||
| "tinybench": "^4.0.1", | ||
| "tsx": "^4.23.12", | ||
| "typescript": "^5.1.3", | ||
| "vitest": "^5.0.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { describe, test } from "vitest"; | ||
| import { | ||
| legacyLog, | ||
| logWithoutSpan, | ||
| noTelemetry, | ||
| parseAndSum, | ||
| } from "./workloads"; | ||
|
|
||
| describe("telemetry", () => { | ||
| test("parse_and_sum", async ({ bench }) => { | ||
| await bench("parse_and_sum", async () => { | ||
| await parseAndSum(); | ||
| }).run(); | ||
| }); | ||
|
|
||
| test("log_without_span", async ({ bench }) => { | ||
| await bench("log_without_span", () => { | ||
| logWithoutSpan(); | ||
| }).run(); | ||
| }); | ||
|
|
||
| test("legacy_log", async ({ bench }) => { | ||
| await bench("legacy_log", () => { | ||
| legacyLog(); | ||
| }).run(); | ||
| }); | ||
|
|
||
| test("no_telemetry", async ({ bench }) => { | ||
| await bench("no_telemetry", () => { | ||
| noTelemetry(); | ||
| }).run(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /** | ||
| * Workloads emitting telemetry in different ways: nested spans with logs in | ||
| * them, a log outside of any span, a log through the legacy logs API, or | ||
| * nothing at all. | ||
| */ | ||
| import { logs, SeverityNumber, trace } from "@opentelemetry/api"; | ||
| import { logs as legacyLogs } from "@opentelemetry/api-logs"; | ||
|
|
||
| const tracer = trace.getTracer("with-telemetry"); | ||
| const logger = logs.getLogger("with-telemetry"); | ||
| const legacyLogger = legacyLogs.getLogger("with-telemetry-legacy"); | ||
|
|
||
| function parse(input: string): number[] { | ||
| return tracer.startActiveSpan("parse", (span) => { | ||
| span.setAttribute("input_len", input.length); | ||
| const values = input | ||
| .split(",") | ||
| .map(Number) | ||
| .filter((value) => !Number.isNaN(value)); | ||
| logger.emit({ | ||
| severityNumber: SeverityNumber.INFO, | ||
| severityText: "INFO", | ||
| body: "parsed values", | ||
| attributes: { count: values.length }, | ||
| }); | ||
| span.end(); | ||
| return values; | ||
| }); | ||
| } | ||
|
|
||
| async function sum(values: number[]): Promise<number> { | ||
| return tracer.startActiveSpan("sum", async (span) => { | ||
| // Awaited, so that the span must follow the async context | ||
| await Promise.resolve(); | ||
| const total = values.reduce((acc, value) => acc + value, 0); | ||
| span.end(); | ||
| return total; | ||
| }); | ||
| } | ||
|
|
||
| /** Nested spans, with logs in them, across an `await`. */ | ||
| export async function parseAndSum(): Promise<number> { | ||
| return tracer.startActiveSpan("parse_and_sum", async (span) => { | ||
| const values = parse("1,2,x,3"); | ||
| if (values.length < 4) { | ||
| logger.emit({ | ||
| severityNumber: SeverityNumber.WARN, | ||
| severityText: "WARN", | ||
| body: "skipped invalid fields", | ||
| attributes: { skipped: 4 - values.length }, | ||
| }); | ||
| } | ||
| const total = await sum(values); | ||
| span.end(); | ||
| return total; | ||
| }); | ||
| } | ||
|
|
||
| /** A log outside of any span. */ | ||
| export function logWithoutSpan(): number { | ||
| const values = [1, 2, 3]; | ||
| logger.emit({ | ||
| severityNumber: SeverityNumber.INFO, | ||
| severityText: "INFO", | ||
| body: "summed without a span", | ||
| attributes: { count: values.length }, | ||
| }); | ||
| return values.reduce((acc, value) => acc + value, 0); | ||
| } | ||
|
|
||
| /** A log through `@opentelemetry/api-logs`, which older instrumentations use. */ | ||
| export function legacyLog(): number { | ||
| const values = [1, 2, 3]; | ||
| legacyLogger.emit({ | ||
| severityText: "INFO", | ||
| body: `summing ${values.length} values, logged with the legacy logs API`, | ||
| }); | ||
| return values.reduce((acc, value) => acc + value, 0); | ||
| } | ||
|
|
||
| /** No telemetry at all. */ | ||
| export function noTelemetry(): number { | ||
| return [1, 2, 3].reduce((acc, value) => acc + value, 0); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "lib": ["es2023"], | ||
| "module": "ESNext", | ||
| "verbatimModuleSyntax": true, | ||
| "target": "es2022", | ||
| "strict": true, | ||
| "esModuleInterop": true, | ||
| "skipLibCheck": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "moduleResolution": "Node" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import codspeedPlugin from "@codspeed/vitest-plugin"; | ||
| import { defineConfig } from "vitest/config"; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [codspeedPlugin()], | ||
| test: { | ||
| benchmark: { | ||
| exclude: ["**/bench/**/*", "**/node_modules/**/*"], | ||
| }, | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new OpenTelemetry SDK packages require Node.js
>=22.15.0, but this repository still supports earlier Node.js 22 releases. If a user's package manager enforces dependency engine requirements, these regular dependencies prevent them from installing any CodSpeed plugin—even when they never enable telemetry. Use compatible dependencies or explicitly raise the supported minimum.Prompt To Fix With AI