playwright-opentelemetry 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,3 +3,9 @@
3
3
  A combination of a Playwright reporter and a fixture that allows you to create opentelemetry spans for playwright end-to-end tests.
4
4
 
5
5
  For more information on getting started, check out the main [readme](../README.md).
6
+
7
+ ## Developing the reporter / fixture
8
+
9
+ - `pnpm dev` starts a dev server that outputs `dist/index.mjs`
10
+ - `pnpm build` otherwise creates a one-off compiled build
11
+ - `pnpm test:e2e` uses the compiled reporter output
package/dist/fixture.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { c as getOrCreateTraceId, f as writeNetworkSpan, m as writePageTestMapping, o as generateSpanId, p as writeNetworkSpanParent, s as getCurrentSpanId, u as readNetworkSpanParent } from "./trace-files-BbTTP4-y.mjs";
1
+ import { c as getOrCreateTraceId, f as writeNetworkSpan, m as writePageTestMapping, o as generateSpanId, p as writeNetworkSpanParent, s as getCurrentSpanId, u as readNetworkSpanParent } from "./trace-files-BK_6TvGO.mjs";
2
2
  import path from "node:path";
3
3
  import { test as test$1 } from "@playwright/test";
4
4
 
@@ -4,6 +4,8 @@ import { FullConfig, FullResult, Reporter, Suite, TestCase, TestResult, TestStep
4
4
  interface PlaywrightOpentelemetryReporterOptions {
5
5
  otlpEndpoint?: string;
6
6
  otlpHeaders?: Record<string, string>;
7
+ playwrightTraceApiEndpoint?: string;
8
+ playwrightTraceApiHeaders?: Record<string, string>;
7
9
  storeTraceZip?: boolean;
8
10
  serviceName?: string;
9
11
  debug?: boolean;
@@ -15,6 +17,8 @@ declare class PlaywrightOpentelemetryReporter implements Reporter {
15
17
  private playwrightVersion?;
16
18
  private resolvedEndpoint;
17
19
  private resolvedHeaders;
20
+ private resolvedTraceApiEndpoint;
21
+ private resolvedTraceApiHeaders;
18
22
  private resolvedServiceName;
19
23
  /** Maps test.id to its project's outputDir */
20
24
  private testOutputDirs;
@@ -36,11 +40,8 @@ declare class PlaywrightOpentelemetryReporter implements Reporter {
36
40
  onStdOut(chunk: string | Buffer, _test: TestCase, _result: TestResult): void;
37
41
  onStdErr(chunk: string | Buffer, _test: TestCase, _result: TestResult): void;
38
42
  printsToStdio(): boolean;
39
- /**
40
- * Get the output directory for a test ID.
41
- * Throws if the test ID was not registered in onBegin.
42
- */
43
43
  private getOutputDir;
44
+ private sendTestJsonToTraceApi;
44
45
  }
45
46
  //#endregion
46
47
  export { PlaywrightOpentelemetryReporterOptions as n, PlaywrightOpentelemetryReporter as t };
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { n as PlaywrightOpentelemetryReporterOptions, t as PlaywrightOpentelemetryReporter } from "./index-DnwmzWGb.mjs";
1
+ import { n as PlaywrightOpentelemetryReporterOptions, t as PlaywrightOpentelemetryReporter } from "./index-Di44jFGW.mjs";
2
2
  import { TestCase, TestStatus } from "@playwright/test/reporter";
3
3
 
4
4
  //#region src/reporter/trace-zip-builder.d.ts
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as PlaywrightOpentelemetryReporter } from "./reporter-Bg93NdnD.mjs";
1
+ import { t as PlaywrightOpentelemetryReporter } from "./reporter-B_dhs9E9.mjs";
2
2
 
3
3
  //#region src/index.ts
4
4
  var src_default = PlaywrightOpentelemetryReporter;
@@ -1,7 +1,7 @@
1
- import { a as createNetworkDirs, c as getOrCreateTraceId, d as writeCurrentSpanId, i as copyScreenshotForTest, l as getScreenshotsDir, n as cleanupTestFiles, o as generateSpanId, r as collectNetworkSpans, t as PW_OTEL_DIR } from "./trace-files-BbTTP4-y.mjs";
1
+ import { a as createNetworkDirs, c as getOrCreateTraceId, d as writeCurrentSpanId, i as copyScreenshotForTest, l as getScreenshotsDir, n as cleanupTestFiles, o as generateSpanId, r as collectNetworkSpans, t as PW_OTEL_DIR } from "./trace-files-BK_6TvGO.mjs";
2
2
  import { existsSync, mkdirSync, watch } from "node:fs";
3
+ import fs, { readFile, readdir } from "node:fs/promises";
3
4
  import path from "node:path";
4
- import fs from "node:fs/promises";
5
5
  import { BlobWriter, TextReader, ZipWriter } from "@zip.js/zip.js";
6
6
 
7
7
  //#region src/reporter/otel-attributes.ts
@@ -21,7 +21,7 @@ const ATTR_TEST_STEP_CATEGORY = "test.step.category";
21
21
 
22
22
  //#endregion
23
23
  //#region package.json
24
- var version = "0.2.0";
24
+ var version = "0.3.1";
25
25
 
26
26
  //#endregion
27
27
  //#region src/reporter/sender.ts
@@ -202,6 +202,8 @@ var PlaywrightOpentelemetryReporter = class {
202
202
  playwrightVersion;
203
203
  resolvedEndpoint;
204
204
  resolvedHeaders;
205
+ resolvedTraceApiEndpoint;
206
+ resolvedTraceApiHeaders;
205
207
  resolvedServiceName;
206
208
  /** Maps test.id to its project's outputDir */
207
209
  testOutputDirs = /* @__PURE__ */ new Map();
@@ -220,8 +222,10 @@ var PlaywrightOpentelemetryReporter = class {
220
222
  ...options.otlpHeaders,
221
223
  ...envHeaders
222
224
  };
225
+ this.resolvedTraceApiEndpoint = options.playwrightTraceApiEndpoint || "";
226
+ this.resolvedTraceApiHeaders = options.playwrightTraceApiHeaders || {};
223
227
  this.resolvedServiceName = process.env.OTEL_SERVICE_NAME || options.serviceName || "playwright-tests";
224
- if (!this.resolvedEndpoint && !this.options.storeTraceZip) throw new Error(getConfigurationErrorMessage());
228
+ if (!this.resolvedEndpoint && !this.resolvedTraceApiEndpoint && !this.options.storeTraceZip) throw new Error(getConfigurationErrorMessage());
225
229
  }
226
230
  onBegin(config, suite) {
227
231
  this.rootDir = config.rootDir;
@@ -329,22 +333,29 @@ var PlaywrightOpentelemetryReporter = class {
329
333
  ...networkSpans
330
334
  ];
331
335
  this.spans.push(...testSpans);
332
- if (this.options.storeTraceZip) {
333
- const relativeFilePath = test.location && this.rootDir ? path.relative(this.rootDir, test.location.file) : test.location?.file ?? "";
334
- const computedDuration = maxEndTime.getTime() - minStartTime.getTime();
335
- await createTraceZip({
336
- outputDir,
337
- testId,
338
- test,
339
- spans: testSpans,
340
- serviceName: this.resolvedServiceName,
341
- playwrightVersion: this.playwrightVersion || "unknown",
342
- relativeFilePath,
343
- status: result.status,
344
- startTime: minStartTime,
345
- duration: computedDuration
346
- });
347
- }
336
+ const relativeFilePath = test.location && this.rootDir ? path.relative(this.rootDir, test.location.file) : test.location?.file ?? "";
337
+ const computedDuration = maxEndTime.getTime() - minStartTime.getTime();
338
+ if (this.options.storeTraceZip) await createTraceZip({
339
+ outputDir,
340
+ testId,
341
+ test,
342
+ spans: testSpans,
343
+ serviceName: this.resolvedServiceName,
344
+ playwrightVersion: this.playwrightVersion || "unknown",
345
+ relativeFilePath,
346
+ status: result.status,
347
+ startTime: minStartTime,
348
+ duration: computedDuration
349
+ });
350
+ if (this.resolvedTraceApiEndpoint) await this.sendTestJsonToTraceApi({
351
+ test,
352
+ result,
353
+ traceId,
354
+ outputDir,
355
+ relativeFilePath,
356
+ startTime: minStartTime,
357
+ computedDuration
358
+ });
348
359
  }
349
360
  async onEnd(_result) {
350
361
  for (const watcher of this.directoryWatchers) watcher.close();
@@ -356,6 +367,13 @@ var PlaywrightOpentelemetryReporter = class {
356
367
  playwrightVersion: this.playwrightVersion || "unknown",
357
368
  debug: this.options.debug ?? false
358
369
  });
370
+ if (this.resolvedTraceApiEndpoint) await sendSpans(this.spans, {
371
+ tracesEndpoint: `${this.resolvedTraceApiEndpoint}/v1/traces`,
372
+ headers: this.resolvedTraceApiHeaders,
373
+ serviceName: this.resolvedServiceName,
374
+ playwrightVersion: this.playwrightVersion || "unknown",
375
+ debug: this.options.debug ?? false
376
+ });
359
377
  for (const [testId, outputDir] of this.testOutputDirs.entries()) await cleanupTestFiles(outputDir, testId);
360
378
  }
361
379
  processTestStep(test, step, parentSpanId, traceId, parentTitlePath, processedSteps) {
@@ -415,15 +433,56 @@ var PlaywrightOpentelemetryReporter = class {
415
433
  printsToStdio() {
416
434
  return this.options.debug ?? false;
417
435
  }
418
- /**
419
- * Get the output directory for a test ID.
420
- * Throws if the test ID was not registered in onBegin.
421
- */
422
436
  getOutputDir(testId) {
423
437
  const outputDir = this.testOutputDirs.get(testId);
424
438
  if (!outputDir) throw new Error(`No outputDir found for test "${testId}" - onBegin must be called first and test must exist in suite`);
425
439
  return outputDir;
426
440
  }
441
+ async sendTestJsonToTraceApi(params) {
442
+ const { test, result, traceId, outputDir, relativeFilePath, startTime, computedDuration } = params;
443
+ const titlePath = test.titlePath();
444
+ const describes = titlePath.length > 4 ? titlePath.slice(3, -1) : [];
445
+ const line = test.location?.line ?? 0;
446
+ const startTimeUnixNano = (startTime.getTime() * 1e6).toString();
447
+ const endTimeUnixNano = ((startTime.getTime() + computedDuration) * 1e6).toString();
448
+ const testJson = {
449
+ name: test.title,
450
+ describes,
451
+ file: relativeFilePath,
452
+ line,
453
+ status: result.status,
454
+ traceId,
455
+ startTimeUnixNano,
456
+ endTimeUnixNano
457
+ };
458
+ const testJsonUrl = `${this.resolvedTraceApiEndpoint}/otel-playwright-reporter/test.json`;
459
+ await fetch(testJsonUrl, {
460
+ method: "PUT",
461
+ headers: {
462
+ "content-type": "application/json",
463
+ "x-trace-id": traceId,
464
+ ...this.resolvedTraceApiHeaders
465
+ },
466
+ body: JSON.stringify(testJson)
467
+ });
468
+ const screenshotsDir = path.join(outputDir, PW_OTEL_DIR, test.id, "screenshots");
469
+ if (existsSync(screenshotsDir)) {
470
+ const screenshotFiles = (await readdir(screenshotsDir)).filter((file) => file.endsWith(".jpeg") || file.endsWith(".jpg"));
471
+ await Promise.all(screenshotFiles.map(async (file) => {
472
+ const buffer = await readFile(path.join(screenshotsDir, file));
473
+ const screenshotUrl = `${this.resolvedTraceApiEndpoint}/otel-playwright-reporter/screenshots/${file}`;
474
+ await fetch(screenshotUrl, {
475
+ method: "PUT",
476
+ headers: {
477
+ "content-type": "image/jpeg",
478
+ "x-trace-id": traceId,
479
+ ...this.resolvedTraceApiHeaders
480
+ },
481
+ body: buffer
482
+ });
483
+ }));
484
+ }
485
+ }
427
486
  };
428
487
  function parseOtlpHeaders(headersString) {
429
488
  const headers = {};
@@ -1,2 +1,2 @@
1
- import { n as PlaywrightOpentelemetryReporterOptions, t as PlaywrightOpentelemetryReporter } from "./index-DnwmzWGb.mjs";
1
+ import { n as PlaywrightOpentelemetryReporterOptions, t as PlaywrightOpentelemetryReporter } from "./index-Di44jFGW.mjs";
2
2
  export { PlaywrightOpentelemetryReporterOptions, PlaywrightOpentelemetryReporter as default };
package/dist/reporter.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as PlaywrightOpentelemetryReporter } from "./reporter-Bg93NdnD.mjs";
1
+ import { t as PlaywrightOpentelemetryReporter } from "./reporter-B_dhs9E9.mjs";
2
2
 
3
3
  //#region src/reporter/index.ts
4
4
  var reporter_default = PlaywrightOpentelemetryReporter;
@@ -1,6 +1,6 @@
1
1
  import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
- import path from "node:path";
3
2
  import fs from "node:fs/promises";
3
+ import path from "node:path";
4
4
 
5
5
  //#region src/shared/trace-files.ts
6
6
  const PW_OTEL_DIR = "playwright-opentelemetry";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playwright-opentelemetry",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "A reporter for Playwright that can export traces and spans in opentelemetry format.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -44,6 +44,9 @@
44
44
  "peerDependencies": {
45
45
  "@playwright/test": "*"
46
46
  },
47
+ "dependencies": {
48
+ "@zip.js/zip.js": "^2.8.11"
49
+ },
47
50
  "devDependencies": {
48
51
  "@biomejs/biome": "2.3.10",
49
52
  "@playwright/test": "^1.57.0",
@@ -51,9 +54,6 @@
51
54
  "@types/node": "^25.0.3",
52
55
  "tsdown": "^0.18.1"
53
56
  },
54
- "dependencies": {
55
- "@zip.js/zip.js": "^2.8.11"
56
- },
57
57
  "scripts": {
58
58
  "build": "tsdown",
59
59
  "dev": "tsdown --watch",