playwright-opentelemetry 0.18.2 → 0.19.0

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.
@@ -2,7 +2,7 @@ import fs from "node:fs/promises";
2
2
  import path from "node:path";
3
3
  import { BlobReader, BlobWriter, TextWriter, ZipReader, ZipWriter } from "@zip.js/zip.js";
4
4
  //#region package.json
5
- var version = "0.18.2";
5
+ var version = "0.19.0";
6
6
  const PLAYWRIGHT_TRACE_API_SOURCE_HEADER = "x-playwright-otel-source";
7
7
  const PLAYWRIGHT_TRACE_API_SOURCE_REPORTER = "reporter";
8
8
  function withPlaywrightTraceApiHeaders(headers) {
@@ -19,6 +19,10 @@ function generateTraceId() {
19
19
  function generateSpanId() {
20
20
  return Array.from({ length: 16 }, () => Math.floor(Math.random() * 16).toString(16)).join("");
21
21
  }
22
+ /** A W3C `traceparent` header for a sampled span. */
23
+ function traceparentHeader(traceId, spanId) {
24
+ return `00-${traceId}-${spanId}-01`;
25
+ }
22
26
  function parseOtlpHeaders(headersString) {
23
27
  const headers = {};
24
28
  if (!headersString) return headers;
@@ -179,6 +183,7 @@ function fixtureHandoffRoot(projectOutputDir) {
179
183
  const TRACE_CONTEXT_HANDOFF_FILE = "trace-context.json";
180
184
  const FIXTURE_SPANS_HANDOFF_FILE = "fixture-spans.json";
181
185
  const SCREENSHOTS_HANDOFF_FILE = "screenshots.zip";
186
+ const API_REQUESTS_HANDOFF_FILE = "api-requests.json";
182
187
  const TRACE_ID_PATTERN = /^[0-9a-f]{32}$/i;
183
188
  const SPAN_ID_PATTERN = /^[0-9a-f]{16}$/i;
184
189
  function isTraceContextHandoff(value) {
@@ -284,8 +289,8 @@ async function createTestTraceContext(testInfo, config) {
284
289
  }
285
290
  /**
286
291
  * Writes the expected-trace marker that lets the Trace API admit spans from the
287
- * app under test. Only browser requests carry the test's traceparent, so the
288
- * marker is skipped when propagation is off; the fixture's own spans are
292
+ * app under test. Requests only carry the test's traceparent with propagation
293
+ * on, so the marker is skipped when it is off; the fixture's own spans are
289
294
  * trusted and stored without it. Never rejects, so callers can start it
290
295
  * without awaiting.
291
296
  */
@@ -427,6 +432,57 @@ function getConfigurationErrorMessage() {
427
432
  return "playwright-opentelemetry reporter requires an OTLP endpoint, trace API endpoint, or storeTraceZip to be configured.\n\nYou can configure it using environment variables:\n\n export OTEL_EXPORTER_OTLP_ENDPOINT=\"https://api.honeycomb.io\"\n export OTEL_EXPORTER_OTLP_HEADERS=\"x-honeycomb-team=YOUR_API_KEY\"\n\nOr via playwright.config.ts:\n\nimport { defineConfig } from '@playwright/test';\nimport type { PlaywrightOpentelemetryUseOptions } from 'playwright-opentelemetry/fixture';\n\nexport default defineConfig<PlaywrightOpentelemetryUseOptions>({\n use: {\n playwrightOpentelemetry: {\n otlpEndpoint: {\n url: 'http://localhost:4317/v1/traces',\n headers: {\n Authorization: 'Bearer YOUR_TOKEN',\n },\n },\n },\n },\n reporter: [['playwright-opentelemetry/reporter']],\n});\n\nNote: Environment variables take precedence over use.playwrightOpentelemetry options.\n";
428
433
  }
429
434
  //#endregion
435
+ //#region src/shared/api-requests.ts
436
+ /**
437
+ * Calls the test made on Playwright's `APIRequestContext`s (`request`,
438
+ * `page.request`, `context.request`, `playwright.request.newContext()`). The
439
+ * fixture records what Playwright's step for the call doesn't have, the
440
+ * response, and hands it to the reporter, which adds it to that step's span.
441
+ */
442
+ const API_REQUESTS_ATTACHMENT_NAME = "playwright-opentelemetry-api-requests";
443
+ const RECORDED_RESPONSE_HEADERS = ["content-type", "cache-control"];
444
+ function parseApiRequestsHandoff(body) {
445
+ const parsed = JSON.parse(body);
446
+ if (!Array.isArray(parsed?.requests)) throw new Error("expected {requests: [...]}");
447
+ return parsed.requests.filter((request) => typeof request?.method === "string" && typeof request.url === "string" && typeof request.startTime === "string" && !Number.isNaN(new Date(request.startTime).getTime()));
448
+ }
449
+ /** HTTP client span attributes for a call on an `APIRequestContext`. */
450
+ function apiRequestHttpAttributes({ method, url, statusCode, responseHeaders, errorType }) {
451
+ const attributes = {
452
+ "http.request.method": method,
453
+ "http.resource.type": "fetch",
454
+ "playwright.request.api": true,
455
+ "url.full": url
456
+ };
457
+ try {
458
+ const parsedUrl = new URL(url);
459
+ attributes["url.path"] = parsedUrl.pathname;
460
+ attributes["server.address"] = parsedUrl.hostname;
461
+ attributes["server.port"] = serverPort(parsedUrl);
462
+ if (parsedUrl.search) attributes["url.query"] = parsedUrl.search.slice(1);
463
+ } catch {}
464
+ if (statusCode !== void 0) {
465
+ attributes["http.response.status_code"] = statusCode;
466
+ if (statusCode >= 400) attributes["error.type"] = statusCode.toString();
467
+ } else if (errorType !== void 0) attributes["error.type"] = errorType;
468
+ const contentType = responseHeaders?.["content-type"];
469
+ const cacheControl = responseHeaders?.["cache-control"];
470
+ if (contentType) attributes["http.response.header.content-type"] = [contentType];
471
+ if (cacheControl) attributes["http.response.header.cache-control"] = [cacheControl];
472
+ return attributes;
473
+ }
474
+ function serverPort(url) {
475
+ if (url.port) return Number.parseInt(url.port, 10);
476
+ return url.protocol === "https:" ? 443 : 80;
477
+ }
478
+ /**
479
+ * The file name the fixture gives its wrapper around `APIRequestContext`
480
+ * methods. Playwright leaves frames of Node's own modules out of the stack it
481
+ * takes a step's location from, so the step for a wrapped call points at the
482
+ * test's line, not at the wrapper.
483
+ */
484
+ const API_REQUEST_WRAPPER_FILE = "node:playwright-opentelemetry/api-request";
485
+ //#endregion
430
486
  //#region src/shared/screencast-screenshots.ts
431
487
  /**
432
488
  * The first Playwright version whose screencast frames carry the browser's
@@ -525,4 +581,4 @@ function viewportOf(screenshot) {
525
581
  } : {};
526
582
  }
527
583
  //#endregion
528
- export { PLAYWRIGHT_TRACE_API_SOURCE_REPORTER as C, sendSpans as D, generateTraceId as E, withPlaywrightTraceApiHeaders as O, PLAYWRIGHT_TRACE_API_SOURCE_HEADER as S, generateSpanId as T, TRACE_CONTEXT_HANDOFF_FILE as _, supportsScreencastScreenshots as a, writeFixtureHandoffFile as b, FIXTURE_SPANS_ATTACHMENT_NAME as c, flushFixtureSpans as d, registerExpectedTrace as f, SCREENSHOTS_HANDOFF_FILE as g, FIXTURE_SPANS_HANDOFF_FILE as h, readScreenshotsZip as i, TRACE_CONTEXT_ATTACHMENT_NAME as l, shouldRetainPlaywrightTrace as m, addScreenshotsToZip as n, resolvePlaywrightOpentelemetryConfig as o, couldRetainPlaywrightTrace as p, createScreenshotsZip as r, BROWSER_SERVICE_NAME as s, SCREENSHOTS_ATTACHMENT_NAME as t, createTestTraceContext as u, fixtureHandoffDir as v, buildOtlpRequest as w, filterRetainedDestinations as x, fixtureHandoffRoot as y };
584
+ export { buildOtlpRequest as A, TRACE_CONTEXT_HANDOFF_FILE as C, filterRetainedDestinations as D, writeFixtureHandoffFile as E, withPlaywrightTraceApiHeaders as F, generateTraceId as M, sendSpans as N, PLAYWRIGHT_TRACE_API_SOURCE_HEADER as O, traceparentHeader as P, SCREENSHOTS_HANDOFF_FILE as S, fixtureHandoffRoot as T, registerExpectedTrace as _, supportsScreencastScreenshots as a, API_REQUESTS_HANDOFF_FILE as b, RECORDED_RESPONSE_HEADERS as c, resolvePlaywrightOpentelemetryConfig as d, BROWSER_SERVICE_NAME as f, flushFixtureSpans as g, createTestTraceContext as h, readScreenshotsZip as i, generateSpanId as j, PLAYWRIGHT_TRACE_API_SOURCE_REPORTER as k, apiRequestHttpAttributes as l, TRACE_CONTEXT_ATTACHMENT_NAME as m, addScreenshotsToZip as n, API_REQUESTS_ATTACHMENT_NAME as o, FIXTURE_SPANS_ATTACHMENT_NAME as p, createScreenshotsZip as r, API_REQUEST_WRAPPER_FILE as s, SCREENSHOTS_ATTACHMENT_NAME as t, parseApiRequestsHandoff as u, couldRetainPlaywrightTrace as v, fixtureHandoffDir as w, FIXTURE_SPANS_HANDOFF_FILE as x, shouldRetainPlaywrightTrace as y };
@@ -26,7 +26,7 @@ let node_path = require("node:path");
26
26
  node_path = __toESM(node_path, 1);
27
27
  let _zip_js_zip_js = require("@zip.js/zip.js");
28
28
  //#region package.json
29
- var version = "0.18.2";
29
+ var version = "0.19.0";
30
30
  const PLAYWRIGHT_TRACE_API_SOURCE_HEADER = "x-playwright-otel-source";
31
31
  const PLAYWRIGHT_TRACE_API_SOURCE_REPORTER = "reporter";
32
32
  function withPlaywrightTraceApiHeaders(headers) {
@@ -43,6 +43,10 @@ function generateTraceId() {
43
43
  function generateSpanId() {
44
44
  return Array.from({ length: 16 }, () => Math.floor(Math.random() * 16).toString(16)).join("");
45
45
  }
46
+ /** A W3C `traceparent` header for a sampled span. */
47
+ function traceparentHeader(traceId, spanId) {
48
+ return `00-${traceId}-${spanId}-01`;
49
+ }
46
50
  function parseOtlpHeaders(headersString) {
47
51
  const headers = {};
48
52
  if (!headersString) return headers;
@@ -203,6 +207,7 @@ function fixtureHandoffRoot(projectOutputDir) {
203
207
  const TRACE_CONTEXT_HANDOFF_FILE = "trace-context.json";
204
208
  const FIXTURE_SPANS_HANDOFF_FILE = "fixture-spans.json";
205
209
  const SCREENSHOTS_HANDOFF_FILE = "screenshots.zip";
210
+ const API_REQUESTS_HANDOFF_FILE = "api-requests.json";
206
211
  const TRACE_ID_PATTERN = /^[0-9a-f]{32}$/i;
207
212
  const SPAN_ID_PATTERN = /^[0-9a-f]{16}$/i;
208
213
  function isTraceContextHandoff(value) {
@@ -308,8 +313,8 @@ async function createTestTraceContext(testInfo, config) {
308
313
  }
309
314
  /**
310
315
  * Writes the expected-trace marker that lets the Trace API admit spans from the
311
- * app under test. Only browser requests carry the test's traceparent, so the
312
- * marker is skipped when propagation is off; the fixture's own spans are
316
+ * app under test. Requests only carry the test's traceparent with propagation
317
+ * on, so the marker is skipped when it is off; the fixture's own spans are
313
318
  * trusted and stored without it. Never rejects, so callers can start it
314
319
  * without awaiting.
315
320
  */
@@ -451,6 +456,57 @@ function getConfigurationErrorMessage() {
451
456
  return "playwright-opentelemetry reporter requires an OTLP endpoint, trace API endpoint, or storeTraceZip to be configured.\n\nYou can configure it using environment variables:\n\n export OTEL_EXPORTER_OTLP_ENDPOINT=\"https://api.honeycomb.io\"\n export OTEL_EXPORTER_OTLP_HEADERS=\"x-honeycomb-team=YOUR_API_KEY\"\n\nOr via playwright.config.ts:\n\nimport { defineConfig } from '@playwright/test';\nimport type { PlaywrightOpentelemetryUseOptions } from 'playwright-opentelemetry/fixture';\n\nexport default defineConfig<PlaywrightOpentelemetryUseOptions>({\n use: {\n playwrightOpentelemetry: {\n otlpEndpoint: {\n url: 'http://localhost:4317/v1/traces',\n headers: {\n Authorization: 'Bearer YOUR_TOKEN',\n },\n },\n },\n },\n reporter: [['playwright-opentelemetry/reporter']],\n});\n\nNote: Environment variables take precedence over use.playwrightOpentelemetry options.\n";
452
457
  }
453
458
  //#endregion
459
+ //#region src/shared/api-requests.ts
460
+ /**
461
+ * Calls the test made on Playwright's `APIRequestContext`s (`request`,
462
+ * `page.request`, `context.request`, `playwright.request.newContext()`). The
463
+ * fixture records what Playwright's step for the call doesn't have, the
464
+ * response, and hands it to the reporter, which adds it to that step's span.
465
+ */
466
+ const API_REQUESTS_ATTACHMENT_NAME = "playwright-opentelemetry-api-requests";
467
+ const RECORDED_RESPONSE_HEADERS = ["content-type", "cache-control"];
468
+ function parseApiRequestsHandoff(body) {
469
+ const parsed = JSON.parse(body);
470
+ if (!Array.isArray(parsed?.requests)) throw new Error("expected {requests: [...]}");
471
+ return parsed.requests.filter((request) => typeof request?.method === "string" && typeof request.url === "string" && typeof request.startTime === "string" && !Number.isNaN(new Date(request.startTime).getTime()));
472
+ }
473
+ /** HTTP client span attributes for a call on an `APIRequestContext`. */
474
+ function apiRequestHttpAttributes({ method, url, statusCode, responseHeaders, errorType }) {
475
+ const attributes = {
476
+ "http.request.method": method,
477
+ "http.resource.type": "fetch",
478
+ "playwright.request.api": true,
479
+ "url.full": url
480
+ };
481
+ try {
482
+ const parsedUrl = new URL(url);
483
+ attributes["url.path"] = parsedUrl.pathname;
484
+ attributes["server.address"] = parsedUrl.hostname;
485
+ attributes["server.port"] = serverPort(parsedUrl);
486
+ if (parsedUrl.search) attributes["url.query"] = parsedUrl.search.slice(1);
487
+ } catch {}
488
+ if (statusCode !== void 0) {
489
+ attributes["http.response.status_code"] = statusCode;
490
+ if (statusCode >= 400) attributes["error.type"] = statusCode.toString();
491
+ } else if (errorType !== void 0) attributes["error.type"] = errorType;
492
+ const contentType = responseHeaders?.["content-type"];
493
+ const cacheControl = responseHeaders?.["cache-control"];
494
+ if (contentType) attributes["http.response.header.content-type"] = [contentType];
495
+ if (cacheControl) attributes["http.response.header.cache-control"] = [cacheControl];
496
+ return attributes;
497
+ }
498
+ function serverPort(url) {
499
+ if (url.port) return Number.parseInt(url.port, 10);
500
+ return url.protocol === "https:" ? 443 : 80;
501
+ }
502
+ /**
503
+ * The file name the fixture gives its wrapper around `APIRequestContext`
504
+ * methods. Playwright leaves frames of Node's own modules out of the stack it
505
+ * takes a step's location from, so the step for a wrapped call points at the
506
+ * test's line, not at the wrapper.
507
+ */
508
+ const API_REQUEST_WRAPPER_FILE = "node:playwright-opentelemetry/api-request";
509
+ //#endregion
454
510
  //#region src/shared/screencast-screenshots.ts
455
511
  /**
456
512
  * The first Playwright version whose screencast frames carry the browser's
@@ -549,6 +605,24 @@ function viewportOf(screenshot) {
549
605
  } : {};
550
606
  }
551
607
  //#endregion
608
+ Object.defineProperty(exports, "API_REQUESTS_ATTACHMENT_NAME", {
609
+ enumerable: true,
610
+ get: function() {
611
+ return API_REQUESTS_ATTACHMENT_NAME;
612
+ }
613
+ });
614
+ Object.defineProperty(exports, "API_REQUESTS_HANDOFF_FILE", {
615
+ enumerable: true,
616
+ get: function() {
617
+ return API_REQUESTS_HANDOFF_FILE;
618
+ }
619
+ });
620
+ Object.defineProperty(exports, "API_REQUEST_WRAPPER_FILE", {
621
+ enumerable: true,
622
+ get: function() {
623
+ return API_REQUEST_WRAPPER_FILE;
624
+ }
625
+ });
552
626
  Object.defineProperty(exports, "BROWSER_SERVICE_NAME", {
553
627
  enumerable: true,
554
628
  get: function() {
@@ -579,6 +653,12 @@ Object.defineProperty(exports, "PLAYWRIGHT_TRACE_API_SOURCE_REPORTER", {
579
653
  return PLAYWRIGHT_TRACE_API_SOURCE_REPORTER;
580
654
  }
581
655
  });
656
+ Object.defineProperty(exports, "RECORDED_RESPONSE_HEADERS", {
657
+ enumerable: true,
658
+ get: function() {
659
+ return RECORDED_RESPONSE_HEADERS;
660
+ }
661
+ });
582
662
  Object.defineProperty(exports, "SCREENSHOTS_ATTACHMENT_NAME", {
583
663
  enumerable: true,
584
664
  get: function() {
@@ -615,6 +695,12 @@ Object.defineProperty(exports, "addScreenshotsToZip", {
615
695
  return addScreenshotsToZip;
616
696
  }
617
697
  });
698
+ Object.defineProperty(exports, "apiRequestHttpAttributes", {
699
+ enumerable: true,
700
+ get: function() {
701
+ return apiRequestHttpAttributes;
702
+ }
703
+ });
618
704
  Object.defineProperty(exports, "buildOtlpRequest", {
619
705
  enumerable: true,
620
706
  get: function() {
@@ -675,6 +761,12 @@ Object.defineProperty(exports, "generateTraceId", {
675
761
  return generateTraceId;
676
762
  }
677
763
  });
764
+ Object.defineProperty(exports, "parseApiRequestsHandoff", {
765
+ enumerable: true,
766
+ get: function() {
767
+ return parseApiRequestsHandoff;
768
+ }
769
+ });
678
770
  Object.defineProperty(exports, "readScreenshotsZip", {
679
771
  enumerable: true,
680
772
  get: function() {
@@ -711,6 +803,12 @@ Object.defineProperty(exports, "supportsScreencastScreenshots", {
711
803
  return supportsScreencastScreenshots;
712
804
  }
713
805
  });
806
+ Object.defineProperty(exports, "traceparentHeader", {
807
+ enumerable: true,
808
+ get: function() {
809
+ return traceparentHeader;
810
+ }
811
+ });
714
812
  Object.defineProperty(exports, "withPlaywrightTraceApiHeaders", {
715
813
  enumerable: true,
716
814
  get: function() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playwright-opentelemetry",
3
- "version": "0.18.2",
3
+ "version": "0.19.0",
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",
@@ -1,25 +0,0 @@
1
- //#region src/shared/otel.d.ts
2
- type SpanAttributeValue = string | number | boolean | string[];
3
- type SpanEvent = {
4
- name: string;
5
- time: Date;
6
- attributes?: Record<string, SpanAttributeValue>;
7
- };
8
- type Span = {
9
- traceId: string;
10
- spanId: string;
11
- parentSpanId?: string;
12
- name: string;
13
- startTime: Date;
14
- endTime: Date;
15
- attributes: Record<string, SpanAttributeValue>;
16
- events?: SpanEvent[];
17
- status?: {
18
- code: number;
19
- message?: string;
20
- };
21
- kind?: number; /** Service name for this span (if different from default). */
22
- serviceName?: string;
23
- };
24
- //#endregion
25
- export { SpanEvent as n, Span as t };
@@ -1,25 +0,0 @@
1
- //#region src/shared/otel.d.ts
2
- type SpanAttributeValue = string | number | boolean | string[];
3
- type SpanEvent = {
4
- name: string;
5
- time: Date;
6
- attributes?: Record<string, SpanAttributeValue>;
7
- };
8
- type Span = {
9
- traceId: string;
10
- spanId: string;
11
- parentSpanId?: string;
12
- name: string;
13
- startTime: Date;
14
- endTime: Date;
15
- attributes: Record<string, SpanAttributeValue>;
16
- events?: SpanEvent[];
17
- status?: {
18
- code: number;
19
- message?: string;
20
- };
21
- kind?: number; /** Service name for this span (if different from default). */
22
- serviceName?: string;
23
- };
24
- //#endregion
25
- export { SpanEvent as n, Span as t };