@procedures/sink 0.0.0-stage → 0.68.18
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/CHANGELOG.md +13 -0
- package/LICENSE +21 -0
- package/README.md +79 -2
- package/dist/api.d.ts +95 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +170 -0
- package/dist/api.js.map +1 -0
- package/dist/call.d.ts +42 -0
- package/dist/call.d.ts.map +1 -0
- package/dist/call.js +44 -0
- package/dist/call.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/module.d.ts +32 -0
- package/dist/module.d.ts.map +1 -0
- package/dist/module.js +97 -0
- package/dist/module.js.map +1 -0
- package/dist/procedures/sink/errors.d.ts +8 -0
- package/dist/procedures/sink/errors.d.ts.map +1 -0
- package/dist/procedures/sink/errors.js +9 -0
- package/dist/procedures/sink/errors.js.map +1 -0
- package/dist/procedures/sink/index.d.ts +6 -0
- package/dist/procedures/sink/index.d.ts.map +1 -0
- package/dist/procedures/sink/index.js +6 -0
- package/dist/procedures/sink/index.js.map +1 -0
- package/dist/procedures/sink/services.d.ts +8 -0
- package/dist/procedures/sink/services.d.ts.map +1 -0
- package/dist/procedures/sink/services.js +9 -0
- package/dist/procedures/sink/services.js.map +1 -0
- package/dist/procedures/sink/slowest.d.ts +8 -0
- package/dist/procedures/sink/slowest.d.ts.map +1 -0
- package/dist/procedures/sink/slowest.js +9 -0
- package/dist/procedures/sink/slowest.js.map +1 -0
- package/dist/procedures/sink/stats.d.ts +8 -0
- package/dist/procedures/sink/stats.d.ts.map +1 -0
- package/dist/procedures/sink/stats.js +9 -0
- package/dist/procedures/sink/stats.js.map +1 -0
- package/dist/procedures/sink/trace.d.ts +8 -0
- package/dist/procedures/sink/trace.d.ts.map +1 -0
- package/dist/procedures/sink/trace.js +9 -0
- package/dist/procedures/sink/trace.js.map +1 -0
- package/dist/register.d.ts +6 -0
- package/dist/register.d.ts.map +1 -0
- package/dist/register.js +8 -0
- package/dist/register.js.map +1 -0
- package/dist/types.d.ts +140 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +98 -0
- package/dist/types.js.map +1 -0
- package/package.json +81 -3
- package/src/api.ts +217 -0
- package/src/call.ts +69 -0
- package/src/index.ts +6 -0
- package/src/module.ts +150 -0
- package/src/procedures/sink/errors.ts +11 -0
- package/src/procedures/sink/index.ts +5 -0
- package/src/procedures/sink/services.ts +11 -0
- package/src/procedures/sink/slowest.ts +11 -0
- package/src/procedures/sink/stats.ts +11 -0
- package/src/procedures/sink/trace.ts +11 -0
- package/src/register.ts +10 -0
- package/src/types.ts +234 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @procedures/sink
|
|
2
|
+
|
|
3
|
+
## 0.68.18
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 9323692: The new package @procedures/sink gives the HTTP queries of the OTLP sink as five procedures: sink.slowest, sink.errors, sink.services, sink.trace and sink.stats. Each one reads the network and changes nothing.
|
|
8
|
+
|
|
9
|
+
bundle-mcp adds the namespace sink to the tools of the MCP server, and mcp-dev-tools records the five tools in its snapshots.
|
|
10
|
+
|
|
11
|
+
PATCH: before 1.0.0, a backward-compatible feature raises PATCH. The change adds tools and removes nothing.
|
|
12
|
+
- @procedures/client@0.68.18
|
|
13
|
+
- @procedures/http@0.68.18
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-2026 Mark Russell
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,80 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @procedures/sink
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@procedures/sink)
|
|
4
|
+
|
|
5
|
+
The HTTP queries of the OTLP sink `@mark1russell7/otel-sink` as procedures. The sink keeps spans, logs and metric points on one computer. It listens on `http://127.0.0.1:4328`. These procedures read it, and they change nothing.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @procedures/sink @procedures/client
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
A host registers the procedures with `createRegistry(sinkModule)`, or with `import "@procedures/sink/register"` for the global registry. The package root and `./module` register nothing.
|
|
14
|
+
|
|
15
|
+
## Procedures
|
|
16
|
+
|
|
17
|
+
Each procedure declares the effects `reads: ["network"]`, `writes: []` and `idempotent: true`.
|
|
18
|
+
|
|
19
|
+
| Procedure | Route | What it gives |
|
|
20
|
+
| --- | --- | --- |
|
|
21
|
+
| `sink.slowest` | `GET /api/slowest` | The slowest spans of a window, the slowest first. |
|
|
22
|
+
| `sink.errors` | `GET /api/errors` | The failed spans and the error logs of a window, the newest first. |
|
|
23
|
+
| `sink.services` | `GET /api/services` | The span count, the failed spans, the error rate and the slowest span of each service. |
|
|
24
|
+
| `sink.trace` | `GET /api/trace/:id` | The spans and the logs of one trace, in the order of their times. |
|
|
25
|
+
| `sink.stats` | `GET /api/stats` | The rows and bytes of each signal, the file size and the retention rules. |
|
|
26
|
+
|
|
27
|
+
The window has three optional fields:
|
|
28
|
+
|
|
29
|
+
- `since`: the start, in milliseconds since the epoch. The default is one hour ago.
|
|
30
|
+
- `limit`: the most rows, from 1 to 1000. The default is 50.
|
|
31
|
+
- `service`: only the rows of this service.
|
|
32
|
+
|
|
33
|
+
Each procedure also takes `baseUrl` (the default is `http://127.0.0.1:4328`) and `timeoutMs` (the default is 10000).
|
|
34
|
+
|
|
35
|
+
## Example
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
const { services } = await client.exec(["sink", "services"], { limit: 10 });
|
|
39
|
+
const { spans, logs } = await client.exec(["sink", "trace"], { id: "0af7651916cd43dd8448eb211c80319c" });
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
From the command line:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
mark sink slowest --limit 5 --service web
|
|
46
|
+
mark sink trace 0af7651916cd43dd8448eb211c80319c
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## A pure builder and a pure reader
|
|
50
|
+
|
|
51
|
+
The HTTP call has two pure parts, and the fetch is a parameter:
|
|
52
|
+
|
|
53
|
+
- `sinkRequest(query, baseUrl)` gives the URL and the headers of a query. It refuses a bad trace id, a bad window or a bad base URL with a `SinkInputError`, before any call.
|
|
54
|
+
- `readSinkAnswer(route, status, text)` gives the typed body of an answer. A `400` with problems gives a `SinkQueryError`. Another status, a body that is not JSON, or a body of an unknown shape gives a `SinkAnswerError`.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { readSinkAnswer, sinkRequest } from "@procedures/sink";
|
|
58
|
+
|
|
59
|
+
const request = sinkRequest({ route: "slowest", window: { limit: 5 } });
|
|
60
|
+
const answer = await fetch(request.url, { headers: request.headers });
|
|
61
|
+
const { spans } = readSinkAnswer("slowest", answer.status, await answer.text());
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## The host policy
|
|
65
|
+
|
|
66
|
+
The request goes through `createHostFetch()` of `@procedures/http`. The default `fetch` lets a request go only to `127.0.0.1:4328`, `localhost:4328` and `[::1]:4328`, and to no public host. So a `baseUrl` cannot send a request to another server. The policy refuses it with the error `FORBIDDEN` and the reason `host-not-allowed`.
|
|
67
|
+
|
|
68
|
+
A host with a sink on another address gives its own `fetch`:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { createHostFetch } from "@procedures/http";
|
|
72
|
+
import { createSinkModule } from "@procedures/sink/module";
|
|
73
|
+
|
|
74
|
+
const registry = createRegistry(createSinkModule({ fetch: createHostFetch({ allow: ["10.0.0.5:4328"], publicHosts: false }) }));
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Errors
|
|
78
|
+
|
|
79
|
+
- A sink that does not answer gives an error that names the base URL and the time limit.
|
|
80
|
+
- An unknown route gives a `404`. The error tells you that the sink is older, or that another server is on the port.
|
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The HTTP queries of the OTLP sink as two pure parts: a request builder and an answer extractor. Neither function
|
|
3
|
+
* sends a request. The caller gives the fetch, so a test needs no network.
|
|
4
|
+
*
|
|
5
|
+
* The sink answers a good query with 200 and the JSON of the answer. It answers bad parameters with 400 and the
|
|
6
|
+
* body `{ "problems": [...] }`.
|
|
7
|
+
*/
|
|
8
|
+
import { type SinkErrorsOutput, type SinkServicesOutput, type SinkSlowestOutput, type SinkStatsOutput, type SinkTraceOutput } from "./types.js";
|
|
9
|
+
/** The path of each query route of the sink. Each route takes the method GET. */
|
|
10
|
+
export declare const SINK_ROUTES: {
|
|
11
|
+
readonly slowest: "/api/slowest";
|
|
12
|
+
readonly errors: "/api/errors";
|
|
13
|
+
readonly services: "/api/services";
|
|
14
|
+
readonly trace: "/api/trace";
|
|
15
|
+
readonly stats: "/api/stats";
|
|
16
|
+
};
|
|
17
|
+
/** The name of a query route */
|
|
18
|
+
export type SinkRoute = keyof typeof SINK_ROUTES;
|
|
19
|
+
/** The names of the query routes, in the order of the routes of the sink */
|
|
20
|
+
export declare const SINK_ROUTE_NAMES: readonly SinkRoute[];
|
|
21
|
+
/**
|
|
22
|
+
* The query parameters that the request of each route sends. The sink also examines a window for `stats`, but its
|
|
23
|
+
* answer does not use the window. Thus the request of `stats` sends no parameter.
|
|
24
|
+
*/
|
|
25
|
+
export declare const SINK_PARAMS: {
|
|
26
|
+
readonly [route in SinkRoute]: readonly string[];
|
|
27
|
+
};
|
|
28
|
+
/** The window of a query. The sink uses its defaults for a field that is not given. */
|
|
29
|
+
export interface SinkWindow {
|
|
30
|
+
/** The start of the window, in milliseconds since the epoch. The default of the sink is one hour ago. */
|
|
31
|
+
readonly since?: number | undefined;
|
|
32
|
+
/** The most rows of the answer, from 1 to `MAX_LIMIT`. The default of the sink is 50. */
|
|
33
|
+
readonly limit?: number | undefined;
|
|
34
|
+
/** Only the rows of this service */
|
|
35
|
+
readonly service?: string | undefined;
|
|
36
|
+
}
|
|
37
|
+
/** One query of the sink: a route and its parameters */
|
|
38
|
+
export type SinkQuery = {
|
|
39
|
+
readonly route: "slowest" | "errors" | "services";
|
|
40
|
+
readonly window?: SinkWindow | undefined;
|
|
41
|
+
} | {
|
|
42
|
+
readonly route: "trace";
|
|
43
|
+
readonly id: string;
|
|
44
|
+
} | {
|
|
45
|
+
readonly route: "stats";
|
|
46
|
+
};
|
|
47
|
+
/** One request to the sink. Its fields fit the `init` of `fetch`. */
|
|
48
|
+
export interface SinkRequest {
|
|
49
|
+
readonly url: string;
|
|
50
|
+
readonly headers: {
|
|
51
|
+
readonly accept: "application/json";
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** The answer of each route, as the procedures give it */
|
|
55
|
+
export interface SinkAnswers {
|
|
56
|
+
readonly slowest: SinkSlowestOutput;
|
|
57
|
+
readonly errors: SinkErrorsOutput;
|
|
58
|
+
readonly services: SinkServicesOutput;
|
|
59
|
+
readonly trace: SinkTraceOutput;
|
|
60
|
+
readonly stats: SinkStatsOutput;
|
|
61
|
+
}
|
|
62
|
+
/** The builder throws this error for a bad parameter. Thus no request goes to the sink. */
|
|
63
|
+
export declare class SinkInputError extends Error {
|
|
64
|
+
readonly name = "SinkInputError";
|
|
65
|
+
readonly problems: readonly string[];
|
|
66
|
+
constructor(problems: readonly string[]);
|
|
67
|
+
}
|
|
68
|
+
/** The extractor throws this error when the sink refuses a query with 400 and its problems. */
|
|
69
|
+
export declare class SinkQueryError extends Error {
|
|
70
|
+
readonly name = "SinkQueryError";
|
|
71
|
+
readonly problems: readonly string[];
|
|
72
|
+
constructor(path: string, problems: readonly string[]);
|
|
73
|
+
}
|
|
74
|
+
/** The extractor throws this error for another status, or for a body with a shape that it does not know. */
|
|
75
|
+
export declare class SinkAnswerError extends Error {
|
|
76
|
+
readonly name = "SinkAnswerError";
|
|
77
|
+
readonly status: number;
|
|
78
|
+
constructor(message: string, status: number);
|
|
79
|
+
}
|
|
80
|
+
/** This function gives the problems of a window, with the rules of the sink. An empty list is a good window. */
|
|
81
|
+
export declare function windowProblems(window: SinkWindow): string[];
|
|
82
|
+
/**
|
|
83
|
+
* This function gives the request of one query. It throws a `SinkInputError` for a bad window, a bad trace id or a
|
|
84
|
+
* bad base URL. The default base URL is `DEFAULT_SINK_URL`.
|
|
85
|
+
*/
|
|
86
|
+
export declare function sinkRequest(query: SinkQuery, baseUrl?: string): SinkRequest;
|
|
87
|
+
/**
|
|
88
|
+
* This function reads the answer of the sink to a query of one route: its HTTP status and its body text.
|
|
89
|
+
*
|
|
90
|
+
* - A 200 with a body of the known shape gives the output of the procedure.
|
|
91
|
+
* - A 400 with problems throws a `SinkQueryError` with the problems of the sink.
|
|
92
|
+
* - Another status, or a body of an unknown shape, throws a `SinkAnswerError`.
|
|
93
|
+
*/
|
|
94
|
+
export declare function readSinkAnswer<R extends SinkRoute>(route: R, status: number, text: string): SinkAnswers[R];
|
|
95
|
+
//# sourceMappingURL=api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EASL,KAAK,gBAAgB,EACrB,KAAK,kBAAkB,EACvB,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,KAAK,eAAe,EACrB,MAAM,YAAY,CAAC;AAEpB,iFAAiF;AACjF,eAAO,MAAM,WAAW;aACtB,OAAO,EAAE,cAAc;aACvB,MAAM,EAAE,aAAa;aACrB,QAAQ,EAAE,eAAe;aACzB,KAAK,EAAE,YAAY;aACnB,KAAK,EAAE,YAAY;CACX,CAAC;AAEX,gCAAgC;AAChC,MAAM,MAAM,SAAS,GAAG,MAAM,OAAO,WAAW,CAAC;AAEjD,4EAA4E;AAC5E,eAAO,MAAM,gBAAgB,EAAE,SAAS,SAAS,EAAwD,CAAC;AAE1G;;;GAGG;AACH,eAAO,MAAM,WAAW,EAAE;IAAE,QAAQ,EAAE,KAAK,IAAI,SAAS,GAAG,SAAS,MAAM,EAAE;CAM3E,CAAC;AAEF,uFAAuF;AACvF,MAAM,WAAW,UAAU;IACzB,yGAAyG;IACzG,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,yFAAyF;IACzF,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,oCAAoC;IACpC,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;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SAAQ,KAAK;IACxC,SAAkB,IAAI,qBAAqB;IAC3C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAExB,YAAY,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAG1C;CACF;AAED,gHAAgH;AAChH,wBAAgB,cAAc,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,EAAE,CAO3D;AAkBD;;;GAGG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,GAAE,MAAyB,GAAG,WAAW,CAe7F;AA8BD;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,CAAC,SAAS,SAAS,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,WAAW,CAAC,CAAC,CAAC,CAkB1G"}
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The HTTP queries of the OTLP sink as two pure parts: a request builder and an answer extractor. Neither function
|
|
3
|
+
* sends a request. The caller gives the fetch, so a test needs no network.
|
|
4
|
+
*
|
|
5
|
+
* The sink answers a good query with 200 and the JSON of the answer. It answers bad parameters with 400 and the
|
|
6
|
+
* body `{ "problems": [...] }`.
|
|
7
|
+
*/
|
|
8
|
+
import * as z from "zod";
|
|
9
|
+
import { DEFAULT_SINK_URL, MAX_LIMIT, TRACE_ID_INPUT, ServiceViewSchema, SinkErrorsOutputSchema, SinkStatsOutputSchema, SinkTraceOutputSchema, SpanViewSchema, } from "./types.js";
|
|
10
|
+
/** The path of each query route of the sink. Each route takes the method GET. */
|
|
11
|
+
export const SINK_ROUTES = {
|
|
12
|
+
slowest: "/api/slowest",
|
|
13
|
+
errors: "/api/errors",
|
|
14
|
+
services: "/api/services",
|
|
15
|
+
trace: "/api/trace",
|
|
16
|
+
stats: "/api/stats",
|
|
17
|
+
};
|
|
18
|
+
/** The names of the query routes, in the order of the routes of the sink */
|
|
19
|
+
export const SINK_ROUTE_NAMES = ["slowest", "errors", "services", "trace", "stats"];
|
|
20
|
+
/**
|
|
21
|
+
* The query parameters that the request of each route sends. The sink also examines a window for `stats`, but its
|
|
22
|
+
* answer does not use the window. Thus the request of `stats` sends no parameter.
|
|
23
|
+
*/
|
|
24
|
+
export const SINK_PARAMS = {
|
|
25
|
+
slowest: ["since", "limit", "service"],
|
|
26
|
+
errors: ["since", "limit", "service"],
|
|
27
|
+
services: ["since", "limit", "service"],
|
|
28
|
+
trace: ["id"],
|
|
29
|
+
stats: [],
|
|
30
|
+
};
|
|
31
|
+
/** The builder throws this error for a bad parameter. Thus no request goes to the sink. */
|
|
32
|
+
export class SinkInputError extends Error {
|
|
33
|
+
name = "SinkInputError";
|
|
34
|
+
problems;
|
|
35
|
+
constructor(problems) {
|
|
36
|
+
super(`The query of the sink has problems: ${problems.join(". ")}`);
|
|
37
|
+
this.problems = problems;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** The extractor throws this error when the sink refuses a query with 400 and its problems. */
|
|
41
|
+
export class SinkQueryError extends Error {
|
|
42
|
+
name = "SinkQueryError";
|
|
43
|
+
problems;
|
|
44
|
+
constructor(path, problems) {
|
|
45
|
+
super(`The sink refused the query ${path}: ${problems.join(". ")}`);
|
|
46
|
+
this.problems = problems;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** The extractor throws this error for another status, or for a body with a shape that it does not know. */
|
|
50
|
+
export class SinkAnswerError extends Error {
|
|
51
|
+
name = "SinkAnswerError";
|
|
52
|
+
status;
|
|
53
|
+
constructor(message, status) {
|
|
54
|
+
super(message);
|
|
55
|
+
this.status = status;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** This function gives the problems of a window, with the rules of the sink. An empty list is a good window. */
|
|
59
|
+
export function windowProblems(window) {
|
|
60
|
+
const problems = [];
|
|
61
|
+
const { since, limit, service } = window;
|
|
62
|
+
if (since !== undefined && (!Number.isFinite(since) || since < 0))
|
|
63
|
+
problems.push("since must be a time in milliseconds from 0");
|
|
64
|
+
if (limit !== undefined && (!Number.isSafeInteger(limit) || limit < 1 || limit > MAX_LIMIT))
|
|
65
|
+
problems.push(`limit must be a whole number from 1 to ${MAX_LIMIT}`);
|
|
66
|
+
if (service === "")
|
|
67
|
+
problems.push("service must not be empty");
|
|
68
|
+
return problems;
|
|
69
|
+
}
|
|
70
|
+
/** This function gives the base URL without a final slash. It throws a `SinkInputError` for a bad URL. */
|
|
71
|
+
function baseOf(baseUrl) {
|
|
72
|
+
let url;
|
|
73
|
+
try {
|
|
74
|
+
url = new URL(baseUrl);
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
throw new SinkInputError([`baseUrl ${JSON.stringify(baseUrl)} is not a URL`]);
|
|
78
|
+
}
|
|
79
|
+
const problems = [];
|
|
80
|
+
if (url.protocol !== "http:" && url.protocol !== "https:")
|
|
81
|
+
problems.push("baseUrl must be an http: or https: URL");
|
|
82
|
+
if (url.search !== "" || url.hash !== "")
|
|
83
|
+
problems.push("baseUrl must have no query and no fragment");
|
|
84
|
+
if (url.username !== "" || url.password !== "")
|
|
85
|
+
problems.push("baseUrl must have no user name and no password");
|
|
86
|
+
if (problems.length > 0)
|
|
87
|
+
throw new SinkInputError(problems);
|
|
88
|
+
return `${url.origin}${url.pathname.replace(/\/+$/, "")}`;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* This function gives the request of one query. It throws a `SinkInputError` for a bad window, a bad trace id or a
|
|
92
|
+
* bad base URL. The default base URL is `DEFAULT_SINK_URL`.
|
|
93
|
+
*/
|
|
94
|
+
export function sinkRequest(query, baseUrl = DEFAULT_SINK_URL) {
|
|
95
|
+
const params = new URLSearchParams();
|
|
96
|
+
if (query.route === "trace") {
|
|
97
|
+
if (!TRACE_ID_INPUT.test(query.id))
|
|
98
|
+
throw new SinkInputError(["id must be a trace id of 32 hex digits"]);
|
|
99
|
+
params.set("id", query.id.toLowerCase());
|
|
100
|
+
}
|
|
101
|
+
else if (query.route !== "stats") {
|
|
102
|
+
const window = query.window ?? {};
|
|
103
|
+
const problems = windowProblems(window);
|
|
104
|
+
if (problems.length > 0)
|
|
105
|
+
throw new SinkInputError(problems);
|
|
106
|
+
if (window.since !== undefined)
|
|
107
|
+
params.set("since", String(window.since));
|
|
108
|
+
if (window.limit !== undefined)
|
|
109
|
+
params.set("limit", String(window.limit));
|
|
110
|
+
if (window.service !== undefined)
|
|
111
|
+
params.set("service", window.service);
|
|
112
|
+
}
|
|
113
|
+
const search = params.toString();
|
|
114
|
+
return { url: `${baseOf(baseUrl)}${SINK_ROUTES[query.route]}${search === "" ? "" : `?${search}`}`, headers: { accept: "application/json" } };
|
|
115
|
+
}
|
|
116
|
+
const spans = z.array(SpanViewSchema);
|
|
117
|
+
const services = z.array(ServiceViewSchema);
|
|
118
|
+
const problemsBody = z.object({ problems: z.array(z.string()).min(1) });
|
|
119
|
+
/** The reader of the body of each route: the body of the sink to the output of the procedure */
|
|
120
|
+
const READERS = {
|
|
121
|
+
slowest: (body) => ({ spans: spans.parse(body) }),
|
|
122
|
+
errors: (body) => SinkErrorsOutputSchema.parse(body),
|
|
123
|
+
services: (body) => ({ services: services.parse(body) }),
|
|
124
|
+
trace: (body) => SinkTraceOutputSchema.parse(body),
|
|
125
|
+
stats: (body) => SinkStatsOutputSchema.parse(body),
|
|
126
|
+
};
|
|
127
|
+
/** This function gives the first characters of a body for a message. */
|
|
128
|
+
function excerpt(text) {
|
|
129
|
+
const flat = text.replace(/\s+/g, " ").trim();
|
|
130
|
+
return flat.length > 200 ? `${flat.slice(0, 200)}...` : flat;
|
|
131
|
+
}
|
|
132
|
+
/** This function parses JSON text, or gives `undefined` for text that is not JSON. */
|
|
133
|
+
function parseJson(text) {
|
|
134
|
+
try {
|
|
135
|
+
return JSON.parse(text);
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* This function reads the answer of the sink to a query of one route: its HTTP status and its body text.
|
|
143
|
+
*
|
|
144
|
+
* - A 200 with a body of the known shape gives the output of the procedure.
|
|
145
|
+
* - A 400 with problems throws a `SinkQueryError` with the problems of the sink.
|
|
146
|
+
* - Another status, or a body of an unknown shape, throws a `SinkAnswerError`.
|
|
147
|
+
*/
|
|
148
|
+
export function readSinkAnswer(route, status, text) {
|
|
149
|
+
const path = `GET ${SINK_ROUTES[route]}`;
|
|
150
|
+
const body = parseJson(text);
|
|
151
|
+
if (status === 400) {
|
|
152
|
+
const refused = problemsBody.safeParse(body);
|
|
153
|
+
if (refused.success)
|
|
154
|
+
throw new SinkQueryError(path, refused.data.problems);
|
|
155
|
+
}
|
|
156
|
+
if (status !== 200) {
|
|
157
|
+
const hint = status === 404 ? " The sink has no such route: it is an older sink, or another server is on the port." : "";
|
|
158
|
+
throw new SinkAnswerError(`The sink gave HTTP ${status} for ${path}: ${excerpt(text)}.${hint}`, status);
|
|
159
|
+
}
|
|
160
|
+
if (body === undefined)
|
|
161
|
+
throw new SinkAnswerError(`The answer of the sink for ${path} is not JSON: ${excerpt(text)}`, status);
|
|
162
|
+
try {
|
|
163
|
+
return READERS[route](body);
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
const detail = error instanceof z.ZodError ? z.prettifyError(error).replace(/\s+/g, " ") : String(error);
|
|
167
|
+
throw new SinkAnswerError(`The answer of the sink for ${path} has an unknown shape: ${detail}`, status);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
//# sourceMappingURL=api.js.map
|
package/dist/api.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AACzB,OAAO,EACL,gBAAgB,EAChB,SAAS,EACT,cAAc,EACd,iBAAiB,EACjB,sBAAsB,EACtB,qBAAqB,EACrB,q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AAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC7E,CAAC;IACD,IAAI,MAAM,KAAK,GAAG,EAAE,CAAC;QACnB,MAAM,IAAI,GAAG,MAAM,KAAK,GAAG,CAAC,CAAC,CAAC,qFAAqF,CAAC,CAAC,CAAC,EAAE,CAAC;QACzH,MAAM,IAAI,eAAe,CAAC,sBAAsB,MAAM,QAAQ,IAAI,KAAK,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,EAAE,MAAM,CAAC,CAAC;IAC1G,CAAC;IACD,IAAI,IAAI,KAAK,SAAS;QAAE,MAAM,IAAI,eAAe,CAAC,8BAA8B,IAAI,iBAAiB,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;IAC9H,IAAI,CAAC;QACH,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC;IAC9B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,MAAM,GAAG,KAAK,YAAY,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACzG,MAAM,IAAI,eAAe,CAAC,8BAA8B,IAAI,0BAA0B,MAAM,EAAE,EAAE,MAAM,CAAC,CAAC;IAC1G,CAAC;AACH,CAAC"}
|
package/dist/call.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One query of the sink through a given fetch: the request of `sinkRequest`, then the extractor `readSinkAnswer`.
|
|
3
|
+
* The default fetch is `createHostFetch()` of `@procedures/http`, with only the loopback addresses of the sink.
|
|
4
|
+
*/
|
|
5
|
+
import { type SinkAnswers, type SinkQuery, type SinkRoute, type SinkWindow } from "./api.js";
|
|
6
|
+
import { type SinkWindowInput } from "./types.js";
|
|
7
|
+
/** The part of `fetch` that the procedures use. `createHostFetch()` of `@procedures/http` fits this type. */
|
|
8
|
+
export type SinkFetch = (url: string, init: {
|
|
9
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
10
|
+
readonly signal: AbortSignal;
|
|
11
|
+
}) => Promise<{
|
|
12
|
+
readonly status: number;
|
|
13
|
+
text(): Promise<string>;
|
|
14
|
+
}>;
|
|
15
|
+
/** The hosts of the default fetch: the sink on the loopback addresses of this computer, at its port 4328 */
|
|
16
|
+
export declare const SINK_HOSTS: readonly string[];
|
|
17
|
+
/**
|
|
18
|
+
* This function gives the default fetch of the procedures. The host policy of `@procedures/http` lets it reach only
|
|
19
|
+
* `SINK_HOSTS`, and no public host. Thus the input `baseUrl` cannot send a request to another server.
|
|
20
|
+
*/
|
|
21
|
+
export declare function defaultSinkFetch(): SinkFetch;
|
|
22
|
+
/** The context of a handler: the fetch of the host and the signal of the call */
|
|
23
|
+
export interface SinkContext {
|
|
24
|
+
readonly fetch: SinkFetch;
|
|
25
|
+
readonly signal?: AbortSignal | undefined;
|
|
26
|
+
}
|
|
27
|
+
/** This function gives the window of the input of a window procedure. */
|
|
28
|
+
export declare function windowOf(input: SinkWindowInput): SinkWindow;
|
|
29
|
+
/** The settings of one query: the base URL, the time limit and the signal of the caller */
|
|
30
|
+
export interface SinkCallOptions {
|
|
31
|
+
readonly baseUrl?: string | undefined;
|
|
32
|
+
readonly timeoutMs: number;
|
|
33
|
+
readonly signal?: AbortSignal | undefined;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* This function sends one query to the sink and reads its answer. The builder examines the input before the
|
|
37
|
+
* request. A refusal of the host policy keeps its code and its reason.
|
|
38
|
+
*/
|
|
39
|
+
export declare function querySink<R extends SinkRoute>(fetch: SinkFetch, query: SinkQuery & {
|
|
40
|
+
readonly route: R;
|
|
41
|
+
}, options: SinkCallOptions): Promise<SinkAnswers[R]>;
|
|
42
|
+
//# sourceMappingURL=call.d.ts.map
|
|
@@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CAAC,CAAC,SAAS,SAAS,EACjD,KAAK,EAAE,SAAS,EAChB,KAAK,EAAE,SAAS,GAAG;IAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;CAAE,EACxC,OAAO,EAAE,eAAe,GACvB,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAgBzB"}
|
package/dist/call.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One query of the sink through a given fetch: the request of `sinkRequest`, then the extractor `readSinkAnswer`.
|
|
3
|
+
* The default fetch is `createHostFetch()` of `@procedures/http`, with only the loopback addresses of the sink.
|
|
4
|
+
*/
|
|
5
|
+
import { createHostFetch, isHostNotAllowed } from "@procedures/http";
|
|
6
|
+
import { readSinkAnswer, sinkRequest } from "./api.js";
|
|
7
|
+
import { DEFAULT_SINK_URL } from "./types.js";
|
|
8
|
+
/** The hosts of the default fetch: the sink on the loopback addresses of this computer, at its port 4328 */
|
|
9
|
+
export const SINK_HOSTS = ["127.0.0.1:4328", "localhost:4328", "[::1]:4328"];
|
|
10
|
+
/**
|
|
11
|
+
* This function gives the default fetch of the procedures. The host policy of `@procedures/http` lets it reach only
|
|
12
|
+
* `SINK_HOSTS`, and no public host. Thus the input `baseUrl` cannot send a request to another server.
|
|
13
|
+
*/
|
|
14
|
+
export function defaultSinkFetch() {
|
|
15
|
+
return createHostFetch({ allow: SINK_HOSTS, publicHosts: false });
|
|
16
|
+
}
|
|
17
|
+
/** This function gives the window of the input of a window procedure. */
|
|
18
|
+
export function windowOf(input) {
|
|
19
|
+
return { since: input.since, limit: input.limit, service: input.service };
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* This function sends one query to the sink and reads its answer. The builder examines the input before the
|
|
23
|
+
* request. A refusal of the host policy keeps its code and its reason.
|
|
24
|
+
*/
|
|
25
|
+
export async function querySink(fetch, query, options) {
|
|
26
|
+
const baseUrl = options.baseUrl ?? DEFAULT_SINK_URL;
|
|
27
|
+
const request = sinkRequest(query, baseUrl);
|
|
28
|
+
const signals = [AbortSignal.timeout(options.timeoutMs), ...(options.signal ? [options.signal] : [])];
|
|
29
|
+
let status;
|
|
30
|
+
let text;
|
|
31
|
+
try {
|
|
32
|
+
const response = await fetch(request.url, { headers: request.headers, signal: AbortSignal.any(signals) });
|
|
33
|
+
status = response.status;
|
|
34
|
+
text = await response.text();
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (isHostNotAllowed(error))
|
|
38
|
+
throw error;
|
|
39
|
+
const reason = error.name === "TimeoutError" ? `no answer in ${options.timeoutMs} ms` : "the request failed";
|
|
40
|
+
throw new Error(`The sink at ${baseUrl} did not answer: ${reason}. Is the sink started?`, { cause: error });
|
|
41
|
+
}
|
|
42
|
+
return readSinkAnswer(query.route, status, text);
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=call.js.map
|
package/dist/call.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"call.js","sourceRoot":"","sources":["../src/call.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACrE,OAAO,EAAE,cAAc,EAAE,WAAW,EAAqE,MAAM,UAAU,CAAC;AAC1H,OAAO,EAAE,gBAAgB,EAAwB,MAAM,YAAY,CAAC;AAQpE,4GAA4G;AAC5G,MAAM,CAAC,MAAM,UAAU,GAAsB,CAAC,gBAAgB,EAAE,gBAAgB,EAAE,YAAY,CAAC,CAAC;AAEhG;;;GAGG;AACH,MAAM,UAAU,gBAAgB;IAC9B,OAAO,eAAe,CAAC,EAAE,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,KAAK,EAAE,CAAC,CAAC;AACpE,CAAC;AAQD,yEAAyE;AACzE,MAAM,UAAU,QAAQ,CAAC,KAAsB;IAC7C,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,KAAK,EAAE,KAA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KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;AACnD,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,4BAA4B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// The root registers nothing: import "<package>/register", or give the module to createRegistry()
|
|
2
|
+
export * from "./module.js";
|
|
3
|
+
export * from "./types.js";
|
|
4
|
+
export * from "./api.js";
|
|
5
|
+
export * from "./call.js";
|
|
6
|
+
export * from "./procedures/sink/index.js";
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,4BAA4B,CAAC"}
|
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The procedures of @procedures/sink as data. An import of this file registers nothing.
|
|
3
|
+
*
|
|
4
|
+
* Each procedure reads one query route of the OTLP sink over HTTP. It changes nothing in the sink.
|
|
5
|
+
*/
|
|
6
|
+
import { type AnyProcedure, type Effects, type ProcedureModule, type ProcedureRegistry } from "@procedures/client";
|
|
7
|
+
import { type SinkFetch } from "./call.js";
|
|
8
|
+
/** The effects of each procedure: a read over the network, and no write. The sink is idempotent for each query. */
|
|
9
|
+
export declare const SINK_EFFECTS: Effects;
|
|
10
|
+
/** The settings of the host for the procedures of this package. The procedure input cannot change them. */
|
|
11
|
+
export interface SinkProceduresOptions {
|
|
12
|
+
/**
|
|
13
|
+
* The function that sends the request. The default is `defaultSinkFetch()`: only the sink on the loopback
|
|
14
|
+
* addresses, at port 4328. A host with a sink on another port gives `createHostFetch({ allow: ["127.0.0.1:4400"],
|
|
15
|
+
* publicHosts: false })`.
|
|
16
|
+
*/
|
|
17
|
+
readonly fetch?: SinkFetch | undefined;
|
|
18
|
+
}
|
|
19
|
+
/** This function gives the procedures of this package with the settings of the host. */
|
|
20
|
+
export declare function createSinkProcedures(options?: SinkProceduresOptions): AnyProcedure[];
|
|
21
|
+
export declare const sinkModule: ProcedureModule;
|
|
22
|
+
export default sinkModule;
|
|
23
|
+
/**
|
|
24
|
+
* This function gives the module with the settings of the host: the `fetch` of the requests. Without options, it
|
|
25
|
+
* gives `sinkModule`.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createSinkModule(options?: SinkProceduresOptions): ProcedureModule;
|
|
28
|
+
/**
|
|
29
|
+
* This function registers the procedures of @procedures/sink in a registry. The default is the global registry.
|
|
30
|
+
*/
|
|
31
|
+
export declare function registerSinkProcedures(registry?: ProcedureRegistry): void;
|
|
32
|
+
//# sourceMappingURL=module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAKL,KAAK,YAAY,EACjB,KAAK,OAAO,EAEZ,KAAK,eAAe,EACpB,KAAK,iBAAiB,EACvB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAsC,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AAgC/E,mHAAmH;AACnH,eAAO,MAAM,YAAY,EAAE,OAA8D,CAAC;AAE1F,2GAA2G;AAC3G,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,SAAS,GAAG,SAAS,CAAC;CACxC;AAED,wFAAwF;AACxF,wBAAgB,oBAAoB,CAAC,OAAO,GAAE,qBAA0B,GAAG,YAAY,EAAE,CAkExF;AAED,eAAO,MAAM,UAAU,EAAE,eAA4D,CAAC;eAEvE,UAAU;AAEzB;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,GAAE,qBAA0B,GAAG,eAAe,CAGrF;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,QAAQ,GAAE,iBAAsC,GAAG,IAAI,CAE7F"}
|