playwright-opentelemetry 0.8.1 → 0.10.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,7 +1,55 @@
1
- import { n as PlaywrightOpentelemetryUseOptions, r as PlaywrightTraceOption, t as PlaywrightOpentelemetryConfig } from "./config-Db932l_s.mjs";
2
- import { n as BrowserPageTracker, r as TestTraceContext, t as createPlaywrightOtelTest } from "./playwright-opentelemetry-fixture-xHcfAc5d.mjs";
1
+ import { n as PlaywrightOpentelemetryUseOptions, r as PlaywrightTraceOption, t as PlaywrightOpentelemetryConfig } from "./config-CSm32mXS.mjs";
2
+ import { n as SpanEvent, t as Span } from "./otel-D0ztpic2.mjs";
3
+ import { ConsoleMessage, Page, Request } from "@playwright/test";
3
4
  export * from "@playwright/test";
4
5
 
6
+ //#region src/fixture/trace-context.d.ts
7
+ interface PlaywrightOtelTraceContextAttachment {
8
+ traceId: string;
9
+ rootSpanId: string;
10
+ }
11
+ type FixtureSpan = Omit<Span, "events"> & {
12
+ events: SpanEvent[];
13
+ };
14
+ interface NetworkRequestTraceContext {
15
+ traceId: string;
16
+ spanId: string;
17
+ parentSpanId: string;
18
+ routeAssociation: "active-route" | "active-page" | "root";
19
+ }
20
+ interface TestTraceContext extends PlaywrightOtelTraceContextAttachment {
21
+ spans: FixtureSpan[];
22
+ requestContexts: WeakMap<object, NetworkRequestTraceContext>;
23
+ addSpan(span: FixtureSpan): void;
24
+ }
25
+ //#endregion
26
+ //#region src/fixture/browser-page-tracker.d.ts
27
+ declare class BrowserPageTracker {
28
+ private readonly traceContext;
29
+ private nextPageId;
30
+ private pageStates;
31
+ private pages;
32
+ constructor(traceContext: TestTraceContext);
33
+ registerPage(page: Page): void;
34
+ unregisterPage(page: Page, endTime?: Date): void;
35
+ finishAll(endTime?: Date): void;
36
+ startDocumentNavigation(request: Request): void;
37
+ handleFrameNavigated(page: Page, url: string): void;
38
+ getNetworkParent(request: Request): {
39
+ spanId: string;
40
+ routeAssociation: "active-route" | "active-page" | "root";
41
+ };
42
+ recordConsoleMessage(page: Page, message: ConsoleMessage, time?: Date): void;
43
+ recordPageError(page: Page, error: Error, time?: Date): void;
44
+ private startDocumentPageSpan;
45
+ private startRouteSpan;
46
+ private finishPageSpans;
47
+ private finishRouteSpan;
48
+ private getActiveBrowserSpan;
49
+ private stateFor;
50
+ private createBrowserSpan;
51
+ }
52
+ //#endregion
5
53
  //#region src/fixture/index.d.ts
6
54
  declare const test: import("@playwright/test").TestType<import("@playwright/test").PlaywrightTestArgs & import("@playwright/test").PlaywrightTestOptions & PlaywrightOpentelemetryUseOptions & {
7
55
  testTraceInfo: {
@@ -14,4 +62,4 @@ declare const test: import("@playwright/test").TestType<import("@playwright/test
14
62
  _playwrightOpentelemetryReporterCheck: void;
15
63
  }>;
16
64
  //#endregion
17
- export { type PlaywrightOpentelemetryConfig, type PlaywrightOpentelemetryUseOptions, type PlaywrightTraceOption, createPlaywrightOtelTest, test };
65
+ export { type PlaywrightOpentelemetryConfig, type PlaywrightOpentelemetryUseOptions, type PlaywrightTraceOption, test };
package/dist/fixture.mjs CHANGED
@@ -1,7 +1,601 @@
1
- import { t as createPlaywrightOtelTest } from "./playwright-opentelemetry-fixture-2vhqZLmh.mjs";
1
+ import { a as createTestTraceContext, l as generateSpanId, n as BROWSER_SERVICE_NAME, o as flushFixtureSpans, t as resolvePlaywrightOpentelemetryConfig } from "./config-DKm2ymJf.mjs";
2
+ import path from "node:path";
2
3
  import { test as test$1 } from "@playwright/test";
3
4
  export * from "@playwright/test";
5
+ //#region src/fixture/browser-page-tracker.ts
6
+ const BROWSER_PAGE_SPAN_NAME = "browser.page";
7
+ const BROWSER_ROUTE_SPAN_NAME = "browser.route";
8
+ const SPAN_STATUS_CODE_UNSET$2 = 0;
9
+ var BrowserPageTracker = class {
10
+ traceContext;
11
+ nextPageId = 1;
12
+ pageStates = /* @__PURE__ */ new WeakMap();
13
+ pages = /* @__PURE__ */ new Set();
14
+ constructor(traceContext) {
15
+ this.traceContext = traceContext;
16
+ }
17
+ registerPage(page) {
18
+ this.stateFor(page);
19
+ this.pages.add(page);
20
+ }
21
+ unregisterPage(page, endTime = /* @__PURE__ */ new Date()) {
22
+ this.finishPageSpans(page, endTime);
23
+ this.pages.delete(page);
24
+ }
25
+ finishAll(endTime = /* @__PURE__ */ new Date()) {
26
+ for (const page of this.pages) this.finishPageSpans(page, endTime);
27
+ this.pages.clear();
28
+ }
29
+ startDocumentNavigation(request) {
30
+ if (!isMainFrameNavigationRequest(request)) return;
31
+ const page = pageForRequest(request);
32
+ if (!page) return;
33
+ this.startDocumentPageSpan(page, request.url(), /* @__PURE__ */ new Date());
34
+ }
35
+ handleFrameNavigated(page, url) {
36
+ const state = this.stateFor(page);
37
+ const previousUrl = state.lastUrl;
38
+ state.lastUrl = url;
39
+ if (!previousUrl || previousUrl === "about:blank") return;
40
+ if (!shouldCreateSameDocumentPageSpan(previousUrl, url, state.activeRouteSpan?.url)) return;
41
+ this.startRouteSpan(page, url, previousUrl, /* @__PURE__ */ new Date());
42
+ }
43
+ getNetworkParent(request) {
44
+ const page = pageForRequest(request);
45
+ if (!page) return {
46
+ spanId: this.traceContext.rootSpanId,
47
+ routeAssociation: "root"
48
+ };
49
+ const state = this.stateFor(page);
50
+ if (state.activeRouteSpan) return {
51
+ spanId: state.activeRouteSpan.span.spanId,
52
+ routeAssociation: "active-route"
53
+ };
54
+ if (state.activeDocumentSpan) return {
55
+ spanId: state.activeDocumentSpan.span.spanId,
56
+ routeAssociation: "active-page"
57
+ };
58
+ return {
59
+ spanId: this.traceContext.rootSpanId,
60
+ routeAssociation: "root"
61
+ };
62
+ }
63
+ recordConsoleMessage(page, message, time = /* @__PURE__ */ new Date()) {
64
+ const activeSpan = this.getActiveBrowserSpan(page);
65
+ if (!activeSpan) return;
66
+ const type = message.type();
67
+ const attributes = {
68
+ message: message.text(),
69
+ "browser.console.type": type,
70
+ "severity.text": consoleSeverityText(type)
71
+ };
72
+ const location = message.location();
73
+ if (location.url) attributes["code.file.path"] = location.url;
74
+ const line = consoleLocationLine(location);
75
+ if (line !== void 0) attributes["code.line.number"] = line + 1;
76
+ activeSpan.events.push({
77
+ name: "log",
78
+ time,
79
+ attributes
80
+ });
81
+ }
82
+ recordPageError(page, error, time = /* @__PURE__ */ new Date()) {
83
+ const activeSpan = this.getActiveBrowserSpan(page);
84
+ if (!activeSpan) return;
85
+ const attributes = {};
86
+ if (error.name) attributes["exception.type"] = error.name;
87
+ if (error.message) attributes["exception.message"] = error.message;
88
+ if (error.stack) attributes["exception.stacktrace"] = error.stack;
89
+ activeSpan.events.push({
90
+ name: "exception",
91
+ time,
92
+ attributes
93
+ });
94
+ }
95
+ startDocumentPageSpan(page, url, startTime) {
96
+ const state = this.stateFor(page);
97
+ this.finishPageSpans(page, startTime);
98
+ const span = this.createBrowserSpan({
99
+ pageId: state.pageId,
100
+ name: BROWSER_PAGE_SPAN_NAME,
101
+ url,
102
+ navigationType: "document",
103
+ parentSpanId: this.traceContext.rootSpanId,
104
+ startTime
105
+ });
106
+ state.activeDocumentSpan = {
107
+ span,
108
+ url
109
+ };
110
+ state.documentUrl = url;
111
+ state.lastUrl = url;
112
+ this.traceContext.addSpan(span);
113
+ }
114
+ startRouteSpan(page, url, previousUrl, startTime) {
115
+ const state = this.stateFor(page);
116
+ this.finishRouteSpan(state, startTime);
117
+ const span = this.createBrowserSpan({
118
+ pageId: state.pageId,
119
+ name: BROWSER_ROUTE_SPAN_NAME,
120
+ url,
121
+ navigationType: "same-document",
122
+ parentSpanId: state.activeDocumentSpan?.span.spanId ?? this.traceContext.rootSpanId,
123
+ startTime,
124
+ previousUrl,
125
+ documentUrl: state.documentUrl
126
+ });
127
+ state.activeRouteSpan = {
128
+ span,
129
+ url
130
+ };
131
+ this.traceContext.addSpan(span);
132
+ }
133
+ finishPageSpans(page, endTime) {
134
+ const state = this.pageStates.get(page);
135
+ if (!state) return;
136
+ this.finishRouteSpan(state, endTime);
137
+ if (state.activeDocumentSpan) {
138
+ state.activeDocumentSpan.span.endTime = endTime;
139
+ state.activeDocumentSpan = void 0;
140
+ }
141
+ }
142
+ finishRouteSpan(state, endTime) {
143
+ if (state.activeRouteSpan) {
144
+ state.activeRouteSpan.span.endTime = endTime;
145
+ state.activeRouteSpan = void 0;
146
+ }
147
+ }
148
+ getActiveBrowserSpan(page) {
149
+ const state = this.pageStates.get(page);
150
+ return state?.activeRouteSpan?.span ?? state?.activeDocumentSpan?.span;
151
+ }
152
+ stateFor(page) {
153
+ const existing = this.pageStates.get(page);
154
+ if (existing) return existing;
155
+ const state = {
156
+ pageId: `page-${this.nextPageId++}`,
157
+ lastUrl: page.url()
158
+ };
159
+ this.pageStates.set(page, state);
160
+ return state;
161
+ }
162
+ createBrowserSpan({ pageId, name, url, navigationType, parentSpanId, startTime, previousUrl, documentUrl }) {
163
+ return {
164
+ traceId: this.traceContext.traceId,
165
+ spanId: generateSpanId(),
166
+ parentSpanId,
167
+ name,
168
+ startTime,
169
+ endTime: startTime,
170
+ status: { code: SPAN_STATUS_CODE_UNSET$2 },
171
+ attributes: pageAttributes({
172
+ pageId,
173
+ url,
174
+ navigationType,
175
+ previousUrl,
176
+ documentUrl
177
+ }),
178
+ events: [],
179
+ serviceName: BROWSER_SERVICE_NAME
180
+ };
181
+ }
182
+ };
183
+ function consoleSeverityText(type) {
184
+ switch (type) {
185
+ case "debug": return "DEBUG";
186
+ case "warning": return "WARN";
187
+ case "error":
188
+ case "assert": return "ERROR";
189
+ case "trace": return "TRACE";
190
+ default: return "INFO";
191
+ }
192
+ }
193
+ function consoleLocationLine(location) {
194
+ return location.line ?? location.lineNumber;
195
+ }
196
+ function pageAttributes({ pageId, url, navigationType, previousUrl, documentUrl }) {
197
+ const attributes = {
198
+ "browser.resource.type": navigationType === "document" ? "page" : "route",
199
+ "browser.page.id": pageId,
200
+ "browser.page.navigation.type": navigationType,
201
+ "url.full": url
202
+ };
203
+ try {
204
+ const parsedUrl = new URL(url);
205
+ attributes["url.path"] = parsedUrl.pathname;
206
+ if (parsedUrl.search) attributes["url.query"] = parsedUrl.search.slice(1);
207
+ } catch {}
208
+ if (previousUrl) attributes["browser.route.previous_url"] = previousUrl;
209
+ if (documentUrl) attributes["browser.document.url"] = documentUrl;
210
+ return attributes;
211
+ }
212
+ function isMainFrameNavigationRequest(request) {
213
+ if (!request.isNavigationRequest()) return false;
214
+ try {
215
+ const frame = request.frame();
216
+ return frame === frame.page().mainFrame();
217
+ } catch {
218
+ return false;
219
+ }
220
+ }
221
+ function pageForRequest(request) {
222
+ try {
223
+ return request.frame().page();
224
+ } catch {
225
+ return;
226
+ }
227
+ }
228
+ function stripHash(url) {
229
+ try {
230
+ const parsed = new URL(url);
231
+ parsed.hash = "";
232
+ return parsed.toString();
233
+ } catch {
234
+ return url.split("#")[0] ?? url;
235
+ }
236
+ }
237
+ function shouldCreateSameDocumentPageSpan(previousUrl, nextUrl, activePageUrl) {
238
+ if (activePageUrl && stripHash(activePageUrl) === stripHash(nextUrl)) return false;
239
+ return stripHash(previousUrl) !== stripHash(nextUrl);
240
+ }
241
+ //#endregion
242
+ //#region src/fixture/network-propagator.ts
243
+ function storeRequestTraceContext({ request, traceContext, parentSpanId, routeAssociation }) {
244
+ const spanId = generateSpanId();
245
+ traceContext.requestContexts.set(request, {
246
+ traceId: traceContext.traceId,
247
+ spanId,
248
+ parentSpanId,
249
+ routeAssociation
250
+ });
251
+ return spanId;
252
+ }
253
+ /**
254
+ * Intercepts network requests to propagate trace context via traceparent header.
255
+ * @param options - The propagator options
256
+ */
257
+ async function propagateRouteTraceHeaders({ route, request, traceId, spanId }) {
258
+ const traceHeader = `00-${traceId}-${spanId}-01`;
259
+ await route.fallback({ headers: {
260
+ ...request.headers(),
261
+ traceparent: traceHeader
262
+ } });
263
+ }
264
+ //#endregion
265
+ //#region src/fixture/reporter-config.ts
266
+ const PLAYWRIGHT_OPENTELEMETRY_REPORTER_NAMES = new Set(["playwright-opentelemetry", "playwright-opentelemetry/reporter"]);
267
+ const PLAYWRIGHT_OPENTELEMETRY_REPORTER_PATHS = ["dist/reporter.mjs", "dist/reporter.cjs"];
268
+ const MISSING_PLAYWRIGHT_OPENTELEMETRY_REPORTER_ERROR = `playwright-opentelemetry fixture is enabled, but the reporter is missing.
269
+
270
+ Add it to playwright.config.ts:
271
+
272
+ reporter: [["playwright-opentelemetry/reporter"]]`;
273
+ function hasPlaywrightOpentelemetryReporter(reporter) {
274
+ return reporter.some(([reporterName]) => isPlaywrightOpentelemetryReporterName(reporterName));
275
+ }
276
+ function isPlaywrightOpentelemetryReporterName(reporterName) {
277
+ if (PLAYWRIGHT_OPENTELEMETRY_REPORTER_NAMES.has(reporterName)) return true;
278
+ const normalizedReporterName = reporterName.replace(/\\/g, "/");
279
+ return PLAYWRIGHT_OPENTELEMETRY_REPORTER_PATHS.some((reporterPath) => normalizedReporterName === reporterPath || normalizedReporterName.endsWith(`/${reporterPath}`));
280
+ }
281
+ //#endregion
282
+ //#region src/fixture/request-response-capture.ts
283
+ /**
284
+ * Detects the resource type based on Content-Type header with URL extension fallback.
285
+ * This categorization mirrors Chrome DevTools Network tab behavior.
286
+ *
287
+ * @param contentType - The Content-Type response header value (may be null)
288
+ * @param url - The request URL (used as fallback)
289
+ * @returns The detected resource type
290
+ */
291
+ function detectResourceType(contentType, url) {
292
+ if (contentType) {
293
+ const mimeType = contentType.split(";")[0].trim().toLowerCase();
294
+ if (mimeType !== "application/octet-stream") {
295
+ if (mimeType === "text/html" || mimeType === "application/xhtml+xml") return "document";
296
+ if (mimeType === "application/javascript" || mimeType === "text/javascript" || mimeType === "application/x-javascript" || mimeType === "application/ecmascript" || mimeType === "text/ecmascript" || mimeType === "module" || mimeType === "application/wasm") return "script";
297
+ if (mimeType === "text/css") return "stylesheet";
298
+ if (mimeType.startsWith("image/")) return "image";
299
+ if (mimeType.startsWith("font/") || mimeType === "application/font-woff" || mimeType === "application/font-woff2" || mimeType === "application/x-font-ttf" || mimeType === "application/x-font-opentype") return "font";
300
+ if (mimeType.startsWith("video/") || mimeType.startsWith("audio/")) return "media";
301
+ if (mimeType === "application/json" || mimeType === "text/plain" || mimeType === "application/xml" || mimeType === "text/xml" || mimeType.endsWith("+json") || mimeType.endsWith("+xml")) return "fetch";
302
+ }
303
+ }
304
+ const resourceType = detectResourceTypeFromUrl(url);
305
+ if (resourceType) return resourceType;
306
+ return "other";
307
+ }
308
+ /**
309
+ * Detects resource type from URL file extension.
310
+ * Used as fallback when Content-Type header is missing or generic.
311
+ */
312
+ function detectResourceTypeFromUrl(url) {
313
+ try {
314
+ const lastSegment = new URL(url).pathname.toLowerCase().split("/").pop() || "";
315
+ const dotIndex = lastSegment.lastIndexOf(".");
316
+ if (dotIndex === -1) return null;
317
+ const extension = lastSegment.slice(dotIndex + 1);
318
+ if (extension === "html" || extension === "htm" || extension === "xhtml") return "document";
319
+ if (extension === "js" || extension === "mjs" || extension === "cjs" || extension === "jsx" || extension === "ts" || extension === "tsx" || extension === "wasm") return "script";
320
+ if (extension === "css") return "stylesheet";
321
+ if (extension === "png" || extension === "jpg" || extension === "jpeg" || extension === "gif" || extension === "svg" || extension === "webp" || extension === "ico" || extension === "bmp" || extension === "avif") return "image";
322
+ if (extension === "woff" || extension === "woff2" || extension === "ttf" || extension === "otf" || extension === "eot") return "font";
323
+ if (extension === "mp4" || extension === "webm" || extension === "ogg" || extension === "mp3" || extension === "wav" || extension === "m4a" || extension === "aac" || extension === "flac" || extension === "avi" || extension === "mov") return "media";
324
+ if (extension === "json" || extension === "xml") return "fetch";
325
+ return null;
326
+ } catch {
327
+ return null;
328
+ }
329
+ }
330
+ /**
331
+ * OpenTelemetry SpanKind values
332
+ * @see https://opentelemetry.io/docs/specs/otel/trace/api/#spankind
333
+ */
334
+ const SPAN_KIND_CLIENT$1 = 3;
335
+ /**
336
+ * OpenTelemetry SpanStatusCode values
337
+ * @see https://opentelemetry.io/docs/specs/otel/trace/api/#set-status
338
+ */
339
+ const SPAN_STATUS_CODE_UNSET$1 = 0;
340
+ const SPAN_STATUS_CODE_ERROR$1 = 2;
341
+ /**
342
+ * Captures request/response data and stores the complete network span in the
343
+ * current test trace context. Called after the request has completed.
344
+ *
345
+ * @param options - The capture options
346
+ */
347
+ async function fixtureCaptureRequestResponse({ request, response, traceContext }) {
348
+ const url = request.url();
349
+ const method = request.method();
350
+ const statusCode = response.status();
351
+ const requestTraceContext = traceContext.requestContexts.get(request);
352
+ if (!requestTraceContext) return;
353
+ const timing = request.timing();
354
+ const startTime = new Date(timing.startTime);
355
+ const endTime = timing.responseEnd >= 0 ? new Date(timing.startTime + timing.responseEnd) : new Date(timing.startTime);
356
+ const parsedUrl = new URL(url);
357
+ const serverAddress = parsedUrl.hostname;
358
+ const serverPort = parsedUrl.port !== "" ? Number.parseInt(parsedUrl.port, 10) : parsedUrl.protocol === "https:" ? 443 : 80;
359
+ let statusCodeValue = SPAN_STATUS_CODE_UNSET$1;
360
+ if (statusCode >= 400) statusCodeValue = SPAN_STATUS_CODE_ERROR$1;
361
+ const resourceType = detectResourceType(await getResponseHeaderValue(response, "content-type"), url);
362
+ const attributes = {
363
+ "http.request.method": method,
364
+ "url.full": url,
365
+ "url.path": parsedUrl.pathname,
366
+ "server.address": serverAddress,
367
+ "server.port": serverPort,
368
+ "http.response.status_code": statusCode,
369
+ "http.resource.type": resourceType,
370
+ "browser.request.route_association": requestTraceContext.routeAssociation
371
+ };
372
+ if (parsedUrl.search) attributes["url.query"] = parsedUrl.search.slice(1);
373
+ if (statusCodeValue === SPAN_STATUS_CODE_ERROR$1) attributes["error.type"] = statusCode.toString();
374
+ const networkSpan = {
375
+ traceId: requestTraceContext.traceId,
376
+ spanId: requestTraceContext.spanId,
377
+ parentSpanId: requestTraceContext.parentSpanId,
378
+ name: `HTTP ${method}`,
379
+ kind: SPAN_KIND_CLIENT$1,
380
+ startTime,
381
+ endTime,
382
+ status: { code: statusCodeValue },
383
+ attributes,
384
+ events: [],
385
+ serviceName: BROWSER_SERVICE_NAME
386
+ };
387
+ traceContext.addSpan(networkSpan);
388
+ }
389
+ async function getResponseHeaderValue(response, name) {
390
+ try {
391
+ return await response.headerValue(name);
392
+ } catch {
393
+ return null;
394
+ }
395
+ }
396
+ //#endregion
397
+ //#region src/fixture/test-fetch-capture.ts
398
+ const SPAN_KIND_CLIENT = 3;
399
+ const SPAN_STATUS_CODE_UNSET = 0;
400
+ const SPAN_STATUS_CODE_ERROR = 2;
401
+ let originalFetch;
402
+ let activeTraceContext;
403
+ function runWithTestFetchCapture(traceContext, callback) {
404
+ ensureFetchPatched();
405
+ const previousTraceContext = activeTraceContext;
406
+ activeTraceContext = traceContext;
407
+ return (async () => {
408
+ try {
409
+ return await callback();
410
+ } finally {
411
+ activeTraceContext = previousTraceContext;
412
+ }
413
+ })();
414
+ }
415
+ function ensureFetchPatched() {
416
+ if (globalThis.fetch === patchedFetch) return;
417
+ originalFetch = globalThis.fetch.bind(globalThis);
418
+ globalThis.fetch = patchedFetch;
419
+ }
420
+ const patchedFetch = async (input, init) => {
421
+ const traceContext = activeTraceContext;
422
+ if (!traceContext) return originalFetch ? originalFetch(input, init) : globalThis.fetch(input, init);
423
+ if (!originalFetch) return globalThis.fetch(input, init);
424
+ const startTime = /* @__PURE__ */ new Date();
425
+ try {
426
+ const response = await originalFetch(input, init);
427
+ traceContext.addSpan(createFetchSpan({
428
+ input,
429
+ init,
430
+ traceContext,
431
+ startTime,
432
+ endTime: /* @__PURE__ */ new Date(),
433
+ statusCode: response.status
434
+ }));
435
+ return response;
436
+ } catch (error) {
437
+ traceContext.addSpan(createFetchSpan({
438
+ input,
439
+ init,
440
+ traceContext,
441
+ startTime,
442
+ endTime: /* @__PURE__ */ new Date(),
443
+ error
444
+ }));
445
+ throw error;
446
+ }
447
+ };
448
+ function createFetchSpan({ input, init, traceContext, startTime, endTime, statusCode, error }) {
449
+ const method = getFetchMethod(input, init);
450
+ const attributes = createHttpAttributes({
451
+ method,
452
+ url: getFetchUrl(input),
453
+ statusCode,
454
+ error
455
+ });
456
+ const failed = error !== void 0 || statusCode !== void 0 && statusCode >= 400;
457
+ return {
458
+ traceId: traceContext.traceId,
459
+ spanId: generateSpanId(),
460
+ parentSpanId: traceContext.rootSpanId,
461
+ name: `HTTP ${method}`,
462
+ kind: SPAN_KIND_CLIENT,
463
+ startTime,
464
+ endTime,
465
+ status: failed ? {
466
+ code: SPAN_STATUS_CODE_ERROR,
467
+ message: errorMessage(error)
468
+ } : { code: SPAN_STATUS_CODE_UNSET },
469
+ attributes,
470
+ events: []
471
+ };
472
+ }
473
+ function createHttpAttributes({ method, url, statusCode, error }) {
474
+ const attributes = {
475
+ "http.request.method": method,
476
+ "http.resource.type": "fetch",
477
+ "url.full": url
478
+ };
479
+ try {
480
+ const parsedUrl = new URL(url);
481
+ attributes["url.path"] = parsedUrl.pathname;
482
+ attributes["server.address"] = parsedUrl.hostname;
483
+ attributes["server.port"] = serverPort(parsedUrl);
484
+ if (parsedUrl.search) attributes["url.query"] = parsedUrl.search.slice(1);
485
+ } catch {}
486
+ if (statusCode !== void 0) {
487
+ attributes["http.response.status_code"] = statusCode;
488
+ if (statusCode >= 400) attributes["error.type"] = statusCode.toString();
489
+ } else if (error !== void 0) attributes["error.type"] = errorType(error);
490
+ return attributes;
491
+ }
492
+ function getFetchMethod(input, init) {
493
+ return (init?.method ?? (input instanceof Request ? input.method : "GET")).toUpperCase();
494
+ }
495
+ function getFetchUrl(input) {
496
+ if (input instanceof Request) return input.url;
497
+ return input.toString();
498
+ }
499
+ function serverPort(url) {
500
+ if (url.port) return Number.parseInt(url.port, 10);
501
+ return url.protocol === "https:" ? 443 : 80;
502
+ }
503
+ function errorType(error) {
504
+ if (error instanceof Error && error.name) return error.name;
505
+ return "Error";
506
+ }
507
+ function errorMessage(error) {
508
+ return error instanceof Error && error.message ? error.message : void 0;
509
+ }
510
+ //#endregion
511
+ //#region src/fixture/playwright-opentelemetry-fixture.ts
512
+ function createPlaywrightOtelTest(testBase) {
513
+ return testBase.extend({
514
+ playwrightOpentelemetry: [void 0, { option: true }],
515
+ _playwrightOpentelemetryReporterCheck: [async ({}, use, workerInfo) => {
516
+ if (!hasPlaywrightOpentelemetryReporter(workerInfo.config.reporter)) throw new Error(MISSING_PLAYWRIGHT_OPENTELEMETRY_REPORTER_ERROR);
517
+ await use();
518
+ }, {
519
+ scope: "worker",
520
+ auto: true
521
+ }],
522
+ testTraceInfo: [async ({ playwright }, use, testInfo) => {
523
+ const outputDir = path.dirname(testInfo.outputDir);
524
+ await use({
525
+ testId: testInfo.testId,
526
+ outputDir
527
+ });
528
+ }, { auto: true }],
529
+ testTraceContext: [async ({ playwrightOpentelemetry, trace }, use, testInfo) => {
530
+ const config = resolvePlaywrightOpentelemetryConfig(playwrightOpentelemetry);
531
+ const traceContext = await createTestTraceContext(testInfo);
532
+ await runWithTestFetchCapture(traceContext, () => use(traceContext));
533
+ await flushFixtureSpans(traceContext, config, {
534
+ trace: config.trace ?? trace,
535
+ testInfo
536
+ });
537
+ }, { auto: true }],
538
+ browserPageTracker: [async ({ testTraceContext }, use) => {
539
+ const tracker = new BrowserPageTracker(testTraceContext);
540
+ await use(tracker);
541
+ tracker.finishAll();
542
+ }, { auto: true }],
543
+ context: async ({ context, playwrightOpentelemetry, testTraceContext, browserPageTracker }, use) => {
544
+ const config = resolvePlaywrightOpentelemetryConfig(playwrightOpentelemetry);
545
+ context.route("**", async (route, request) => {
546
+ browserPageTracker.startDocumentNavigation(request);
547
+ const networkParent = browserPageTracker.getNetworkParent(request);
548
+ const spanId = storeRequestTraceContext({
549
+ request,
550
+ traceContext: testTraceContext,
551
+ parentSpanId: networkParent.spanId,
552
+ routeAssociation: networkParent.routeAssociation
553
+ });
554
+ if (config.propagateTraceHeaders) {
555
+ await propagateRouteTraceHeaders({
556
+ route,
557
+ request,
558
+ traceId: testTraceContext.traceId,
559
+ spanId
560
+ });
561
+ return;
562
+ }
563
+ await route.fallback();
564
+ });
565
+ await use(context);
566
+ },
567
+ page: async ({ page, testTraceContext, browserPageTracker }, use) => {
568
+ browserPageTracker.registerPage(page);
569
+ page.on("close", () => browserPageTracker.unregisterPage(page));
570
+ page.on("console", (message) => {
571
+ browserPageTracker.recordConsoleMessage(page, message);
572
+ });
573
+ page.on("pageerror", (error) => {
574
+ browserPageTracker.recordPageError(page, error);
575
+ });
576
+ page.on("framenavigated", (frame) => {
577
+ if (frame === page.mainFrame()) browserPageTracker.handleFrameNavigated(page, frame.url());
578
+ });
579
+ const pendingRequests = /* @__PURE__ */ new Map();
580
+ page.on("response", (response) => {
581
+ pendingRequests.set(response.request(), response);
582
+ });
583
+ page.on("requestfinished", async (request) => {
584
+ const response = pendingRequests.get(request);
585
+ pendingRequests.delete(request);
586
+ if (!response) return;
587
+ await fixtureCaptureRequestResponse({
588
+ request,
589
+ response,
590
+ traceContext: testTraceContext
591
+ });
592
+ });
593
+ await use(page);
594
+ }
595
+ });
596
+ }
597
+ //#endregion
4
598
  //#region src/fixture/index.ts
5
599
  const test = createPlaywrightOtelTest(test$1);
6
600
  //#endregion
7
- export { createPlaywrightOtelTest, test };
601
+ export { test };
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
1
  //#region src/index.ts
2
- var src_default = require("./reporter-DKlbB_nC.cjs").PlaywrightOpentelemetryReporter;
2
+ var src_default = require("./reporter-yOZUJcsx.cjs").PlaywrightOpentelemetryReporter;
3
3
  //#endregion
4
4
  module.exports = src_default;
package/dist/index.d.cts CHANGED
@@ -1,3 +1,3 @@
1
- import { n as PlaywrightOpentelemetryUseOptions, r as PlaywrightTraceOption, t as PlaywrightOpentelemetryConfig } from "./config-Db932l_s.cjs";
1
+ import { n as PlaywrightOpentelemetryUseOptions, r as PlaywrightTraceOption, t as PlaywrightOpentelemetryConfig } from "./config-CSm32mXS.cjs";
2
2
  import { t as PlaywrightOpentelemetryReporter } from "./reporter-DaCojFom.cjs";
3
3
  export { type PlaywrightOpentelemetryConfig, type PlaywrightOpentelemetryUseOptions, type PlaywrightTraceOption, PlaywrightOpentelemetryReporter as default };
package/dist/index.d.mts CHANGED
@@ -1,3 +1,3 @@
1
- import { n as PlaywrightOpentelemetryUseOptions, r as PlaywrightTraceOption, t as PlaywrightOpentelemetryConfig } from "./config-Db932l_s.mjs";
1
+ import { n as PlaywrightOpentelemetryUseOptions, r as PlaywrightTraceOption, t as PlaywrightOpentelemetryConfig } from "./config-CSm32mXS.mjs";
2
2
  import { t as PlaywrightOpentelemetryReporter } from "./reporter-DaCojFom.mjs";
3
3
  export { type PlaywrightOpentelemetryConfig, type PlaywrightOpentelemetryUseOptions, type PlaywrightTraceOption, PlaywrightOpentelemetryReporter as default };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as PlaywrightOpentelemetryReporter } from "./reporter-CM4W2jxM.mjs";
1
+ import { t as PlaywrightOpentelemetryReporter } from "./reporter-BGDFTuxD.mjs";
2
2
  //#region src/index.ts
3
3
  var src_default = PlaywrightOpentelemetryReporter;
4
4
  //#endregion