playwright-opentelemetry 0.5.0 → 0.6.1
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-Df7HwxXO.mjs → fixture-C3xMi3bo.mjs} +192 -99
- package/dist/fixture-IJvYGCtD.d.mts +61 -0
- package/dist/{factory-kMxwg_DI.cjs → fixture-KBHTI5WC.cjs} +193 -100
- 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-D-ONZUZE.mjs +429 -0
- package/dist/reporter-DaCojFom.d.cts +25 -0
- package/dist/reporter-DaCojFom.d.mts +25 -0
- package/dist/reporter-pVDPoXTx.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-Cck0LEoL.mjs +205 -0
- package/dist/trace-context-Dd2Fmx9q.cjs +286 -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
|
@@ -1,461 +0,0 @@
|
|
|
1
|
-
import { a as createNetworkDirs, c as getOrCreateTraceId, d as writeCurrentSpanId, i as collectNetworkSpans, n as cleanupTestFiles, o as generateSpanId, r as collectBrowserPageSpans, t as PW_OTEL_DIR } from "./trace-files-B6bmvwUq.mjs";
|
|
2
|
-
import { mkdirSync } from "node:fs";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import fs from "node:fs/promises";
|
|
5
|
-
import { BlobReader, BlobWriter, ZipReader, ZipWriter } from "@zip.js/zip.js";
|
|
6
|
-
//#region src/reporter/otel-attributes.ts
|
|
7
|
-
const ATTR_CODE_FILE_PATH = "code.file.path";
|
|
8
|
-
const ATTR_CODE_LINE_NUMBER = "code.line.number";
|
|
9
|
-
const ATTR_TEST_CASE_NAME = "test.case.name";
|
|
10
|
-
const ATTR_TEST_CASE_TITLE = "test.case.title";
|
|
11
|
-
const ATTR_TEST_CASE_RESULT_STATUS = "test.case.result.status";
|
|
12
|
-
//#endregion
|
|
13
|
-
//#region src/reporter/reporter-attributes.ts
|
|
14
|
-
const TEST_SPAN_NAME = "playwright.test";
|
|
15
|
-
const TEST_STEP_SPAN_NAME = "playwright.test.step";
|
|
16
|
-
const ATTR_TEST_STEP_NAME = "test.step.name";
|
|
17
|
-
const ATTR_TEST_STEP_TITLE = "test.step.title";
|
|
18
|
-
const ATTR_TEST_STEP_CATEGORY = "test.step.category";
|
|
19
|
-
//#endregion
|
|
20
|
-
//#region package.json
|
|
21
|
-
var version = "0.5.0";
|
|
22
|
-
//#endregion
|
|
23
|
-
//#region src/reporter/sender.ts
|
|
24
|
-
function dateToNanoseconds(date) {
|
|
25
|
-
return (BigInt(date.getTime()) * BigInt(1e6)).toString();
|
|
26
|
-
}
|
|
27
|
-
function toOtlpAttributes(attributes) {
|
|
28
|
-
return Object.entries(attributes).map(([key, value]) => {
|
|
29
|
-
if (Array.isArray(value)) return {
|
|
30
|
-
key,
|
|
31
|
-
value: { arrayValue: { values: value.map((item) => ({ stringValue: item })) } }
|
|
32
|
-
};
|
|
33
|
-
else if (typeof value === "number") return {
|
|
34
|
-
key,
|
|
35
|
-
value: Number.isInteger(value) ? { intValue: value } : { doubleValue: value }
|
|
36
|
-
};
|
|
37
|
-
else if (typeof value === "boolean") return {
|
|
38
|
-
key,
|
|
39
|
-
value: { boolValue: value }
|
|
40
|
-
};
|
|
41
|
-
else return {
|
|
42
|
-
key,
|
|
43
|
-
value: { stringValue: value }
|
|
44
|
-
};
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
const SPAN_KIND_INTERNAL = 1;
|
|
48
|
-
function buildResourceSpan(spans, serviceName, playwrightVersion) {
|
|
49
|
-
const otlpSpans = spans.map((span) => ({
|
|
50
|
-
traceId: span.traceId,
|
|
51
|
-
spanId: span.spanId,
|
|
52
|
-
parentSpanId: span.parentSpanId || void 0,
|
|
53
|
-
name: span.name,
|
|
54
|
-
kind: span.kind ?? SPAN_KIND_INTERNAL,
|
|
55
|
-
startTimeUnixNano: dateToNanoseconds(span.startTime),
|
|
56
|
-
endTimeUnixNano: dateToNanoseconds(span.endTime),
|
|
57
|
-
attributes: toOtlpAttributes(span.attributes),
|
|
58
|
-
droppedAttributesCount: 0,
|
|
59
|
-
events: [],
|
|
60
|
-
droppedEventsCount: 0,
|
|
61
|
-
status: span.status,
|
|
62
|
-
links: [],
|
|
63
|
-
droppedLinksCount: 0
|
|
64
|
-
}));
|
|
65
|
-
return {
|
|
66
|
-
resource: { attributes: [
|
|
67
|
-
{
|
|
68
|
-
key: "service.name",
|
|
69
|
-
value: { stringValue: serviceName }
|
|
70
|
-
},
|
|
71
|
-
{
|
|
72
|
-
key: "service.namespace",
|
|
73
|
-
value: { stringValue: "playwright" }
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
key: "service.version",
|
|
77
|
-
value: { stringValue: playwrightVersion }
|
|
78
|
-
}
|
|
79
|
-
] },
|
|
80
|
-
scopeSpans: [{
|
|
81
|
-
scope: {
|
|
82
|
-
name: "playwright-opentelemetry",
|
|
83
|
-
version
|
|
84
|
-
},
|
|
85
|
-
spans: otlpSpans
|
|
86
|
-
}]
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
function buildOtlpRequest(spans, serviceName, playwrightVersion) {
|
|
90
|
-
const spansByService = /* @__PURE__ */ new Map();
|
|
91
|
-
for (const span of spans) {
|
|
92
|
-
const spanServiceName = span.serviceName ?? serviceName;
|
|
93
|
-
const serviceSpans = spansByService.get(spanServiceName);
|
|
94
|
-
if (serviceSpans) serviceSpans.push(span);
|
|
95
|
-
else spansByService.set(spanServiceName, [span]);
|
|
96
|
-
}
|
|
97
|
-
const resourceSpans = [];
|
|
98
|
-
for (const [svcName, svcSpans] of spansByService) resourceSpans.push(buildResourceSpan(svcSpans, svcName, playwrightVersion));
|
|
99
|
-
return { resourceSpans };
|
|
100
|
-
}
|
|
101
|
-
async function sendSpans(spans, options) {
|
|
102
|
-
if (spans.length === 0) return;
|
|
103
|
-
const endpoint = options.tracesEndpoint;
|
|
104
|
-
const headers = options.headers || {};
|
|
105
|
-
const body = JSON.stringify(buildOtlpRequest(spans, options.serviceName, options.playwrightVersion));
|
|
106
|
-
if (options.debug) console.log("Sending spans to", endpoint);
|
|
107
|
-
const response = await fetch(endpoint, {
|
|
108
|
-
method: "POST",
|
|
109
|
-
body,
|
|
110
|
-
headers: {
|
|
111
|
-
"content-type": "application/json",
|
|
112
|
-
...headers
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
if (!response.ok) {
|
|
116
|
-
const error = await response.text();
|
|
117
|
-
throw new Error(`Failed to send spans: ${response.status} ${response.statusText}, ${error}`);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
//#endregion
|
|
121
|
-
//#region src/reporter/trace-zip-builder.ts
|
|
122
|
-
/**
|
|
123
|
-
* Get the zip filename based on test location and ID.
|
|
124
|
-
* Format: {basename(file)}:{line}-{testId}-pw-otel.zip
|
|
125
|
-
* Fallback: {testId}-pw-otel.zip (if no location info)
|
|
126
|
-
*/
|
|
127
|
-
function getZipFilename(test, testId) {
|
|
128
|
-
if (test.location) return `${path.basename(test.location.file)}:${test.location.line}-${testId}-pw-otel.zip`;
|
|
129
|
-
return `${testId}-pw-otel.zip`;
|
|
130
|
-
}
|
|
131
|
-
const PLAYWRIGHT_TRACE_RESOURCES_DIR = "resources/";
|
|
132
|
-
/**
|
|
133
|
-
* Pattern to match Playwright screenshot filenames: {name}@{hex-hash}-{timestamp}.jpeg
|
|
134
|
-
* Examples: page@f06f11f7c14d6ce1060d47d79f05c154-1766833384425.jpeg
|
|
135
|
-
*/
|
|
136
|
-
const PLAYWRIGHT_SCREENSHOT_PATTERN = /^.+@[a-f0-9]+-\d+\.jpe?g$/i;
|
|
137
|
-
/**
|
|
138
|
-
* Extract screenshots from a Playwright trace ZIP file.
|
|
139
|
-
* Screenshots are stored in the resources/ directory with names like:
|
|
140
|
-
* {pageGuid}-{timestamp}.jpeg (e.g., page@abc123-1766929201038.jpeg)
|
|
141
|
-
*
|
|
142
|
-
* @param traceZipPath - Path to the Playwright trace.zip file
|
|
143
|
-
* @returns Map of filename (without resources/ prefix) to Blob
|
|
144
|
-
*/
|
|
145
|
-
async function extractScreenshotsFromPlaywrightTrace(traceZipPath) {
|
|
146
|
-
const screenshots = /* @__PURE__ */ new Map();
|
|
147
|
-
try {
|
|
148
|
-
const zipBuffer = await fs.readFile(traceZipPath);
|
|
149
|
-
const zipReader = new ZipReader(new BlobReader(new Blob([zipBuffer])));
|
|
150
|
-
const entries = await zipReader.getEntries();
|
|
151
|
-
await Promise.all(entries.filter((entry) => {
|
|
152
|
-
if (!isFileEntry(entry)) return false;
|
|
153
|
-
if (!entry.filename.startsWith(PLAYWRIGHT_TRACE_RESOURCES_DIR)) return false;
|
|
154
|
-
const name = entry.filename.slice(10);
|
|
155
|
-
return PLAYWRIGHT_SCREENSHOT_PATTERN.test(name);
|
|
156
|
-
}).map(async (entry) => {
|
|
157
|
-
const filename = entry.filename.slice(10);
|
|
158
|
-
const blob = await entry.getData(new BlobWriter("image/jpeg"));
|
|
159
|
-
screenshots.set(filename, blob);
|
|
160
|
-
}));
|
|
161
|
-
await zipReader.close();
|
|
162
|
-
} catch (err) {
|
|
163
|
-
if (err.code !== "ENOENT") console.warn(`Warning: Could not extract screenshots from trace ZIP: ${err}`);
|
|
164
|
-
}
|
|
165
|
-
return screenshots;
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Create a zip file containing the OTLP trace JSON and screenshots for a test.
|
|
169
|
-
*/
|
|
170
|
-
async function createTraceZip(options) {
|
|
171
|
-
const { outputDir, test, spans, serviceName, playwrightVersion, screenshots } = options;
|
|
172
|
-
const otlpRequest = buildOtlpRequest(spans, serviceName, playwrightVersion);
|
|
173
|
-
const traceJson = JSON.stringify(otlpRequest, null, 2);
|
|
174
|
-
const zipWriter = new ZipWriter(new BlobWriter("application/zip"));
|
|
175
|
-
await zipWriter.add("traces/playwright-opentelemetry.json", new Blob([traceJson]).stream());
|
|
176
|
-
await Promise.all(Array.from(screenshots.entries()).map(([filename, blob]) => zipWriter.add(`screenshots/${filename}`, blob.stream())));
|
|
177
|
-
const zipBlob = await zipWriter.close();
|
|
178
|
-
const zipFilename = getZipFilename(test, test.id);
|
|
179
|
-
const zipPath = path.join(outputDir, zipFilename);
|
|
180
|
-
const arrayBuffer = await zipBlob.arrayBuffer();
|
|
181
|
-
await fs.writeFile(zipPath, Buffer.from(arrayBuffer));
|
|
182
|
-
}
|
|
183
|
-
function isFileEntry(entry) {
|
|
184
|
-
return !entry.directory;
|
|
185
|
-
}
|
|
186
|
-
//#endregion
|
|
187
|
-
//#region src/reporter/reporter.ts
|
|
188
|
-
var PlaywrightOpentelemetryReporter = class {
|
|
189
|
-
options;
|
|
190
|
-
spans = [];
|
|
191
|
-
rootDir;
|
|
192
|
-
playwrightVersion;
|
|
193
|
-
resolvedEndpoint;
|
|
194
|
-
resolvedHeaders;
|
|
195
|
-
resolvedTraceApiEndpoint;
|
|
196
|
-
resolvedTraceApiHeaders;
|
|
197
|
-
resolvedServiceName;
|
|
198
|
-
/** Maps test.id to its project's outputDir */
|
|
199
|
-
testOutputDirs = /* @__PURE__ */ new Map();
|
|
200
|
-
testSpans = /* @__PURE__ */ new Map();
|
|
201
|
-
testTraceIds = /* @__PURE__ */ new Map();
|
|
202
|
-
stepSpanIds = /* @__PURE__ */ new Map();
|
|
203
|
-
/** Tracks the span context stack per test for append-only file writes */
|
|
204
|
-
spanContextStacks = /* @__PURE__ */ new Map();
|
|
205
|
-
constructor(options = {}) {
|
|
206
|
-
this.options = options;
|
|
207
|
-
this.resolvedEndpoint = process.env.OTEL_EXPORTER_OTLP_ENDPOINT || options.otlpEndpoint || "";
|
|
208
|
-
const envHeaders = process.env.OTEL_EXPORTER_OTLP_HEADERS ? parseOtlpHeaders(process.env.OTEL_EXPORTER_OTLP_HEADERS) : {};
|
|
209
|
-
this.resolvedHeaders = {
|
|
210
|
-
...options.otlpHeaders,
|
|
211
|
-
...envHeaders
|
|
212
|
-
};
|
|
213
|
-
this.resolvedTraceApiEndpoint = options.playwrightTraceApiEndpoint || "";
|
|
214
|
-
this.resolvedTraceApiHeaders = options.playwrightTraceApiHeaders || {};
|
|
215
|
-
this.resolvedServiceName = process.env.OTEL_SERVICE_NAME || options.serviceName || "playwright-tests";
|
|
216
|
-
if (!this.resolvedEndpoint && !this.resolvedTraceApiEndpoint && !this.options.storeTraceZip) throw new Error(getConfigurationErrorMessage());
|
|
217
|
-
}
|
|
218
|
-
onBegin(config, suite) {
|
|
219
|
-
this.rootDir = config.rootDir;
|
|
220
|
-
this.playwrightVersion = config.version;
|
|
221
|
-
for (const test of suite.allTests()) {
|
|
222
|
-
const project = test.parent.project();
|
|
223
|
-
if (project?.outputDir) {
|
|
224
|
-
this.testOutputDirs.set(test.id, project.outputDir);
|
|
225
|
-
mkdirSync(path.join(project.outputDir, PW_OTEL_DIR), { recursive: true });
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
onTestBegin(test, result) {
|
|
230
|
-
const testId = test.id;
|
|
231
|
-
const outputDir = this.getOutputDir(testId);
|
|
232
|
-
const traceId = getOrCreateTraceId(outputDir, testId);
|
|
233
|
-
this.testTraceIds.set(testId, traceId);
|
|
234
|
-
const testSpanId = generateSpanId();
|
|
235
|
-
this.testSpans.set(testId, testSpanId);
|
|
236
|
-
this.spanContextStacks.set(testId, [testSpanId]);
|
|
237
|
-
writeCurrentSpanId(outputDir, testId, testSpanId);
|
|
238
|
-
createNetworkDirs(outputDir, testId);
|
|
239
|
-
}
|
|
240
|
-
async onStepBegin(test, _result, step) {
|
|
241
|
-
const testId = test.id;
|
|
242
|
-
const outputDir = this.getOutputDir(testId);
|
|
243
|
-
const stepId = getStepId(test, step);
|
|
244
|
-
const stepSpanId = generateSpanId();
|
|
245
|
-
this.stepSpanIds.set(stepId, stepSpanId);
|
|
246
|
-
const stack = this.spanContextStacks.get(testId);
|
|
247
|
-
if (stack) {
|
|
248
|
-
stack.push(stepSpanId);
|
|
249
|
-
writeCurrentSpanId(outputDir, testId, stepSpanId);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
onStepEnd(test, _result, _step) {
|
|
253
|
-
const testId = test.id;
|
|
254
|
-
const outputDir = this.getOutputDir(testId);
|
|
255
|
-
const stack = this.spanContextStacks.get(testId);
|
|
256
|
-
if (stack && stack.length > 1) {
|
|
257
|
-
stack.pop();
|
|
258
|
-
const currentParent = stack[stack.length - 1];
|
|
259
|
-
writeCurrentSpanId(outputDir, testId, currentParent);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
async onTestEnd(test, result) {
|
|
263
|
-
const testId = test.id;
|
|
264
|
-
const outputDir = this.getOutputDir(testId);
|
|
265
|
-
const traceId = this.testTraceIds.get(testId);
|
|
266
|
-
const testSpanId = this.testSpans.get(testId);
|
|
267
|
-
if (!traceId || !testSpanId) throw new Error(`Test ${testId} not found`);
|
|
268
|
-
const traceAttachment = result.attachments.find((attachment) => attachment.name === "trace" && attachment.contentType === "application/zip" && attachment.path);
|
|
269
|
-
if (!traceAttachment?.path) return;
|
|
270
|
-
result.attachments.push({
|
|
271
|
-
name: "playwright-opentelemetry-trace-id",
|
|
272
|
-
contentType: "text/plain",
|
|
273
|
-
body: Buffer.from(traceId, "utf-8")
|
|
274
|
-
});
|
|
275
|
-
const attributes = {};
|
|
276
|
-
const titlePath = test.titlePath();
|
|
277
|
-
if (titlePath.length >= 3) attributes[ATTR_TEST_CASE_NAME] = titlePath.slice(3).join(" > ");
|
|
278
|
-
const describes = titlePath.length > 4 ? titlePath.slice(3, -1) : [];
|
|
279
|
-
attributes[ATTR_TEST_CASE_TITLE] = test.title;
|
|
280
|
-
attributes[ATTR_TEST_CASE_RESULT_STATUS] = result.status === "passed" ? "pass" : "fail";
|
|
281
|
-
attributes["playwright.test.status"] = result.status;
|
|
282
|
-
attributes["playwright.test.describes"] = describes;
|
|
283
|
-
if (test.location) {
|
|
284
|
-
const { file, line } = test.location;
|
|
285
|
-
attributes[ATTR_CODE_FILE_PATH] = this.rootDir ? path.relative(this.rootDir, file) : file;
|
|
286
|
-
attributes[ATTR_CODE_LINE_NUMBER] = line;
|
|
287
|
-
}
|
|
288
|
-
const processedSteps = /* @__PURE__ */ new Map();
|
|
289
|
-
if (result.steps && result.steps.length > 0) for (const step of result.steps) this.processTestStep(test, step, testSpanId, traceId, [], processedSteps);
|
|
290
|
-
const stepSpans = [];
|
|
291
|
-
for (const [key, stepSpan] of processedSteps.entries()) if (!key.startsWith("__skip__")) stepSpans.push(stepSpan);
|
|
292
|
-
const networkSpans = await collectNetworkSpans(outputDir, testId);
|
|
293
|
-
const browserPageSpans = await collectBrowserPageSpans(outputDir, testId, networkSpans, new Date(result.startTime.getTime() + result.duration));
|
|
294
|
-
let minStartTime = result.startTime;
|
|
295
|
-
let maxEndTime = new Date(result.startTime.getTime() + result.duration);
|
|
296
|
-
for (const stepSpan of stepSpans) {
|
|
297
|
-
if (stepSpan.startTime < minStartTime) minStartTime = stepSpan.startTime;
|
|
298
|
-
if (stepSpan.endTime > maxEndTime) maxEndTime = stepSpan.endTime;
|
|
299
|
-
}
|
|
300
|
-
for (const browserSpan of [...browserPageSpans, ...networkSpans]) {
|
|
301
|
-
if (browserSpan.startTime < minStartTime) minStartTime = browserSpan.startTime;
|
|
302
|
-
if (browserSpan.endTime > maxEndTime) maxEndTime = browserSpan.endTime;
|
|
303
|
-
}
|
|
304
|
-
const testSpans = [
|
|
305
|
-
{
|
|
306
|
-
traceId,
|
|
307
|
-
spanId: testSpanId,
|
|
308
|
-
name: TEST_SPAN_NAME,
|
|
309
|
-
startTime: minStartTime,
|
|
310
|
-
endTime: maxEndTime,
|
|
311
|
-
attributes,
|
|
312
|
-
status: { code: result.status === test.expectedStatus ? 1 : 2 }
|
|
313
|
-
},
|
|
314
|
-
...stepSpans,
|
|
315
|
-
...browserPageSpans,
|
|
316
|
-
...networkSpans
|
|
317
|
-
];
|
|
318
|
-
this.spans.push(...testSpans);
|
|
319
|
-
const relativeFilePath = test.location && this.rootDir ? path.relative(this.rootDir, test.location.file) : test.location?.file ?? "";
|
|
320
|
-
const computedDuration = maxEndTime.getTime() - minStartTime.getTime();
|
|
321
|
-
const screenshots = await extractScreenshotsFromPlaywrightTrace(traceAttachment.path);
|
|
322
|
-
if (this.options.storeTraceZip) await createTraceZip({
|
|
323
|
-
outputDir,
|
|
324
|
-
testId,
|
|
325
|
-
test,
|
|
326
|
-
spans: testSpans,
|
|
327
|
-
serviceName: this.resolvedServiceName,
|
|
328
|
-
playwrightVersion: this.playwrightVersion || "unknown",
|
|
329
|
-
relativeFilePath,
|
|
330
|
-
status: result.status,
|
|
331
|
-
startTime: minStartTime,
|
|
332
|
-
duration: computedDuration,
|
|
333
|
-
screenshots
|
|
334
|
-
});
|
|
335
|
-
if (this.resolvedTraceApiEndpoint) await this.sendScreenshotsToTraceApi({
|
|
336
|
-
traceId,
|
|
337
|
-
screenshots
|
|
338
|
-
});
|
|
339
|
-
}
|
|
340
|
-
async onEnd(_result) {
|
|
341
|
-
if (this.resolvedEndpoint && this.spans.length > 0) await sendSpans(this.spans, {
|
|
342
|
-
tracesEndpoint: this.resolvedEndpoint,
|
|
343
|
-
headers: this.resolvedHeaders,
|
|
344
|
-
serviceName: this.resolvedServiceName,
|
|
345
|
-
playwrightVersion: this.playwrightVersion || "unknown",
|
|
346
|
-
debug: this.options.debug ?? false
|
|
347
|
-
});
|
|
348
|
-
if (this.resolvedTraceApiEndpoint && this.spans.length > 0) await sendSpans(this.spans, {
|
|
349
|
-
tracesEndpoint: `${this.resolvedTraceApiEndpoint}/v1/traces`,
|
|
350
|
-
headers: this.resolvedTraceApiHeaders,
|
|
351
|
-
serviceName: this.resolvedServiceName,
|
|
352
|
-
playwrightVersion: this.playwrightVersion || "unknown",
|
|
353
|
-
debug: this.options.debug ?? false
|
|
354
|
-
});
|
|
355
|
-
for (const [testId, outputDir] of this.testOutputDirs.entries()) await cleanupTestFiles(outputDir, testId);
|
|
356
|
-
}
|
|
357
|
-
processTestStep(test, step, parentSpanId, traceId, parentTitlePath, processedSteps) {
|
|
358
|
-
const isInternalFixture = step.category === "fixture" && step.location?.file.includes("playwright-opentelemetry") && (step.location?.file.endsWith("fixture.mjs") || step.location?.file.endsWith("fixture/index.ts"));
|
|
359
|
-
const stepId = getStepId(test, step);
|
|
360
|
-
if (isInternalFixture) {
|
|
361
|
-
processedSteps.set(`__skip__${stepId}`, {});
|
|
362
|
-
if (processedSteps.has(stepId)) processedSteps.delete(stepId);
|
|
363
|
-
if (step.steps && step.steps.length > 0) for (const childStep of step.steps) this.processTestStep(test, childStep, parentSpanId, traceId, parentTitlePath, processedSteps);
|
|
364
|
-
return;
|
|
365
|
-
}
|
|
366
|
-
if (processedSteps.has(`__skip__${stepId}`)) {
|
|
367
|
-
if (step.steps && step.steps.length > 0) for (const childStep of step.steps) this.processTestStep(test, childStep, parentSpanId, traceId, parentTitlePath, processedSteps);
|
|
368
|
-
return;
|
|
369
|
-
}
|
|
370
|
-
const currentTitlePath = [...parentTitlePath, step.title];
|
|
371
|
-
const stepSpanId = this.stepSpanIds.get(stepId) ?? generateSpanId();
|
|
372
|
-
const existingSpan = processedSteps.get(stepId);
|
|
373
|
-
if (existingSpan) {
|
|
374
|
-
if (step.location && !existingSpan.attributes["code.file.path"]) {
|
|
375
|
-
const { file, line } = step.location;
|
|
376
|
-
const relativePath = this.rootDir ? path.relative(this.rootDir, file) : file;
|
|
377
|
-
existingSpan.attributes[ATTR_CODE_FILE_PATH] = relativePath;
|
|
378
|
-
existingSpan.attributes[ATTR_CODE_LINE_NUMBER] = line;
|
|
379
|
-
}
|
|
380
|
-
if (step.steps && step.steps.length > 0) for (const childStep of step.steps) this.processTestStep(test, childStep, existingSpan.spanId, traceId, currentTitlePath, processedSteps);
|
|
381
|
-
return;
|
|
382
|
-
}
|
|
383
|
-
const attributes = {};
|
|
384
|
-
attributes[ATTR_TEST_STEP_NAME] = currentTitlePath.join(" > ");
|
|
385
|
-
attributes[ATTR_TEST_STEP_TITLE] = step.title;
|
|
386
|
-
attributes[ATTR_TEST_STEP_CATEGORY] = step.category;
|
|
387
|
-
if (step.location) {
|
|
388
|
-
const { file, line } = step.location;
|
|
389
|
-
attributes[ATTR_CODE_FILE_PATH] = this.rootDir ? path.relative(this.rootDir, file) : file;
|
|
390
|
-
attributes[ATTR_CODE_LINE_NUMBER] = line;
|
|
391
|
-
}
|
|
392
|
-
const stepSpan = {
|
|
393
|
-
traceId,
|
|
394
|
-
spanId: stepSpanId,
|
|
395
|
-
parentSpanId,
|
|
396
|
-
name: TEST_STEP_SPAN_NAME,
|
|
397
|
-
startTime: step.startTime,
|
|
398
|
-
endTime: new Date(step.startTime.getTime() + step.duration),
|
|
399
|
-
attributes,
|
|
400
|
-
status: { code: step.error ? 2 : 1 }
|
|
401
|
-
};
|
|
402
|
-
processedSteps.set(stepId, stepSpan);
|
|
403
|
-
if (step.steps && step.steps.length > 0) for (const childStep of step.steps) this.processTestStep(test, childStep, stepSpan.spanId, traceId, currentTitlePath, processedSteps);
|
|
404
|
-
}
|
|
405
|
-
onStdOut(chunk, _test, _result) {
|
|
406
|
-
if (this.options.debug) console.log(chunk.toString().slice(0, -1));
|
|
407
|
-
}
|
|
408
|
-
onStdErr(chunk, _test, _result) {
|
|
409
|
-
if (this.options.debug) console.log(chunk.toString().slice(0, -1));
|
|
410
|
-
}
|
|
411
|
-
printsToStdio() {
|
|
412
|
-
return this.options.debug ?? false;
|
|
413
|
-
}
|
|
414
|
-
getOutputDir(testId) {
|
|
415
|
-
const outputDir = this.testOutputDirs.get(testId);
|
|
416
|
-
if (!outputDir) throw new Error(`No outputDir found for test "${testId}" - onBegin must be called first and test must exist in suite`);
|
|
417
|
-
return outputDir;
|
|
418
|
-
}
|
|
419
|
-
async sendScreenshotsToTraceApi(params) {
|
|
420
|
-
const { traceId, screenshots } = params;
|
|
421
|
-
await Promise.all(Array.from(screenshots.entries()).map(async ([filename, blob]) => {
|
|
422
|
-
const screenshotUrl = `${this.resolvedTraceApiEndpoint}/playwright-otel-reporter/v1/screenshots/${filename}`;
|
|
423
|
-
const response = await fetch(screenshotUrl, {
|
|
424
|
-
method: "PUT",
|
|
425
|
-
headers: {
|
|
426
|
-
"content-type": "image/jpeg",
|
|
427
|
-
"x-trace-id": traceId,
|
|
428
|
-
...this.resolvedTraceApiHeaders
|
|
429
|
-
},
|
|
430
|
-
body: blob
|
|
431
|
-
});
|
|
432
|
-
if (!response.ok) {
|
|
433
|
-
const error = await response.text();
|
|
434
|
-
throw new Error(`Failed to send screenshot ${filename}: ${response.status} ${response.statusText}, ${error}`);
|
|
435
|
-
}
|
|
436
|
-
}));
|
|
437
|
-
}
|
|
438
|
-
};
|
|
439
|
-
function parseOtlpHeaders(headersString) {
|
|
440
|
-
const headers = {};
|
|
441
|
-
const pairs = headersString.split(",");
|
|
442
|
-
for (const pair of pairs) {
|
|
443
|
-
const [key, ...valueParts] = pair.split("=");
|
|
444
|
-
if (key && valueParts.length > 0) headers[key.trim()] = valueParts.join("=").trim();
|
|
445
|
-
}
|
|
446
|
-
return headers;
|
|
447
|
-
}
|
|
448
|
-
function getConfigurationErrorMessage() {
|
|
449
|
-
return "playwright-opentelemetry reporter requires an OTLP endpoint 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 { PlaywrightOpentelemetryReporterOptions } from 'playwright-opentelemetry';\n\nexport default defineConfig({\n reporter: [\n [\n 'playwright-opentelemetry/reporter',\n {\n otlpEndpoint: 'http://localhost:4317/v1/traces',\n otlpHeaders: {\n Authorization: 'Bearer YOUR_TOKEN',\n },\n serviceName: 'my-service',\n } satisfies PlaywrightOpentelemetryReporterOptions,\n ],\n ],\n});\n\nNote: Environment variables take precedence over config file options.\n";
|
|
450
|
-
}
|
|
451
|
-
function getStepId(test, step) {
|
|
452
|
-
const startTimeMs = step.startTime.getTime();
|
|
453
|
-
return [
|
|
454
|
-
test.id,
|
|
455
|
-
step.category,
|
|
456
|
-
startTimeMs,
|
|
457
|
-
...step.titlePath()
|
|
458
|
-
].join(" > ");
|
|
459
|
-
}
|
|
460
|
-
//#endregion
|
|
461
|
-
export { PlaywrightOpentelemetryReporter as t };
|