@prisma-next/middleware-telemetry 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -0
- package/package.json +44 -0
- package/src/exports/index.ts +2 -0
- package/src/telemetry-middleware.ts +67 -0
package/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# @prisma-next/middleware-telemetry
|
|
2
|
+
|
|
3
|
+
A generic, family-agnostic telemetry middleware for Prisma Next runtimes.
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
This package is a **proof-of-concept** demonstrating that the `RuntimeMiddleware` SPI works across both SQL and Mongo runtimes. It validates the cross-family middleware contract by implementing a single middleware that operates identically in both families without any family-specific code.
|
|
8
|
+
|
|
9
|
+
It is **not** intended as a production observability solution. Because framework-level middleware can only access `PlanMeta` (lane, target, storageHash, refs), they cannot inspect query content (SQL strings, Mongo commands, ASTs). Production telemetry will typically use family-specific middleware interfaces (`SqlMiddleware`, `MongoMiddleware`) that expose the full plan type.
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { createTelemetryMiddleware } from '@prisma-next/middleware-telemetry';
|
|
15
|
+
|
|
16
|
+
// Default: logs events via ctx.log.info
|
|
17
|
+
const middleware = createTelemetryMiddleware();
|
|
18
|
+
|
|
19
|
+
// Custom: collect events programmatically
|
|
20
|
+
const middleware = createTelemetryMiddleware({
|
|
21
|
+
onEvent: (event) => console.log(event),
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The middleware emits a `TelemetryEvent` before and after each query execution, containing lane, target, storageHash, and (after execution) row count, latency, and completion status.
|
|
26
|
+
|
|
27
|
+
## See also
|
|
28
|
+
|
|
29
|
+
- [Runtime & Middleware Framework](../../../docs/architecture%20docs/subsystems/4.%20Runtime%20&%20Middleware%20Framework.md) — SPI design and middleware lifecycle
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@prisma-next/middleware-telemetry",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"description": "Generic telemetry middleware for Prisma Next runtimes",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsdown",
|
|
9
|
+
"test": "vitest run",
|
|
10
|
+
"test:coverage": "vitest run --coverage",
|
|
11
|
+
"typecheck": "tsc --project tsconfig.json --noEmit",
|
|
12
|
+
"lint": "biome check . --error-on-warnings",
|
|
13
|
+
"lint:fix": "biome check --write .",
|
|
14
|
+
"lint:fix:unsafe": "biome check --write --unsafe .",
|
|
15
|
+
"clean": "rm -rf dist dist-tsc dist-tsc-prod coverage .tmp-output"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@prisma-next/contract": "workspace:*",
|
|
19
|
+
"@prisma-next/framework-components": "workspace:*"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@prisma-next/tsconfig": "workspace:*",
|
|
23
|
+
"@prisma-next/tsdown": "workspace:*",
|
|
24
|
+
"tsdown": "catalog:",
|
|
25
|
+
"typescript": "catalog:",
|
|
26
|
+
"vitest": "catalog:"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"src"
|
|
31
|
+
],
|
|
32
|
+
"exports": {
|
|
33
|
+
".": "./dist/index.mjs",
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"main": "./dist/index.mjs",
|
|
37
|
+
"module": "./dist/index.mjs",
|
|
38
|
+
"types": "./dist/index.d.mts",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "https://github.com/prisma/prisma-next.git",
|
|
42
|
+
"directory": "packages/3-extensions/middleware-telemetry"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { PlanMeta } from '@prisma-next/contract/types';
|
|
2
|
+
import type {
|
|
3
|
+
AfterExecuteResult,
|
|
4
|
+
RuntimeMiddleware,
|
|
5
|
+
RuntimeMiddlewareContext,
|
|
6
|
+
} from '@prisma-next/framework-components/runtime';
|
|
7
|
+
|
|
8
|
+
export interface TelemetryEvent {
|
|
9
|
+
readonly phase: 'beforeExecute' | 'afterExecute';
|
|
10
|
+
readonly lane: string;
|
|
11
|
+
readonly target: string;
|
|
12
|
+
readonly storageHash: string;
|
|
13
|
+
readonly rowCount?: number;
|
|
14
|
+
readonly latencyMs?: number;
|
|
15
|
+
readonly completed?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface TelemetryMiddlewareOptions {
|
|
19
|
+
readonly onEvent?: (event: TelemetryEvent) => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function createTelemetryMiddleware(options?: TelemetryMiddlewareOptions): RuntimeMiddleware {
|
|
23
|
+
const emit = (event: TelemetryEvent, ctx: RuntimeMiddlewareContext) => {
|
|
24
|
+
try {
|
|
25
|
+
if (options?.onEvent) {
|
|
26
|
+
options.onEvent(event);
|
|
27
|
+
} else {
|
|
28
|
+
ctx.log.info(event);
|
|
29
|
+
}
|
|
30
|
+
} catch (error) {
|
|
31
|
+
ctx.log.warn({ message: 'telemetry sink error', error, event });
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
name: 'telemetry',
|
|
37
|
+
async beforeExecute(plan: { readonly meta: PlanMeta }, ctx: RuntimeMiddlewareContext) {
|
|
38
|
+
emit(
|
|
39
|
+
{
|
|
40
|
+
phase: 'beforeExecute',
|
|
41
|
+
lane: plan.meta.lane,
|
|
42
|
+
target: plan.meta.target,
|
|
43
|
+
storageHash: plan.meta.storageHash,
|
|
44
|
+
},
|
|
45
|
+
ctx,
|
|
46
|
+
);
|
|
47
|
+
},
|
|
48
|
+
async afterExecute(
|
|
49
|
+
plan: { readonly meta: PlanMeta },
|
|
50
|
+
result: AfterExecuteResult,
|
|
51
|
+
ctx: RuntimeMiddlewareContext,
|
|
52
|
+
) {
|
|
53
|
+
emit(
|
|
54
|
+
{
|
|
55
|
+
phase: 'afterExecute',
|
|
56
|
+
lane: plan.meta.lane,
|
|
57
|
+
target: plan.meta.target,
|
|
58
|
+
storageHash: plan.meta.storageHash,
|
|
59
|
+
rowCount: result.rowCount,
|
|
60
|
+
latencyMs: result.latencyMs,
|
|
61
|
+
completed: result.completed,
|
|
62
|
+
},
|
|
63
|
+
ctx,
|
|
64
|
+
);
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|