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.
@@ -1,4 +1,4 @@
1
- import { C as PLAYWRIGHT_TRACE_API_SOURCE_REPORTER, D as sendSpans, E as generateTraceId, O as withPlaywrightTraceApiHeaders, S as PLAYWRIGHT_TRACE_API_SOURCE_HEADER, T as generateSpanId, a as supportsScreencastScreenshots, c as FIXTURE_SPANS_ATTACHMENT_NAME, i as readScreenshotsZip, l as TRACE_CONTEXT_ATTACHMENT_NAME, m as shouldRetainPlaywrightTrace, n as addScreenshotsToZip, o as resolvePlaywrightOpentelemetryConfig, r as createScreenshotsZip, t as SCREENSHOTS_ATTACHMENT_NAME, v as fixtureHandoffDir, w as buildOtlpRequest, x as filterRetainedDestinations, y as fixtureHandoffRoot } from "./screenshots-zip-DnK_MAtf.mjs";
1
+ import { A as buildOtlpRequest, D as filterRetainedDestinations, F as withPlaywrightTraceApiHeaders, M as generateTraceId, N as sendSpans, O as PLAYWRIGHT_TRACE_API_SOURCE_HEADER, T as fixtureHandoffRoot, a as supportsScreencastScreenshots, d as resolvePlaywrightOpentelemetryConfig, i as readScreenshotsZip, j as generateSpanId, k as PLAYWRIGHT_TRACE_API_SOURCE_REPORTER, l as apiRequestHttpAttributes, m as TRACE_CONTEXT_ATTACHMENT_NAME, n as addScreenshotsToZip, o as API_REQUESTS_ATTACHMENT_NAME, p as FIXTURE_SPANS_ATTACHMENT_NAME, r as createScreenshotsZip, t as SCREENSHOTS_ATTACHMENT_NAME, u as parseApiRequestsHandoff, w as fixtureHandoffDir, y as shouldRetainPlaywrightTrace } from "./screenshots-zip-oICwRNl5.mjs";
2
2
  import fsSync from "node:fs";
3
3
  import fs from "node:fs/promises";
4
4
  import path from "node:path";
@@ -24,6 +24,80 @@ const ATTR_TEST_STEP_PARAM_PREFIX = "test.step.param.";
24
24
  const ATTR_TEST_STEP_CATEGORY = "test.step.category";
25
25
  const ATTR_TEST_PHASE = "playwright.test.phase";
26
26
  //#endregion
27
+ //#region src/reporter/api-request-matcher.ts
28
+ const HTTP_METHODS = "DELETE|FETCH|GET|HEAD|PATCH|POST|PUT";
29
+ const QUOTED_URL_TITLE = new RegExp(`^(${HTTP_METHODS}|\\{method\\})\\s+"`);
30
+ const API_NAME_TITLE = new RegExp(`^apiRequestContext\\.(${HTTP_METHODS})\\(`, "i");
31
+ const NO_METHOD_TITLE = "{method}";
32
+ /**
33
+ * Recognises the step of an `APIRequestContext` call. From Playwright 1.63
34
+ * such a step reports the call's url and method as params; before that they
35
+ * only show in its title.
36
+ */
37
+ function apiRequestStepCall(step) {
38
+ if (step.category !== "pw:api") return;
39
+ if (step.params) {
40
+ const { url, method } = step.params;
41
+ if (typeof url !== "string") return;
42
+ if (typeof method === "string") return {
43
+ method: method.toUpperCase(),
44
+ url
45
+ };
46
+ return step.title === NO_METHOD_TITLE ? { url } : void 0;
47
+ }
48
+ const method = (QUOTED_URL_TITLE.exec(step.title) ?? API_NAME_TITLE.exec(step.title))?.[1];
49
+ if (!method) return;
50
+ const upperCased = method.toUpperCase();
51
+ return upperCased === "FETCH" || upperCased === NO_METHOD_TITLE.toUpperCase() ? {} : { method: upperCased };
52
+ }
53
+ /**
54
+ * Pairs each request step with the call the fixture recorded for it. The
55
+ * fixture records where the test made the call, which is exactly the step's
56
+ * location, so the nth call from a place belongs to the nth step from that
57
+ * place.
58
+ */
59
+ var ApiRequestMatcher = class {
60
+ rootDir;
61
+ unclaimed;
62
+ /** Whether Playwright located a step in the fixture's wrapper. */
63
+ wrapperWasVisible = false;
64
+ constructor(requests, rootDir) {
65
+ this.rootDir = rootDir;
66
+ this.unclaimed = [...requests];
67
+ }
68
+ take(step, call) {
69
+ const locatedInWrapper = isApiRequestWrapperFile(step.location?.file);
70
+ this.wrapperWasVisible ||= locatedInWrapper;
71
+ const candidates = this.unclaimed.filter((request) => locatedInWrapper || !request.location ? agreesOnMethod(call, request) : this.isSameLocation(step, request.location));
72
+ const match = candidates.find((request) => agreesOnMethod(call, request) && agreesOnUrl(call, request)) ?? candidates.find((request) => agreesOnMethod(call, request)) ?? candidates[0];
73
+ if (match) this.unclaimed.splice(this.unclaimed.indexOf(match), 1);
74
+ return match;
75
+ }
76
+ /** Recorded calls no step reported. */
77
+ unmatched() {
78
+ return [...this.unclaimed];
79
+ }
80
+ isSameLocation(step, location) {
81
+ if (!step.location) return false;
82
+ const stepFile = this.rootDir ? path.relative(this.rootDir, step.location.file) : step.location.file;
83
+ return step.location.line === location.line && step.location.column === location.column && withForwardSlashes(stepFile) === withForwardSlashes(location.file);
84
+ }
85
+ };
86
+ function isApiRequestWrapperFile(file) {
87
+ return file !== void 0 && file.includes("node:playwright-opentelemetry/api-request");
88
+ }
89
+ function agreesOnMethod(call, request) {
90
+ return call.method === void 0 || call.method === request.method;
91
+ }
92
+ const CUT_SHORT = "…";
93
+ function agreesOnUrl(call, request) {
94
+ if (call.url === void 0) return true;
95
+ return call.url.endsWith(CUT_SHORT) ? request.url.startsWith(call.url.slice(0, -1)) : call.url === request.url;
96
+ }
97
+ function withForwardSlashes(file) {
98
+ return file.replace(/\\/g, "/");
99
+ }
100
+ //#endregion
27
101
  //#region src/reporter/legacy/trace-zip-screenshots.ts
28
102
  /**
29
103
  * LEGACY: reads screenshots out of Playwright's trace.zip, for Playwright
@@ -180,6 +254,7 @@ var PlaywrightOpentelemetryReporter = class {
180
254
  fixtureHandoffRoots = /* @__PURE__ */ new Set();
181
255
  rootDir;
182
256
  playwrightVersion;
257
+ warnedAboutVisibleWrapper = false;
183
258
  debug = false;
184
259
  constructor() {}
185
260
  onBegin(config, _suite) {
@@ -235,7 +310,16 @@ var PlaywrightOpentelemetryReporter = class {
235
310
  attributes[ATTR_CODE_COLUMN_NUMBER] = column;
236
311
  }
237
312
  const stepSpans = [];
238
- for (const step of result.steps ?? []) this.processTestStep(step, testSpanId, traceId, [], stepSpans);
313
+ const apiRequests = new ApiRequestMatcher(parseApiRequests(fixtureData.apiRequests, testId), this.rootDir);
314
+ for (const step of result.steps ?? []) this.processTestStep(step, testSpanId, traceId, [], stepSpans, apiRequests);
315
+ for (const request of apiRequests.unmatched()) stepSpans.push(unmatchedApiRequestSpan(request, {
316
+ traceId,
317
+ parentSpanId: testSpanId
318
+ }));
319
+ if (apiRequests.wrapperWasVisible && !this.warnedAboutVisibleWrapper) {
320
+ this.warnedAboutVisibleWrapper = true;
321
+ console.warn(`Playwright ${this.playwrightVersion ?? ""} reports playwright-opentelemetry's request wrapper as the location of request steps. Traces still have the right locations, but Playwright's own reports don't. Please report this to playwright-opentelemetry.`);
322
+ }
239
323
  let minStartTime = result.startTime;
240
324
  let maxEndTime = new Date(result.startTime.getTime() + result.duration);
241
325
  for (const stepSpan of stepSpans) {
@@ -328,6 +412,7 @@ var PlaywrightOpentelemetryReporter = class {
328
412
  const attached = {
329
413
  traceContext: attachment(TRACE_CONTEXT_ATTACHMENT_NAME)?.body,
330
414
  fixtureSpans: attachment(FIXTURE_SPANS_ATTACHMENT_NAME)?.body,
415
+ apiRequests: attachment(API_REQUESTS_ATTACHMENT_NAME)?.body,
331
416
  screenshotsZip: screenshotsAttachmentPath ? readFixtureFile(screenshotsAttachmentPath, { required: true }) : void 0
332
417
  };
333
418
  const outputDir = getProjectOutputDir(test);
@@ -338,6 +423,7 @@ var PlaywrightOpentelemetryReporter = class {
338
423
  return {
339
424
  traceContext: attached.traceContext ?? readFixtureFile(path.join(dir, "trace-context.json")),
340
425
  fixtureSpans: attached.fixtureSpans ?? readFixtureFile(path.join(dir, "fixture-spans.json")),
426
+ apiRequests: attached.apiRequests ?? readFixtureFile(path.join(dir, "api-requests.json")),
341
427
  screenshotsZip: attached.screenshotsZip ?? readFixtureFile(path.join(dir, "screenshots.zip"))
342
428
  };
343
429
  } finally {
@@ -365,14 +451,14 @@ var PlaywrightOpentelemetryReporter = class {
365
451
  const [traceZipBlob] = await Promise.all([traceZipBlobPromise, ...uploadPromises]);
366
452
  return { traceZipBlob };
367
453
  }
368
- processTestStep(step, parentSpanId, traceId, parentTitlePath, stepSpans, inheritedPhase) {
454
+ processTestStep(step, parentSpanId, traceId, parentTitlePath, stepSpans, apiRequests, inheritedPhase) {
369
455
  let phase = inheritedPhase;
370
456
  if (step.category === "hook") {
371
457
  if (step.title === "Before Hooks") phase = "before";
372
458
  if (step.title === "After Hooks" || step.title === "Worker Cleanup") phase = "after";
373
459
  }
374
460
  if (isInternalFixtureStep(step) || isInternalApiRequestStep(step)) {
375
- for (const childStep of step.steps ?? []) this.processTestStep(childStep, parentSpanId, traceId, parentTitlePath, stepSpans, phase);
461
+ for (const childStep of step.steps ?? []) this.processTestStep(childStep, parentSpanId, traceId, parentTitlePath, stepSpans, apiRequests, phase);
376
462
  return;
377
463
  }
378
464
  const currentTitlePath = [...parentTitlePath, step.title];
@@ -386,24 +472,35 @@ var PlaywrightOpentelemetryReporter = class {
386
472
  }
387
473
  attributes[ATTR_TEST_STEP_CATEGORY] = step.category;
388
474
  if (phase) attributes[ATTR_TEST_PHASE] = phase;
389
- if (step.location) {
475
+ const apiRequestCall = apiRequestStepCall(step);
476
+ const apiRequest = apiRequestCall ? apiRequests.take(step, apiRequestCall) : void 0;
477
+ if (isApiRequestWrapperFile(step.location?.file)) {
478
+ if (apiRequest?.location) {
479
+ attributes[ATTR_CODE_FILE_PATH] = apiRequest.location.file;
480
+ attributes[ATTR_CODE_LINE_NUMBER] = apiRequest.location.line;
481
+ attributes[ATTR_CODE_COLUMN_NUMBER] = apiRequest.location.column;
482
+ }
483
+ } else if (step.location) {
390
484
  const { file, line, column } = step.location;
391
485
  attributes[ATTR_CODE_FILE_PATH] = this.rootDir ? path.relative(this.rootDir, file) : file;
392
486
  attributes[ATTR_CODE_LINE_NUMBER] = line;
393
487
  attributes[ATTR_CODE_COLUMN_NUMBER] = column;
394
488
  }
489
+ const httpAttributes = apiRequest ? capturedApiRequestAttributes(apiRequest) : apiRequestCall && apiRequestStepAttributes(apiRequestCall);
490
+ Object.assign(attributes, httpAttributes);
395
491
  const stepSpan = {
396
492
  traceId,
397
- spanId: generateSpanId(),
493
+ spanId: sentSpanId(apiRequest) ?? generateSpanId(),
398
494
  parentSpanId,
399
495
  name: TEST_STEP_SPAN_NAME,
400
496
  startTime: step.startTime,
401
497
  endTime: new Date(step.startTime.getTime() + step.duration),
402
498
  attributes
403
499
  };
500
+ if (httpAttributes) stepSpan.kind = SPAN_KIND_CLIENT;
404
501
  if (step.error) stepSpan.status = errorStatus(step.error.message);
405
502
  stepSpans.push(stepSpan);
406
- for (const childStep of step.steps ?? []) this.processTestStep(childStep, stepSpan.spanId, traceId, currentTitlePath, stepSpans, phase);
503
+ for (const childStep of step.steps ?? []) this.processTestStep(childStep, stepSpan.spanId, traceId, currentTitlePath, stepSpans, apiRequests, phase);
407
504
  }
408
505
  onStdOut(chunk, _test, _result) {
409
506
  if (this.debug) console.log(chunk.toString().slice(0, -1));
@@ -578,17 +675,70 @@ function isInternalFixtureStep(step) {
578
675
  if (step.category !== "fixture" || !step.location?.file) return false;
579
676
  return isInternalFixtureFile(step.location.file);
580
677
  }
678
+ /**
679
+ * Earlier fixtures wrapped `request` in a Proxy, which made the fixture the
680
+ * location of each call's step, and recorded the calls as spans of their own.
681
+ * Their data can still arrive through `merge-reports`.
682
+ */
581
683
  function isInternalApiRequestStep(step) {
582
- if (step.category !== "pw:api" || !step.location?.file) return false;
583
- return isApiRequestStep(step) && isInternalFixtureFile(step.location.file);
684
+ if (!step.location?.file) return false;
685
+ return apiRequestStepCall(step) !== void 0 && isInternalFixtureFile(step.location.file);
686
+ }
687
+ const SPAN_KIND_CLIENT = 3;
688
+ function capturedApiRequestAttributes(request) {
689
+ return apiRequestHttpAttributes({
690
+ method: request.method,
691
+ url: request.responseUrl ?? request.url,
692
+ statusCode: request.statusCode,
693
+ responseHeaders: request.responseHeaders,
694
+ errorType: request.errorType
695
+ });
584
696
  }
585
697
  /**
586
- * `APIRequestContext` calls. From Playwright 1.63 they report a url and a
587
- * method; before that they show up only in the title, e.g. `GET "/api"`.
698
+ * For a call the fixture didn't record, such as one made in a `beforeAll`
699
+ * hook: what the step itself tells, from Playwright 1.63. There is no
700
+ * response to report.
588
701
  */
589
- function isApiRequestStep(step) {
590
- if (step.params) return typeof step.params.url === "string" && typeof step.params.method === "string";
591
- return /^(?:DELETE|FETCH|GET|HEAD|PATCH|POST|PUT)\s+"/.test(step.title);
702
+ function apiRequestStepAttributes(call) {
703
+ if (call.url === void 0 || call.url.endsWith("…")) return;
704
+ return apiRequestHttpAttributes({
705
+ method: call.method ?? "GET",
706
+ url: call.url
707
+ });
708
+ }
709
+ function unmatchedApiRequestSpan(request, options) {
710
+ const startTime = new Date(request.startTime);
711
+ const endTime = request.endTime ? new Date(request.endTime) : startTime;
712
+ const attributes = capturedApiRequestAttributes(request);
713
+ if (request.location) {
714
+ attributes[ATTR_CODE_FILE_PATH] = request.location.file;
715
+ attributes[ATTR_CODE_LINE_NUMBER] = request.location.line;
716
+ attributes[ATTR_CODE_COLUMN_NUMBER] = request.location.column;
717
+ }
718
+ const failed = request.errorType !== void 0 || request.statusCode !== void 0 && request.statusCode >= 400;
719
+ return {
720
+ traceId: options.traceId,
721
+ spanId: sentSpanId(request) ?? generateSpanId(),
722
+ parentSpanId: options.parentSpanId,
723
+ name: `HTTP ${request.method}`,
724
+ kind: SPAN_KIND_CLIENT,
725
+ startTime,
726
+ endTime: Number.isNaN(endTime.getTime()) ? startTime : endTime,
727
+ attributes,
728
+ status: { code: failed ? 2 : 0 }
729
+ };
730
+ }
731
+ function sentSpanId(request) {
732
+ return typeof request?.spanId === "string" && /^[0-9a-f]{16}$/.test(request.spanId) ? request.spanId : void 0;
733
+ }
734
+ function parseApiRequests(body, testId) {
735
+ if (!body) return [];
736
+ try {
737
+ return parseApiRequestsHandoff(body.toString("utf-8"));
738
+ } catch (error) {
739
+ console.warn(`Ignoring invalid ${API_REQUESTS_ATTACHMENT_NAME} for test ${testId}: ${errorMessage(error)}`);
740
+ return [];
741
+ }
592
742
  }
593
743
  /**
594
744
  * Span attributes hold primitives and string arrays, so other param values
@@ -1,4 +1,4 @@
1
- const require_screenshots_zip = require("./screenshots-zip-nJZiEIbI.cjs");
1
+ const require_screenshots_zip = require("./screenshots-zip-pitrmbIG.cjs");
2
2
  let node_fs = require("node:fs");
3
3
  node_fs = require_screenshots_zip.__toESM(node_fs, 1);
4
4
  let node_fs_promises = require("node:fs/promises");
@@ -27,6 +27,80 @@ const ATTR_TEST_STEP_PARAM_PREFIX = "test.step.param.";
27
27
  const ATTR_TEST_STEP_CATEGORY = "test.step.category";
28
28
  const ATTR_TEST_PHASE = "playwright.test.phase";
29
29
  //#endregion
30
+ //#region src/reporter/api-request-matcher.ts
31
+ const HTTP_METHODS = "DELETE|FETCH|GET|HEAD|PATCH|POST|PUT";
32
+ const QUOTED_URL_TITLE = new RegExp(`^(${HTTP_METHODS}|\\{method\\})\\s+"`);
33
+ const API_NAME_TITLE = new RegExp(`^apiRequestContext\\.(${HTTP_METHODS})\\(`, "i");
34
+ const NO_METHOD_TITLE = "{method}";
35
+ /**
36
+ * Recognises the step of an `APIRequestContext` call. From Playwright 1.63
37
+ * such a step reports the call's url and method as params; before that they
38
+ * only show in its title.
39
+ */
40
+ function apiRequestStepCall(step) {
41
+ if (step.category !== "pw:api") return;
42
+ if (step.params) {
43
+ const { url, method } = step.params;
44
+ if (typeof url !== "string") return;
45
+ if (typeof method === "string") return {
46
+ method: method.toUpperCase(),
47
+ url
48
+ };
49
+ return step.title === NO_METHOD_TITLE ? { url } : void 0;
50
+ }
51
+ const method = (QUOTED_URL_TITLE.exec(step.title) ?? API_NAME_TITLE.exec(step.title))?.[1];
52
+ if (!method) return;
53
+ const upperCased = method.toUpperCase();
54
+ return upperCased === "FETCH" || upperCased === NO_METHOD_TITLE.toUpperCase() ? {} : { method: upperCased };
55
+ }
56
+ /**
57
+ * Pairs each request step with the call the fixture recorded for it. The
58
+ * fixture records where the test made the call, which is exactly the step's
59
+ * location, so the nth call from a place belongs to the nth step from that
60
+ * place.
61
+ */
62
+ var ApiRequestMatcher = class {
63
+ rootDir;
64
+ unclaimed;
65
+ /** Whether Playwright located a step in the fixture's wrapper. */
66
+ wrapperWasVisible = false;
67
+ constructor(requests, rootDir) {
68
+ this.rootDir = rootDir;
69
+ this.unclaimed = [...requests];
70
+ }
71
+ take(step, call) {
72
+ const locatedInWrapper = isApiRequestWrapperFile(step.location?.file);
73
+ this.wrapperWasVisible ||= locatedInWrapper;
74
+ const candidates = this.unclaimed.filter((request) => locatedInWrapper || !request.location ? agreesOnMethod(call, request) : this.isSameLocation(step, request.location));
75
+ const match = candidates.find((request) => agreesOnMethod(call, request) && agreesOnUrl(call, request)) ?? candidates.find((request) => agreesOnMethod(call, request)) ?? candidates[0];
76
+ if (match) this.unclaimed.splice(this.unclaimed.indexOf(match), 1);
77
+ return match;
78
+ }
79
+ /** Recorded calls no step reported. */
80
+ unmatched() {
81
+ return [...this.unclaimed];
82
+ }
83
+ isSameLocation(step, location) {
84
+ if (!step.location) return false;
85
+ const stepFile = this.rootDir ? node_path.default.relative(this.rootDir, step.location.file) : step.location.file;
86
+ return step.location.line === location.line && step.location.column === location.column && withForwardSlashes(stepFile) === withForwardSlashes(location.file);
87
+ }
88
+ };
89
+ function isApiRequestWrapperFile(file) {
90
+ return file !== void 0 && file.includes("node:playwright-opentelemetry/api-request");
91
+ }
92
+ function agreesOnMethod(call, request) {
93
+ return call.method === void 0 || call.method === request.method;
94
+ }
95
+ const CUT_SHORT = "…";
96
+ function agreesOnUrl(call, request) {
97
+ if (call.url === void 0) return true;
98
+ return call.url.endsWith(CUT_SHORT) ? request.url.startsWith(call.url.slice(0, -1)) : call.url === request.url;
99
+ }
100
+ function withForwardSlashes(file) {
101
+ return file.replace(/\\/g, "/");
102
+ }
103
+ //#endregion
30
104
  //#region src/reporter/legacy/trace-zip-screenshots.ts
31
105
  /**
32
106
  * LEGACY: reads screenshots out of Playwright's trace.zip, for Playwright
@@ -183,6 +257,7 @@ var PlaywrightOpentelemetryReporter = class {
183
257
  fixtureHandoffRoots = /* @__PURE__ */ new Set();
184
258
  rootDir;
185
259
  playwrightVersion;
260
+ warnedAboutVisibleWrapper = false;
186
261
  debug = false;
187
262
  constructor() {}
188
263
  onBegin(config, _suite) {
@@ -238,7 +313,16 @@ var PlaywrightOpentelemetryReporter = class {
238
313
  attributes[ATTR_CODE_COLUMN_NUMBER] = column;
239
314
  }
240
315
  const stepSpans = [];
241
- for (const step of result.steps ?? []) this.processTestStep(step, testSpanId, traceId, [], stepSpans);
316
+ const apiRequests = new ApiRequestMatcher(parseApiRequests(fixtureData.apiRequests, testId), this.rootDir);
317
+ for (const step of result.steps ?? []) this.processTestStep(step, testSpanId, traceId, [], stepSpans, apiRequests);
318
+ for (const request of apiRequests.unmatched()) stepSpans.push(unmatchedApiRequestSpan(request, {
319
+ traceId,
320
+ parentSpanId: testSpanId
321
+ }));
322
+ if (apiRequests.wrapperWasVisible && !this.warnedAboutVisibleWrapper) {
323
+ this.warnedAboutVisibleWrapper = true;
324
+ console.warn(`Playwright ${this.playwrightVersion ?? ""} reports playwright-opentelemetry's request wrapper as the location of request steps. Traces still have the right locations, but Playwright's own reports don't. Please report this to playwright-opentelemetry.`);
325
+ }
242
326
  let minStartTime = result.startTime;
243
327
  let maxEndTime = new Date(result.startTime.getTime() + result.duration);
244
328
  for (const stepSpan of stepSpans) {
@@ -331,6 +415,7 @@ var PlaywrightOpentelemetryReporter = class {
331
415
  const attached = {
332
416
  traceContext: attachment(require_screenshots_zip.TRACE_CONTEXT_ATTACHMENT_NAME)?.body,
333
417
  fixtureSpans: attachment(require_screenshots_zip.FIXTURE_SPANS_ATTACHMENT_NAME)?.body,
418
+ apiRequests: attachment(require_screenshots_zip.API_REQUESTS_ATTACHMENT_NAME)?.body,
334
419
  screenshotsZip: screenshotsAttachmentPath ? readFixtureFile(screenshotsAttachmentPath, { required: true }) : void 0
335
420
  };
336
421
  const outputDir = getProjectOutputDir(test);
@@ -341,6 +426,7 @@ var PlaywrightOpentelemetryReporter = class {
341
426
  return {
342
427
  traceContext: attached.traceContext ?? readFixtureFile(node_path.default.join(dir, "trace-context.json")),
343
428
  fixtureSpans: attached.fixtureSpans ?? readFixtureFile(node_path.default.join(dir, "fixture-spans.json")),
429
+ apiRequests: attached.apiRequests ?? readFixtureFile(node_path.default.join(dir, "api-requests.json")),
344
430
  screenshotsZip: attached.screenshotsZip ?? readFixtureFile(node_path.default.join(dir, "screenshots.zip"))
345
431
  };
346
432
  } finally {
@@ -368,14 +454,14 @@ var PlaywrightOpentelemetryReporter = class {
368
454
  const [traceZipBlob] = await Promise.all([traceZipBlobPromise, ...uploadPromises]);
369
455
  return { traceZipBlob };
370
456
  }
371
- processTestStep(step, parentSpanId, traceId, parentTitlePath, stepSpans, inheritedPhase) {
457
+ processTestStep(step, parentSpanId, traceId, parentTitlePath, stepSpans, apiRequests, inheritedPhase) {
372
458
  let phase = inheritedPhase;
373
459
  if (step.category === "hook") {
374
460
  if (step.title === "Before Hooks") phase = "before";
375
461
  if (step.title === "After Hooks" || step.title === "Worker Cleanup") phase = "after";
376
462
  }
377
463
  if (isInternalFixtureStep(step) || isInternalApiRequestStep(step)) {
378
- for (const childStep of step.steps ?? []) this.processTestStep(childStep, parentSpanId, traceId, parentTitlePath, stepSpans, phase);
464
+ for (const childStep of step.steps ?? []) this.processTestStep(childStep, parentSpanId, traceId, parentTitlePath, stepSpans, apiRequests, phase);
379
465
  return;
380
466
  }
381
467
  const currentTitlePath = [...parentTitlePath, step.title];
@@ -389,24 +475,35 @@ var PlaywrightOpentelemetryReporter = class {
389
475
  }
390
476
  attributes[ATTR_TEST_STEP_CATEGORY] = step.category;
391
477
  if (phase) attributes[ATTR_TEST_PHASE] = phase;
392
- if (step.location) {
478
+ const apiRequestCall = apiRequestStepCall(step);
479
+ const apiRequest = apiRequestCall ? apiRequests.take(step, apiRequestCall) : void 0;
480
+ if (isApiRequestWrapperFile(step.location?.file)) {
481
+ if (apiRequest?.location) {
482
+ attributes[ATTR_CODE_FILE_PATH] = apiRequest.location.file;
483
+ attributes[ATTR_CODE_LINE_NUMBER] = apiRequest.location.line;
484
+ attributes[ATTR_CODE_COLUMN_NUMBER] = apiRequest.location.column;
485
+ }
486
+ } else if (step.location) {
393
487
  const { file, line, column } = step.location;
394
488
  attributes[ATTR_CODE_FILE_PATH] = this.rootDir ? node_path.default.relative(this.rootDir, file) : file;
395
489
  attributes[ATTR_CODE_LINE_NUMBER] = line;
396
490
  attributes[ATTR_CODE_COLUMN_NUMBER] = column;
397
491
  }
492
+ const httpAttributes = apiRequest ? capturedApiRequestAttributes(apiRequest) : apiRequestCall && apiRequestStepAttributes(apiRequestCall);
493
+ Object.assign(attributes, httpAttributes);
398
494
  const stepSpan = {
399
495
  traceId,
400
- spanId: require_screenshots_zip.generateSpanId(),
496
+ spanId: sentSpanId(apiRequest) ?? require_screenshots_zip.generateSpanId(),
401
497
  parentSpanId,
402
498
  name: TEST_STEP_SPAN_NAME,
403
499
  startTime: step.startTime,
404
500
  endTime: new Date(step.startTime.getTime() + step.duration),
405
501
  attributes
406
502
  };
503
+ if (httpAttributes) stepSpan.kind = SPAN_KIND_CLIENT;
407
504
  if (step.error) stepSpan.status = errorStatus(step.error.message);
408
505
  stepSpans.push(stepSpan);
409
- for (const childStep of step.steps ?? []) this.processTestStep(childStep, stepSpan.spanId, traceId, currentTitlePath, stepSpans, phase);
506
+ for (const childStep of step.steps ?? []) this.processTestStep(childStep, stepSpan.spanId, traceId, currentTitlePath, stepSpans, apiRequests, phase);
410
507
  }
411
508
  onStdOut(chunk, _test, _result) {
412
509
  if (this.debug) console.log(chunk.toString().slice(0, -1));
@@ -581,17 +678,70 @@ function isInternalFixtureStep(step) {
581
678
  if (step.category !== "fixture" || !step.location?.file) return false;
582
679
  return isInternalFixtureFile(step.location.file);
583
680
  }
681
+ /**
682
+ * Earlier fixtures wrapped `request` in a Proxy, which made the fixture the
683
+ * location of each call's step, and recorded the calls as spans of their own.
684
+ * Their data can still arrive through `merge-reports`.
685
+ */
584
686
  function isInternalApiRequestStep(step) {
585
- if (step.category !== "pw:api" || !step.location?.file) return false;
586
- return isApiRequestStep(step) && isInternalFixtureFile(step.location.file);
687
+ if (!step.location?.file) return false;
688
+ return apiRequestStepCall(step) !== void 0 && isInternalFixtureFile(step.location.file);
689
+ }
690
+ const SPAN_KIND_CLIENT = 3;
691
+ function capturedApiRequestAttributes(request) {
692
+ return require_screenshots_zip.apiRequestHttpAttributes({
693
+ method: request.method,
694
+ url: request.responseUrl ?? request.url,
695
+ statusCode: request.statusCode,
696
+ responseHeaders: request.responseHeaders,
697
+ errorType: request.errorType
698
+ });
587
699
  }
588
700
  /**
589
- * `APIRequestContext` calls. From Playwright 1.63 they report a url and a
590
- * method; before that they show up only in the title, e.g. `GET "/api"`.
701
+ * For a call the fixture didn't record, such as one made in a `beforeAll`
702
+ * hook: what the step itself tells, from Playwright 1.63. There is no
703
+ * response to report.
591
704
  */
592
- function isApiRequestStep(step) {
593
- if (step.params) return typeof step.params.url === "string" && typeof step.params.method === "string";
594
- return /^(?:DELETE|FETCH|GET|HEAD|PATCH|POST|PUT)\s+"/.test(step.title);
705
+ function apiRequestStepAttributes(call) {
706
+ if (call.url === void 0 || call.url.endsWith("…")) return;
707
+ return require_screenshots_zip.apiRequestHttpAttributes({
708
+ method: call.method ?? "GET",
709
+ url: call.url
710
+ });
711
+ }
712
+ function unmatchedApiRequestSpan(request, options) {
713
+ const startTime = new Date(request.startTime);
714
+ const endTime = request.endTime ? new Date(request.endTime) : startTime;
715
+ const attributes = capturedApiRequestAttributes(request);
716
+ if (request.location) {
717
+ attributes[ATTR_CODE_FILE_PATH] = request.location.file;
718
+ attributes[ATTR_CODE_LINE_NUMBER] = request.location.line;
719
+ attributes[ATTR_CODE_COLUMN_NUMBER] = request.location.column;
720
+ }
721
+ const failed = request.errorType !== void 0 || request.statusCode !== void 0 && request.statusCode >= 400;
722
+ return {
723
+ traceId: options.traceId,
724
+ spanId: sentSpanId(request) ?? require_screenshots_zip.generateSpanId(),
725
+ parentSpanId: options.parentSpanId,
726
+ name: `HTTP ${request.method}`,
727
+ kind: SPAN_KIND_CLIENT,
728
+ startTime,
729
+ endTime: Number.isNaN(endTime.getTime()) ? startTime : endTime,
730
+ attributes,
731
+ status: { code: failed ? 2 : 0 }
732
+ };
733
+ }
734
+ function sentSpanId(request) {
735
+ return typeof request?.spanId === "string" && /^[0-9a-f]{16}$/.test(request.spanId) ? request.spanId : void 0;
736
+ }
737
+ function parseApiRequests(body, testId) {
738
+ if (!body) return [];
739
+ try {
740
+ return require_screenshots_zip.parseApiRequestsHandoff(body.toString("utf-8"));
741
+ } catch (error) {
742
+ console.warn(`Ignoring invalid ${require_screenshots_zip.API_REQUESTS_ATTACHMENT_NAME} for test ${testId}: ${errorMessage(error)}`);
743
+ return [];
744
+ }
595
745
  }
596
746
  /**
597
747
  * Span attributes hold primitives and string arrays, so other param values
@@ -1,5 +1,29 @@
1
1
  import { FullConfig, FullResult, Reporter, Suite, TestCase, TestResult, TestStep } from "@playwright/test/reporter";
2
2
 
3
+ //#region src/shared/otel.d.ts
4
+ type SpanAttributeValue = string | number | boolean | string[];
5
+ type SpanEvent = {
6
+ name: string;
7
+ time: Date;
8
+ attributes?: Record<string, SpanAttributeValue>;
9
+ };
10
+ type Span = {
11
+ traceId: string;
12
+ spanId: string;
13
+ parentSpanId?: string;
14
+ name: string;
15
+ startTime: Date;
16
+ endTime: Date;
17
+ attributes: Record<string, SpanAttributeValue>;
18
+ events?: SpanEvent[];
19
+ status?: {
20
+ code: number;
21
+ message?: string;
22
+ };
23
+ kind?: number; /** Service name for this span (if different from default). */
24
+ serviceName?: string;
25
+ };
26
+ //#endregion
3
27
  //#region src/reporter/reporter.d.ts
4
28
  declare class PlaywrightOpentelemetryReporter implements Reporter {
5
29
  private spanBatches;
@@ -7,6 +31,7 @@ declare class PlaywrightOpentelemetryReporter implements Reporter {
7
31
  private fixtureHandoffRoots;
8
32
  private rootDir?;
9
33
  private playwrightVersion?;
34
+ private warnedAboutVisibleWrapper;
10
35
  private debug;
11
36
  constructor();
12
37
  onBegin(config: FullConfig, _suite: Suite): void;
@@ -29,4 +54,4 @@ declare class PlaywrightOpentelemetryReporter implements Reporter {
29
54
  private sendScreenshotsZipToTraceApi;
30
55
  }
31
56
  //#endregion
32
- export { PlaywrightOpentelemetryReporter as t };
57
+ export { Span as n, SpanEvent as r, PlaywrightOpentelemetryReporter as t };
@@ -1,5 +1,29 @@
1
1
  import { FullConfig, FullResult, Reporter, Suite, TestCase, TestResult, TestStep } from "@playwright/test/reporter";
2
2
 
3
+ //#region src/shared/otel.d.ts
4
+ type SpanAttributeValue = string | number | boolean | string[];
5
+ type SpanEvent = {
6
+ name: string;
7
+ time: Date;
8
+ attributes?: Record<string, SpanAttributeValue>;
9
+ };
10
+ type Span = {
11
+ traceId: string;
12
+ spanId: string;
13
+ parentSpanId?: string;
14
+ name: string;
15
+ startTime: Date;
16
+ endTime: Date;
17
+ attributes: Record<string, SpanAttributeValue>;
18
+ events?: SpanEvent[];
19
+ status?: {
20
+ code: number;
21
+ message?: string;
22
+ };
23
+ kind?: number; /** Service name for this span (if different from default). */
24
+ serviceName?: string;
25
+ };
26
+ //#endregion
3
27
  //#region src/reporter/reporter.d.ts
4
28
  declare class PlaywrightOpentelemetryReporter implements Reporter {
5
29
  private spanBatches;
@@ -7,6 +31,7 @@ declare class PlaywrightOpentelemetryReporter implements Reporter {
7
31
  private fixtureHandoffRoots;
8
32
  private rootDir?;
9
33
  private playwrightVersion?;
34
+ private warnedAboutVisibleWrapper;
10
35
  private debug;
11
36
  constructor();
12
37
  onBegin(config: FullConfig, _suite: Suite): void;
@@ -29,4 +54,4 @@ declare class PlaywrightOpentelemetryReporter implements Reporter {
29
54
  private sendScreenshotsZipToTraceApi;
30
55
  }
31
56
  //#endregion
32
- export { PlaywrightOpentelemetryReporter as t };
57
+ export { Span as n, SpanEvent as r, PlaywrightOpentelemetryReporter as t };
package/dist/reporter.cjs CHANGED
@@ -1,4 +1,4 @@
1
1
  //#region src/reporter/index.ts
2
- var reporter_default = require("./reporter-D4i206BM.cjs").PlaywrightOpentelemetryReporter;
2
+ var reporter_default = require("./reporter-DQ0vACOz.cjs").PlaywrightOpentelemetryReporter;
3
3
  //#endregion
4
4
  module.exports = reporter_default;
@@ -1,2 +1,2 @@
1
- import { t as PlaywrightOpentelemetryReporter } from "./reporter-C1TwRMjP.cjs";
1
+ import { t as PlaywrightOpentelemetryReporter } from "./reporter-Ttd0SnXX.cjs";
2
2
  export = PlaywrightOpentelemetryReporter;
@@ -1,2 +1,2 @@
1
- import { t as PlaywrightOpentelemetryReporter } from "./reporter-C1TwRMjP.mjs";
1
+ import { t as PlaywrightOpentelemetryReporter } from "./reporter-Ttd0SnXX.mjs";
2
2
  export { PlaywrightOpentelemetryReporter as default };
package/dist/reporter.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as PlaywrightOpentelemetryReporter } from "./reporter-DKhFsfiL.mjs";
1
+ import { t as PlaywrightOpentelemetryReporter } from "./reporter-CxtAJif2.mjs";
2
2
  //#region src/reporter/index.ts
3
3
  var reporter_default = PlaywrightOpentelemetryReporter;
4
4
  //#endregion