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,286 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region package.json
|
|
24
|
+
var version = "0.6.0";
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/shared/otel.ts
|
|
27
|
+
/** Generate a random 32-character hex trace ID. */
|
|
28
|
+
function generateTraceId() {
|
|
29
|
+
return Array.from({ length: 32 }, () => Math.floor(Math.random() * 16).toString(16)).join("");
|
|
30
|
+
}
|
|
31
|
+
/** Generate a random 16-character hex span ID. */
|
|
32
|
+
function generateSpanId() {
|
|
33
|
+
return Array.from({ length: 16 }, () => Math.floor(Math.random() * 16).toString(16)).join("");
|
|
34
|
+
}
|
|
35
|
+
function parseOtlpHeaders(headersString) {
|
|
36
|
+
const headers = {};
|
|
37
|
+
if (!headersString) return headers;
|
|
38
|
+
for (const pair of headersString.split(",")) {
|
|
39
|
+
const [key, ...valueParts] = pair.split("=");
|
|
40
|
+
if (key && valueParts.length > 0) headers[key.trim()] = valueParts.join("=").trim();
|
|
41
|
+
}
|
|
42
|
+
return headers;
|
|
43
|
+
}
|
|
44
|
+
function dateToNanoseconds(date) {
|
|
45
|
+
return (BigInt(date.getTime()) * BigInt(1e6)).toString();
|
|
46
|
+
}
|
|
47
|
+
function toOtlpAttributes(attributes) {
|
|
48
|
+
return Object.entries(attributes).map(([key, value]) => {
|
|
49
|
+
if (Array.isArray(value)) return {
|
|
50
|
+
key,
|
|
51
|
+
value: { arrayValue: { values: value.map((item) => ({ stringValue: item })) } }
|
|
52
|
+
};
|
|
53
|
+
if (typeof value === "number") return {
|
|
54
|
+
key,
|
|
55
|
+
value: Number.isInteger(value) ? { intValue: value } : { doubleValue: value }
|
|
56
|
+
};
|
|
57
|
+
if (typeof value === "boolean") return {
|
|
58
|
+
key,
|
|
59
|
+
value: { boolValue: value }
|
|
60
|
+
};
|
|
61
|
+
return {
|
|
62
|
+
key,
|
|
63
|
+
value: { stringValue: value }
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
const SPAN_KIND_INTERNAL = 1;
|
|
68
|
+
function buildResourceSpan(spans, serviceName, playwrightVersion) {
|
|
69
|
+
const otlpSpans = spans.map((span) => ({
|
|
70
|
+
traceId: span.traceId,
|
|
71
|
+
spanId: span.spanId,
|
|
72
|
+
parentSpanId: span.parentSpanId || void 0,
|
|
73
|
+
name: span.name,
|
|
74
|
+
kind: span.kind ?? SPAN_KIND_INTERNAL,
|
|
75
|
+
startTimeUnixNano: dateToNanoseconds(span.startTime),
|
|
76
|
+
endTimeUnixNano: dateToNanoseconds(span.endTime),
|
|
77
|
+
attributes: toOtlpAttributes(span.attributes),
|
|
78
|
+
droppedAttributesCount: 0,
|
|
79
|
+
events: [],
|
|
80
|
+
droppedEventsCount: 0,
|
|
81
|
+
status: span.status,
|
|
82
|
+
links: [],
|
|
83
|
+
droppedLinksCount: 0
|
|
84
|
+
}));
|
|
85
|
+
return {
|
|
86
|
+
resource: { attributes: [
|
|
87
|
+
{
|
|
88
|
+
key: "service.name",
|
|
89
|
+
value: { stringValue: serviceName }
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
key: "service.namespace",
|
|
93
|
+
value: { stringValue: "playwright" }
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
key: "service.version",
|
|
97
|
+
value: { stringValue: playwrightVersion }
|
|
98
|
+
}
|
|
99
|
+
] },
|
|
100
|
+
scopeSpans: [{
|
|
101
|
+
scope: {
|
|
102
|
+
name: "playwright-opentelemetry",
|
|
103
|
+
version
|
|
104
|
+
},
|
|
105
|
+
spans: otlpSpans
|
|
106
|
+
}]
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function buildOtlpRequest(spans, serviceName, playwrightVersion) {
|
|
110
|
+
const spansByService = /* @__PURE__ */ new Map();
|
|
111
|
+
for (const span of spans) {
|
|
112
|
+
const spanServiceName = span.serviceName ?? serviceName;
|
|
113
|
+
const serviceSpans = spansByService.get(spanServiceName);
|
|
114
|
+
if (serviceSpans) serviceSpans.push(span);
|
|
115
|
+
else spansByService.set(spanServiceName, [span]);
|
|
116
|
+
}
|
|
117
|
+
const resourceSpans = [];
|
|
118
|
+
for (const [svcName, svcSpans] of spansByService) resourceSpans.push(buildResourceSpan(svcSpans, svcName, playwrightVersion));
|
|
119
|
+
return { resourceSpans };
|
|
120
|
+
}
|
|
121
|
+
async function sendSpans(spans, options) {
|
|
122
|
+
if (spans.length === 0) return;
|
|
123
|
+
const body = JSON.stringify(buildOtlpRequest(spans, options.serviceName, options.playwrightVersion));
|
|
124
|
+
if (options.debug) console.log("Sending spans to", options.tracesEndpoint);
|
|
125
|
+
const response = await fetch(options.tracesEndpoint, {
|
|
126
|
+
method: "POST",
|
|
127
|
+
body,
|
|
128
|
+
headers: {
|
|
129
|
+
"content-type": "application/json",
|
|
130
|
+
...options.headers || {}
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
if (!response.ok) {
|
|
134
|
+
const error = await response.text();
|
|
135
|
+
throw new Error(`Failed to send spans: ${response.status} ${response.statusText}, ${error}`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
//#endregion
|
|
139
|
+
//#region src/shared/config.ts
|
|
140
|
+
function resolvePlaywrightOpentelemetryConfig(config, options = {}) {
|
|
141
|
+
const envOtlpHeaders = parseOtlpHeaders(process.env.OTEL_EXPORTER_OTLP_HEADERS);
|
|
142
|
+
const envTraceApiHeaders = parseOtlpHeaders(process.env.PLAYWRIGHT_TRACE_API_HEADERS);
|
|
143
|
+
const debugEnv = process.env.PLAYWRIGHT_OPENTELEMETRY_DEBUG;
|
|
144
|
+
const resolvedConfig = {
|
|
145
|
+
otlpEndpoint: process.env.OTEL_EXPORTER_OTLP_ENDPOINT || config?.otlpEndpoint || "",
|
|
146
|
+
otlpHeaders: {
|
|
147
|
+
...config?.otlpHeaders,
|
|
148
|
+
...envOtlpHeaders
|
|
149
|
+
},
|
|
150
|
+
playwrightTraceApiEndpoint: process.env.PLAYWRIGHT_TRACE_API_ENDPOINT || config?.playwrightTraceApiEndpoint || "",
|
|
151
|
+
playwrightTraceApiHeaders: {
|
|
152
|
+
...config?.playwrightTraceApiHeaders,
|
|
153
|
+
...envTraceApiHeaders
|
|
154
|
+
},
|
|
155
|
+
storeTraceZip: config?.storeTraceZip === true,
|
|
156
|
+
serviceName: process.env.OTEL_SERVICE_NAME || config?.serviceName || "playwright-tests",
|
|
157
|
+
debug: debugEnv === void 0 ? config?.debug ?? false : debugEnv === "1" || debugEnv === "true"
|
|
158
|
+
};
|
|
159
|
+
if (options.requireDestination === true && !hasPlaywrightOpentelemetryDestination(resolvedConfig)) throw new Error(getConfigurationErrorMessage());
|
|
160
|
+
return resolvedConfig;
|
|
161
|
+
}
|
|
162
|
+
function hasPlaywrightOpentelemetryDestination(config) {
|
|
163
|
+
return Boolean(config.otlpEndpoint || config.playwrightTraceApiEndpoint || config.storeTraceZip);
|
|
164
|
+
}
|
|
165
|
+
function getConfigurationErrorMessage() {
|
|
166
|
+
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";
|
|
167
|
+
}
|
|
168
|
+
//#endregion
|
|
169
|
+
//#region src/fixture/trace-context.ts
|
|
170
|
+
const TRACE_CONTEXT_ATTACHMENT_NAME = "playwright-opentelemetry-trace-context";
|
|
171
|
+
const FIXTURE_SPANS_ATTACHMENT_NAME = "playwright-opentelemetry-fixture-spans";
|
|
172
|
+
async function createTestTraceContext(testInfo) {
|
|
173
|
+
const traceContext = {
|
|
174
|
+
traceId: generateTraceId(),
|
|
175
|
+
rootSpanId: generateSpanId(),
|
|
176
|
+
spans: [],
|
|
177
|
+
requestContexts: /* @__PURE__ */ new WeakMap(),
|
|
178
|
+
addSpan(span) {
|
|
179
|
+
this.spans.push(span);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
await testInfo.attach(TRACE_CONTEXT_ATTACHMENT_NAME, {
|
|
183
|
+
body: JSON.stringify({
|
|
184
|
+
traceId: traceContext.traceId,
|
|
185
|
+
rootSpanId: traceContext.rootSpanId
|
|
186
|
+
}),
|
|
187
|
+
contentType: "application/json"
|
|
188
|
+
});
|
|
189
|
+
return traceContext;
|
|
190
|
+
}
|
|
191
|
+
async function flushFixtureSpans(traceContext, config, testInfo) {
|
|
192
|
+
if (traceContext.spans.length === 0) return;
|
|
193
|
+
if (config.storeTraceZip && testInfo) await testInfo.attach(FIXTURE_SPANS_ATTACHMENT_NAME, {
|
|
194
|
+
body: JSON.stringify({ spans: traceContext.spans.map(serializeSpanForAttachment) }),
|
|
195
|
+
contentType: "application/json"
|
|
196
|
+
});
|
|
197
|
+
const destinations = fixtureSpanDestinations(config);
|
|
198
|
+
if (destinations.length === 0) return;
|
|
199
|
+
await Promise.all(destinations.map((destination) => sendSpans(traceContext.spans, {
|
|
200
|
+
tracesEndpoint: destination.tracesEndpoint,
|
|
201
|
+
headers: destination.headers,
|
|
202
|
+
serviceName: "playwright-browser",
|
|
203
|
+
playwrightVersion: "unknown",
|
|
204
|
+
debug: config.debug
|
|
205
|
+
})));
|
|
206
|
+
}
|
|
207
|
+
function serializeSpanForAttachment(span) {
|
|
208
|
+
return {
|
|
209
|
+
...span,
|
|
210
|
+
startTime: span.startTime.toISOString(),
|
|
211
|
+
endTime: span.endTime.toISOString()
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
function fixtureSpanDestinations(config) {
|
|
215
|
+
const destinations = [];
|
|
216
|
+
if (config.playwrightTraceApiEndpoint) destinations.push({
|
|
217
|
+
tracesEndpoint: `${config.playwrightTraceApiEndpoint}/v1/traces`,
|
|
218
|
+
headers: config.playwrightTraceApiHeaders
|
|
219
|
+
});
|
|
220
|
+
if (config.otlpEndpoint) destinations.push({
|
|
221
|
+
tracesEndpoint: config.otlpEndpoint,
|
|
222
|
+
headers: config.otlpHeaders
|
|
223
|
+
});
|
|
224
|
+
return destinations;
|
|
225
|
+
}
|
|
226
|
+
//#endregion
|
|
227
|
+
Object.defineProperty(exports, "FIXTURE_SPANS_ATTACHMENT_NAME", {
|
|
228
|
+
enumerable: true,
|
|
229
|
+
get: function() {
|
|
230
|
+
return FIXTURE_SPANS_ATTACHMENT_NAME;
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
Object.defineProperty(exports, "TRACE_CONTEXT_ATTACHMENT_NAME", {
|
|
234
|
+
enumerable: true,
|
|
235
|
+
get: function() {
|
|
236
|
+
return TRACE_CONTEXT_ATTACHMENT_NAME;
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
Object.defineProperty(exports, "__toESM", {
|
|
240
|
+
enumerable: true,
|
|
241
|
+
get: function() {
|
|
242
|
+
return __toESM;
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
Object.defineProperty(exports, "buildOtlpRequest", {
|
|
246
|
+
enumerable: true,
|
|
247
|
+
get: function() {
|
|
248
|
+
return buildOtlpRequest;
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
Object.defineProperty(exports, "createTestTraceContext", {
|
|
252
|
+
enumerable: true,
|
|
253
|
+
get: function() {
|
|
254
|
+
return createTestTraceContext;
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
Object.defineProperty(exports, "flushFixtureSpans", {
|
|
258
|
+
enumerable: true,
|
|
259
|
+
get: function() {
|
|
260
|
+
return flushFixtureSpans;
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
Object.defineProperty(exports, "generateSpanId", {
|
|
264
|
+
enumerable: true,
|
|
265
|
+
get: function() {
|
|
266
|
+
return generateSpanId;
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
Object.defineProperty(exports, "generateTraceId", {
|
|
270
|
+
enumerable: true,
|
|
271
|
+
get: function() {
|
|
272
|
+
return generateTraceId;
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
Object.defineProperty(exports, "resolvePlaywrightOpentelemetryConfig", {
|
|
276
|
+
enumerable: true,
|
|
277
|
+
get: function() {
|
|
278
|
+
return resolvePlaywrightOpentelemetryConfig;
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
Object.defineProperty(exports, "sendSpans", {
|
|
282
|
+
enumerable: true,
|
|
283
|
+
get: function() {
|
|
284
|
+
return sendSpans;
|
|
285
|
+
}
|
|
286
|
+
});
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
//#region package.json
|
|
2
|
+
var version = "0.6.0";
|
|
3
|
+
//#endregion
|
|
4
|
+
//#region src/shared/otel.ts
|
|
5
|
+
/** Generate a random 32-character hex trace ID. */
|
|
6
|
+
function generateTraceId() {
|
|
7
|
+
return Array.from({ length: 32 }, () => Math.floor(Math.random() * 16).toString(16)).join("");
|
|
8
|
+
}
|
|
9
|
+
/** Generate a random 16-character hex span ID. */
|
|
10
|
+
function generateSpanId() {
|
|
11
|
+
return Array.from({ length: 16 }, () => Math.floor(Math.random() * 16).toString(16)).join("");
|
|
12
|
+
}
|
|
13
|
+
function parseOtlpHeaders(headersString) {
|
|
14
|
+
const headers = {};
|
|
15
|
+
if (!headersString) return headers;
|
|
16
|
+
for (const pair of headersString.split(",")) {
|
|
17
|
+
const [key, ...valueParts] = pair.split("=");
|
|
18
|
+
if (key && valueParts.length > 0) headers[key.trim()] = valueParts.join("=").trim();
|
|
19
|
+
}
|
|
20
|
+
return headers;
|
|
21
|
+
}
|
|
22
|
+
function dateToNanoseconds(date) {
|
|
23
|
+
return (BigInt(date.getTime()) * BigInt(1e6)).toString();
|
|
24
|
+
}
|
|
25
|
+
function toOtlpAttributes(attributes) {
|
|
26
|
+
return Object.entries(attributes).map(([key, value]) => {
|
|
27
|
+
if (Array.isArray(value)) return {
|
|
28
|
+
key,
|
|
29
|
+
value: { arrayValue: { values: value.map((item) => ({ stringValue: item })) } }
|
|
30
|
+
};
|
|
31
|
+
if (typeof value === "number") return {
|
|
32
|
+
key,
|
|
33
|
+
value: Number.isInteger(value) ? { intValue: value } : { doubleValue: value }
|
|
34
|
+
};
|
|
35
|
+
if (typeof value === "boolean") return {
|
|
36
|
+
key,
|
|
37
|
+
value: { boolValue: value }
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
key,
|
|
41
|
+
value: { stringValue: value }
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const SPAN_KIND_INTERNAL = 1;
|
|
46
|
+
function buildResourceSpan(spans, serviceName, playwrightVersion) {
|
|
47
|
+
const otlpSpans = spans.map((span) => ({
|
|
48
|
+
traceId: span.traceId,
|
|
49
|
+
spanId: span.spanId,
|
|
50
|
+
parentSpanId: span.parentSpanId || void 0,
|
|
51
|
+
name: span.name,
|
|
52
|
+
kind: span.kind ?? SPAN_KIND_INTERNAL,
|
|
53
|
+
startTimeUnixNano: dateToNanoseconds(span.startTime),
|
|
54
|
+
endTimeUnixNano: dateToNanoseconds(span.endTime),
|
|
55
|
+
attributes: toOtlpAttributes(span.attributes),
|
|
56
|
+
droppedAttributesCount: 0,
|
|
57
|
+
events: [],
|
|
58
|
+
droppedEventsCount: 0,
|
|
59
|
+
status: span.status,
|
|
60
|
+
links: [],
|
|
61
|
+
droppedLinksCount: 0
|
|
62
|
+
}));
|
|
63
|
+
return {
|
|
64
|
+
resource: { attributes: [
|
|
65
|
+
{
|
|
66
|
+
key: "service.name",
|
|
67
|
+
value: { stringValue: serviceName }
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
key: "service.namespace",
|
|
71
|
+
value: { stringValue: "playwright" }
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
key: "service.version",
|
|
75
|
+
value: { stringValue: playwrightVersion }
|
|
76
|
+
}
|
|
77
|
+
] },
|
|
78
|
+
scopeSpans: [{
|
|
79
|
+
scope: {
|
|
80
|
+
name: "playwright-opentelemetry",
|
|
81
|
+
version
|
|
82
|
+
},
|
|
83
|
+
spans: otlpSpans
|
|
84
|
+
}]
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function buildOtlpRequest(spans, serviceName, playwrightVersion) {
|
|
88
|
+
const spansByService = /* @__PURE__ */ new Map();
|
|
89
|
+
for (const span of spans) {
|
|
90
|
+
const spanServiceName = span.serviceName ?? serviceName;
|
|
91
|
+
const serviceSpans = spansByService.get(spanServiceName);
|
|
92
|
+
if (serviceSpans) serviceSpans.push(span);
|
|
93
|
+
else spansByService.set(spanServiceName, [span]);
|
|
94
|
+
}
|
|
95
|
+
const resourceSpans = [];
|
|
96
|
+
for (const [svcName, svcSpans] of spansByService) resourceSpans.push(buildResourceSpan(svcSpans, svcName, playwrightVersion));
|
|
97
|
+
return { resourceSpans };
|
|
98
|
+
}
|
|
99
|
+
async function sendSpans(spans, options) {
|
|
100
|
+
if (spans.length === 0) return;
|
|
101
|
+
const body = JSON.stringify(buildOtlpRequest(spans, options.serviceName, options.playwrightVersion));
|
|
102
|
+
if (options.debug) console.log("Sending spans to", options.tracesEndpoint);
|
|
103
|
+
const response = await fetch(options.tracesEndpoint, {
|
|
104
|
+
method: "POST",
|
|
105
|
+
body,
|
|
106
|
+
headers: {
|
|
107
|
+
"content-type": "application/json",
|
|
108
|
+
...options.headers || {}
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
if (!response.ok) {
|
|
112
|
+
const error = await response.text();
|
|
113
|
+
throw new Error(`Failed to send spans: ${response.status} ${response.statusText}, ${error}`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region src/shared/config.ts
|
|
118
|
+
function resolvePlaywrightOpentelemetryConfig(config, options = {}) {
|
|
119
|
+
const envOtlpHeaders = parseOtlpHeaders(process.env.OTEL_EXPORTER_OTLP_HEADERS);
|
|
120
|
+
const envTraceApiHeaders = parseOtlpHeaders(process.env.PLAYWRIGHT_TRACE_API_HEADERS);
|
|
121
|
+
const debugEnv = process.env.PLAYWRIGHT_OPENTELEMETRY_DEBUG;
|
|
122
|
+
const resolvedConfig = {
|
|
123
|
+
otlpEndpoint: process.env.OTEL_EXPORTER_OTLP_ENDPOINT || config?.otlpEndpoint || "",
|
|
124
|
+
otlpHeaders: {
|
|
125
|
+
...config?.otlpHeaders,
|
|
126
|
+
...envOtlpHeaders
|
|
127
|
+
},
|
|
128
|
+
playwrightTraceApiEndpoint: process.env.PLAYWRIGHT_TRACE_API_ENDPOINT || config?.playwrightTraceApiEndpoint || "",
|
|
129
|
+
playwrightTraceApiHeaders: {
|
|
130
|
+
...config?.playwrightTraceApiHeaders,
|
|
131
|
+
...envTraceApiHeaders
|
|
132
|
+
},
|
|
133
|
+
storeTraceZip: config?.storeTraceZip === true,
|
|
134
|
+
serviceName: process.env.OTEL_SERVICE_NAME || config?.serviceName || "playwright-tests",
|
|
135
|
+
debug: debugEnv === void 0 ? config?.debug ?? false : debugEnv === "1" || debugEnv === "true"
|
|
136
|
+
};
|
|
137
|
+
if (options.requireDestination === true && !hasPlaywrightOpentelemetryDestination(resolvedConfig)) throw new Error(getConfigurationErrorMessage());
|
|
138
|
+
return resolvedConfig;
|
|
139
|
+
}
|
|
140
|
+
function hasPlaywrightOpentelemetryDestination(config) {
|
|
141
|
+
return Boolean(config.otlpEndpoint || config.playwrightTraceApiEndpoint || config.storeTraceZip);
|
|
142
|
+
}
|
|
143
|
+
function getConfigurationErrorMessage() {
|
|
144
|
+
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";
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/fixture/trace-context.ts
|
|
148
|
+
const TRACE_CONTEXT_ATTACHMENT_NAME = "playwright-opentelemetry-trace-context";
|
|
149
|
+
const FIXTURE_SPANS_ATTACHMENT_NAME = "playwright-opentelemetry-fixture-spans";
|
|
150
|
+
async function createTestTraceContext(testInfo) {
|
|
151
|
+
const traceContext = {
|
|
152
|
+
traceId: generateTraceId(),
|
|
153
|
+
rootSpanId: generateSpanId(),
|
|
154
|
+
spans: [],
|
|
155
|
+
requestContexts: /* @__PURE__ */ new WeakMap(),
|
|
156
|
+
addSpan(span) {
|
|
157
|
+
this.spans.push(span);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
await testInfo.attach(TRACE_CONTEXT_ATTACHMENT_NAME, {
|
|
161
|
+
body: JSON.stringify({
|
|
162
|
+
traceId: traceContext.traceId,
|
|
163
|
+
rootSpanId: traceContext.rootSpanId
|
|
164
|
+
}),
|
|
165
|
+
contentType: "application/json"
|
|
166
|
+
});
|
|
167
|
+
return traceContext;
|
|
168
|
+
}
|
|
169
|
+
async function flushFixtureSpans(traceContext, config, testInfo) {
|
|
170
|
+
if (traceContext.spans.length === 0) return;
|
|
171
|
+
if (config.storeTraceZip && testInfo) await testInfo.attach(FIXTURE_SPANS_ATTACHMENT_NAME, {
|
|
172
|
+
body: JSON.stringify({ spans: traceContext.spans.map(serializeSpanForAttachment) }),
|
|
173
|
+
contentType: "application/json"
|
|
174
|
+
});
|
|
175
|
+
const destinations = fixtureSpanDestinations(config);
|
|
176
|
+
if (destinations.length === 0) return;
|
|
177
|
+
await Promise.all(destinations.map((destination) => sendSpans(traceContext.spans, {
|
|
178
|
+
tracesEndpoint: destination.tracesEndpoint,
|
|
179
|
+
headers: destination.headers,
|
|
180
|
+
serviceName: "playwright-browser",
|
|
181
|
+
playwrightVersion: "unknown",
|
|
182
|
+
debug: config.debug
|
|
183
|
+
})));
|
|
184
|
+
}
|
|
185
|
+
function serializeSpanForAttachment(span) {
|
|
186
|
+
return {
|
|
187
|
+
...span,
|
|
188
|
+
startTime: span.startTime.toISOString(),
|
|
189
|
+
endTime: span.endTime.toISOString()
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
function fixtureSpanDestinations(config) {
|
|
193
|
+
const destinations = [];
|
|
194
|
+
if (config.playwrightTraceApiEndpoint) destinations.push({
|
|
195
|
+
tracesEndpoint: `${config.playwrightTraceApiEndpoint}/v1/traces`,
|
|
196
|
+
headers: config.playwrightTraceApiHeaders
|
|
197
|
+
});
|
|
198
|
+
if (config.otlpEndpoint) destinations.push({
|
|
199
|
+
tracesEndpoint: config.otlpEndpoint,
|
|
200
|
+
headers: config.otlpHeaders
|
|
201
|
+
});
|
|
202
|
+
return destinations;
|
|
203
|
+
}
|
|
204
|
+
//#endregion
|
|
205
|
+
export { resolvePlaywrightOpentelemetryConfig as a, generateTraceId as c, flushFixtureSpans as i, sendSpans as l, TRACE_CONTEXT_ATTACHMENT_NAME as n, buildOtlpRequest as o, createTestTraceContext as r, generateSpanId as s, FIXTURE_SPANS_ATTACHMENT_NAME as t };
|
package/package.json
CHANGED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { FullConfig, FullResult, Reporter, Suite, TestCase, TestResult, TestStep } from "@playwright/test/reporter";
|
|
2
|
-
|
|
3
|
-
//#region src/reporter/reporter.d.ts
|
|
4
|
-
interface PlaywrightOpentelemetryReporterOptions {
|
|
5
|
-
otlpEndpoint?: string;
|
|
6
|
-
otlpHeaders?: Record<string, string>;
|
|
7
|
-
playwrightTraceApiEndpoint?: string;
|
|
8
|
-
playwrightTraceApiHeaders?: Record<string, string>;
|
|
9
|
-
storeTraceZip?: boolean;
|
|
10
|
-
serviceName?: string;
|
|
11
|
-
debug?: boolean;
|
|
12
|
-
}
|
|
13
|
-
declare class PlaywrightOpentelemetryReporter implements Reporter {
|
|
14
|
-
private options;
|
|
15
|
-
private spans;
|
|
16
|
-
private rootDir?;
|
|
17
|
-
private playwrightVersion?;
|
|
18
|
-
private resolvedEndpoint;
|
|
19
|
-
private resolvedHeaders;
|
|
20
|
-
private resolvedTraceApiEndpoint;
|
|
21
|
-
private resolvedTraceApiHeaders;
|
|
22
|
-
private resolvedServiceName;
|
|
23
|
-
/** Maps test.id to its project's outputDir */
|
|
24
|
-
private testOutputDirs;
|
|
25
|
-
private testSpans;
|
|
26
|
-
private testTraceIds;
|
|
27
|
-
private stepSpanIds;
|
|
28
|
-
/** Tracks the span context stack per test for append-only file writes */
|
|
29
|
-
private spanContextStacks;
|
|
30
|
-
constructor(options?: PlaywrightOpentelemetryReporterOptions);
|
|
31
|
-
onBegin(config: FullConfig, suite: Suite): void;
|
|
32
|
-
onTestBegin(test: TestCase, result: TestResult): void;
|
|
33
|
-
onStepBegin(test: TestCase, _result: TestResult, step: TestStep): Promise<void>;
|
|
34
|
-
onStepEnd(test: TestCase, _result: TestResult, _step: TestStep): void;
|
|
35
|
-
onTestEnd(test: TestCase, result: TestResult): Promise<void>;
|
|
36
|
-
onEnd(_result: FullResult): Promise<void>;
|
|
37
|
-
private processTestStep;
|
|
38
|
-
onStdOut(chunk: string | Buffer, _test: TestCase, _result: TestResult): void;
|
|
39
|
-
onStdErr(chunk: string | Buffer, _test: TestCase, _result: TestResult): void;
|
|
40
|
-
printsToStdio(): boolean;
|
|
41
|
-
private getOutputDir;
|
|
42
|
-
private sendScreenshotsToTraceApi;
|
|
43
|
-
}
|
|
44
|
-
//#endregion
|
|
45
|
-
export { PlaywrightOpentelemetryReporterOptions as n, PlaywrightOpentelemetryReporter as t };
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { FullConfig, FullResult, Reporter, Suite, TestCase, TestResult, TestStep } from "@playwright/test/reporter";
|
|
2
|
-
|
|
3
|
-
//#region src/reporter/reporter.d.ts
|
|
4
|
-
interface PlaywrightOpentelemetryReporterOptions {
|
|
5
|
-
otlpEndpoint?: string;
|
|
6
|
-
otlpHeaders?: Record<string, string>;
|
|
7
|
-
playwrightTraceApiEndpoint?: string;
|
|
8
|
-
playwrightTraceApiHeaders?: Record<string, string>;
|
|
9
|
-
storeTraceZip?: boolean;
|
|
10
|
-
serviceName?: string;
|
|
11
|
-
debug?: boolean;
|
|
12
|
-
}
|
|
13
|
-
declare class PlaywrightOpentelemetryReporter implements Reporter {
|
|
14
|
-
private options;
|
|
15
|
-
private spans;
|
|
16
|
-
private rootDir?;
|
|
17
|
-
private playwrightVersion?;
|
|
18
|
-
private resolvedEndpoint;
|
|
19
|
-
private resolvedHeaders;
|
|
20
|
-
private resolvedTraceApiEndpoint;
|
|
21
|
-
private resolvedTraceApiHeaders;
|
|
22
|
-
private resolvedServiceName;
|
|
23
|
-
/** Maps test.id to its project's outputDir */
|
|
24
|
-
private testOutputDirs;
|
|
25
|
-
private testSpans;
|
|
26
|
-
private testTraceIds;
|
|
27
|
-
private stepSpanIds;
|
|
28
|
-
/** Tracks the span context stack per test for append-only file writes */
|
|
29
|
-
private spanContextStacks;
|
|
30
|
-
constructor(options?: PlaywrightOpentelemetryReporterOptions);
|
|
31
|
-
onBegin(config: FullConfig, suite: Suite): void;
|
|
32
|
-
onTestBegin(test: TestCase, result: TestResult): void;
|
|
33
|
-
onStepBegin(test: TestCase, _result: TestResult, step: TestStep): Promise<void>;
|
|
34
|
-
onStepEnd(test: TestCase, _result: TestResult, _step: TestStep): void;
|
|
35
|
-
onTestEnd(test: TestCase, result: TestResult): Promise<void>;
|
|
36
|
-
onEnd(_result: FullResult): Promise<void>;
|
|
37
|
-
private processTestStep;
|
|
38
|
-
onStdOut(chunk: string | Buffer, _test: TestCase, _result: TestResult): void;
|
|
39
|
-
onStdErr(chunk: string | Buffer, _test: TestCase, _result: TestResult): void;
|
|
40
|
-
printsToStdio(): boolean;
|
|
41
|
-
private getOutputDir;
|
|
42
|
-
private sendScreenshotsToTraceApi;
|
|
43
|
-
}
|
|
44
|
-
//#endregion
|
|
45
|
-
export { PlaywrightOpentelemetryReporterOptions as n, PlaywrightOpentelemetryReporter as t };
|