playwright-opentelemetry 0.9.0 → 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.
@@ -17,7 +17,6 @@ interface PlaywrightOpentelemetryConfig {
17
17
  storeTraceZip?: boolean;
18
18
  trace?: PlaywrightTraceOption;
19
19
  propagateTraceHeaders?: boolean;
20
- serviceName?: string;
21
20
  debug?: boolean;
22
21
  }
23
22
  interface PlaywrightOpentelemetryUseOptions {
@@ -17,7 +17,6 @@ interface PlaywrightOpentelemetryConfig {
17
17
  storeTraceZip?: boolean;
18
18
  trace?: PlaywrightTraceOption;
19
19
  propagateTraceHeaders?: boolean;
20
- serviceName?: string;
21
20
  debug?: boolean;
22
21
  }
23
22
  interface PlaywrightOpentelemetryUseOptions {
@@ -1,7 +1,5 @@
1
1
  //#region package.json
2
- var version = "0.9.0";
3
- //#endregion
4
- //#region src/shared/otel.ts
2
+ var version = "0.10.0";
5
3
  /** Generate a random 32-character hex trace ID. */
6
4
  function generateTraceId() {
7
5
  return Array.from({ length: 32 }, () => Math.floor(Math.random() * 16).toString(16)).join("");
@@ -89,10 +87,10 @@ function buildResourceSpan(spans, serviceName, playwrightVersion) {
89
87
  }]
90
88
  };
91
89
  }
92
- function buildOtlpRequest(spans, serviceName, playwrightVersion) {
90
+ function buildOtlpRequest(spans, playwrightVersion) {
93
91
  const spansByService = /* @__PURE__ */ new Map();
94
92
  for (const span of spans) {
95
- const spanServiceName = span.serviceName ?? serviceName;
93
+ const spanServiceName = span.serviceName ?? "playwright-tests";
96
94
  const serviceSpans = spansByService.get(spanServiceName);
97
95
  if (serviceSpans) serviceSpans.push(span);
98
96
  else spansByService.set(spanServiceName, [span]);
@@ -103,7 +101,7 @@ function buildOtlpRequest(spans, serviceName, playwrightVersion) {
103
101
  }
104
102
  async function sendSpans(spans, options) {
105
103
  if (spans.length === 0) return;
106
- const body = JSON.stringify(buildOtlpRequest(spans, options.serviceName, options.playwrightVersion));
104
+ const body = JSON.stringify(buildOtlpRequest(spans, options.playwrightVersion));
107
105
  if (options.debug) console.log("Sending spans to", options.tracesEndpoint);
108
106
  const response = await fetch(options.tracesEndpoint, {
109
107
  method: "POST",
@@ -142,6 +140,7 @@ function normalizeTraceMode(trace) {
142
140
  //#region src/fixture/trace-context.ts
143
141
  const TRACE_CONTEXT_ATTACHMENT_NAME = "playwright-opentelemetry-trace-context";
144
142
  const FIXTURE_SPANS_ATTACHMENT_NAME = "playwright-opentelemetry-fixture-spans";
143
+ const BROWSER_SERVICE_NAME = "playwright-browser";
145
144
  async function createTestTraceContext(testInfo) {
146
145
  const traceContext = {
147
146
  traceId: generateTraceId(),
@@ -173,7 +172,6 @@ async function flushFixtureSpans(traceContext, config, options) {
173
172
  await Promise.all(destinations.map((destination) => sendSpans(traceContext.spans, {
174
173
  tracesEndpoint: destination.tracesEndpoint,
175
174
  headers: destination.headers,
176
- serviceName: "playwright-browser",
177
175
  playwrightVersion: "unknown",
178
176
  debug: config.debug
179
177
  })));
@@ -221,7 +219,6 @@ function resolvePlaywrightOpentelemetryConfig(config, options = {}) {
221
219
  storeTraceZip: config?.storeTraceZip === true,
222
220
  trace: config?.trace ?? null,
223
221
  propagateTraceHeaders: config?.propagateTraceHeaders ?? true,
224
- serviceName: process.env.OTEL_SERVICE_NAME || config?.serviceName || "playwright-tests",
225
222
  debug: debugEnv === void 0 ? config?.debug ?? false : debugEnv === "1" || debugEnv === "true"
226
223
  };
227
224
  if (options.requireDestination === true && !hasPlaywrightOpentelemetryDestination(resolvedConfig)) throw new Error(getConfigurationErrorMessage());
@@ -231,7 +228,7 @@ function hasPlaywrightOpentelemetryDestination(config) {
231
228
  return Boolean(config.otlpEndpoint || config.playwrightTraceApiEndpoint || config.storeTraceZip);
232
229
  }
233
230
  function getConfigurationErrorMessage() {
234
- return "playwright-opentelemetry reporter requires an OTLP endpoint, trace API endpoint, or storeTraceZip to be configured.\n\nYou can configure it using environment variables:\n\n export OTEL_EXPORTER_OTLP_ENDPOINT=\"https://api.honeycomb.io\"\n export OTEL_EXPORTER_OTLP_HEADERS=\"x-honeycomb-team=YOUR_API_KEY\"\n export OTEL_SERVICE_NAME=\"my-service\"\n\nOr via playwright.config.ts:\n\nimport { defineConfig } from '@playwright/test';\nimport type { PlaywrightOpentelemetryUseOptions } from 'playwright-opentelemetry/fixture';\n\nexport default defineConfig<PlaywrightOpentelemetryUseOptions>({\n use: {\n playwrightOpentelemetry: {\n otlpEndpoint: 'http://localhost:4317/v1/traces',\n otlpHeaders: {\n Authorization: 'Bearer YOUR_TOKEN',\n },\n serviceName: 'my-service',\n },\n },\n reporter: [['playwright-opentelemetry/reporter']],\n});\n\nNote: Environment variables take precedence over use.playwrightOpentelemetry options.\n";
231
+ return "playwright-opentelemetry reporter requires an OTLP endpoint, trace API endpoint, or storeTraceZip to be configured.\n\nYou can configure it using environment variables:\n\n export OTEL_EXPORTER_OTLP_ENDPOINT=\"https://api.honeycomb.io\"\n export OTEL_EXPORTER_OTLP_HEADERS=\"x-honeycomb-team=YOUR_API_KEY\"\n\nOr via playwright.config.ts:\n\nimport { defineConfig } from '@playwright/test';\nimport type { PlaywrightOpentelemetryUseOptions } from 'playwright-opentelemetry/fixture';\n\nexport default defineConfig<PlaywrightOpentelemetryUseOptions>({\n use: {\n playwrightOpentelemetry: {\n otlpEndpoint: 'http://localhost:4317/v1/traces',\n otlpHeaders: {\n Authorization: 'Bearer YOUR_TOKEN',\n },\n },\n },\n reporter: [['playwright-opentelemetry/reporter']],\n});\n\nNote: Environment variables take precedence over use.playwrightOpentelemetry options.\n";
235
232
  }
236
233
  //#endregion
237
- export { flushFixtureSpans as a, generateSpanId as c, createTestTraceContext as i, generateTraceId as l, FIXTURE_SPANS_ATTACHMENT_NAME as n, shouldRetainPlaywrightTrace as o, TRACE_CONTEXT_ATTACHMENT_NAME as r, buildOtlpRequest as s, resolvePlaywrightOpentelemetryConfig as t, sendSpans as u };
234
+ export { createTestTraceContext as a, buildOtlpRequest as c, sendSpans as d, TRACE_CONTEXT_ATTACHMENT_NAME as i, generateSpanId as l, BROWSER_SERVICE_NAME as n, flushFixtureSpans as o, FIXTURE_SPANS_ATTACHMENT_NAME as r, shouldRetainPlaywrightTrace as s, resolvePlaywrightOpentelemetryConfig as t, generateTraceId as u };
@@ -21,9 +21,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
21
21
  }) : target, mod));
22
22
  //#endregion
23
23
  //#region package.json
24
- var version = "0.9.0";
25
- //#endregion
26
- //#region src/shared/otel.ts
24
+ var version = "0.10.0";
27
25
  /** Generate a random 32-character hex trace ID. */
28
26
  function generateTraceId() {
29
27
  return Array.from({ length: 32 }, () => Math.floor(Math.random() * 16).toString(16)).join("");
@@ -111,10 +109,10 @@ function buildResourceSpan(spans, serviceName, playwrightVersion) {
111
109
  }]
112
110
  };
113
111
  }
114
- function buildOtlpRequest(spans, serviceName, playwrightVersion) {
112
+ function buildOtlpRequest(spans, playwrightVersion) {
115
113
  const spansByService = /* @__PURE__ */ new Map();
116
114
  for (const span of spans) {
117
- const spanServiceName = span.serviceName ?? serviceName;
115
+ const spanServiceName = span.serviceName ?? "playwright-tests";
118
116
  const serviceSpans = spansByService.get(spanServiceName);
119
117
  if (serviceSpans) serviceSpans.push(span);
120
118
  else spansByService.set(spanServiceName, [span]);
@@ -125,7 +123,7 @@ function buildOtlpRequest(spans, serviceName, playwrightVersion) {
125
123
  }
126
124
  async function sendSpans(spans, options) {
127
125
  if (spans.length === 0) return;
128
- const body = JSON.stringify(buildOtlpRequest(spans, options.serviceName, options.playwrightVersion));
126
+ const body = JSON.stringify(buildOtlpRequest(spans, options.playwrightVersion));
129
127
  if (options.debug) console.log("Sending spans to", options.tracesEndpoint);
130
128
  const response = await fetch(options.tracesEndpoint, {
131
129
  method: "POST",
@@ -164,6 +162,7 @@ function normalizeTraceMode(trace) {
164
162
  //#region src/fixture/trace-context.ts
165
163
  const TRACE_CONTEXT_ATTACHMENT_NAME = "playwright-opentelemetry-trace-context";
166
164
  const FIXTURE_SPANS_ATTACHMENT_NAME = "playwright-opentelemetry-fixture-spans";
165
+ const BROWSER_SERVICE_NAME = "playwright-browser";
167
166
  async function createTestTraceContext(testInfo) {
168
167
  const traceContext = {
169
168
  traceId: generateTraceId(),
@@ -195,7 +194,6 @@ async function flushFixtureSpans(traceContext, config, options) {
195
194
  await Promise.all(destinations.map((destination) => sendSpans(traceContext.spans, {
196
195
  tracesEndpoint: destination.tracesEndpoint,
197
196
  headers: destination.headers,
198
- serviceName: "playwright-browser",
199
197
  playwrightVersion: "unknown",
200
198
  debug: config.debug
201
199
  })));
@@ -243,7 +241,6 @@ function resolvePlaywrightOpentelemetryConfig(config, options = {}) {
243
241
  storeTraceZip: config?.storeTraceZip === true,
244
242
  trace: config?.trace ?? null,
245
243
  propagateTraceHeaders: config?.propagateTraceHeaders ?? true,
246
- serviceName: process.env.OTEL_SERVICE_NAME || config?.serviceName || "playwright-tests",
247
244
  debug: debugEnv === void 0 ? config?.debug ?? false : debugEnv === "1" || debugEnv === "true"
248
245
  };
249
246
  if (options.requireDestination === true && !hasPlaywrightOpentelemetryDestination(resolvedConfig)) throw new Error(getConfigurationErrorMessage());
@@ -253,9 +250,15 @@ function hasPlaywrightOpentelemetryDestination(config) {
253
250
  return Boolean(config.otlpEndpoint || config.playwrightTraceApiEndpoint || config.storeTraceZip);
254
251
  }
255
252
  function getConfigurationErrorMessage() {
256
- return "playwright-opentelemetry reporter requires an OTLP endpoint, trace API endpoint, or storeTraceZip to be configured.\n\nYou can configure it using environment variables:\n\n export OTEL_EXPORTER_OTLP_ENDPOINT=\"https://api.honeycomb.io\"\n export OTEL_EXPORTER_OTLP_HEADERS=\"x-honeycomb-team=YOUR_API_KEY\"\n export OTEL_SERVICE_NAME=\"my-service\"\n\nOr via playwright.config.ts:\n\nimport { defineConfig } from '@playwright/test';\nimport type { PlaywrightOpentelemetryUseOptions } from 'playwright-opentelemetry/fixture';\n\nexport default defineConfig<PlaywrightOpentelemetryUseOptions>({\n use: {\n playwrightOpentelemetry: {\n otlpEndpoint: 'http://localhost:4317/v1/traces',\n otlpHeaders: {\n Authorization: 'Bearer YOUR_TOKEN',\n },\n serviceName: 'my-service',\n },\n },\n reporter: [['playwright-opentelemetry/reporter']],\n});\n\nNote: Environment variables take precedence over use.playwrightOpentelemetry options.\n";
253
+ return "playwright-opentelemetry reporter requires an OTLP endpoint, trace API endpoint, or storeTraceZip to be configured.\n\nYou can configure it using environment variables:\n\n export OTEL_EXPORTER_OTLP_ENDPOINT=\"https://api.honeycomb.io\"\n export OTEL_EXPORTER_OTLP_HEADERS=\"x-honeycomb-team=YOUR_API_KEY\"\n\nOr via playwright.config.ts:\n\nimport { defineConfig } from '@playwright/test';\nimport type { PlaywrightOpentelemetryUseOptions } from 'playwright-opentelemetry/fixture';\n\nexport default defineConfig<PlaywrightOpentelemetryUseOptions>({\n use: {\n playwrightOpentelemetry: {\n otlpEndpoint: 'http://localhost:4317/v1/traces',\n otlpHeaders: {\n Authorization: 'Bearer YOUR_TOKEN',\n },\n },\n },\n reporter: [['playwright-opentelemetry/reporter']],\n});\n\nNote: Environment variables take precedence over use.playwrightOpentelemetry options.\n";
257
254
  }
258
255
  //#endregion
256
+ Object.defineProperty(exports, "BROWSER_SERVICE_NAME", {
257
+ enumerable: true,
258
+ get: function() {
259
+ return BROWSER_SERVICE_NAME;
260
+ }
261
+ });
259
262
  Object.defineProperty(exports, "FIXTURE_SPANS_ATTACHMENT_NAME", {
260
263
  enumerable: true,
261
264
  get: function() {
package/dist/fixture.cjs CHANGED
@@ -1,13 +1,12 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_config = require("./config-DtWFdgrv.cjs");
2
+ const require_config = require("./config-DiOttOMJ.cjs");
3
3
  let node_path = require("node:path");
4
4
  node_path = require_config.__toESM(node_path, 1);
5
5
  let _playwright_test = require("@playwright/test");
6
6
  //#region src/fixture/browser-page-tracker.ts
7
7
  const BROWSER_PAGE_SPAN_NAME = "browser.page";
8
8
  const BROWSER_ROUTE_SPAN_NAME = "browser.route";
9
- const BROWSER_SERVICE_NAME = "playwright-browser";
10
- const SPAN_STATUS_CODE_UNSET$1 = 0;
9
+ const SPAN_STATUS_CODE_UNSET$2 = 0;
11
10
  var BrowserPageTracker = class {
12
11
  traceContext;
13
12
  nextPageId = 1;
@@ -169,7 +168,7 @@ var BrowserPageTracker = class {
169
168
  name,
170
169
  startTime,
171
170
  endTime: startTime,
172
- status: { code: SPAN_STATUS_CODE_UNSET$1 },
171
+ status: { code: SPAN_STATUS_CODE_UNSET$2 },
173
172
  attributes: pageAttributes({
174
173
  pageId,
175
174
  url,
@@ -178,7 +177,7 @@ var BrowserPageTracker = class {
178
177
  documentUrl
179
178
  }),
180
179
  events: [],
181
- serviceName: BROWSER_SERVICE_NAME
180
+ serviceName: require_config.BROWSER_SERVICE_NAME
182
181
  };
183
182
  }
184
183
  };
@@ -333,13 +332,13 @@ function detectResourceTypeFromUrl(url) {
333
332
  * OpenTelemetry SpanKind values
334
333
  * @see https://opentelemetry.io/docs/specs/otel/trace/api/#spankind
335
334
  */
336
- const SPAN_KIND_CLIENT = 3;
335
+ const SPAN_KIND_CLIENT$1 = 3;
337
336
  /**
338
337
  * OpenTelemetry SpanStatusCode values
339
338
  * @see https://opentelemetry.io/docs/specs/otel/trace/api/#set-status
340
339
  */
341
- const SPAN_STATUS_CODE_UNSET = 0;
342
- const SPAN_STATUS_CODE_ERROR = 2;
340
+ const SPAN_STATUS_CODE_UNSET$1 = 0;
341
+ const SPAN_STATUS_CODE_ERROR$1 = 2;
343
342
  /**
344
343
  * Captures request/response data and stores the complete network span in the
345
344
  * current test trace context. Called after the request has completed.
@@ -358,8 +357,8 @@ async function fixtureCaptureRequestResponse({ request, response, traceContext }
358
357
  const parsedUrl = new URL(url);
359
358
  const serverAddress = parsedUrl.hostname;
360
359
  const serverPort = parsedUrl.port !== "" ? Number.parseInt(parsedUrl.port, 10) : parsedUrl.protocol === "https:" ? 443 : 80;
361
- let statusCodeValue = SPAN_STATUS_CODE_UNSET;
362
- if (statusCode >= 400) statusCodeValue = SPAN_STATUS_CODE_ERROR;
360
+ let statusCodeValue = SPAN_STATUS_CODE_UNSET$1;
361
+ if (statusCode >= 400) statusCodeValue = SPAN_STATUS_CODE_ERROR$1;
363
362
  const resourceType = detectResourceType(await getResponseHeaderValue(response, "content-type"), url);
364
363
  const attributes = {
365
364
  "http.request.method": method,
@@ -372,19 +371,19 @@ async function fixtureCaptureRequestResponse({ request, response, traceContext }
372
371
  "browser.request.route_association": requestTraceContext.routeAssociation
373
372
  };
374
373
  if (parsedUrl.search) attributes["url.query"] = parsedUrl.search.slice(1);
375
- if (statusCodeValue === SPAN_STATUS_CODE_ERROR) attributes["error.type"] = statusCode.toString();
374
+ if (statusCodeValue === SPAN_STATUS_CODE_ERROR$1) attributes["error.type"] = statusCode.toString();
376
375
  const networkSpan = {
377
376
  traceId: requestTraceContext.traceId,
378
377
  spanId: requestTraceContext.spanId,
379
378
  parentSpanId: requestTraceContext.parentSpanId,
380
379
  name: `HTTP ${method}`,
381
- kind: SPAN_KIND_CLIENT,
380
+ kind: SPAN_KIND_CLIENT$1,
382
381
  startTime,
383
382
  endTime,
384
383
  status: { code: statusCodeValue },
385
384
  attributes,
386
385
  events: [],
387
- serviceName: "playwright-browser"
386
+ serviceName: require_config.BROWSER_SERVICE_NAME
388
387
  };
389
388
  traceContext.addSpan(networkSpan);
390
389
  }
@@ -396,6 +395,120 @@ async function getResponseHeaderValue(response, name) {
396
395
  }
397
396
  }
398
397
  //#endregion
398
+ //#region src/fixture/test-fetch-capture.ts
399
+ const SPAN_KIND_CLIENT = 3;
400
+ const SPAN_STATUS_CODE_UNSET = 0;
401
+ const SPAN_STATUS_CODE_ERROR = 2;
402
+ let originalFetch;
403
+ let activeTraceContext;
404
+ function runWithTestFetchCapture(traceContext, callback) {
405
+ ensureFetchPatched();
406
+ const previousTraceContext = activeTraceContext;
407
+ activeTraceContext = traceContext;
408
+ return (async () => {
409
+ try {
410
+ return await callback();
411
+ } finally {
412
+ activeTraceContext = previousTraceContext;
413
+ }
414
+ })();
415
+ }
416
+ function ensureFetchPatched() {
417
+ if (globalThis.fetch === patchedFetch) return;
418
+ originalFetch = globalThis.fetch.bind(globalThis);
419
+ globalThis.fetch = patchedFetch;
420
+ }
421
+ const patchedFetch = async (input, init) => {
422
+ const traceContext = activeTraceContext;
423
+ if (!traceContext) return originalFetch ? originalFetch(input, init) : globalThis.fetch(input, init);
424
+ if (!originalFetch) return globalThis.fetch(input, init);
425
+ const startTime = /* @__PURE__ */ new Date();
426
+ try {
427
+ const response = await originalFetch(input, init);
428
+ traceContext.addSpan(createFetchSpan({
429
+ input,
430
+ init,
431
+ traceContext,
432
+ startTime,
433
+ endTime: /* @__PURE__ */ new Date(),
434
+ statusCode: response.status
435
+ }));
436
+ return response;
437
+ } catch (error) {
438
+ traceContext.addSpan(createFetchSpan({
439
+ input,
440
+ init,
441
+ traceContext,
442
+ startTime,
443
+ endTime: /* @__PURE__ */ new Date(),
444
+ error
445
+ }));
446
+ throw error;
447
+ }
448
+ };
449
+ function createFetchSpan({ input, init, traceContext, startTime, endTime, statusCode, error }) {
450
+ const method = getFetchMethod(input, init);
451
+ const attributes = createHttpAttributes({
452
+ method,
453
+ url: getFetchUrl(input),
454
+ statusCode,
455
+ error
456
+ });
457
+ const failed = error !== void 0 || statusCode !== void 0 && statusCode >= 400;
458
+ return {
459
+ traceId: traceContext.traceId,
460
+ spanId: require_config.generateSpanId(),
461
+ parentSpanId: traceContext.rootSpanId,
462
+ name: `HTTP ${method}`,
463
+ kind: SPAN_KIND_CLIENT,
464
+ startTime,
465
+ endTime,
466
+ status: failed ? {
467
+ code: SPAN_STATUS_CODE_ERROR,
468
+ message: errorMessage(error)
469
+ } : { code: SPAN_STATUS_CODE_UNSET },
470
+ attributes,
471
+ events: []
472
+ };
473
+ }
474
+ function createHttpAttributes({ method, url, statusCode, error }) {
475
+ const attributes = {
476
+ "http.request.method": method,
477
+ "http.resource.type": "fetch",
478
+ "url.full": url
479
+ };
480
+ try {
481
+ const parsedUrl = new URL(url);
482
+ attributes["url.path"] = parsedUrl.pathname;
483
+ attributes["server.address"] = parsedUrl.hostname;
484
+ attributes["server.port"] = serverPort(parsedUrl);
485
+ if (parsedUrl.search) attributes["url.query"] = parsedUrl.search.slice(1);
486
+ } catch {}
487
+ if (statusCode !== void 0) {
488
+ attributes["http.response.status_code"] = statusCode;
489
+ if (statusCode >= 400) attributes["error.type"] = statusCode.toString();
490
+ } else if (error !== void 0) attributes["error.type"] = errorType(error);
491
+ return attributes;
492
+ }
493
+ function getFetchMethod(input, init) {
494
+ return (init?.method ?? (input instanceof Request ? input.method : "GET")).toUpperCase();
495
+ }
496
+ function getFetchUrl(input) {
497
+ if (input instanceof Request) return input.url;
498
+ return input.toString();
499
+ }
500
+ function serverPort(url) {
501
+ if (url.port) return Number.parseInt(url.port, 10);
502
+ return url.protocol === "https:" ? 443 : 80;
503
+ }
504
+ function errorType(error) {
505
+ if (error instanceof Error && error.name) return error.name;
506
+ return "Error";
507
+ }
508
+ function errorMessage(error) {
509
+ return error instanceof Error && error.message ? error.message : void 0;
510
+ }
511
+ //#endregion
399
512
  //#region src/fixture/playwright-opentelemetry-fixture.ts
400
513
  function createPlaywrightOtelTest(testBase) {
401
514
  return testBase.extend({
@@ -417,7 +530,7 @@ function createPlaywrightOtelTest(testBase) {
417
530
  testTraceContext: [async ({ playwrightOpentelemetry, trace }, use, testInfo) => {
418
531
  const config = require_config.resolvePlaywrightOpentelemetryConfig(playwrightOpentelemetry);
419
532
  const traceContext = await require_config.createTestTraceContext(testInfo);
420
- await use(traceContext);
533
+ await runWithTestFetchCapture(traceContext, () => use(traceContext));
421
534
  await require_config.flushFixtureSpans(traceContext, config, {
422
535
  trace: config.trace ?? trace,
423
536
  testInfo
@@ -1,4 +1,4 @@
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 { n as SpanEvent, t as Span } from "./otel-D0ztpic2.cjs";
3
3
  import { ConsoleMessage, Page, Request } from "@playwright/test";
4
4
  export * from "@playwright/test";
@@ -1,4 +1,4 @@
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 { n as SpanEvent, t as Span } from "./otel-D0ztpic2.mjs";
3
3
  import { ConsoleMessage, Page, Request } from "@playwright/test";
4
4
  export * from "@playwright/test";
package/dist/fixture.mjs CHANGED
@@ -1,12 +1,11 @@
1
- import { a as flushFixtureSpans, c as generateSpanId, i as createTestTraceContext, t as resolvePlaywrightOpentelemetryConfig } from "./config-Cec6zO6v.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
2
  import path from "node:path";
3
3
  import { test as test$1 } from "@playwright/test";
4
4
  export * from "@playwright/test";
5
5
  //#region src/fixture/browser-page-tracker.ts
6
6
  const BROWSER_PAGE_SPAN_NAME = "browser.page";
7
7
  const BROWSER_ROUTE_SPAN_NAME = "browser.route";
8
- const BROWSER_SERVICE_NAME = "playwright-browser";
9
- const SPAN_STATUS_CODE_UNSET$1 = 0;
8
+ const SPAN_STATUS_CODE_UNSET$2 = 0;
10
9
  var BrowserPageTracker = class {
11
10
  traceContext;
12
11
  nextPageId = 1;
@@ -168,7 +167,7 @@ var BrowserPageTracker = class {
168
167
  name,
169
168
  startTime,
170
169
  endTime: startTime,
171
- status: { code: SPAN_STATUS_CODE_UNSET$1 },
170
+ status: { code: SPAN_STATUS_CODE_UNSET$2 },
172
171
  attributes: pageAttributes({
173
172
  pageId,
174
173
  url,
@@ -332,13 +331,13 @@ function detectResourceTypeFromUrl(url) {
332
331
  * OpenTelemetry SpanKind values
333
332
  * @see https://opentelemetry.io/docs/specs/otel/trace/api/#spankind
334
333
  */
335
- const SPAN_KIND_CLIENT = 3;
334
+ const SPAN_KIND_CLIENT$1 = 3;
336
335
  /**
337
336
  * OpenTelemetry SpanStatusCode values
338
337
  * @see https://opentelemetry.io/docs/specs/otel/trace/api/#set-status
339
338
  */
340
- const SPAN_STATUS_CODE_UNSET = 0;
341
- const SPAN_STATUS_CODE_ERROR = 2;
339
+ const SPAN_STATUS_CODE_UNSET$1 = 0;
340
+ const SPAN_STATUS_CODE_ERROR$1 = 2;
342
341
  /**
343
342
  * Captures request/response data and stores the complete network span in the
344
343
  * current test trace context. Called after the request has completed.
@@ -357,8 +356,8 @@ async function fixtureCaptureRequestResponse({ request, response, traceContext }
357
356
  const parsedUrl = new URL(url);
358
357
  const serverAddress = parsedUrl.hostname;
359
358
  const serverPort = parsedUrl.port !== "" ? Number.parseInt(parsedUrl.port, 10) : parsedUrl.protocol === "https:" ? 443 : 80;
360
- let statusCodeValue = SPAN_STATUS_CODE_UNSET;
361
- if (statusCode >= 400) statusCodeValue = SPAN_STATUS_CODE_ERROR;
359
+ let statusCodeValue = SPAN_STATUS_CODE_UNSET$1;
360
+ if (statusCode >= 400) statusCodeValue = SPAN_STATUS_CODE_ERROR$1;
362
361
  const resourceType = detectResourceType(await getResponseHeaderValue(response, "content-type"), url);
363
362
  const attributes = {
364
363
  "http.request.method": method,
@@ -371,19 +370,19 @@ async function fixtureCaptureRequestResponse({ request, response, traceContext }
371
370
  "browser.request.route_association": requestTraceContext.routeAssociation
372
371
  };
373
372
  if (parsedUrl.search) attributes["url.query"] = parsedUrl.search.slice(1);
374
- if (statusCodeValue === SPAN_STATUS_CODE_ERROR) attributes["error.type"] = statusCode.toString();
373
+ if (statusCodeValue === SPAN_STATUS_CODE_ERROR$1) attributes["error.type"] = statusCode.toString();
375
374
  const networkSpan = {
376
375
  traceId: requestTraceContext.traceId,
377
376
  spanId: requestTraceContext.spanId,
378
377
  parentSpanId: requestTraceContext.parentSpanId,
379
378
  name: `HTTP ${method}`,
380
- kind: SPAN_KIND_CLIENT,
379
+ kind: SPAN_KIND_CLIENT$1,
381
380
  startTime,
382
381
  endTime,
383
382
  status: { code: statusCodeValue },
384
383
  attributes,
385
384
  events: [],
386
- serviceName: "playwright-browser"
385
+ serviceName: BROWSER_SERVICE_NAME
387
386
  };
388
387
  traceContext.addSpan(networkSpan);
389
388
  }
@@ -395,6 +394,120 @@ async function getResponseHeaderValue(response, name) {
395
394
  }
396
395
  }
397
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
398
511
  //#region src/fixture/playwright-opentelemetry-fixture.ts
399
512
  function createPlaywrightOtelTest(testBase) {
400
513
  return testBase.extend({
@@ -416,7 +529,7 @@ function createPlaywrightOtelTest(testBase) {
416
529
  testTraceContext: [async ({ playwrightOpentelemetry, trace }, use, testInfo) => {
417
530
  const config = resolvePlaywrightOpentelemetryConfig(playwrightOpentelemetry);
418
531
  const traceContext = await createTestTraceContext(testInfo);
419
- await use(traceContext);
532
+ await runWithTestFetchCapture(traceContext, () => use(traceContext));
420
533
  await flushFixtureSpans(traceContext, config, {
421
534
  trace: config.trace ?? trace,
422
535
  testInfo
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
1
  //#region src/index.ts
2
- var src_default = require("./reporter-k3LuaBV8.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-BAfvjrKo.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
@@ -1,4 +1,4 @@
1
- import { c as generateSpanId, l as generateTraceId, n as FIXTURE_SPANS_ATTACHMENT_NAME, o as shouldRetainPlaywrightTrace, r as TRACE_CONTEXT_ATTACHMENT_NAME, s as buildOtlpRequest, t as resolvePlaywrightOpentelemetryConfig, u as sendSpans } from "./config-Cec6zO6v.mjs";
1
+ import { c as buildOtlpRequest, d as sendSpans, i as TRACE_CONTEXT_ATTACHMENT_NAME, l as generateSpanId, r as FIXTURE_SPANS_ATTACHMENT_NAME, s as shouldRetainPlaywrightTrace, t as resolvePlaywrightOpentelemetryConfig, u as generateTraceId } from "./config-DKm2ymJf.mjs";
2
2
  import path from "node:path";
3
3
  import fs from "node:fs/promises";
4
4
  import { BlobReader, BlobWriter, TextWriter, ZipReader, ZipWriter } from "@zip.js/zip.js";
@@ -77,14 +77,14 @@ async function createScreenshotsZip(screenshots) {
77
77
  return zipWriter.close();
78
78
  }
79
79
  async function createTraceZipBlob(options) {
80
- const { spans, fixtureSpans, serviceName, playwrightVersion, screenshots } = options;
81
- const otlpRequest = buildOtlpRequest(spans, serviceName, playwrightVersion);
80
+ const { spans, fixtureSpans, playwrightVersion, screenshots } = options;
81
+ const otlpRequest = buildOtlpRequest(spans, playwrightVersion);
82
82
  const traceJson = JSON.stringify(otlpRequest, null, 2);
83
83
  const zipWriter = new ZipWriter(new BlobWriter("application/zip"));
84
84
  await zipWriter.add("traces/playwright-opentelemetry.json", new Blob([traceJson]).stream());
85
85
  if (fixtureSpans.length > 0) {
86
- const fixtureOtlpRequest = buildOtlpRequest(fixtureSpans, "playwright-browser", playwrightVersion);
87
- await zipWriter.add("traces/playwright-browser.json", new Blob([JSON.stringify(fixtureOtlpRequest, null, 2)]).stream());
86
+ const fixtureOtlpRequest = buildOtlpRequest(fixtureSpans, playwrightVersion);
87
+ await zipWriter.add("traces/playwright-fixture-spans.json", new Blob([JSON.stringify(fixtureOtlpRequest, null, 2)]).stream());
88
88
  }
89
89
  await addScreenshotsToZip(zipWriter, screenshots);
90
90
  return zipWriter.close();
@@ -271,14 +271,12 @@ var PlaywrightOpentelemetryReporter = class {
271
271
  if (batch.config.otlpEndpoint) addDestinationSpans(destinations, batch.spans, {
272
272
  tracesEndpoint: batch.config.otlpEndpoint,
273
273
  headers: batch.config.otlpHeaders,
274
- serviceName: batch.config.serviceName,
275
274
  playwrightVersion: this.playwrightVersion || "unknown",
276
275
  debug: batch.config.debug
277
276
  });
278
277
  if (batch.config.playwrightTraceApiEndpoint) addDestinationSpans(destinations, batch.spans, {
279
278
  tracesEndpoint: `${batch.config.playwrightTraceApiEndpoint}/v1/traces`,
280
279
  headers: batch.config.playwrightTraceApiHeaders,
281
- serviceName: batch.config.serviceName,
282
280
  playwrightVersion: this.playwrightVersion || "unknown",
283
281
  debug: batch.config.debug
284
282
  });
@@ -291,7 +289,6 @@ var PlaywrightOpentelemetryReporter = class {
291
289
  test: options.test,
292
290
  spans: options.spans,
293
291
  fixtureSpans: options.fixtureSpans,
294
- serviceName: options.config.serviceName,
295
292
  playwrightVersion: options.playwrightVersion,
296
293
  screenshots
297
294
  }) : void 0;
@@ -1,4 +1,4 @@
1
- const require_config = require("./config-DtWFdgrv.cjs");
1
+ const require_config = require("./config-DiOttOMJ.cjs");
2
2
  let node_path = require("node:path");
3
3
  node_path = require_config.__toESM(node_path, 1);
4
4
  let node_fs_promises = require("node:fs/promises");
@@ -79,14 +79,14 @@ async function createScreenshotsZip(screenshots) {
79
79
  return zipWriter.close();
80
80
  }
81
81
  async function createTraceZipBlob(options) {
82
- const { spans, fixtureSpans, serviceName, playwrightVersion, screenshots } = options;
83
- const otlpRequest = require_config.buildOtlpRequest(spans, serviceName, playwrightVersion);
82
+ const { spans, fixtureSpans, playwrightVersion, screenshots } = options;
83
+ const otlpRequest = require_config.buildOtlpRequest(spans, playwrightVersion);
84
84
  const traceJson = JSON.stringify(otlpRequest, null, 2);
85
85
  const zipWriter = new _zip_js_zip_js.ZipWriter(new _zip_js_zip_js.BlobWriter("application/zip"));
86
86
  await zipWriter.add("traces/playwright-opentelemetry.json", new Blob([traceJson]).stream());
87
87
  if (fixtureSpans.length > 0) {
88
- const fixtureOtlpRequest = require_config.buildOtlpRequest(fixtureSpans, "playwright-browser", playwrightVersion);
89
- await zipWriter.add("traces/playwright-browser.json", new Blob([JSON.stringify(fixtureOtlpRequest, null, 2)]).stream());
88
+ const fixtureOtlpRequest = require_config.buildOtlpRequest(fixtureSpans, playwrightVersion);
89
+ await zipWriter.add("traces/playwright-fixture-spans.json", new Blob([JSON.stringify(fixtureOtlpRequest, null, 2)]).stream());
90
90
  }
91
91
  await addScreenshotsToZip(zipWriter, screenshots);
92
92
  return zipWriter.close();
@@ -273,14 +273,12 @@ var PlaywrightOpentelemetryReporter = class {
273
273
  if (batch.config.otlpEndpoint) addDestinationSpans(destinations, batch.spans, {
274
274
  tracesEndpoint: batch.config.otlpEndpoint,
275
275
  headers: batch.config.otlpHeaders,
276
- serviceName: batch.config.serviceName,
277
276
  playwrightVersion: this.playwrightVersion || "unknown",
278
277
  debug: batch.config.debug
279
278
  });
280
279
  if (batch.config.playwrightTraceApiEndpoint) addDestinationSpans(destinations, batch.spans, {
281
280
  tracesEndpoint: `${batch.config.playwrightTraceApiEndpoint}/v1/traces`,
282
281
  headers: batch.config.playwrightTraceApiHeaders,
283
- serviceName: batch.config.serviceName,
284
282
  playwrightVersion: this.playwrightVersion || "unknown",
285
283
  debug: batch.config.debug
286
284
  });
@@ -293,7 +291,6 @@ var PlaywrightOpentelemetryReporter = class {
293
291
  test: options.test,
294
292
  spans: options.spans,
295
293
  fixtureSpans: options.fixtureSpans,
296
- serviceName: options.config.serviceName,
297
294
  playwrightVersion: options.playwrightVersion,
298
295
  screenshots
299
296
  }) : void 0;
package/dist/reporter.cjs CHANGED
@@ -1,4 +1,4 @@
1
1
  //#region src/reporter/index.ts
2
- var reporter_default = require("./reporter-k3LuaBV8.cjs").PlaywrightOpentelemetryReporter;
2
+ var reporter_default = require("./reporter-yOZUJcsx.cjs").PlaywrightOpentelemetryReporter;
3
3
  //#endregion
4
4
  module.exports = reporter_default;
package/dist/reporter.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as PlaywrightOpentelemetryReporter } from "./reporter-BAfvjrKo.mjs";
1
+ import { t as PlaywrightOpentelemetryReporter } from "./reporter-BGDFTuxD.mjs";
2
2
  //#region src/reporter/index.ts
3
3
  var reporter_default = PlaywrightOpentelemetryReporter;
4
4
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playwright-opentelemetry",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "A reporter for Playwright that can export traces and spans in opentelemetry format.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",