playwright-opentelemetry 0.5.0 → 0.6.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.
- package/dist/config-B9i3jm5s.d.cts +15 -0
- package/dist/config-B9i3jm5s.d.mts +15 -0
- package/dist/fixture/factory.cjs +2 -2
- package/dist/fixture/factory.d.cts +1 -5
- package/dist/fixture/factory.d.mts +1 -5
- package/dist/fixture/factory.mjs +1 -1
- package/dist/fixture-1Zc7Gp0b.d.cts +61 -0
- package/dist/{factory-kMxwg_DI.cjs → fixture-C85kKbWA.cjs} +193 -100
- package/dist/{factory-Df7HwxXO.mjs → fixture-CQtQM60Z.mjs} +192 -99
- package/dist/fixture-IJvYGCtD.d.mts +61 -0
- package/dist/fixture.cjs +3 -3
- package/dist/fixture.d.cts +13 -3
- package/dist/fixture.d.mts +13 -3
- package/dist/fixture.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +3 -2
- package/dist/index.d.mts +3 -2
- package/dist/index.mjs +1 -1
- package/dist/otel-B-kvMiiq.d.cts +18 -0
- package/dist/otel-B-kvMiiq.d.mts +18 -0
- package/dist/reporter-DaCojFom.d.cts +25 -0
- package/dist/reporter-DaCojFom.d.mts +25 -0
- package/dist/reporter-S_gsTMix.mjs +429 -0
- package/dist/reporter-XVwaSQDK.cjs +436 -0
- package/dist/reporter.cjs +1 -1
- package/dist/reporter.d.cts +2 -2
- package/dist/reporter.d.mts +2 -2
- package/dist/reporter.mjs +1 -1
- package/dist/trace-context-88H-vhOr.cjs +286 -0
- package/dist/trace-context-e11YEu8B.mjs +205 -0
- package/package.json +1 -1
- package/dist/index-BNfDd8Mg.d.cts +0 -45
- package/dist/index-BNfDd8Mg.d.mts +0 -45
- package/dist/reporter-BE9S9CtV.mjs +0 -461
- package/dist/reporter-BzHSG-gx.cjs +0 -468
- package/dist/trace-files-B6bmvwUq.mjs +0 -218
- package/dist/trace-files-DTK0XBIq.cjs +0 -325
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
import { a as resolvePlaywrightOpentelemetryConfig, c as generateTraceId, l as sendSpans, n as TRACE_CONTEXT_ATTACHMENT_NAME, o as buildOtlpRequest, s as generateSpanId, t as FIXTURE_SPANS_ATTACHMENT_NAME } from "./trace-context-e11YEu8B.mjs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import fs from "node:fs/promises";
|
|
4
|
+
import { BlobReader, BlobWriter, ZipReader, ZipWriter } from "@zip.js/zip.js";
|
|
5
|
+
//#region src/reporter/otel-attributes.ts
|
|
6
|
+
const ATTR_CODE_FILE_PATH = "code.file.path";
|
|
7
|
+
const ATTR_CODE_LINE_NUMBER = "code.line.number";
|
|
8
|
+
const ATTR_TEST_CASE_NAME = "test.case.name";
|
|
9
|
+
const ATTR_TEST_CASE_TITLE = "test.case.title";
|
|
10
|
+
const ATTR_TEST_CASE_RESULT_STATUS = "test.case.result.status";
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/reporter/reporter-attributes.ts
|
|
13
|
+
const TEST_SPAN_NAME = "playwright.test";
|
|
14
|
+
const TEST_STEP_SPAN_NAME = "playwright.test.step";
|
|
15
|
+
const ATTR_TEST_STEP_NAME = "test.step.name";
|
|
16
|
+
const ATTR_TEST_STEP_TITLE = "test.step.title";
|
|
17
|
+
const ATTR_TEST_STEP_CATEGORY = "test.step.category";
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/reporter/trace-zip-builder.ts
|
|
20
|
+
/**
|
|
21
|
+
* Get the zip filename based on test location and ID.
|
|
22
|
+
* Format: {basename(file)}:{line}-{testId}-pw-otel.zip
|
|
23
|
+
* Fallback: {testId}-pw-otel.zip (if no location info)
|
|
24
|
+
*/
|
|
25
|
+
function getZipFilename(test, testId) {
|
|
26
|
+
if (test.location) return `${path.basename(test.location.file)}:${test.location.line}-${testId}-pw-otel.zip`;
|
|
27
|
+
return `${testId}-pw-otel.zip`;
|
|
28
|
+
}
|
|
29
|
+
const PLAYWRIGHT_TRACE_RESOURCES_DIR = "resources/";
|
|
30
|
+
/**
|
|
31
|
+
* Pattern to match Playwright screenshot filenames: {name}@{hex-hash}-{timestamp}.jpeg
|
|
32
|
+
* Examples: page@f06f11f7c14d6ce1060d47d79f05c154-1766833384425.jpeg
|
|
33
|
+
*/
|
|
34
|
+
const PLAYWRIGHT_SCREENSHOT_PATTERN = /^.+@[a-f0-9]+-\d+\.jpe?g$/i;
|
|
35
|
+
/**
|
|
36
|
+
* Extract screenshots from a Playwright trace ZIP file.
|
|
37
|
+
* Screenshots are stored in the resources/ directory with names like:
|
|
38
|
+
* {pageGuid}-{timestamp}.jpeg (e.g., page@abc123-1766929201038.jpeg)
|
|
39
|
+
*
|
|
40
|
+
* @param traceZipPath - Path to the Playwright trace.zip file
|
|
41
|
+
* @returns Map of filename (without resources/ prefix) to Blob
|
|
42
|
+
*/
|
|
43
|
+
async function extractScreenshotsFromPlaywrightTrace(traceZipPath) {
|
|
44
|
+
const screenshots = /* @__PURE__ */ new Map();
|
|
45
|
+
try {
|
|
46
|
+
const zipBuffer = await fs.readFile(traceZipPath);
|
|
47
|
+
const zipReader = new ZipReader(new BlobReader(new Blob([zipBuffer])));
|
|
48
|
+
const entries = await zipReader.getEntries();
|
|
49
|
+
await Promise.all(entries.filter((entry) => {
|
|
50
|
+
if (!isFileEntry(entry)) return false;
|
|
51
|
+
if (!entry.filename.startsWith(PLAYWRIGHT_TRACE_RESOURCES_DIR)) return false;
|
|
52
|
+
const name = entry.filename.slice(10);
|
|
53
|
+
return PLAYWRIGHT_SCREENSHOT_PATTERN.test(name);
|
|
54
|
+
}).map(async (entry) => {
|
|
55
|
+
const filename = entry.filename.slice(10);
|
|
56
|
+
const blob = await entry.getData(new BlobWriter("image/jpeg"));
|
|
57
|
+
screenshots.set(filename, blob);
|
|
58
|
+
}));
|
|
59
|
+
await zipReader.close();
|
|
60
|
+
} catch (err) {
|
|
61
|
+
if (err.code !== "ENOENT") console.warn(`Warning: Could not extract screenshots from trace ZIP: ${err}`);
|
|
62
|
+
}
|
|
63
|
+
return screenshots;
|
|
64
|
+
}
|
|
65
|
+
async function createScreenshotsZip(screenshots) {
|
|
66
|
+
const zipWriter = new ZipWriter(new BlobWriter("application/zip"));
|
|
67
|
+
await addScreenshotsToZip(zipWriter, screenshots);
|
|
68
|
+
return zipWriter.close();
|
|
69
|
+
}
|
|
70
|
+
async function createTraceZipBlob(options) {
|
|
71
|
+
const { spans, fixtureSpans, serviceName, playwrightVersion, screenshots } = options;
|
|
72
|
+
const otlpRequest = buildOtlpRequest(spans, serviceName, playwrightVersion);
|
|
73
|
+
const traceJson = JSON.stringify(otlpRequest, null, 2);
|
|
74
|
+
const zipWriter = new ZipWriter(new BlobWriter("application/zip"));
|
|
75
|
+
await zipWriter.add("traces/playwright-opentelemetry.json", new Blob([traceJson]).stream());
|
|
76
|
+
if (fixtureSpans.length > 0) {
|
|
77
|
+
const fixtureOtlpRequest = buildOtlpRequest(fixtureSpans, "playwright-browser", playwrightVersion);
|
|
78
|
+
await zipWriter.add("traces/playwright-browser.json", new Blob([JSON.stringify(fixtureOtlpRequest, null, 2)]).stream());
|
|
79
|
+
}
|
|
80
|
+
await addScreenshotsToZip(zipWriter, screenshots);
|
|
81
|
+
return zipWriter.close();
|
|
82
|
+
}
|
|
83
|
+
async function writeTraceZip(outputDir, test, zipBlob) {
|
|
84
|
+
const zipFilename = getZipFilename(test, test.id);
|
|
85
|
+
const zipPath = path.join(outputDir, zipFilename);
|
|
86
|
+
const arrayBuffer = await zipBlob.arrayBuffer();
|
|
87
|
+
await fs.writeFile(zipPath, Buffer.from(arrayBuffer));
|
|
88
|
+
}
|
|
89
|
+
async function addScreenshotsToZip(zipWriter, screenshots) {
|
|
90
|
+
const manifest = createScreenshotManifest(screenshots);
|
|
91
|
+
await zipWriter.add("manifest.json", new Blob([JSON.stringify(manifest, null, 2)], { type: "application/json" }).stream());
|
|
92
|
+
for (const [filename, blob] of screenshots.entries()) await zipWriter.add(`screenshots/${filename}`, blob.stream());
|
|
93
|
+
}
|
|
94
|
+
function createScreenshotManifest(screenshots) {
|
|
95
|
+
return {
|
|
96
|
+
version: 1,
|
|
97
|
+
screenshots: Array.from(screenshots.keys()).map((filename) => ({
|
|
98
|
+
timestamp: extractTimestampFromFilename(filename),
|
|
99
|
+
file: filename,
|
|
100
|
+
path: `screenshots/${filename}`,
|
|
101
|
+
contentType: getMimeType(filename)
|
|
102
|
+
})).sort((a, b) => a.timestamp - b.timestamp)
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function extractTimestampFromFilename(filename) {
|
|
106
|
+
const lastDashIndex = filename.lastIndexOf("-");
|
|
107
|
+
if (lastDashIndex === -1) return 0;
|
|
108
|
+
const timestamp = Number.parseInt(filename.slice(lastDashIndex + 1).replace(/\.[^.]+$/, ""), 10);
|
|
109
|
+
return Number.isNaN(timestamp) ? 0 : timestamp;
|
|
110
|
+
}
|
|
111
|
+
function getMimeType(filename) {
|
|
112
|
+
switch (filename.split(".").pop()?.toLowerCase()) {
|
|
113
|
+
case "png": return "image/png";
|
|
114
|
+
case "jpg":
|
|
115
|
+
case "jpeg": return "image/jpeg";
|
|
116
|
+
case "webp": return "image/webp";
|
|
117
|
+
default: return "application/octet-stream";
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function isFileEntry(entry) {
|
|
121
|
+
return !entry.directory;
|
|
122
|
+
}
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region src/reporter/reporter.ts
|
|
125
|
+
var PlaywrightOpentelemetryReporter = class {
|
|
126
|
+
spanBatches = [];
|
|
127
|
+
pendingTraceArtifacts = [];
|
|
128
|
+
rootDir;
|
|
129
|
+
playwrightVersion;
|
|
130
|
+
debug = false;
|
|
131
|
+
constructor() {}
|
|
132
|
+
onBegin(config, _suite) {
|
|
133
|
+
this.rootDir = config.rootDir;
|
|
134
|
+
this.playwrightVersion = config.version;
|
|
135
|
+
const projects = Array.isArray(config.projects) ? config.projects : [];
|
|
136
|
+
for (const project of projects) {
|
|
137
|
+
const resolvedConfig = resolvePlaywrightOpentelemetryConfig(getProjectPlaywrightOpentelemetryConfig(project), { requireDestination: true });
|
|
138
|
+
this.debug ||= resolvedConfig.debug;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
onTestBegin(_test, _result) {}
|
|
142
|
+
onStepBegin(_test, _result, _step) {}
|
|
143
|
+
onStepEnd(_test, _result, _step) {}
|
|
144
|
+
onTestEnd(test, result) {
|
|
145
|
+
const testId = test.id;
|
|
146
|
+
const outputDir = getTestOutputDir(test);
|
|
147
|
+
const config = getTestConfig(test);
|
|
148
|
+
const { traceId, rootSpanId: testSpanId } = readTraceContextAttachment(result, testId, config.storeTraceZip);
|
|
149
|
+
const traceAttachment = result.attachments.find((attachment) => attachment.name === "trace" && attachment.contentType === "application/zip" && attachment.path);
|
|
150
|
+
const fixtureSpans = config.storeTraceZip ? readFixtureSpansAttachment(result, testId) : [];
|
|
151
|
+
result.attachments.push({
|
|
152
|
+
name: "playwright-opentelemetry-trace-id",
|
|
153
|
+
contentType: "text/plain",
|
|
154
|
+
body: Buffer.from(traceId, "utf-8")
|
|
155
|
+
});
|
|
156
|
+
const attributes = {};
|
|
157
|
+
const titlePath = test.titlePath();
|
|
158
|
+
if (titlePath.length >= 3) attributes[ATTR_TEST_CASE_NAME] = titlePath.slice(3).join(" > ");
|
|
159
|
+
const describes = titlePath.length > 4 ? titlePath.slice(3, -1) : [];
|
|
160
|
+
attributes[ATTR_TEST_CASE_TITLE] = test.title;
|
|
161
|
+
attributes[ATTR_TEST_CASE_RESULT_STATUS] = result.status === "passed" ? "pass" : "fail";
|
|
162
|
+
attributes["playwright.test.status"] = result.status;
|
|
163
|
+
attributes["playwright.test.describes"] = describes;
|
|
164
|
+
if (test.location) {
|
|
165
|
+
const { file, line } = test.location;
|
|
166
|
+
attributes[ATTR_CODE_FILE_PATH] = this.rootDir ? path.relative(this.rootDir, file) : file;
|
|
167
|
+
attributes[ATTR_CODE_LINE_NUMBER] = line;
|
|
168
|
+
}
|
|
169
|
+
const processedSteps = /* @__PURE__ */ new Map();
|
|
170
|
+
const skippedStepIds = /* @__PURE__ */ new Set();
|
|
171
|
+
if (result.steps && result.steps.length > 0) for (const step of result.steps) this.processTestStep(test, step, testSpanId, traceId, [], processedSteps, skippedStepIds);
|
|
172
|
+
const stepSpans = Array.from(processedSteps.values());
|
|
173
|
+
let minStartTime = result.startTime;
|
|
174
|
+
let maxEndTime = new Date(result.startTime.getTime() + result.duration);
|
|
175
|
+
for (const stepSpan of stepSpans) {
|
|
176
|
+
if (stepSpan.startTime < minStartTime) minStartTime = stepSpan.startTime;
|
|
177
|
+
if (stepSpan.endTime > maxEndTime) maxEndTime = stepSpan.endTime;
|
|
178
|
+
}
|
|
179
|
+
const testSpans = [{
|
|
180
|
+
traceId,
|
|
181
|
+
spanId: testSpanId,
|
|
182
|
+
name: TEST_SPAN_NAME,
|
|
183
|
+
startTime: minStartTime,
|
|
184
|
+
endTime: maxEndTime,
|
|
185
|
+
attributes,
|
|
186
|
+
status: { code: result.status === test.expectedStatus ? 1 : 2 }
|
|
187
|
+
}, ...stepSpans];
|
|
188
|
+
this.spanBatches.push({
|
|
189
|
+
spans: testSpans,
|
|
190
|
+
config
|
|
191
|
+
});
|
|
192
|
+
if (config.storeTraceZip || config.playwrightTraceApiEndpoint) {
|
|
193
|
+
const prepared = this.prepareTraceArtifact({
|
|
194
|
+
test,
|
|
195
|
+
spans: testSpans,
|
|
196
|
+
fixtureSpans,
|
|
197
|
+
traceAttachmentPath: traceAttachment?.path,
|
|
198
|
+
traceId,
|
|
199
|
+
config,
|
|
200
|
+
playwrightVersion: this.playwrightVersion || "unknown"
|
|
201
|
+
}).catch((error) => ({ error }));
|
|
202
|
+
this.pendingTraceArtifacts.push({
|
|
203
|
+
outputDir,
|
|
204
|
+
test,
|
|
205
|
+
prepared
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
async onEnd(_result) {
|
|
210
|
+
for (const artifact of this.pendingTraceArtifacts) {
|
|
211
|
+
const prepared = await artifact.prepared;
|
|
212
|
+
if ("error" in prepared) throw prepared.error;
|
|
213
|
+
if (prepared.traceZipBlob) await writeTraceZip(artifact.outputDir, artifact.test, prepared.traceZipBlob);
|
|
214
|
+
}
|
|
215
|
+
const destinations = /* @__PURE__ */ new Map();
|
|
216
|
+
for (const batch of this.spanBatches) {
|
|
217
|
+
if (batch.config.otlpEndpoint) addDestinationSpans(destinations, batch.spans, {
|
|
218
|
+
tracesEndpoint: batch.config.otlpEndpoint,
|
|
219
|
+
headers: batch.config.otlpHeaders,
|
|
220
|
+
serviceName: batch.config.serviceName,
|
|
221
|
+
playwrightVersion: this.playwrightVersion || "unknown",
|
|
222
|
+
debug: batch.config.debug
|
|
223
|
+
});
|
|
224
|
+
if (batch.config.playwrightTraceApiEndpoint) addDestinationSpans(destinations, batch.spans, {
|
|
225
|
+
tracesEndpoint: `${batch.config.playwrightTraceApiEndpoint}/v1/traces`,
|
|
226
|
+
headers: batch.config.playwrightTraceApiHeaders,
|
|
227
|
+
serviceName: batch.config.serviceName,
|
|
228
|
+
playwrightVersion: this.playwrightVersion || "unknown",
|
|
229
|
+
debug: batch.config.debug
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
for (const destination of destinations.values()) await sendSpans(destination.spans, destination.options);
|
|
233
|
+
}
|
|
234
|
+
async prepareTraceArtifact(options) {
|
|
235
|
+
const screenshots = options.traceAttachmentPath ? await extractScreenshotsFromPlaywrightTrace(options.traceAttachmentPath) : /* @__PURE__ */ new Map();
|
|
236
|
+
const traceZipBlobPromise = options.config.storeTraceZip ? createTraceZipBlob({
|
|
237
|
+
test: options.test,
|
|
238
|
+
spans: options.spans,
|
|
239
|
+
fixtureSpans: options.fixtureSpans,
|
|
240
|
+
serviceName: options.config.serviceName,
|
|
241
|
+
playwrightVersion: options.playwrightVersion,
|
|
242
|
+
screenshots
|
|
243
|
+
}) : void 0;
|
|
244
|
+
const uploadPromise = options.config.playwrightTraceApiEndpoint ? this.sendScreenshotsZipToTraceApi({
|
|
245
|
+
traceId: options.traceId,
|
|
246
|
+
screenshots,
|
|
247
|
+
config: options.config
|
|
248
|
+
}) : void 0;
|
|
249
|
+
const [traceZipBlob] = await Promise.all([traceZipBlobPromise, uploadPromise]);
|
|
250
|
+
return { traceZipBlob };
|
|
251
|
+
}
|
|
252
|
+
processTestStep(test, step, parentSpanId, traceId, parentTitlePath, processedSteps, skippedStepIds) {
|
|
253
|
+
const isInternalFixture = step.category === "fixture" && step.location?.file.includes("playwright-opentelemetry") && (step.location?.file.endsWith("fixture.mjs") || step.location?.file.endsWith("fixture/index.ts"));
|
|
254
|
+
const stepId = getStepId(test, step);
|
|
255
|
+
if (isInternalFixture) {
|
|
256
|
+
skippedStepIds.add(stepId);
|
|
257
|
+
processedSteps.delete(stepId);
|
|
258
|
+
if (step.steps && step.steps.length > 0) for (const childStep of step.steps) this.processTestStep(test, childStep, parentSpanId, traceId, parentTitlePath, processedSteps, skippedStepIds);
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
if (skippedStepIds.has(stepId)) {
|
|
262
|
+
if (step.steps && step.steps.length > 0) for (const childStep of step.steps) this.processTestStep(test, childStep, parentSpanId, traceId, parentTitlePath, processedSteps, skippedStepIds);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const currentTitlePath = [...parentTitlePath, step.title];
|
|
266
|
+
const existingSpan = processedSteps.get(stepId);
|
|
267
|
+
if (existingSpan) {
|
|
268
|
+
if (step.location && !existingSpan.attributes["code.file.path"]) {
|
|
269
|
+
const { file, line } = step.location;
|
|
270
|
+
const relativePath = this.rootDir ? path.relative(this.rootDir, file) : file;
|
|
271
|
+
existingSpan.attributes[ATTR_CODE_FILE_PATH] = relativePath;
|
|
272
|
+
existingSpan.attributes[ATTR_CODE_LINE_NUMBER] = line;
|
|
273
|
+
}
|
|
274
|
+
if (step.steps && step.steps.length > 0) for (const childStep of step.steps) this.processTestStep(test, childStep, existingSpan.spanId, traceId, currentTitlePath, processedSteps, skippedStepIds);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
const attributes = {};
|
|
278
|
+
attributes[ATTR_TEST_STEP_NAME] = currentTitlePath.join(" > ");
|
|
279
|
+
attributes[ATTR_TEST_STEP_TITLE] = step.title;
|
|
280
|
+
attributes[ATTR_TEST_STEP_CATEGORY] = step.category;
|
|
281
|
+
if (step.location) {
|
|
282
|
+
const { file, line } = step.location;
|
|
283
|
+
attributes[ATTR_CODE_FILE_PATH] = this.rootDir ? path.relative(this.rootDir, file) : file;
|
|
284
|
+
attributes[ATTR_CODE_LINE_NUMBER] = line;
|
|
285
|
+
}
|
|
286
|
+
const stepSpan = {
|
|
287
|
+
traceId,
|
|
288
|
+
spanId: generateSpanId(),
|
|
289
|
+
parentSpanId,
|
|
290
|
+
name: TEST_STEP_SPAN_NAME,
|
|
291
|
+
startTime: step.startTime,
|
|
292
|
+
endTime: new Date(step.startTime.getTime() + step.duration),
|
|
293
|
+
attributes,
|
|
294
|
+
status: { code: step.error ? 2 : 1 }
|
|
295
|
+
};
|
|
296
|
+
processedSteps.set(stepId, stepSpan);
|
|
297
|
+
if (step.steps && step.steps.length > 0) for (const childStep of step.steps) this.processTestStep(test, childStep, stepSpan.spanId, traceId, currentTitlePath, processedSteps, skippedStepIds);
|
|
298
|
+
}
|
|
299
|
+
onStdOut(chunk, _test, _result) {
|
|
300
|
+
if (this.debug) console.log(chunk.toString().slice(0, -1));
|
|
301
|
+
}
|
|
302
|
+
onStdErr(chunk, _test, _result) {
|
|
303
|
+
if (this.debug) console.log(chunk.toString().slice(0, -1));
|
|
304
|
+
}
|
|
305
|
+
printsToStdio() {
|
|
306
|
+
return this.debug;
|
|
307
|
+
}
|
|
308
|
+
async sendScreenshotsZipToTraceApi(params) {
|
|
309
|
+
const { traceId, screenshots, config } = params;
|
|
310
|
+
const screenshotsZip = await createScreenshotsZip(screenshots);
|
|
311
|
+
const screenshotUrl = `${config.playwrightTraceApiEndpoint}/playwright-otel-reporter/v1/screenshots.zip`;
|
|
312
|
+
const response = await fetch(screenshotUrl, {
|
|
313
|
+
method: "PUT",
|
|
314
|
+
headers: {
|
|
315
|
+
"content-type": "application/zip",
|
|
316
|
+
"x-trace-id": traceId,
|
|
317
|
+
...config.playwrightTraceApiHeaders
|
|
318
|
+
},
|
|
319
|
+
body: screenshotsZip
|
|
320
|
+
});
|
|
321
|
+
if (!response.ok) {
|
|
322
|
+
const error = await response.text();
|
|
323
|
+
throw new Error(`Failed to send screenshots ZIP: ${response.status} ${response.statusText}, ${error}`);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
function getProjectPlaywrightOpentelemetryConfig(project) {
|
|
328
|
+
return project?.use?.playwrightOpentelemetry;
|
|
329
|
+
}
|
|
330
|
+
function getTestConfig(test) {
|
|
331
|
+
return resolvePlaywrightOpentelemetryConfig(getProjectPlaywrightOpentelemetryConfig(test.parent.project()), { requireDestination: true });
|
|
332
|
+
}
|
|
333
|
+
function getTestOutputDir(test) {
|
|
334
|
+
const outputDir = test.parent.project()?.outputDir;
|
|
335
|
+
if (!outputDir) throw new Error(`No outputDir found for test "${test.id}"`);
|
|
336
|
+
return outputDir;
|
|
337
|
+
}
|
|
338
|
+
function addDestinationSpans(destinations, spans, options) {
|
|
339
|
+
const key = JSON.stringify(options);
|
|
340
|
+
const destination = destinations.get(key);
|
|
341
|
+
if (destination) {
|
|
342
|
+
destination.spans.push(...spans);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
destinations.set(key, {
|
|
346
|
+
spans: [...spans],
|
|
347
|
+
options
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
function readTraceContextAttachment(result, testId, allowReporterOnlyFallback) {
|
|
351
|
+
const attachment = result.attachments.find((attachment) => attachment.name === TRACE_CONTEXT_ATTACHMENT_NAME);
|
|
352
|
+
if (!attachment?.body) {
|
|
353
|
+
if (allowReporterOnlyFallback) return {
|
|
354
|
+
traceId: generateTraceId(),
|
|
355
|
+
rootSpanId: generateSpanId()
|
|
356
|
+
};
|
|
357
|
+
throw new Error(`playwright-opentelemetry fixture missing for test ${testId}: expected ${TRACE_CONTEXT_ATTACHMENT_NAME} attachment. Import test from "playwright-opentelemetry/fixture" or wrap your base test with createPlaywrightOtelTest().`);
|
|
358
|
+
}
|
|
359
|
+
let parsed;
|
|
360
|
+
try {
|
|
361
|
+
parsed = JSON.parse(attachment.body.toString("utf-8"));
|
|
362
|
+
} catch (error) {
|
|
363
|
+
throw new Error(`Invalid ${TRACE_CONTEXT_ATTACHMENT_NAME} attachment for test ${testId}: ${error}`);
|
|
364
|
+
}
|
|
365
|
+
if (!isTraceContextAttachment(parsed)) throw new Error(`Invalid ${TRACE_CONTEXT_ATTACHMENT_NAME} attachment for test ${testId}: expected { traceId, rootSpanId }`);
|
|
366
|
+
return parsed;
|
|
367
|
+
}
|
|
368
|
+
function readFixtureSpansAttachment(result, testId) {
|
|
369
|
+
const attachment = result.attachments.find((attachment) => attachment.name === FIXTURE_SPANS_ATTACHMENT_NAME);
|
|
370
|
+
if (!attachment?.body) return [];
|
|
371
|
+
let parsed;
|
|
372
|
+
try {
|
|
373
|
+
parsed = JSON.parse(attachment.body.toString("utf-8"));
|
|
374
|
+
} catch (error) {
|
|
375
|
+
throw new Error(`Invalid ${FIXTURE_SPANS_ATTACHMENT_NAME} attachment for test ${testId}: ${error}`);
|
|
376
|
+
}
|
|
377
|
+
if (!isRecord(parsed) || !Array.isArray(parsed.spans)) throw new Error(`Invalid ${FIXTURE_SPANS_ATTACHMENT_NAME} attachment for test ${testId}: expected { spans }`);
|
|
378
|
+
return parsed.spans.map((span, index) => parseFixtureSpan(span, testId, index));
|
|
379
|
+
}
|
|
380
|
+
function parseFixtureSpan(value, testId, index) {
|
|
381
|
+
if (!isRecord(value)) throw invalidFixtureSpanError(testId, index);
|
|
382
|
+
const startTime = parseAttachmentDate(value.startTime);
|
|
383
|
+
const endTime = parseAttachmentDate(value.endTime);
|
|
384
|
+
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);
|
|
385
|
+
return {
|
|
386
|
+
traceId: value.traceId,
|
|
387
|
+
spanId: value.spanId,
|
|
388
|
+
parentSpanId: typeof value.parentSpanId === "string" ? value.parentSpanId : void 0,
|
|
389
|
+
name: value.name,
|
|
390
|
+
startTime,
|
|
391
|
+
endTime,
|
|
392
|
+
attributes: value.attributes,
|
|
393
|
+
status: value.status,
|
|
394
|
+
kind: typeof value.kind === "number" ? value.kind : void 0,
|
|
395
|
+
serviceName: typeof value.serviceName === "string" ? value.serviceName : void 0
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
function invalidFixtureSpanError(testId, index) {
|
|
399
|
+
return /* @__PURE__ */ new Error(`Invalid ${FIXTURE_SPANS_ATTACHMENT_NAME} attachment for test ${testId}: invalid span at index ${index}`);
|
|
400
|
+
}
|
|
401
|
+
function parseAttachmentDate(value) {
|
|
402
|
+
if (typeof value !== "string") return;
|
|
403
|
+
const date = new Date(value);
|
|
404
|
+
return Number.isNaN(date.getTime()) ? void 0 : date;
|
|
405
|
+
}
|
|
406
|
+
function isSpanStatus(value) {
|
|
407
|
+
return isRecord(value) && typeof value.code === "number";
|
|
408
|
+
}
|
|
409
|
+
function isSpanAttributes(value) {
|
|
410
|
+
if (!isRecord(value)) return false;
|
|
411
|
+
return Object.values(value).every((attributeValue) => typeof attributeValue === "string" || typeof attributeValue === "number" || typeof attributeValue === "boolean" || Array.isArray(attributeValue) && attributeValue.every((item) => typeof item === "string"));
|
|
412
|
+
}
|
|
413
|
+
function isRecord(value) {
|
|
414
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
415
|
+
}
|
|
416
|
+
function isTraceContextAttachment(value) {
|
|
417
|
+
return typeof value === "object" && value !== null && "traceId" in value && "rootSpanId" in value && typeof value.traceId === "string" && typeof value.rootSpanId === "string";
|
|
418
|
+
}
|
|
419
|
+
function getStepId(test, step) {
|
|
420
|
+
const startTimeMs = step.startTime.getTime();
|
|
421
|
+
return [
|
|
422
|
+
test.id,
|
|
423
|
+
step.category,
|
|
424
|
+
startTimeMs,
|
|
425
|
+
...step.titlePath()
|
|
426
|
+
].join(" > ");
|
|
427
|
+
}
|
|
428
|
+
//#endregion
|
|
429
|
+
export { PlaywrightOpentelemetryReporter as t };
|