playwright-opentelemetry 0.18.1 → 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-CU1X4mzh.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";
@@ -6,6 +6,7 @@ import { BlobReader, BlobWriter, TextWriter, ZipReader, ZipWriter } from "@zip.j
6
6
  //#region src/reporter/otel-attributes.ts
7
7
  const ATTR_CODE_FILE_PATH = "code.file.path";
8
8
  const ATTR_CODE_LINE_NUMBER = "code.line.number";
9
+ const ATTR_CODE_COLUMN_NUMBER = "code.column.number";
9
10
  const ATTR_TEST_CASE_NAME = "test.case.name";
10
11
  const ATTR_TEST_CASE_TITLE = "test.case.title";
11
12
  const ATTR_TEST_CASE_RESULT_STATUS = "test.case.result.status";
@@ -23,6 +24,80 @@ const ATTR_TEST_STEP_PARAM_PREFIX = "test.step.param.";
23
24
  const ATTR_TEST_STEP_CATEGORY = "test.step.category";
24
25
  const ATTR_TEST_PHASE = "playwright.test.phase";
25
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
26
101
  //#region src/reporter/legacy/trace-zip-screenshots.ts
27
102
  /**
28
103
  * LEGACY: reads screenshots out of Playwright's trace.zip, for Playwright
@@ -179,6 +254,7 @@ var PlaywrightOpentelemetryReporter = class {
179
254
  fixtureHandoffRoots = /* @__PURE__ */ new Set();
180
255
  rootDir;
181
256
  playwrightVersion;
257
+ warnedAboutVisibleWrapper = false;
182
258
  debug = false;
183
259
  constructor() {}
184
260
  onBegin(config, _suite) {
@@ -228,12 +304,22 @@ var PlaywrightOpentelemetryReporter = class {
228
304
  attributes["playwright.test.status"] = result.status;
229
305
  attributes["playwright.test.describes"] = describes;
230
306
  if (test.location) {
231
- const { file, line } = test.location;
307
+ const { file, line, column } = test.location;
232
308
  attributes[ATTR_CODE_FILE_PATH] = this.rootDir ? path.relative(this.rootDir, file) : file;
233
309
  attributes[ATTR_CODE_LINE_NUMBER] = line;
310
+ attributes[ATTR_CODE_COLUMN_NUMBER] = column;
234
311
  }
235
312
  const stepSpans = [];
236
- 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
+ }
237
323
  let minStartTime = result.startTime;
238
324
  let maxEndTime = new Date(result.startTime.getTime() + result.duration);
239
325
  for (const stepSpan of stepSpans) {
@@ -326,6 +412,7 @@ var PlaywrightOpentelemetryReporter = class {
326
412
  const attached = {
327
413
  traceContext: attachment(TRACE_CONTEXT_ATTACHMENT_NAME)?.body,
328
414
  fixtureSpans: attachment(FIXTURE_SPANS_ATTACHMENT_NAME)?.body,
415
+ apiRequests: attachment(API_REQUESTS_ATTACHMENT_NAME)?.body,
329
416
  screenshotsZip: screenshotsAttachmentPath ? readFixtureFile(screenshotsAttachmentPath, { required: true }) : void 0
330
417
  };
331
418
  const outputDir = getProjectOutputDir(test);
@@ -336,6 +423,7 @@ var PlaywrightOpentelemetryReporter = class {
336
423
  return {
337
424
  traceContext: attached.traceContext ?? readFixtureFile(path.join(dir, "trace-context.json")),
338
425
  fixtureSpans: attached.fixtureSpans ?? readFixtureFile(path.join(dir, "fixture-spans.json")),
426
+ apiRequests: attached.apiRequests ?? readFixtureFile(path.join(dir, "api-requests.json")),
339
427
  screenshotsZip: attached.screenshotsZip ?? readFixtureFile(path.join(dir, "screenshots.zip"))
340
428
  };
341
429
  } finally {
@@ -363,14 +451,14 @@ var PlaywrightOpentelemetryReporter = class {
363
451
  const [traceZipBlob] = await Promise.all([traceZipBlobPromise, ...uploadPromises]);
364
452
  return { traceZipBlob };
365
453
  }
366
- processTestStep(step, parentSpanId, traceId, parentTitlePath, stepSpans, inheritedPhase) {
454
+ processTestStep(step, parentSpanId, traceId, parentTitlePath, stepSpans, apiRequests, inheritedPhase) {
367
455
  let phase = inheritedPhase;
368
456
  if (step.category === "hook") {
369
457
  if (step.title === "Before Hooks") phase = "before";
370
458
  if (step.title === "After Hooks" || step.title === "Worker Cleanup") phase = "after";
371
459
  }
372
460
  if (isInternalFixtureStep(step) || isInternalApiRequestStep(step)) {
373
- 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);
374
462
  return;
375
463
  }
376
464
  const currentTitlePath = [...parentTitlePath, step.title];
@@ -384,23 +472,35 @@ var PlaywrightOpentelemetryReporter = class {
384
472
  }
385
473
  attributes[ATTR_TEST_STEP_CATEGORY] = step.category;
386
474
  if (phase) attributes[ATTR_TEST_PHASE] = phase;
387
- if (step.location) {
388
- const { file, line } = 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) {
484
+ const { file, line, column } = step.location;
389
485
  attributes[ATTR_CODE_FILE_PATH] = this.rootDir ? path.relative(this.rootDir, file) : file;
390
486
  attributes[ATTR_CODE_LINE_NUMBER] = line;
487
+ attributes[ATTR_CODE_COLUMN_NUMBER] = column;
391
488
  }
489
+ const httpAttributes = apiRequest ? capturedApiRequestAttributes(apiRequest) : apiRequestCall && apiRequestStepAttributes(apiRequestCall);
490
+ Object.assign(attributes, httpAttributes);
392
491
  const stepSpan = {
393
492
  traceId,
394
- spanId: generateSpanId(),
493
+ spanId: sentSpanId(apiRequest) ?? generateSpanId(),
395
494
  parentSpanId,
396
495
  name: TEST_STEP_SPAN_NAME,
397
496
  startTime: step.startTime,
398
497
  endTime: new Date(step.startTime.getTime() + step.duration),
399
498
  attributes
400
499
  };
500
+ if (httpAttributes) stepSpan.kind = SPAN_KIND_CLIENT;
401
501
  if (step.error) stepSpan.status = errorStatus(step.error.message);
402
502
  stepSpans.push(stepSpan);
403
- 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);
404
504
  }
405
505
  onStdOut(chunk, _test, _result) {
406
506
  if (this.debug) console.log(chunk.toString().slice(0, -1));
@@ -575,17 +675,70 @@ function isInternalFixtureStep(step) {
575
675
  if (step.category !== "fixture" || !step.location?.file) return false;
576
676
  return isInternalFixtureFile(step.location.file);
577
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
+ */
578
683
  function isInternalApiRequestStep(step) {
579
- if (step.category !== "pw:api" || !step.location?.file) return false;
580
- 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
+ });
581
696
  }
582
697
  /**
583
- * `APIRequestContext` calls. From Playwright 1.63 they report a url and a
584
- * 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.
585
701
  */
586
- function isApiRequestStep(step) {
587
- if (step.params) return typeof step.params.url === "string" && typeof step.params.method === "string";
588
- 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
+ }
589
742
  }
590
743
  /**
591
744
  * Span attributes hold primitives and string arrays, so other param values
@@ -1,4 +1,4 @@
1
- const require_screenshots_zip = require("./screenshots-zip-Cy2Wivw7.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");
@@ -9,6 +9,7 @@ let _zip_js_zip_js = require("@zip.js/zip.js");
9
9
  //#region src/reporter/otel-attributes.ts
10
10
  const ATTR_CODE_FILE_PATH = "code.file.path";
11
11
  const ATTR_CODE_LINE_NUMBER = "code.line.number";
12
+ const ATTR_CODE_COLUMN_NUMBER = "code.column.number";
12
13
  const ATTR_TEST_CASE_NAME = "test.case.name";
13
14
  const ATTR_TEST_CASE_TITLE = "test.case.title";
14
15
  const ATTR_TEST_CASE_RESULT_STATUS = "test.case.result.status";
@@ -26,6 +27,80 @@ const ATTR_TEST_STEP_PARAM_PREFIX = "test.step.param.";
26
27
  const ATTR_TEST_STEP_CATEGORY = "test.step.category";
27
28
  const ATTR_TEST_PHASE = "playwright.test.phase";
28
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
29
104
  //#region src/reporter/legacy/trace-zip-screenshots.ts
30
105
  /**
31
106
  * LEGACY: reads screenshots out of Playwright's trace.zip, for Playwright
@@ -182,6 +257,7 @@ var PlaywrightOpentelemetryReporter = class {
182
257
  fixtureHandoffRoots = /* @__PURE__ */ new Set();
183
258
  rootDir;
184
259
  playwrightVersion;
260
+ warnedAboutVisibleWrapper = false;
185
261
  debug = false;
186
262
  constructor() {}
187
263
  onBegin(config, _suite) {
@@ -231,12 +307,22 @@ var PlaywrightOpentelemetryReporter = class {
231
307
  attributes["playwright.test.status"] = result.status;
232
308
  attributes["playwright.test.describes"] = describes;
233
309
  if (test.location) {
234
- const { file, line } = test.location;
310
+ const { file, line, column } = test.location;
235
311
  attributes[ATTR_CODE_FILE_PATH] = this.rootDir ? node_path.default.relative(this.rootDir, file) : file;
236
312
  attributes[ATTR_CODE_LINE_NUMBER] = line;
313
+ attributes[ATTR_CODE_COLUMN_NUMBER] = column;
237
314
  }
238
315
  const stepSpans = [];
239
- 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
+ }
240
326
  let minStartTime = result.startTime;
241
327
  let maxEndTime = new Date(result.startTime.getTime() + result.duration);
242
328
  for (const stepSpan of stepSpans) {
@@ -329,6 +415,7 @@ var PlaywrightOpentelemetryReporter = class {
329
415
  const attached = {
330
416
  traceContext: attachment(require_screenshots_zip.TRACE_CONTEXT_ATTACHMENT_NAME)?.body,
331
417
  fixtureSpans: attachment(require_screenshots_zip.FIXTURE_SPANS_ATTACHMENT_NAME)?.body,
418
+ apiRequests: attachment(require_screenshots_zip.API_REQUESTS_ATTACHMENT_NAME)?.body,
332
419
  screenshotsZip: screenshotsAttachmentPath ? readFixtureFile(screenshotsAttachmentPath, { required: true }) : void 0
333
420
  };
334
421
  const outputDir = getProjectOutputDir(test);
@@ -339,6 +426,7 @@ var PlaywrightOpentelemetryReporter = class {
339
426
  return {
340
427
  traceContext: attached.traceContext ?? readFixtureFile(node_path.default.join(dir, "trace-context.json")),
341
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")),
342
430
  screenshotsZip: attached.screenshotsZip ?? readFixtureFile(node_path.default.join(dir, "screenshots.zip"))
343
431
  };
344
432
  } finally {
@@ -366,14 +454,14 @@ var PlaywrightOpentelemetryReporter = class {
366
454
  const [traceZipBlob] = await Promise.all([traceZipBlobPromise, ...uploadPromises]);
367
455
  return { traceZipBlob };
368
456
  }
369
- processTestStep(step, parentSpanId, traceId, parentTitlePath, stepSpans, inheritedPhase) {
457
+ processTestStep(step, parentSpanId, traceId, parentTitlePath, stepSpans, apiRequests, inheritedPhase) {
370
458
  let phase = inheritedPhase;
371
459
  if (step.category === "hook") {
372
460
  if (step.title === "Before Hooks") phase = "before";
373
461
  if (step.title === "After Hooks" || step.title === "Worker Cleanup") phase = "after";
374
462
  }
375
463
  if (isInternalFixtureStep(step) || isInternalApiRequestStep(step)) {
376
- 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);
377
465
  return;
378
466
  }
379
467
  const currentTitlePath = [...parentTitlePath, step.title];
@@ -387,23 +475,35 @@ var PlaywrightOpentelemetryReporter = class {
387
475
  }
388
476
  attributes[ATTR_TEST_STEP_CATEGORY] = step.category;
389
477
  if (phase) attributes[ATTR_TEST_PHASE] = phase;
390
- if (step.location) {
391
- const { file, line } = 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) {
487
+ const { file, line, column } = step.location;
392
488
  attributes[ATTR_CODE_FILE_PATH] = this.rootDir ? node_path.default.relative(this.rootDir, file) : file;
393
489
  attributes[ATTR_CODE_LINE_NUMBER] = line;
490
+ attributes[ATTR_CODE_COLUMN_NUMBER] = column;
394
491
  }
492
+ const httpAttributes = apiRequest ? capturedApiRequestAttributes(apiRequest) : apiRequestCall && apiRequestStepAttributes(apiRequestCall);
493
+ Object.assign(attributes, httpAttributes);
395
494
  const stepSpan = {
396
495
  traceId,
397
- spanId: require_screenshots_zip.generateSpanId(),
496
+ spanId: sentSpanId(apiRequest) ?? require_screenshots_zip.generateSpanId(),
398
497
  parentSpanId,
399
498
  name: TEST_STEP_SPAN_NAME,
400
499
  startTime: step.startTime,
401
500
  endTime: new Date(step.startTime.getTime() + step.duration),
402
501
  attributes
403
502
  };
503
+ if (httpAttributes) stepSpan.kind = SPAN_KIND_CLIENT;
404
504
  if (step.error) stepSpan.status = errorStatus(step.error.message);
405
505
  stepSpans.push(stepSpan);
406
- 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);
407
507
  }
408
508
  onStdOut(chunk, _test, _result) {
409
509
  if (this.debug) console.log(chunk.toString().slice(0, -1));
@@ -578,17 +678,70 @@ function isInternalFixtureStep(step) {
578
678
  if (step.category !== "fixture" || !step.location?.file) return false;
579
679
  return isInternalFixtureFile(step.location.file);
580
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
+ */
581
686
  function isInternalApiRequestStep(step) {
582
- if (step.category !== "pw:api" || !step.location?.file) return false;
583
- 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
+ });
584
699
  }
585
700
  /**
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"`.
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.
588
704
  */
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);
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
+ }
592
745
  }
593
746
  /**
594
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-BuViICz8.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-_CyyuDJs.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