playwright-opentelemetry 0.0.6 → 0.1.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/README.md +55 -2
- package/dist/fixture.d.mts +12 -0
- package/dist/fixture.mjs +136 -0
- package/dist/reporter.d.mts +43 -0
- package/dist/reporter.mjs +319 -0
- package/dist/trace-files-BPKxZrC_.mjs +151 -0
- package/package.json +17 -10
- package/dist/index.d.mts +0 -23
- package/dist/index.mjs +0 -198
package/README.md
CHANGED
|
@@ -4,11 +4,64 @@ We're building a reporter for Playwright that can export traces and spans in ope
|
|
|
4
4
|
|
|
5
5
|
We're just getting started, this project is in early development. Reach out if you're interested in learning more!
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
Two things need to be set up for complete opentelemetry tracing:
|
|
10
|
+
|
|
11
|
+
1. A reporter that sends traces to your provider of choice
|
|
12
|
+
2. A fixture that propogates opentelemetry trace headers to enable nested spans
|
|
13
|
+
|
|
14
|
+
### Configure the reporter
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { defineConfig, devices } from "@playwright/test";
|
|
18
|
+
import type { PlaywrightOpentelemetryReporterOptions } from "playwright-opentelemetry/reporter";
|
|
19
|
+
|
|
20
|
+
export default defineConfig({
|
|
21
|
+
// ... other Playwright config
|
|
22
|
+
reporter: [
|
|
23
|
+
[
|
|
24
|
+
"playwright-opentelemetry/reporter",
|
|
25
|
+
{
|
|
26
|
+
// Or OTEL_EXPORTER_OTLP_ENDPOINT
|
|
27
|
+
otlpEndpoint: "https://api.eu1.honeycomb.io/v1/traces",
|
|
28
|
+
// Or OTEL_EXPORTER_OTLP_HEADERS
|
|
29
|
+
otlpHeaders: {
|
|
30
|
+
"x-honeycomb-team": "xxxabc",
|
|
31
|
+
}
|
|
32
|
+
} satisfies PlaywrightOpentelemetryReporterOptions,
|
|
33
|
+
],
|
|
34
|
+
],
|
|
35
|
+
// ... rest of Playwright config
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Configure the fixture
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { expect } from "@playwright/test";
|
|
43
|
+
import { test } from "playwright-opentelemetry/fixture";
|
|
44
|
+
|
|
45
|
+
test("has title", async ({ page }) => {
|
|
46
|
+
await page.goto("https://playwright.dev/");
|
|
47
|
+
|
|
48
|
+
await expect(page).toHaveTitle(/Playwright/);
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Contributing
|
|
53
|
+
|
|
54
|
+
### Developing `playwright-opentelemetry`
|
|
8
55
|
|
|
9
56
|
- `pnpm dev` starts a dev server that outputs `dist/index.mjs`
|
|
10
57
|
- `pnpm build` otherwise creates a one-off compiled build
|
|
11
58
|
- `pnpm test:unit` to run the unit tests
|
|
12
59
|
- `pnpm test:e2e` uses the compiled reporter output
|
|
13
60
|
- `pnpm typecheck` for typescript
|
|
14
|
-
- `pnpm format` to format files
|
|
61
|
+
- `pnpm format` to format files
|
|
62
|
+
|
|
63
|
+
### Releasing
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
npx bumpp
|
|
67
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as _playwright_test0 from "@playwright/test";
|
|
2
|
+
|
|
3
|
+
//#region src/fixture/index.d.ts
|
|
4
|
+
type TestTraceInfo = {
|
|
5
|
+
testId: string;
|
|
6
|
+
outputDir: string;
|
|
7
|
+
};
|
|
8
|
+
declare const test: _playwright_test0.TestType<_playwright_test0.PlaywrightTestArgs & _playwright_test0.PlaywrightTestOptions & {
|
|
9
|
+
testTraceInfo: TestTraceInfo;
|
|
10
|
+
}, _playwright_test0.PlaywrightWorkerArgs & _playwright_test0.PlaywrightWorkerOptions>;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { test };
|
package/dist/fixture.mjs
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { a as generateSpanId, c as readNetworkSpanParent, d as writeNetworkSpanParent, o as getCurrentSpanId, s as getOrCreateTraceId, u as writeNetworkSpan } from "./trace-files-BPKxZrC_.mjs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { test as test$1 } from "@playwright/test";
|
|
4
|
+
|
|
5
|
+
//#region src/fixture/network-propagator.ts
|
|
6
|
+
/**
|
|
7
|
+
* Intercepts network requests to propagate trace context via traceparent header.
|
|
8
|
+
* Also records the parent span ID for later correlation by the response handler.
|
|
9
|
+
*
|
|
10
|
+
* This function only:
|
|
11
|
+
* 1. Creates a span ID for the request
|
|
12
|
+
* 2. Injects the traceparent header
|
|
13
|
+
* 3. Writes a parent file with the current parent span ID
|
|
14
|
+
*
|
|
15
|
+
* The actual span data (timing, status, attributes) is captured by
|
|
16
|
+
* fixtureCaptureRequestResponse when the response is received.
|
|
17
|
+
*
|
|
18
|
+
* @param options - The propagator options
|
|
19
|
+
*/
|
|
20
|
+
async function fixtureOtelHeaderPropagator({ route, request, testId, outputDir }) {
|
|
21
|
+
const traceId = getOrCreateTraceId(outputDir, testId);
|
|
22
|
+
const parentSpanId = getCurrentSpanId(outputDir, testId);
|
|
23
|
+
const traceHeader = `00-${traceId}-${generateSpanId()}-01`;
|
|
24
|
+
writeNetworkSpanParent({
|
|
25
|
+
outputDir,
|
|
26
|
+
testId,
|
|
27
|
+
parentSpanId,
|
|
28
|
+
traceHeader
|
|
29
|
+
});
|
|
30
|
+
await route.fallback({ headers: {
|
|
31
|
+
traceparent: traceHeader,
|
|
32
|
+
...request.headers()
|
|
33
|
+
} });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region src/fixture/request-response-capture.ts
|
|
38
|
+
/**
|
|
39
|
+
* OpenTelemetry SpanKind values
|
|
40
|
+
* @see https://opentelemetry.io/docs/specs/otel/trace/api/#spankind
|
|
41
|
+
*/
|
|
42
|
+
const SPAN_KIND_CLIENT = 3;
|
|
43
|
+
/**
|
|
44
|
+
* OpenTelemetry SpanStatusCode values
|
|
45
|
+
* @see https://opentelemetry.io/docs/specs/otel/trace/api/#set-status
|
|
46
|
+
*/
|
|
47
|
+
const SPAN_STATUS_CODE_UNSET = 0;
|
|
48
|
+
const SPAN_STATUS_CODE_ERROR = 2;
|
|
49
|
+
/**
|
|
50
|
+
* Captures request/response data and writes the complete network span.
|
|
51
|
+
* Called from page.on("response") after the request has completed.
|
|
52
|
+
*
|
|
53
|
+
* This function:
|
|
54
|
+
* 1. Extracts the traceparent header to find the parent file
|
|
55
|
+
* 2. Reads the parent span ID from the parent file
|
|
56
|
+
* 3. Builds the complete span with all HTTP attributes
|
|
57
|
+
* 4. Writes the network span for the reporter to collect
|
|
58
|
+
*
|
|
59
|
+
* @param options - The capture options
|
|
60
|
+
*/
|
|
61
|
+
async function fixtureCaptureRequestResponse({ request, response, testId, outputDir }) {
|
|
62
|
+
const url = request.url();
|
|
63
|
+
const method = request.method();
|
|
64
|
+
const statusCode = response.status();
|
|
65
|
+
const traceHeader = await request.headerValue("traceparent");
|
|
66
|
+
if (!traceHeader) throw new Error(`No traceparent header found for request ${request.url()}`);
|
|
67
|
+
const parentSpanId = readNetworkSpanParent(outputDir, testId, traceHeader);
|
|
68
|
+
if (!parentSpanId) throw new Error(`No parent span ID found for trace header ${traceHeader}`);
|
|
69
|
+
const parts = traceHeader.split("-");
|
|
70
|
+
if (parts.length !== 4) return;
|
|
71
|
+
const [, traceId, spanId] = parts;
|
|
72
|
+
const timing = request.timing();
|
|
73
|
+
const startTime = new Date(timing.startTime);
|
|
74
|
+
const endTime = timing.responseEnd >= 0 ? new Date(timing.startTime + timing.responseEnd) : new Date(timing.startTime);
|
|
75
|
+
const parsedUrl = new URL(url);
|
|
76
|
+
const serverAddress = parsedUrl.hostname;
|
|
77
|
+
const serverPort = parsedUrl.port !== "" ? Number.parseInt(parsedUrl.port, 10) : parsedUrl.protocol === "https:" ? 443 : 80;
|
|
78
|
+
let statusCodeValue = SPAN_STATUS_CODE_UNSET;
|
|
79
|
+
if (statusCode >= 400) statusCodeValue = SPAN_STATUS_CODE_ERROR;
|
|
80
|
+
const attributes = {
|
|
81
|
+
"http.request.method": method,
|
|
82
|
+
"url.full": url,
|
|
83
|
+
"server.address": serverAddress,
|
|
84
|
+
"server.port": serverPort,
|
|
85
|
+
"http.response.status_code": statusCode
|
|
86
|
+
};
|
|
87
|
+
if (statusCodeValue === SPAN_STATUS_CODE_ERROR) attributes["error.type"] = statusCode.toString();
|
|
88
|
+
writeNetworkSpan(outputDir, testId, traceHeader, {
|
|
89
|
+
traceId,
|
|
90
|
+
spanId,
|
|
91
|
+
parentSpanId,
|
|
92
|
+
name: `HTTP ${method}`,
|
|
93
|
+
kind: SPAN_KIND_CLIENT,
|
|
94
|
+
startTime,
|
|
95
|
+
endTime,
|
|
96
|
+
status: { code: statusCodeValue },
|
|
97
|
+
attributes
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/fixture/index.ts
|
|
103
|
+
const test = test$1.extend({
|
|
104
|
+
testTraceInfo: [async ({ playwright }, use, testInfo) => {
|
|
105
|
+
const outputDir = path.dirname(testInfo.outputDir);
|
|
106
|
+
await use({
|
|
107
|
+
testId: testInfo.testId,
|
|
108
|
+
outputDir
|
|
109
|
+
});
|
|
110
|
+
}, { auto: true }],
|
|
111
|
+
context: async ({ context, testTraceInfo: { testId, outputDir } }, use) => {
|
|
112
|
+
context.route("**", async (route, request) => await fixtureOtelHeaderPropagator({
|
|
113
|
+
route,
|
|
114
|
+
request,
|
|
115
|
+
testId,
|
|
116
|
+
outputDir
|
|
117
|
+
}));
|
|
118
|
+
await use(context);
|
|
119
|
+
},
|
|
120
|
+
page: async ({ page, testTraceInfo: { testId, outputDir } }, use) => {
|
|
121
|
+
page.on("requestfinished", async (request) => {
|
|
122
|
+
const response = await request.response();
|
|
123
|
+
if (!response) return;
|
|
124
|
+
await fixtureCaptureRequestResponse({
|
|
125
|
+
request,
|
|
126
|
+
response,
|
|
127
|
+
testId,
|
|
128
|
+
outputDir
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
await use(page);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
//#endregion
|
|
136
|
+
export { test };
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
serviceName?: string;
|
|
8
|
+
debug?: boolean;
|
|
9
|
+
}
|
|
10
|
+
declare class PlaywrightOpentelemetryReporter implements Reporter {
|
|
11
|
+
private options;
|
|
12
|
+
private spans;
|
|
13
|
+
private rootDir?;
|
|
14
|
+
private playwrightVersion?;
|
|
15
|
+
private resolvedEndpoint;
|
|
16
|
+
private resolvedHeaders;
|
|
17
|
+
private resolvedServiceName;
|
|
18
|
+
/** Maps test.id to its project's outputDir */
|
|
19
|
+
private testOutputDirs;
|
|
20
|
+
private testSpans;
|
|
21
|
+
private testTraceIds;
|
|
22
|
+
private stepSpanIds;
|
|
23
|
+
/** Tracks the span context stack per test for append-only file writes */
|
|
24
|
+
private spanContextStacks;
|
|
25
|
+
constructor(options?: PlaywrightOpentelemetryReporterOptions);
|
|
26
|
+
onBegin(config: FullConfig, suite: Suite): void;
|
|
27
|
+
onTestBegin(test: TestCase): void;
|
|
28
|
+
onStepBegin(test: TestCase, _result: TestResult, step: TestStep): Promise<void>;
|
|
29
|
+
onStepEnd(test: TestCase, _result: TestResult, _step: TestStep): void;
|
|
30
|
+
onTestEnd(test: TestCase, result: TestResult): Promise<void>;
|
|
31
|
+
onEnd(_result: FullResult): Promise<void>;
|
|
32
|
+
private processTestStep;
|
|
33
|
+
onStdOut(chunk: string | Buffer, _test: TestCase, _result: TestResult): void;
|
|
34
|
+
onStdErr(chunk: string | Buffer, _test: TestCase, _result: TestResult): void;
|
|
35
|
+
printsToStdio(): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Get the output directory for a test ID.
|
|
38
|
+
* Throws if the test ID was not registered in onBegin.
|
|
39
|
+
*/
|
|
40
|
+
private getOutputDir;
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
export { type PlaywrightOpentelemetryReporterOptions, PlaywrightOpentelemetryReporter as default };
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import { a as generateSpanId, i as createNetworkDirs, l as writeCurrentSpanId, n as cleanupTestFiles, r as collectNetworkSpans, s as getOrCreateTraceId, t as OTEL_DIR } from "./trace-files-BPKxZrC_.mjs";
|
|
2
|
+
import { mkdirSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
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
|
+
|
|
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
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region package.json
|
|
22
|
+
var version = "0.1.0";
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/reporter/sender.ts
|
|
26
|
+
function dateToNanoseconds(date) {
|
|
27
|
+
return (BigInt(date.getTime()) * BigInt(1e6)).toString();
|
|
28
|
+
}
|
|
29
|
+
function toOtlpAttributes(attributes) {
|
|
30
|
+
return Object.entries(attributes).map(([key, value]) => {
|
|
31
|
+
if (typeof value === "number") return {
|
|
32
|
+
key,
|
|
33
|
+
value: Number.isInteger(value) ? { intValue: value } : { doubleValue: value }
|
|
34
|
+
};
|
|
35
|
+
else if (typeof value === "boolean") return {
|
|
36
|
+
key,
|
|
37
|
+
value: { boolValue: value }
|
|
38
|
+
};
|
|
39
|
+
else return {
|
|
40
|
+
key,
|
|
41
|
+
value: { stringValue: value }
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const SPAN_KIND_INTERNAL = 1;
|
|
46
|
+
function buildOtlpRequest(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_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 { resourceSpans: [{
|
|
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
|
+
async function sendSpans(spans, options) {
|
|
88
|
+
if (spans.length === 0) return;
|
|
89
|
+
const endpoint = options.tracesEndpoint;
|
|
90
|
+
const headers = options.headers || {};
|
|
91
|
+
const body = JSON.stringify(buildOtlpRequest(spans, options.serviceName, options.playwrightVersion));
|
|
92
|
+
if (options.debug) console.log("Sending spans to", endpoint);
|
|
93
|
+
const response = await fetch(endpoint, {
|
|
94
|
+
method: "POST",
|
|
95
|
+
body,
|
|
96
|
+
headers: {
|
|
97
|
+
"content-type": "application/json",
|
|
98
|
+
...headers
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
if (!response.ok) {
|
|
102
|
+
const error = await response.text();
|
|
103
|
+
throw new Error(`Failed to send spans: ${response.status} ${response.statusText}, ${error}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/reporter/reporter.ts
|
|
109
|
+
var PlaywrightOpentelemetryReporter = class {
|
|
110
|
+
spans = [];
|
|
111
|
+
rootDir;
|
|
112
|
+
playwrightVersion;
|
|
113
|
+
resolvedEndpoint;
|
|
114
|
+
resolvedHeaders;
|
|
115
|
+
resolvedServiceName;
|
|
116
|
+
/** Maps test.id to its project's outputDir */
|
|
117
|
+
testOutputDirs = /* @__PURE__ */ new Map();
|
|
118
|
+
testSpans = /* @__PURE__ */ new Map();
|
|
119
|
+
testTraceIds = /* @__PURE__ */ new Map();
|
|
120
|
+
stepSpanIds = /* @__PURE__ */ new Map();
|
|
121
|
+
/** Tracks the span context stack per test for append-only file writes */
|
|
122
|
+
spanContextStacks = /* @__PURE__ */ new Map();
|
|
123
|
+
constructor(options = {}) {
|
|
124
|
+
this.options = options;
|
|
125
|
+
this.resolvedEndpoint = process.env.OTEL_EXPORTER_OTLP_ENDPOINT || options.otlpEndpoint || "";
|
|
126
|
+
const envHeaders = process.env.OTEL_EXPORTER_OTLP_HEADERS ? parseOtlpHeaders(process.env.OTEL_EXPORTER_OTLP_HEADERS) : {};
|
|
127
|
+
this.resolvedHeaders = {
|
|
128
|
+
...options.otlpHeaders,
|
|
129
|
+
...envHeaders
|
|
130
|
+
};
|
|
131
|
+
this.resolvedServiceName = process.env.OTEL_SERVICE_NAME || options.serviceName || "playwright-tests";
|
|
132
|
+
if (!this.resolvedEndpoint) throw new Error(getConfigurationErrorMessage());
|
|
133
|
+
}
|
|
134
|
+
onBegin(config, suite) {
|
|
135
|
+
this.rootDir = config.rootDir;
|
|
136
|
+
this.playwrightVersion = config.version;
|
|
137
|
+
for (const test of suite.allTests()) {
|
|
138
|
+
const project = test.parent.project();
|
|
139
|
+
if (project?.outputDir) {
|
|
140
|
+
mkdirSync(path.join(project.outputDir, OTEL_DIR), { recursive: true });
|
|
141
|
+
this.testOutputDirs.set(test.id, project.outputDir);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
onTestBegin(test) {
|
|
146
|
+
const testId = test.id;
|
|
147
|
+
const outputDir = this.getOutputDir(testId);
|
|
148
|
+
const traceId = getOrCreateTraceId(outputDir, testId);
|
|
149
|
+
this.testTraceIds.set(testId, traceId);
|
|
150
|
+
const testSpanId = generateSpanId();
|
|
151
|
+
this.testSpans.set(testId, testSpanId);
|
|
152
|
+
this.spanContextStacks.set(testId, [testSpanId]);
|
|
153
|
+
writeCurrentSpanId(outputDir, testId, testSpanId);
|
|
154
|
+
createNetworkDirs(outputDir, testId);
|
|
155
|
+
}
|
|
156
|
+
async onStepBegin(test, _result, step) {
|
|
157
|
+
const testId = test.id;
|
|
158
|
+
const outputDir = this.getOutputDir(testId);
|
|
159
|
+
const stepId = getStepId(test, step);
|
|
160
|
+
const stepSpanId = generateSpanId();
|
|
161
|
+
this.stepSpanIds.set(stepId, stepSpanId);
|
|
162
|
+
const stack = this.spanContextStacks.get(testId);
|
|
163
|
+
if (stack) {
|
|
164
|
+
stack.push(stepSpanId);
|
|
165
|
+
writeCurrentSpanId(outputDir, testId, stepSpanId);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
onStepEnd(test, _result, _step) {
|
|
169
|
+
const testId = test.id;
|
|
170
|
+
const outputDir = this.getOutputDir(testId);
|
|
171
|
+
const stack = this.spanContextStacks.get(testId);
|
|
172
|
+
if (stack && stack.length > 1) {
|
|
173
|
+
stack.pop();
|
|
174
|
+
const currentParent = stack[stack.length - 1];
|
|
175
|
+
writeCurrentSpanId(outputDir, testId, currentParent);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
async onTestEnd(test, result) {
|
|
179
|
+
const testId = test.id;
|
|
180
|
+
const outputDir = this.getOutputDir(testId);
|
|
181
|
+
const traceId = this.testTraceIds.get(testId);
|
|
182
|
+
const testSpanId = this.testSpans.get(testId);
|
|
183
|
+
if (!traceId || !testSpanId) throw new Error(`Test ${testId} not found`);
|
|
184
|
+
const attributes = {};
|
|
185
|
+
const titlePath = test.titlePath();
|
|
186
|
+
if (titlePath.length >= 3) attributes[ATTR_TEST_CASE_NAME] = titlePath.slice(3).join(" > ");
|
|
187
|
+
attributes[ATTR_TEST_CASE_TITLE] = test.title;
|
|
188
|
+
attributes[ATTR_TEST_CASE_RESULT_STATUS] = result.status;
|
|
189
|
+
if (test.location) {
|
|
190
|
+
const { file, line } = test.location;
|
|
191
|
+
attributes[ATTR_CODE_FILE_PATH] = this.rootDir ? path.relative(this.rootDir, file) : file;
|
|
192
|
+
attributes[ATTR_CODE_LINE_NUMBER] = line;
|
|
193
|
+
}
|
|
194
|
+
const span = {
|
|
195
|
+
traceId,
|
|
196
|
+
spanId: testSpanId,
|
|
197
|
+
name: TEST_SPAN_NAME,
|
|
198
|
+
startTime: result.startTime,
|
|
199
|
+
endTime: new Date(result.startTime.getTime() + result.duration),
|
|
200
|
+
attributes,
|
|
201
|
+
status: { code: result.status === test.expectedStatus ? 1 : 2 }
|
|
202
|
+
};
|
|
203
|
+
this.spans.push(span);
|
|
204
|
+
const processedSteps = /* @__PURE__ */ new Map();
|
|
205
|
+
if (result.steps && result.steps.length > 0) for (const step of result.steps) this.processTestStep(test, step, testSpanId, traceId, [], processedSteps);
|
|
206
|
+
const networkSpans = await collectNetworkSpans(outputDir, testId);
|
|
207
|
+
for (const networkSpan of networkSpans) this.spans.push(networkSpan);
|
|
208
|
+
}
|
|
209
|
+
async onEnd(_result) {
|
|
210
|
+
await sendSpans(this.spans, {
|
|
211
|
+
tracesEndpoint: this.resolvedEndpoint,
|
|
212
|
+
headers: this.resolvedHeaders,
|
|
213
|
+
serviceName: this.resolvedServiceName,
|
|
214
|
+
playwrightVersion: this.playwrightVersion || "unknown",
|
|
215
|
+
debug: this.options.debug ?? false
|
|
216
|
+
});
|
|
217
|
+
for (const [testId, outputDir] of this.testOutputDirs.entries()) await cleanupTestFiles(outputDir, testId);
|
|
218
|
+
}
|
|
219
|
+
processTestStep(test, step, parentSpanId, traceId, parentTitlePath, processedSteps) {
|
|
220
|
+
const isInternalFixture = step.category === "fixture" && step.location?.file.includes("playwright-opentelemetry") && (step.location?.file.endsWith("fixture.mjs") || step.location?.file.endsWith("fixture/index.ts"));
|
|
221
|
+
const stepId = getStepId(test, step);
|
|
222
|
+
if (isInternalFixture) {
|
|
223
|
+
processedSteps.set(`__skip__${stepId}`, {});
|
|
224
|
+
const existingSpan$1 = processedSteps.get(stepId);
|
|
225
|
+
if (existingSpan$1) {
|
|
226
|
+
const spanIndex = this.spans.indexOf(existingSpan$1);
|
|
227
|
+
if (spanIndex !== -1) this.spans.splice(spanIndex, 1);
|
|
228
|
+
processedSteps.delete(stepId);
|
|
229
|
+
}
|
|
230
|
+
if (step.steps && step.steps.length > 0) for (const childStep of step.steps) this.processTestStep(test, childStep, parentSpanId, traceId, parentTitlePath, processedSteps);
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (processedSteps.has(`__skip__${stepId}`)) {
|
|
234
|
+
if (step.steps && step.steps.length > 0) for (const childStep of step.steps) this.processTestStep(test, childStep, parentSpanId, traceId, parentTitlePath, processedSteps);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
const currentTitlePath = [...parentTitlePath, step.title];
|
|
238
|
+
const stepSpanId = this.stepSpanIds.get(stepId) ?? generateSpanId();
|
|
239
|
+
const existingSpan = processedSteps.get(stepId);
|
|
240
|
+
if (existingSpan) {
|
|
241
|
+
if (step.location && !existingSpan.attributes[ATTR_CODE_FILE_PATH]) {
|
|
242
|
+
const { file, line } = step.location;
|
|
243
|
+
const relativePath = this.rootDir ? path.relative(this.rootDir, file) : file;
|
|
244
|
+
existingSpan.attributes[ATTR_CODE_FILE_PATH] = relativePath;
|
|
245
|
+
existingSpan.attributes[ATTR_CODE_LINE_NUMBER] = line;
|
|
246
|
+
}
|
|
247
|
+
if (step.steps && step.steps.length > 0) for (const childStep of step.steps) this.processTestStep(test, childStep, existingSpan.spanId, traceId, currentTitlePath, processedSteps);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const attributes = {};
|
|
251
|
+
attributes[ATTR_TEST_STEP_NAME] = currentTitlePath.join(" > ");
|
|
252
|
+
attributes[ATTR_TEST_STEP_TITLE] = step.title;
|
|
253
|
+
attributes[ATTR_TEST_STEP_CATEGORY] = step.category;
|
|
254
|
+
if (step.location) {
|
|
255
|
+
const { file, line } = step.location;
|
|
256
|
+
attributes[ATTR_CODE_FILE_PATH] = this.rootDir ? path.relative(this.rootDir, file) : file;
|
|
257
|
+
attributes[ATTR_CODE_LINE_NUMBER] = line;
|
|
258
|
+
}
|
|
259
|
+
const stepSpan = {
|
|
260
|
+
traceId,
|
|
261
|
+
spanId: stepSpanId,
|
|
262
|
+
parentSpanId,
|
|
263
|
+
name: TEST_STEP_SPAN_NAME,
|
|
264
|
+
startTime: step.startTime,
|
|
265
|
+
endTime: new Date(step.startTime.getTime() + step.duration),
|
|
266
|
+
attributes,
|
|
267
|
+
status: { code: step.error ? 2 : 1 }
|
|
268
|
+
};
|
|
269
|
+
this.spans.push(stepSpan);
|
|
270
|
+
processedSteps.set(stepId, stepSpan);
|
|
271
|
+
if (step.steps && step.steps.length > 0) for (const childStep of step.steps) this.processTestStep(test, childStep, stepSpan.spanId, traceId, currentTitlePath, processedSteps);
|
|
272
|
+
}
|
|
273
|
+
onStdOut(chunk, _test, _result) {
|
|
274
|
+
if (this.options.debug) console.log(chunk.toString().slice(0, -1));
|
|
275
|
+
}
|
|
276
|
+
onStdErr(chunk, _test, _result) {
|
|
277
|
+
if (this.options.debug) console.log(chunk.toString().slice(0, -1));
|
|
278
|
+
}
|
|
279
|
+
printsToStdio() {
|
|
280
|
+
return this.options.debug ?? false;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Get the output directory for a test ID.
|
|
284
|
+
* Throws if the test ID was not registered in onBegin.
|
|
285
|
+
*/
|
|
286
|
+
getOutputDir(testId) {
|
|
287
|
+
const outputDir = this.testOutputDirs.get(testId);
|
|
288
|
+
if (!outputDir) throw new Error(`No outputDir found for test "${testId}" - onBegin must be called first and test must exist in suite`);
|
|
289
|
+
return outputDir;
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
function parseOtlpHeaders(headersString) {
|
|
293
|
+
const headers = {};
|
|
294
|
+
const pairs = headersString.split(",");
|
|
295
|
+
for (const pair of pairs) {
|
|
296
|
+
const [key, ...valueParts] = pair.split("=");
|
|
297
|
+
if (key && valueParts.length > 0) headers[key.trim()] = valueParts.join("=").trim();
|
|
298
|
+
}
|
|
299
|
+
return headers;
|
|
300
|
+
}
|
|
301
|
+
function getConfigurationErrorMessage() {
|
|
302
|
+
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";
|
|
303
|
+
}
|
|
304
|
+
function getStepId(test, step) {
|
|
305
|
+
const startTimeMs = step.startTime.getTime();
|
|
306
|
+
return [
|
|
307
|
+
test.id,
|
|
308
|
+
step.category,
|
|
309
|
+
startTimeMs,
|
|
310
|
+
...step.titlePath()
|
|
311
|
+
].join(" > ");
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
//#endregion
|
|
315
|
+
//#region src/reporter/index.ts
|
|
316
|
+
var reporter_default = PlaywrightOpentelemetryReporter;
|
|
317
|
+
|
|
318
|
+
//#endregion
|
|
319
|
+
export { reporter_default as default };
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import fs from "node:fs/promises";
|
|
4
|
+
|
|
5
|
+
//#region src/shared/trace-files.ts
|
|
6
|
+
const OTEL_DIR = "otel";
|
|
7
|
+
function getOrCreateTraceId(outputDir, testId) {
|
|
8
|
+
const tracePath = getTracePath(outputDir, testId);
|
|
9
|
+
if (existsSync(tracePath)) return readFileSync(tracePath, "utf-8");
|
|
10
|
+
const traceId = generateTraceId();
|
|
11
|
+
writeFileSync(tracePath, traceId);
|
|
12
|
+
return traceId;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Write the current span ID to the span context file.
|
|
16
|
+
* This is an append-only operation - the file maintains a log of all span context changes.
|
|
17
|
+
* The fixture reads the last line to get the current parent span ID.
|
|
18
|
+
*/
|
|
19
|
+
function writeCurrentSpanId(outputDir, testId, spanId) {
|
|
20
|
+
writeFileSync(getSpanPath(outputDir, testId), `${spanId}\n`, { flag: "a" });
|
|
21
|
+
}
|
|
22
|
+
function getCurrentSpanId(outputDir, testId) {
|
|
23
|
+
const lines = readFileSync(getSpanPath(outputDir, testId), "utf-8").trim().split("\n").filter((line) => line.length > 0);
|
|
24
|
+
if (lines.length === 0) throw new Error(`No span context found for test ${testId}`);
|
|
25
|
+
const lastLine = lines[lines.length - 1];
|
|
26
|
+
if (!lastLine) throw new Error(`No span context found for test ${testId}`);
|
|
27
|
+
return lastLine;
|
|
28
|
+
}
|
|
29
|
+
function createNetworkDirs(outputDir, testId) {
|
|
30
|
+
mkdirSync(getNetworkParentDir(outputDir, testId), { recursive: true });
|
|
31
|
+
mkdirSync(getNetworkSpanDir(outputDir, testId), { recursive: true });
|
|
32
|
+
}
|
|
33
|
+
function writeNetworkSpanParent({ outputDir, testId, parentSpanId, traceHeader }) {
|
|
34
|
+
writeFileSync(getNetworkParentPath(outputDir, testId, traceHeader), parentSpanId);
|
|
35
|
+
}
|
|
36
|
+
function readNetworkSpanParent(outputDir, testId, traceHeader) {
|
|
37
|
+
const parentPath = getNetworkParentPath(outputDir, testId, traceHeader);
|
|
38
|
+
try {
|
|
39
|
+
return readFileSync(parentPath, "utf-8");
|
|
40
|
+
} catch (err) {
|
|
41
|
+
if (err.code === "ENOENT") return;
|
|
42
|
+
throw err;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function writeNetworkSpan(outputDir, testId, traceHeader, span) {
|
|
46
|
+
const networkSpanPath = getNetworkSpanPath(outputDir, testId, traceHeader);
|
|
47
|
+
const serialized = {
|
|
48
|
+
...span,
|
|
49
|
+
startTime: span.startTime.toISOString(),
|
|
50
|
+
endTime: span.endTime.toISOString()
|
|
51
|
+
};
|
|
52
|
+
writeFileSync(networkSpanPath, JSON.stringify(serialized));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Collect all network spans for a test.
|
|
56
|
+
* Called by the reporter at onTestEnd to gather HTTP client spans.
|
|
57
|
+
* Reads all files from the network spans directory.
|
|
58
|
+
*
|
|
59
|
+
* @param outputDir - The Playwright output directory
|
|
60
|
+
* @param testId - The unique test identifier
|
|
61
|
+
* @returns Array of network spans
|
|
62
|
+
*/
|
|
63
|
+
async function collectNetworkSpans(outputDir, testId) {
|
|
64
|
+
const networkSpanDir = getNetworkSpanDir(outputDir, testId);
|
|
65
|
+
try {
|
|
66
|
+
const files = await fs.readdir(networkSpanDir);
|
|
67
|
+
const spans = [];
|
|
68
|
+
for (const file of files) {
|
|
69
|
+
const content = await fs.readFile(path.join(networkSpanDir, file), "utf-8");
|
|
70
|
+
const parsed = JSON.parse(content);
|
|
71
|
+
spans.push({
|
|
72
|
+
...parsed,
|
|
73
|
+
startTime: new Date(parsed.startTime),
|
|
74
|
+
endTime: new Date(parsed.endTime)
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
return spans;
|
|
78
|
+
} catch (err) {
|
|
79
|
+
if (err.code === "ENOENT") return [];
|
|
80
|
+
throw err;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Cleanup all trace files for a test.
|
|
85
|
+
* Called by the reporter after onTestEnd has processed the spans.
|
|
86
|
+
*
|
|
87
|
+
* @param outputDir - The Playwright output directory
|
|
88
|
+
* @param testId - The unique test identifier
|
|
89
|
+
*/
|
|
90
|
+
async function cleanupTestFiles(outputDir, testId) {
|
|
91
|
+
try {
|
|
92
|
+
await fs.unlink(getTracePath(outputDir, testId));
|
|
93
|
+
} catch (err) {
|
|
94
|
+
if (err.code !== "ENOENT") throw err;
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
await fs.unlink(getSpanPath(outputDir, testId));
|
|
98
|
+
} catch (err) {
|
|
99
|
+
if (err.code !== "ENOENT") throw err;
|
|
100
|
+
}
|
|
101
|
+
try {
|
|
102
|
+
await fs.rm(getNetworkParentDir(outputDir, testId), { recursive: true });
|
|
103
|
+
} catch (err) {
|
|
104
|
+
if (err.code !== "ENOENT") throw err;
|
|
105
|
+
}
|
|
106
|
+
try {
|
|
107
|
+
await fs.rm(getNetworkSpanDir(outputDir, testId), { recursive: true });
|
|
108
|
+
} catch (err) {
|
|
109
|
+
if (err.code !== "ENOENT") throw err;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function getTracePath(outputDir, testId) {
|
|
113
|
+
return path.join(outputDir, OTEL_DIR, `${sanitizeTestId(testId)}.trace`);
|
|
114
|
+
}
|
|
115
|
+
function getSpanPath(outputDir, testId) {
|
|
116
|
+
return path.join(outputDir, OTEL_DIR, `${sanitizeTestId(testId)}.span`);
|
|
117
|
+
}
|
|
118
|
+
function getNetworkParentDir(outputDir, testId) {
|
|
119
|
+
return path.join(outputDir, OTEL_DIR, `${sanitizeTestId(testId)}-network-parents`);
|
|
120
|
+
}
|
|
121
|
+
function getNetworkSpanDir(outputDir, testId) {
|
|
122
|
+
return path.join(outputDir, OTEL_DIR, `${sanitizeTestId(testId)}-network-spans`);
|
|
123
|
+
}
|
|
124
|
+
function getNetworkParentPath(outputDir, testId, traceHeader) {
|
|
125
|
+
return path.join(getNetworkParentDir(outputDir, testId), traceHeader);
|
|
126
|
+
}
|
|
127
|
+
function getNetworkSpanPath(outputDir, testId, traceHeader) {
|
|
128
|
+
return path.join(getNetworkSpanDir(outputDir, testId), traceHeader);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Sanitize test ID for use as filename.
|
|
132
|
+
* Replaces characters that are invalid in filenames.
|
|
133
|
+
*/
|
|
134
|
+
function sanitizeTestId(testId) {
|
|
135
|
+
return testId.replace(/[<>:"/\\|?*]/g, "_");
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Generate a random 32-character hex trace ID.
|
|
139
|
+
*/
|
|
140
|
+
function generateTraceId() {
|
|
141
|
+
return Array.from({ length: 32 }, () => Math.floor(Math.random() * 16).toString(16)).join("");
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Generate a random 16-character hex span ID.
|
|
145
|
+
*/
|
|
146
|
+
function generateSpanId() {
|
|
147
|
+
return Array.from({ length: 16 }, () => Math.floor(Math.random() * 16).toString(16)).join("");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
//#endregion
|
|
151
|
+
export { generateSpanId as a, readNetworkSpanParent as c, writeNetworkSpanParent as d, createNetworkDirs as i, writeCurrentSpanId as l, cleanupTestFiles as n, getCurrentSpanId as o, collectNetworkSpans as r, getOrCreateTraceId as s, OTEL_DIR as t, writeNetworkSpan as u };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "playwright-opentelemetry",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.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",
|
|
@@ -23,20 +23,27 @@
|
|
|
23
23
|
"files": [
|
|
24
24
|
"dist"
|
|
25
25
|
],
|
|
26
|
-
"main": "./dist/index.mjs",
|
|
27
|
-
"module": "./dist/index.mjs",
|
|
28
|
-
"types": "./dist/index.d.mts",
|
|
29
26
|
"exports": {
|
|
30
|
-
"
|
|
27
|
+
"./fixture": {
|
|
28
|
+
"types": "./dist/fixture.d.mts",
|
|
29
|
+
"default": "./dist/fixture.mjs"
|
|
30
|
+
},
|
|
31
|
+
"./reporter": {
|
|
32
|
+
"types": "./dist/reporter.d.mts",
|
|
33
|
+
"default": "./dist/reporter.mjs"
|
|
34
|
+
},
|
|
31
35
|
"./package.json": "./package.json"
|
|
32
36
|
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@playwright/test": "*"
|
|
39
|
+
},
|
|
33
40
|
"devDependencies": {
|
|
34
|
-
"@biomejs/biome": "2.3.
|
|
35
|
-
"@playwright/test": "^1.
|
|
36
|
-
"@types/node": "^
|
|
37
|
-
"tsdown": "^0.
|
|
41
|
+
"@biomejs/biome": "2.3.10",
|
|
42
|
+
"@playwright/test": "^1.57.0",
|
|
43
|
+
"@types/node": "^25.0.3",
|
|
44
|
+
"tsdown": "^0.18.1",
|
|
38
45
|
"typescript": "^5.9.3",
|
|
39
|
-
"vitest": "^4.0.
|
|
46
|
+
"vitest": "^4.0.16"
|
|
40
47
|
},
|
|
41
48
|
"scripts": {
|
|
42
49
|
"build": "tsdown",
|
package/dist/index.d.mts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { FullConfig, FullResult, Reporter, Suite, TestCase, TestResult } from "@playwright/test/reporter";
|
|
2
|
-
|
|
3
|
-
//#region src/reporter.d.ts
|
|
4
|
-
interface PlaywrightOpentelemetryReporterOptions {
|
|
5
|
-
tracesEndpoint: string;
|
|
6
|
-
headers?: Record<string, string>;
|
|
7
|
-
debug?: boolean;
|
|
8
|
-
}
|
|
9
|
-
declare class PlaywrightOpentelemetryReporter implements Reporter {
|
|
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;
|
|
18
|
-
onTestEnd(test: TestCase, result: TestResult): void;
|
|
19
|
-
private processTestStep;
|
|
20
|
-
printsToStdio(): boolean;
|
|
21
|
-
}
|
|
22
|
-
//#endregion
|
|
23
|
-
export { type PlaywrightOpentelemetryReporterOptions, PlaywrightOpentelemetryReporter as default };
|
package/dist/index.mjs
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
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
|
|
110
|
-
var PlaywrightOpentelemetryReporter = class {
|
|
111
|
-
spans = [];
|
|
112
|
-
rootDir;
|
|
113
|
-
playwrightVersion;
|
|
114
|
-
constructor(options) {
|
|
115
|
-
this.options = options;
|
|
116
|
-
if (!options || !options.tracesEndpoint) throw new Error(getConfigurationErrorMessage());
|
|
117
|
-
}
|
|
118
|
-
onBegin(config, _suite) {
|
|
119
|
-
this.rootDir = config.rootDir;
|
|
120
|
-
this.playwrightVersion = config.version;
|
|
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) {}
|
|
131
|
-
onTestEnd(test, result) {
|
|
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;
|
|
181
|
-
}
|
|
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;
|
|
196
|
-
|
|
197
|
-
//#endregion
|
|
198
|
-
export { src_default as default };
|