playwright-opentelemetry 0.18.2 → 0.19.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.
@@ -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.1";
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.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",