@kopai/otel-testing-harness 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.
Files changed (2) hide show
  1. package/README.md +74 -0
  2. package/package.json +73 -0
package/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # @kopai/otel-testing-harness
2
+
3
+ [![npm](https://img.shields.io/npm/v/@kopai/otel-testing-harness?label=latest)](https://www.npmjs.com/package/@kopai/otel-testing-harness)
4
+
5
+ In-process OTLP collector for testing OpenTelemetry instrumentation. Spins up a real HTTP server backed by in-memory SQLite so you can send telemetry via the OTel SDK and assert on it directly in your tests.
6
+
7
+ ## Usage
8
+
9
+ ```typescript
10
+ import { createOtelTestingHarness } from "@kopai/otel-testing-harness";
11
+ import { trace } from "@opentelemetry/api";
12
+ import {
13
+ BasicTracerProvider,
14
+ SimpleSpanProcessor,
15
+ } from "@opentelemetry/sdk-trace-base";
16
+ import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
17
+ import { resourceFromAttributes } from "@opentelemetry/resources";
18
+
19
+ const harness = await createOtelTestingHarness({ port: 0 });
20
+
21
+ const tracerProvider = new BasicTracerProvider({
22
+ resource: resourceFromAttributes({ "service.name": "my-service" }),
23
+ spanProcessors: [
24
+ new SimpleSpanProcessor(
25
+ new OTLPTraceExporter({
26
+ url: `http://127.0.0.1:${harness.port}/v1/traces`,
27
+ })
28
+ ),
29
+ ],
30
+ });
31
+ trace.setGlobalTracerProvider(tracerProvider);
32
+
33
+ const span = trace.getTracer("test").startSpan("my-span");
34
+ span.end();
35
+ await tracerProvider.forceFlush();
36
+
37
+ const { data } = await harness.getTraces({});
38
+ expect(data[0].SpanName).toBe("my-span");
39
+
40
+ await tracerProvider.shutdown();
41
+ trace.disable();
42
+ await harness.stop();
43
+ ```
44
+
45
+ ## API
46
+
47
+ `createOtelTestingHarness(opts?)` returns a `Promise<OtelTestingHarness>` with:
48
+
49
+ | Member | Description |
50
+ | -------------------- | ----------------------------------------------------- |
51
+ | `port` | Actual port the server is listening on |
52
+ | `getTraces(filter?)` | Query stored traces |
53
+ | `getLogs(filter?)` | Query stored logs |
54
+ | `getMetrics(filter)` | Query stored metrics (`metricType` required) |
55
+ | `discoverMetrics()` | List all collected metric names and types |
56
+ | `clear()` | Delete all telemetry data |
57
+ | `stop()` | Shut down the server and close the database |
58
+ | `datasource` | Direct access to the underlying `TelemetryDatasource` |
59
+
60
+ ## Options
61
+
62
+ | Option | Default | Description |
63
+ | ------ | ------------- | ------------------------------------------------------- |
64
+ | `port` | `4318` | Port to listen on. Use `0` for a random available port. |
65
+ | `host` | `"localhost"` | Host to bind to. |
66
+
67
+ ## Cross-Runner Support
68
+
69
+ Works with any Node.js test runner. See [`examples/`](./examples) for complete working tests using:
70
+
71
+ - [vitest](./examples/vitest)
72
+ - [jest](./examples/jest)
73
+ - [node-tap](./examples/tap)
74
+ - [node:test](./examples/node-test)
package/package.json ADDED
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "@kopai/otel-testing-harness",
3
+ "version": "0.1.0",
4
+ "license": "Apache-2.0",
5
+ "author": "Vladimir Adamic",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/kopai-app/kopai-mono.git",
9
+ "directory": "packages/otel-testing-harness"
10
+ },
11
+ "homepage": "https://github.com/kopai-app/kopai-mono/tree/main/packages/otel-testing-harness#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/kopai-app/kopai-mono/issues"
14
+ },
15
+ "type": "module",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.mts",
19
+ "import": "./dist/index.mjs",
20
+ "require": "./dist/index.cjs"
21
+ }
22
+ },
23
+ "main": "./dist/index.cjs",
24
+ "module": "./dist/index.mjs",
25
+ "types": "./dist/index.d.mts",
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "scripts": {
30
+ "build": "tsdown",
31
+ "dev": "tsdown --watch",
32
+ "lint": "eslint src",
33
+ "type-check": "tsc --noEmit",
34
+ "test": "vitest run && pnpm test:examples:vitest && pnpm test:examples:jest && pnpm test:examples:tap && pnpm test:examples:node-test",
35
+ "test:examples:vitest": "vitest run --config examples/vitest/vitest.config.ts",
36
+ "test:examples:jest": "pnpm build && cd examples/jest && NODE_OPTIONS='--experimental-vm-modules' npx jest",
37
+ "test:examples:tap": "tsx examples/tap/harness-tap.test.ts",
38
+ "test:examples:node-test": "node --import tsx examples/node-test/harness-node-test.ts",
39
+ "test:smoke": "node test/smoke-esm.mjs && node test/smoke-cjs.cjs"
40
+ },
41
+ "dependencies": {
42
+ "@kopai/collector": "workspace:*",
43
+ "@kopai/core": "workspace:*",
44
+ "@kopai/sqlite-datasource": "workspace:*",
45
+ "fastify": "^5.8.2",
46
+ "fastify-type-provider-zod": "^6.1.0",
47
+ "zod": "^4.3.6"
48
+ },
49
+ "devDependencies": {
50
+ "@jest/globals": "^30.3.0",
51
+ "@kopai/tsconfig": "workspace:*",
52
+ "@opentelemetry/api": "^1.9.0",
53
+ "@opentelemetry/api-logs": "^0.213.0",
54
+ "@opentelemetry/exporter-logs-otlp-http": "^0.213.0",
55
+ "@opentelemetry/exporter-logs-otlp-proto": "^0.213.0",
56
+ "@opentelemetry/exporter-metrics-otlp-http": "^0.213.0",
57
+ "@opentelemetry/exporter-metrics-otlp-proto": "^0.213.0",
58
+ "@opentelemetry/exporter-trace-otlp-http": "^0.213.0",
59
+ "@opentelemetry/exporter-trace-otlp-proto": "^0.213.0",
60
+ "@opentelemetry/resources": "^2.6.0",
61
+ "@opentelemetry/sdk-logs": "^0.213.0",
62
+ "@opentelemetry/sdk-metrics": "^2.6.0",
63
+ "@opentelemetry/sdk-trace-base": "^2.6.0",
64
+ "@opentelemetry/semantic-conventions": "^1.40.0",
65
+ "@tapjs/tsx": "^3.4.2",
66
+ "@types/node": "^25.4.0",
67
+ "jest": "^30.3.0",
68
+ "tap": "^21.6.2",
69
+ "ts-jest": "^29.4.6",
70
+ "tsdown": "^0.21.2",
71
+ "tsx": "^4.21.0"
72
+ }
73
+ }