playwright-opentelemetry 0.6.1 → 0.7.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,8 +1,8 @@
1
- const require_trace_context = require("./trace-context-Dd2Fmx9q.cjs");
1
+ const require_config = require("./config-C8VVYvBv.cjs");
2
2
  let node_path = require("node:path");
3
- node_path = require_trace_context.__toESM(node_path, 1);
3
+ node_path = require_config.__toESM(node_path, 1);
4
4
  let node_fs_promises = require("node:fs/promises");
5
- node_fs_promises = require_trace_context.__toESM(node_fs_promises, 1);
5
+ node_fs_promises = require_config.__toESM(node_fs_promises, 1);
6
6
  let _zip_js_zip_js = require("@zip.js/zip.js");
7
7
  //#region src/reporter/otel-attributes.ts
8
8
  const ATTR_CODE_FILE_PATH = "code.file.path";
@@ -71,12 +71,12 @@ async function createScreenshotsZip(screenshots) {
71
71
  }
72
72
  async function createTraceZipBlob(options) {
73
73
  const { spans, fixtureSpans, serviceName, playwrightVersion, screenshots } = options;
74
- const otlpRequest = require_trace_context.buildOtlpRequest(spans, serviceName, playwrightVersion);
74
+ const otlpRequest = require_config.buildOtlpRequest(spans, serviceName, playwrightVersion);
75
75
  const traceJson = JSON.stringify(otlpRequest, null, 2);
76
76
  const zipWriter = new _zip_js_zip_js.ZipWriter(new _zip_js_zip_js.BlobWriter("application/zip"));
77
77
  await zipWriter.add("traces/playwright-opentelemetry.json", new Blob([traceJson]).stream());
78
78
  if (fixtureSpans.length > 0) {
79
- const fixtureOtlpRequest = require_trace_context.buildOtlpRequest(fixtureSpans, "playwright-browser", playwrightVersion);
79
+ const fixtureOtlpRequest = require_config.buildOtlpRequest(fixtureSpans, "playwright-browser", playwrightVersion);
80
80
  await zipWriter.add("traces/playwright-browser.json", new Blob([JSON.stringify(fixtureOtlpRequest, null, 2)]).stream());
81
81
  }
82
82
  await addScreenshotsToZip(zipWriter, screenshots);
@@ -136,7 +136,7 @@ var PlaywrightOpentelemetryReporter = class {
136
136
  this.playwrightVersion = config.version;
137
137
  const projects = Array.isArray(config.projects) ? config.projects : [];
138
138
  for (const project of projects) {
139
- const resolvedConfig = require_trace_context.resolvePlaywrightOpentelemetryConfig(getProjectPlaywrightOpentelemetryConfig(project), { requireDestination: true });
139
+ const resolvedConfig = require_config.resolvePlaywrightOpentelemetryConfig(getProjectPlaywrightOpentelemetryConfig(project), { requireDestination: true });
140
140
  this.debug ||= resolvedConfig.debug;
141
141
  }
142
142
  }
@@ -144,11 +144,16 @@ var PlaywrightOpentelemetryReporter = class {
144
144
  onStepBegin(_test, _result, _step) {}
145
145
  onStepEnd(_test, _result, _step) {}
146
146
  onTestEnd(test, result) {
147
- const testId = test.id;
148
- const outputDir = getTestOutputDir(test);
149
147
  const config = getTestConfig(test);
150
- const { traceId, rootSpanId: testSpanId } = readTraceContextAttachment(result, testId, config.storeTraceZip);
151
148
  const traceAttachment = result.attachments.find((attachment) => attachment.name === "trace" && attachment.contentType === "application/zip" && attachment.path);
149
+ if (!(config.trace === null ? Boolean(traceAttachment) : require_config.shouldRetainPlaywrightTrace(config.trace, {
150
+ expectedStatus: test.expectedStatus,
151
+ retry: result.retry,
152
+ status: result.status
153
+ }))) return;
154
+ const testId = test.id;
155
+ const outputDir = getTestOutputDir(test);
156
+ const { traceId, rootSpanId: testSpanId } = readTraceContextAttachment(result, testId);
152
157
  const fixtureSpans = config.storeTraceZip ? readFixtureSpansAttachment(result, testId) : [];
153
158
  result.attachments.push({
154
159
  name: "playwright-opentelemetry-trace-id",
@@ -178,15 +183,16 @@ var PlaywrightOpentelemetryReporter = class {
178
183
  if (stepSpan.startTime < minStartTime) minStartTime = stepSpan.startTime;
179
184
  if (stepSpan.endTime > maxEndTime) maxEndTime = stepSpan.endTime;
180
185
  }
181
- const testSpans = [{
186
+ const span = {
182
187
  traceId,
183
188
  spanId: testSpanId,
184
189
  name: TEST_SPAN_NAME,
185
190
  startTime: minStartTime,
186
191
  endTime: maxEndTime,
187
- attributes,
188
- status: { code: result.status === test.expectedStatus ? 1 : 2 }
189
- }, ...stepSpans];
192
+ attributes
193
+ };
194
+ if (result.status !== test.expectedStatus) span.status = { code: 2 };
195
+ const testSpans = [span, ...stepSpans];
190
196
  this.spanBatches.push({
191
197
  spans: testSpans,
192
198
  config
@@ -231,7 +237,7 @@ var PlaywrightOpentelemetryReporter = class {
231
237
  debug: batch.config.debug
232
238
  });
233
239
  }
234
- for (const destination of destinations.values()) await require_trace_context.sendSpans(destination.spans, destination.options);
240
+ for (const destination of destinations.values()) await require_config.sendSpans(destination.spans, destination.options);
235
241
  }
236
242
  async prepareTraceArtifact(options) {
237
243
  const screenshots = options.traceAttachmentPath ? await extractScreenshotsFromPlaywrightTrace(options.traceAttachmentPath) : /* @__PURE__ */ new Map();
@@ -252,9 +258,8 @@ var PlaywrightOpentelemetryReporter = class {
252
258
  return { traceZipBlob };
253
259
  }
254
260
  processTestStep(test, step, parentSpanId, traceId, parentTitlePath, processedSteps, skippedStepIds) {
255
- const isInternalFixture = step.category === "fixture" && step.location?.file.includes("playwright-opentelemetry") && (step.location?.file.endsWith("fixture.mjs") || step.location?.file.endsWith("fixture/index.ts"));
256
261
  const stepId = getStepId(test, step);
257
- if (isInternalFixture) {
262
+ if (isInternalFixtureStep(step)) {
258
263
  skippedStepIds.add(stepId);
259
264
  processedSteps.delete(stepId);
260
265
  if (step.steps && step.steps.length > 0) for (const childStep of step.steps) this.processTestStep(test, childStep, parentSpanId, traceId, parentTitlePath, processedSteps, skippedStepIds);
@@ -267,6 +272,7 @@ var PlaywrightOpentelemetryReporter = class {
267
272
  const currentTitlePath = [...parentTitlePath, step.title];
268
273
  const existingSpan = processedSteps.get(stepId);
269
274
  if (existingSpan) {
275
+ if (step.error) existingSpan.status = errorStatus(step.error.message);
270
276
  if (step.location && !existingSpan.attributes["code.file.path"]) {
271
277
  const { file, line } = step.location;
272
278
  const relativePath = this.rootDir ? node_path.default.relative(this.rootDir, file) : file;
@@ -287,14 +293,14 @@ var PlaywrightOpentelemetryReporter = class {
287
293
  }
288
294
  const stepSpan = {
289
295
  traceId,
290
- spanId: require_trace_context.generateSpanId(),
296
+ spanId: require_config.generateSpanId(),
291
297
  parentSpanId,
292
298
  name: TEST_STEP_SPAN_NAME,
293
299
  startTime: step.startTime,
294
300
  endTime: new Date(step.startTime.getTime() + step.duration),
295
- attributes,
296
- status: { code: step.error ? 2 : 1 }
301
+ attributes
297
302
  };
303
+ if (step.error) stepSpan.status = errorStatus(step.error.message);
298
304
  processedSteps.set(stepId, stepSpan);
299
305
  if (step.steps && step.steps.length > 0) for (const childStep of step.steps) this.processTestStep(test, childStep, stepSpan.spanId, traceId, currentTitlePath, processedSteps, skippedStepIds);
300
306
  }
@@ -330,7 +336,7 @@ function getProjectPlaywrightOpentelemetryConfig(project) {
330
336
  return project?.use?.playwrightOpentelemetry;
331
337
  }
332
338
  function getTestConfig(test) {
333
- return require_trace_context.resolvePlaywrightOpentelemetryConfig(getProjectPlaywrightOpentelemetryConfig(test.parent.project()), { requireDestination: true });
339
+ return require_config.resolvePlaywrightOpentelemetryConfig(getProjectPlaywrightOpentelemetryConfig(test.parent.project()), { requireDestination: true });
334
340
  }
335
341
  function getTestOutputDir(test) {
336
342
  const outputDir = test.parent.project()?.outputDir;
@@ -349,41 +355,38 @@ function addDestinationSpans(destinations, spans, options) {
349
355
  options
350
356
  });
351
357
  }
352
- function readTraceContextAttachment(result, testId, allowReporterOnlyFallback) {
353
- const attachment = result.attachments.find((attachment) => attachment.name === require_trace_context.TRACE_CONTEXT_ATTACHMENT_NAME);
354
- if (!attachment?.body) {
355
- if (allowReporterOnlyFallback) return {
356
- traceId: require_trace_context.generateTraceId(),
357
- rootSpanId: require_trace_context.generateSpanId()
358
- };
359
- throw new Error(`playwright-opentelemetry fixture missing for test ${testId}: expected ${require_trace_context.TRACE_CONTEXT_ATTACHMENT_NAME} attachment. Import test from "playwright-opentelemetry/fixture" or wrap your base test with createPlaywrightOtelTest().`);
360
- }
358
+ function readTraceContextAttachment(result, testId) {
359
+ const attachment = result.attachments.find((attachment) => attachment.name === require_config.TRACE_CONTEXT_ATTACHMENT_NAME);
360
+ if (!attachment?.body) return {
361
+ traceId: require_config.generateTraceId(),
362
+ rootSpanId: require_config.generateSpanId()
363
+ };
361
364
  let parsed;
362
365
  try {
363
366
  parsed = JSON.parse(attachment.body.toString("utf-8"));
364
367
  } catch (error) {
365
- throw new Error(`Invalid ${require_trace_context.TRACE_CONTEXT_ATTACHMENT_NAME} attachment for test ${testId}: ${error}`);
368
+ throw new Error(`Invalid ${require_config.TRACE_CONTEXT_ATTACHMENT_NAME} attachment for test ${testId}: ${error}`);
366
369
  }
367
- if (!isTraceContextAttachment(parsed)) throw new Error(`Invalid ${require_trace_context.TRACE_CONTEXT_ATTACHMENT_NAME} attachment for test ${testId}: expected { traceId, rootSpanId }`);
370
+ if (!isTraceContextAttachment(parsed)) throw new Error(`Invalid ${require_config.TRACE_CONTEXT_ATTACHMENT_NAME} attachment for test ${testId}: expected { traceId, rootSpanId }`);
368
371
  return parsed;
369
372
  }
370
373
  function readFixtureSpansAttachment(result, testId) {
371
- const attachment = result.attachments.find((attachment) => attachment.name === require_trace_context.FIXTURE_SPANS_ATTACHMENT_NAME);
374
+ const attachment = result.attachments.find((attachment) => attachment.name === require_config.FIXTURE_SPANS_ATTACHMENT_NAME);
372
375
  if (!attachment?.body) return [];
373
376
  let parsed;
374
377
  try {
375
378
  parsed = JSON.parse(attachment.body.toString("utf-8"));
376
379
  } catch (error) {
377
- throw new Error(`Invalid ${require_trace_context.FIXTURE_SPANS_ATTACHMENT_NAME} attachment for test ${testId}: ${error}`);
380
+ throw new Error(`Invalid ${require_config.FIXTURE_SPANS_ATTACHMENT_NAME} attachment for test ${testId}: ${error}`);
378
381
  }
379
- if (!isRecord(parsed) || !Array.isArray(parsed.spans)) throw new Error(`Invalid ${require_trace_context.FIXTURE_SPANS_ATTACHMENT_NAME} attachment for test ${testId}: expected { spans }`);
382
+ if (!isRecord(parsed) || !Array.isArray(parsed.spans)) throw new Error(`Invalid ${require_config.FIXTURE_SPANS_ATTACHMENT_NAME} attachment for test ${testId}: expected { spans }`);
380
383
  return parsed.spans.map((span, index) => parseFixtureSpan(span, testId, index));
381
384
  }
382
385
  function parseFixtureSpan(value, testId, index) {
383
386
  if (!isRecord(value)) throw invalidFixtureSpanError(testId, index);
384
387
  const startTime = parseAttachmentDate(value.startTime);
385
388
  const endTime = parseAttachmentDate(value.endTime);
386
- if (typeof value.traceId !== "string" || typeof value.spanId !== "string" || typeof value.name !== "string" || !startTime || !endTime || !isSpanAttributes(value.attributes) || !isSpanStatus(value.status)) throw invalidFixtureSpanError(testId, index);
389
+ if (typeof value.traceId !== "string" || typeof value.spanId !== "string" || typeof value.name !== "string" || !startTime || !endTime || !isSpanAttributes(value.attributes) || !isSpanEvents(value.events) || value.status !== void 0 && !isSpanStatus(value.status)) throw invalidFixtureSpanError(testId, index);
387
390
  return {
388
391
  traceId: value.traceId,
389
392
  spanId: value.spanId,
@@ -392,13 +395,18 @@ function parseFixtureSpan(value, testId, index) {
392
395
  startTime,
393
396
  endTime,
394
397
  attributes: value.attributes,
398
+ events: value.events.map((event) => ({
399
+ name: event.name,
400
+ time: parseAttachmentDate(event.time),
401
+ attributes: event.attributes
402
+ })),
395
403
  status: value.status,
396
404
  kind: typeof value.kind === "number" ? value.kind : void 0,
397
405
  serviceName: typeof value.serviceName === "string" ? value.serviceName : void 0
398
406
  };
399
407
  }
400
408
  function invalidFixtureSpanError(testId, index) {
401
- return /* @__PURE__ */ new Error(`Invalid ${require_trace_context.FIXTURE_SPANS_ATTACHMENT_NAME} attachment for test ${testId}: invalid span at index ${index}`);
409
+ return /* @__PURE__ */ new Error(`Invalid ${require_config.FIXTURE_SPANS_ATTACHMENT_NAME} attachment for test ${testId}: invalid span at index ${index}`);
402
410
  }
403
411
  function parseAttachmentDate(value) {
404
412
  if (typeof value !== "string") return;
@@ -408,10 +416,38 @@ function parseAttachmentDate(value) {
408
416
  function isSpanStatus(value) {
409
417
  return isRecord(value) && typeof value.code === "number";
410
418
  }
419
+ const ANSI_ESCAPE_PATTERN = /[\u001b\u009b][[\]()#;?]*(?:(?:(?:[a-zA-Z\d]*(?:;[a-zA-Z\d]*)*)?\u0007)|(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]))/g;
420
+ const ORPHANED_ANSI_SGR_PATTERN = /\[(?:\d{1,3};)*\d{1,3}m/g;
421
+ function errorStatus(message) {
422
+ if (!message) return { code: 2 };
423
+ const cleanedMessage = cleanErrorMessage(message);
424
+ return cleanedMessage ? {
425
+ code: 2,
426
+ message: cleanedMessage
427
+ } : { code: 2 };
428
+ }
429
+ function cleanErrorMessage(message) {
430
+ return message.replace(ANSI_ESCAPE_PATTERN, "").replace(ORPHANED_ANSI_SGR_PATTERN, "").replace(/\r\n?/g, "\n").trim();
431
+ }
432
+ function isInternalFixtureStep(step) {
433
+ if (step.category !== "fixture" || !step.location?.file) return false;
434
+ const file = step.location.file.replace(/\\/g, "/");
435
+ return INTERNAL_FIXTURE_FILE_PATTERNS.some((pattern) => pattern.test(file));
436
+ }
437
+ const INTERNAL_FIXTURE_FILE_PATTERNS = [
438
+ /(?:^|\/)dist\/playwright-opentelemetry-fixture-[^/]+\.(?:mjs|cjs|js)$/,
439
+ /(?:^|\/)playwright-opentelemetry\/dist\/fixture\.(?:mjs|cjs|js)$/,
440
+ /(?:^|\/)playwright-opentelemetry\/dist\/playwright-opentelemetry-fixture-[^/]+\.(?:mjs|cjs|js)$/,
441
+ /(?:^|\/)playwright-opentelemetry\/dist\/fixture\/index\.(?:mjs|cjs|js)$/,
442
+ /(?:^|\/)playwright-opentelemetry\/(?:reporter\/)?src\/fixture\/playwright-opentelemetry-fixture\.ts$/
443
+ ];
411
444
  function isSpanAttributes(value) {
412
445
  if (!isRecord(value)) return false;
413
446
  return Object.values(value).every((attributeValue) => typeof attributeValue === "string" || typeof attributeValue === "number" || typeof attributeValue === "boolean" || Array.isArray(attributeValue) && attributeValue.every((item) => typeof item === "string"));
414
447
  }
448
+ function isSpanEvents(value) {
449
+ return Array.isArray(value) && value.every((event) => isRecord(event) && typeof event.name === "string" && parseAttachmentDate(event.time) !== void 0 && (event.attributes === void 0 || isSpanAttributes(event.attributes)));
450
+ }
415
451
  function isRecord(value) {
416
452
  return typeof value === "object" && value !== null && !Array.isArray(value);
417
453
  }
package/dist/reporter.cjs CHANGED
@@ -1,4 +1,4 @@
1
1
  //#region src/reporter/index.ts
2
- var reporter_default = require("./reporter-pVDPoXTx.cjs").PlaywrightOpentelemetryReporter;
2
+ var reporter_default = require("./reporter-D4iIeblG.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-D-ONZUZE.mjs";
1
+ import { t as PlaywrightOpentelemetryReporter } from "./reporter-CtflXoLs.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.6.1",
3
+ "version": "0.7.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",
@@ -1,15 +0,0 @@
1
- //#region src/shared/config.d.ts
2
- interface PlaywrightOpentelemetryConfig {
3
- otlpEndpoint?: string;
4
- otlpHeaders?: Record<string, string>;
5
- playwrightTraceApiEndpoint?: string;
6
- playwrightTraceApiHeaders?: Record<string, string>;
7
- storeTraceZip?: boolean;
8
- serviceName?: string;
9
- debug?: boolean;
10
- }
11
- interface PlaywrightOpentelemetryUseOptions {
12
- playwrightOpentelemetry?: PlaywrightOpentelemetryConfig;
13
- }
14
- //#endregion
15
- export { PlaywrightOpentelemetryUseOptions as n, PlaywrightOpentelemetryConfig as t };
@@ -1,15 +0,0 @@
1
- //#region src/shared/config.d.ts
2
- interface PlaywrightOpentelemetryConfig {
3
- otlpEndpoint?: string;
4
- otlpHeaders?: Record<string, string>;
5
- playwrightTraceApiEndpoint?: string;
6
- playwrightTraceApiHeaders?: Record<string, string>;
7
- storeTraceZip?: boolean;
8
- serviceName?: string;
9
- debug?: boolean;
10
- }
11
- interface PlaywrightOpentelemetryUseOptions {
12
- playwrightOpentelemetry?: PlaywrightOpentelemetryConfig;
13
- }
14
- //#endregion
15
- export { PlaywrightOpentelemetryUseOptions as n, PlaywrightOpentelemetryConfig as t };