playwright-opentelemetry 0.0.4 → 0.0.6
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/README.md +5 -1
- package/dist/index.d.mts +18 -5
- package/dist/index.mjs +190 -7
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -7,4 +7,8 @@ We're just getting started, this project is in early development. Reach out if y
|
|
|
7
7
|
## Development
|
|
8
8
|
|
|
9
9
|
- `pnpm dev` starts a dev server that outputs `dist/index.mjs`
|
|
10
|
-
- `pnpm
|
|
10
|
+
- `pnpm build` otherwise creates a one-off compiled build
|
|
11
|
+
- `pnpm test:unit` to run the unit tests
|
|
12
|
+
- `pnpm test:e2e` uses the compiled reporter output
|
|
13
|
+
- `pnpm typecheck` for typescript
|
|
14
|
+
- `pnpm format` to format files
|
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,23 @@
|
|
|
1
|
-
import { FullResult, Reporter, TestCase, TestResult } from "@playwright/test/reporter";
|
|
1
|
+
import { FullConfig, FullResult, Reporter, Suite, TestCase, TestResult } from "@playwright/test/reporter";
|
|
2
2
|
|
|
3
|
-
//#region src/
|
|
3
|
+
//#region src/reporter.d.ts
|
|
4
|
+
interface PlaywrightOpentelemetryReporterOptions {
|
|
5
|
+
tracesEndpoint: string;
|
|
6
|
+
headers?: Record<string, string>;
|
|
7
|
+
debug?: boolean;
|
|
8
|
+
}
|
|
4
9
|
declare class PlaywrightOpentelemetryReporter implements Reporter {
|
|
5
|
-
|
|
6
|
-
|
|
10
|
+
private options;
|
|
11
|
+
private spans;
|
|
12
|
+
private rootDir?;
|
|
13
|
+
private playwrightVersion?;
|
|
14
|
+
constructor(options: PlaywrightOpentelemetryReporterOptions);
|
|
15
|
+
onBegin(config: FullConfig, _suite: Suite): void;
|
|
16
|
+
onEnd(_result: FullResult): Promise<void>;
|
|
17
|
+
onTestBegin(_test: TestCase): void;
|
|
7
18
|
onTestEnd(test: TestCase, result: TestResult): void;
|
|
19
|
+
private processTestStep;
|
|
20
|
+
printsToStdio(): boolean;
|
|
8
21
|
}
|
|
9
22
|
//#endregion
|
|
10
|
-
export { PlaywrightOpentelemetryReporter as default };
|
|
23
|
+
export { type PlaywrightOpentelemetryReporterOptions, PlaywrightOpentelemetryReporter as default };
|
package/dist/index.mjs
CHANGED
|
@@ -1,15 +1,198 @@
|
|
|
1
|
-
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
//#region src/otel-attributes.ts
|
|
4
|
+
const ATTR_CODE_FILE_PATH = "code.file.path";
|
|
5
|
+
const ATTR_CODE_LINE_NUMBER = "code.line.number";
|
|
6
|
+
const ATTR_TEST_CASE_NAME = "test.case.name";
|
|
7
|
+
const ATTR_TEST_CASE_TITLE = "test.case.title";
|
|
8
|
+
const ATTR_TEST_CASE_RESULT_STATUS = "test.case.result.status";
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/reporter-attributes.ts
|
|
12
|
+
const TEST_SPAN_NAME = "playwright.test";
|
|
13
|
+
const TEST_STEP_SPAN_NAME = "playwright.test.step";
|
|
14
|
+
const ATTR_TEST_STEP_NAME = "test.step.name";
|
|
15
|
+
const ATTR_TEST_STEP_TITLE = "test.step.title";
|
|
16
|
+
const ATTR_TEST_STEP_CATEGORY = "test.step.category";
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region package.json
|
|
20
|
+
var version = "0.0.6";
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/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 (typeof value === "number") return {
|
|
30
|
+
key,
|
|
31
|
+
value: Number.isInteger(value) ? { intValue: value } : { doubleValue: value }
|
|
32
|
+
};
|
|
33
|
+
else if (typeof value === "boolean") return {
|
|
34
|
+
key,
|
|
35
|
+
value: { boolValue: value }
|
|
36
|
+
};
|
|
37
|
+
else return {
|
|
38
|
+
key,
|
|
39
|
+
value: { stringValue: value }
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
const SPAN_KIND_INTERNAL = 1;
|
|
44
|
+
function buildOtlpRequest(spans, playwrightVersion) {
|
|
45
|
+
const otlpSpans = spans.map((span) => ({
|
|
46
|
+
traceId: span.traceId,
|
|
47
|
+
spanId: span.spanId,
|
|
48
|
+
parentSpanId: span.parentSpanId || void 0,
|
|
49
|
+
name: span.name,
|
|
50
|
+
kind: SPAN_KIND_INTERNAL,
|
|
51
|
+
startTimeUnixNano: dateToNanoseconds(span.startTime),
|
|
52
|
+
endTimeUnixNano: dateToNanoseconds(span.endTime),
|
|
53
|
+
attributes: toOtlpAttributes(span.attributes),
|
|
54
|
+
droppedAttributesCount: 0,
|
|
55
|
+
events: [],
|
|
56
|
+
droppedEventsCount: 0,
|
|
57
|
+
status: span.status,
|
|
58
|
+
links: [],
|
|
59
|
+
droppedLinksCount: 0
|
|
60
|
+
}));
|
|
61
|
+
return { resourceSpans: [{
|
|
62
|
+
resource: { attributes: [
|
|
63
|
+
{
|
|
64
|
+
key: "service.name",
|
|
65
|
+
value: { stringValue: "playwright-tests" }
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
key: "service.namespace",
|
|
69
|
+
value: { stringValue: "playwright" }
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
key: "service.version",
|
|
73
|
+
value: { stringValue: playwrightVersion }
|
|
74
|
+
}
|
|
75
|
+
] },
|
|
76
|
+
scopeSpans: [{
|
|
77
|
+
scope: {
|
|
78
|
+
name: "playwright-opentelemetry",
|
|
79
|
+
version
|
|
80
|
+
},
|
|
81
|
+
spans: otlpSpans
|
|
82
|
+
}]
|
|
83
|
+
}] };
|
|
84
|
+
}
|
|
85
|
+
async function sendSpans(spans, options) {
|
|
86
|
+
if (spans.length === 0) return;
|
|
87
|
+
const endpoint = options.tracesEndpoint;
|
|
88
|
+
const headers = options.headers || {};
|
|
89
|
+
const body = JSON.stringify(buildOtlpRequest(spans, options.playwrightVersion));
|
|
90
|
+
if (options.debug) {
|
|
91
|
+
console.log("Sending spans to", endpoint);
|
|
92
|
+
console.log("Body:", body);
|
|
93
|
+
}
|
|
94
|
+
const response = await fetch(endpoint, {
|
|
95
|
+
method: "POST",
|
|
96
|
+
body,
|
|
97
|
+
headers: {
|
|
98
|
+
"content-type": "application/json",
|
|
99
|
+
...headers
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
if (!response.ok) {
|
|
103
|
+
const error = await response.text();
|
|
104
|
+
throw new Error(`Failed to send spans: ${response.status} ${response.statusText}, ${error}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
//#endregion
|
|
109
|
+
//#region src/reporter.ts
|
|
2
110
|
var PlaywrightOpentelemetryReporter = class {
|
|
3
|
-
|
|
4
|
-
|
|
111
|
+
spans = [];
|
|
112
|
+
rootDir;
|
|
113
|
+
playwrightVersion;
|
|
114
|
+
constructor(options) {
|
|
115
|
+
this.options = options;
|
|
116
|
+
if (!options || !options.tracesEndpoint) throw new Error(getConfigurationErrorMessage());
|
|
5
117
|
}
|
|
6
|
-
|
|
7
|
-
|
|
118
|
+
onBegin(config, _suite) {
|
|
119
|
+
this.rootDir = config.rootDir;
|
|
120
|
+
this.playwrightVersion = config.version;
|
|
8
121
|
}
|
|
122
|
+
async onEnd(_result) {
|
|
123
|
+
await sendSpans(this.spans, {
|
|
124
|
+
tracesEndpoint: this.options.tracesEndpoint,
|
|
125
|
+
headers: this.options.headers,
|
|
126
|
+
playwrightVersion: this.playwrightVersion || "unknown",
|
|
127
|
+
debug: this.options.debug ?? false
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
onTestBegin(_test) {}
|
|
9
131
|
onTestEnd(test, result) {
|
|
10
|
-
|
|
132
|
+
const attributes = {};
|
|
133
|
+
const titlePath = test.titlePath();
|
|
134
|
+
if (titlePath.length >= 3) attributes[ATTR_TEST_CASE_NAME] = titlePath.slice(3).join(" > ");
|
|
135
|
+
attributes[ATTR_TEST_CASE_TITLE] = test.title;
|
|
136
|
+
attributes[ATTR_TEST_CASE_RESULT_STATUS] = result.status;
|
|
137
|
+
if (test.location) {
|
|
138
|
+
const { file, line } = test.location;
|
|
139
|
+
attributes[ATTR_CODE_FILE_PATH] = this.rootDir ? path.relative(this.rootDir, file) : file;
|
|
140
|
+
attributes[ATTR_CODE_LINE_NUMBER] = line;
|
|
141
|
+
}
|
|
142
|
+
const traceId = generateTraceId();
|
|
143
|
+
const span = {
|
|
144
|
+
traceId,
|
|
145
|
+
spanId: generateSpanId(),
|
|
146
|
+
name: TEST_SPAN_NAME,
|
|
147
|
+
startTime: result.startTime,
|
|
148
|
+
endTime: new Date(result.startTime.getTime() + result.duration),
|
|
149
|
+
attributes,
|
|
150
|
+
status: { code: result.status === test.expectedStatus ? 1 : 2 }
|
|
151
|
+
};
|
|
152
|
+
this.spans.push(span);
|
|
153
|
+
if (result.steps && result.steps.length > 0) for (const step of result.steps) this.processTestStep(step, span.spanId, traceId, []);
|
|
154
|
+
}
|
|
155
|
+
processTestStep(step, parentSpanId, traceId, parentTitlePath) {
|
|
156
|
+
const attributes = {};
|
|
157
|
+
const currentTitlePath = [...parentTitlePath, step.title];
|
|
158
|
+
attributes[ATTR_TEST_STEP_NAME] = currentTitlePath.join(" > ");
|
|
159
|
+
attributes[ATTR_TEST_STEP_TITLE] = step.title;
|
|
160
|
+
attributes[ATTR_TEST_STEP_CATEGORY] = step.category;
|
|
161
|
+
if (step.location) {
|
|
162
|
+
const { file, line } = step.location;
|
|
163
|
+
attributes[ATTR_CODE_FILE_PATH] = this.rootDir ? path.relative(this.rootDir, file) : file;
|
|
164
|
+
attributes[ATTR_CODE_LINE_NUMBER] = line;
|
|
165
|
+
}
|
|
166
|
+
const stepSpan = {
|
|
167
|
+
traceId,
|
|
168
|
+
spanId: generateSpanId(),
|
|
169
|
+
parentSpanId,
|
|
170
|
+
name: TEST_STEP_SPAN_NAME,
|
|
171
|
+
startTime: step.startTime,
|
|
172
|
+
endTime: new Date(step.startTime.getTime() + step.duration),
|
|
173
|
+
attributes,
|
|
174
|
+
status: { code: step.error ? 2 : 1 }
|
|
175
|
+
};
|
|
176
|
+
this.spans.push(stepSpan);
|
|
177
|
+
if (step.steps && step.steps.length > 0) for (const childStep of step.steps) this.processTestStep(childStep, stepSpan.spanId, traceId, currentTitlePath);
|
|
178
|
+
}
|
|
179
|
+
printsToStdio() {
|
|
180
|
+
return this.options.debug ?? false;
|
|
11
181
|
}
|
|
12
182
|
};
|
|
183
|
+
function generateTraceId() {
|
|
184
|
+
return Array.from({ length: 32 }, () => Math.floor(Math.random() * 16).toString(16)).join("");
|
|
185
|
+
}
|
|
186
|
+
function generateSpanId() {
|
|
187
|
+
return Array.from({ length: 16 }, () => Math.floor(Math.random() * 16).toString(16)).join("");
|
|
188
|
+
}
|
|
189
|
+
function getConfigurationErrorMessage() {
|
|
190
|
+
return "playwright-opentelemetry reporter requires options with 'tracesEndpoint' to be provided.\n\nExample configuration in 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',\n {\n tracesEndpoint: 'http://localhost:4317/v1/traces',\n headers: {\n Authorization: 'Bearer YOUR_TOKEN',\n },\n } satisfies PlaywrightOpentelemetryReporterOptions,\n ],\n ],\n});\n";
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
//#endregion
|
|
194
|
+
//#region src/index.ts
|
|
195
|
+
var src_default = PlaywrightOpentelemetryReporter;
|
|
13
196
|
|
|
14
197
|
//#endregion
|
|
15
|
-
export {
|
|
198
|
+
export { src_default as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "playwright-opentelemetry",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
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",
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"./package.json": "./package.json"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
+
"@biomejs/biome": "2.3.4",
|
|
34
35
|
"@playwright/test": "^1.56.1",
|
|
35
36
|
"@types/node": "^24.10.0",
|
|
36
37
|
"tsdown": "^0.16.0",
|
|
@@ -42,6 +43,7 @@
|
|
|
42
43
|
"dev": "tsdown --watch",
|
|
43
44
|
"test:unit": "vitest",
|
|
44
45
|
"test:e2e": "playwright test",
|
|
45
|
-
"typecheck": "tsc --noEmit"
|
|
46
|
+
"typecheck": "tsc --noEmit",
|
|
47
|
+
"format": "biome format --write"
|
|
46
48
|
}
|
|
47
49
|
}
|