@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/package.json
CHANGED
|
@@ -1,6 +1,84 @@
|
|
|
1
1
|
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/package",
|
|
2
3
|
"name": "@procedures/sink",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
4
|
+
"version": "0.68.18",
|
|
5
|
+
"description": "The queries of a local OTLP sink as procedures: the slowest spans, the errors, the counts of each service, one trace and the sizes of the store.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"opentelemetry",
|
|
8
|
+
"otlp",
|
|
9
|
+
"traces",
|
|
10
|
+
"logs",
|
|
11
|
+
"procedures"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://mark1russell7.github.io/client/",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/mark1russell7/client/issues"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/mark1russell7/client.git",
|
|
20
|
+
"directory": "packages/sink"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"author": "Mark Russell (https://github.com/mark1russell7)",
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.js",
|
|
31
|
+
"default": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./module": {
|
|
34
|
+
"types": "./dist/module.d.ts",
|
|
35
|
+
"import": "./dist/module.js",
|
|
36
|
+
"default": "./dist/module.js"
|
|
37
|
+
},
|
|
38
|
+
"./register": {
|
|
39
|
+
"types": "./dist/register.d.ts",
|
|
40
|
+
"import": "./dist/register.js",
|
|
41
|
+
"default": "./dist/register.js"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"dist",
|
|
46
|
+
"!dist/.tsbuildinfo",
|
|
47
|
+
"src",
|
|
48
|
+
"!src/**/*.test.ts",
|
|
49
|
+
"!src/**/*.test.tsx",
|
|
50
|
+
"!src/**/__tests__",
|
|
51
|
+
"CHANGELOG.md"
|
|
52
|
+
],
|
|
53
|
+
"sideEffects": [
|
|
54
|
+
"./dist/register.js"
|
|
55
|
+
],
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"typescript": "^7.0.2",
|
|
58
|
+
"@types/node": "^22.0.0",
|
|
59
|
+
"@mark1russell7/cue": "^2.1.0",
|
|
60
|
+
"vitest": "^5.0.3"
|
|
61
|
+
},
|
|
62
|
+
"engines": {
|
|
63
|
+
"node": ">=22"
|
|
64
|
+
},
|
|
65
|
+
"publishConfig": {
|
|
66
|
+
"access": "public",
|
|
67
|
+
"provenance": true
|
|
68
|
+
},
|
|
69
|
+
"client": {
|
|
70
|
+
"procedures": "./dist/register.js"
|
|
71
|
+
},
|
|
72
|
+
"dependencies": {
|
|
73
|
+
"@procedures/client": "0.68.18",
|
|
74
|
+
"@procedures/http": "0.68.18",
|
|
75
|
+
"zod": "^4.6.5"
|
|
76
|
+
},
|
|
77
|
+
"scripts": {
|
|
78
|
+
"build": "tsc -b",
|
|
79
|
+
"typecheck": "tsc --noEmit && tsc -p tsconfig.test.json",
|
|
80
|
+
"clean": "rm -rf dist .tsbuildinfo",
|
|
81
|
+
"test": "vitest run",
|
|
82
|
+
"test:watch": "vitest"
|
|
83
|
+
}
|
|
6
84
|
}
|
package/src/api.ts
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
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
|
+
|
|
9
|
+
import * as z from "zod";
|
|
10
|
+
import {
|
|
11
|
+
DEFAULT_SINK_URL,
|
|
12
|
+
MAX_LIMIT,
|
|
13
|
+
TRACE_ID_INPUT,
|
|
14
|
+
ServiceViewSchema,
|
|
15
|
+
SinkErrorsOutputSchema,
|
|
16
|
+
SinkStatsOutputSchema,
|
|
17
|
+
SinkTraceOutputSchema,
|
|
18
|
+
SpanViewSchema,
|
|
19
|
+
type SinkErrorsOutput,
|
|
20
|
+
type SinkServicesOutput,
|
|
21
|
+
type SinkSlowestOutput,
|
|
22
|
+
type SinkStatsOutput,
|
|
23
|
+
type SinkTraceOutput,
|
|
24
|
+
} from "./types.js";
|
|
25
|
+
|
|
26
|
+
/** The path of each query route of the sink. Each route takes the method GET. */
|
|
27
|
+
export const SINK_ROUTES = {
|
|
28
|
+
slowest: "/api/slowest",
|
|
29
|
+
errors: "/api/errors",
|
|
30
|
+
services: "/api/services",
|
|
31
|
+
trace: "/api/trace",
|
|
32
|
+
stats: "/api/stats",
|
|
33
|
+
} as const;
|
|
34
|
+
|
|
35
|
+
/** The name of a query route */
|
|
36
|
+
export type SinkRoute = keyof typeof SINK_ROUTES;
|
|
37
|
+
|
|
38
|
+
/** The names of the query routes, in the order of the routes of the sink */
|
|
39
|
+
export const SINK_ROUTE_NAMES: readonly SinkRoute[] = ["slowest", "errors", "services", "trace", "stats"];
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The query parameters that the request of each route sends. The sink also examines a window for `stats`, but its
|
|
43
|
+
* answer does not use the window. Thus the request of `stats` sends no parameter.
|
|
44
|
+
*/
|
|
45
|
+
export const SINK_PARAMS: { readonly [route in SinkRoute]: readonly string[] } = {
|
|
46
|
+
slowest: ["since", "limit", "service"],
|
|
47
|
+
errors: ["since", "limit", "service"],
|
|
48
|
+
services: ["since", "limit", "service"],
|
|
49
|
+
trace: ["id"],
|
|
50
|
+
stats: [],
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** The window of a query. The sink uses its defaults for a field that is not given. */
|
|
54
|
+
export interface SinkWindow {
|
|
55
|
+
/** The start of the window, in milliseconds since the epoch. The default of the sink is one hour ago. */
|
|
56
|
+
readonly since?: number | undefined;
|
|
57
|
+
/** The most rows of the answer, from 1 to `MAX_LIMIT`. The default of the sink is 50. */
|
|
58
|
+
readonly limit?: number | undefined;
|
|
59
|
+
/** Only the rows of this service */
|
|
60
|
+
readonly service?: string | undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** One query of the sink: a route and its parameters */
|
|
64
|
+
export type SinkQuery =
|
|
65
|
+
| { readonly route: "slowest" | "errors" | "services"; readonly window?: SinkWindow | undefined }
|
|
66
|
+
| { readonly route: "trace"; readonly id: string }
|
|
67
|
+
| { readonly route: "stats" };
|
|
68
|
+
|
|
69
|
+
/** One request to the sink. Its fields fit the `init` of `fetch`. */
|
|
70
|
+
export interface SinkRequest {
|
|
71
|
+
readonly url: string;
|
|
72
|
+
readonly headers: { readonly accept: "application/json" };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The answer of each route, as the procedures give it */
|
|
76
|
+
export interface SinkAnswers {
|
|
77
|
+
readonly slowest: SinkSlowestOutput;
|
|
78
|
+
readonly errors: SinkErrorsOutput;
|
|
79
|
+
readonly services: SinkServicesOutput;
|
|
80
|
+
readonly trace: SinkTraceOutput;
|
|
81
|
+
readonly stats: SinkStatsOutput;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The builder throws this error for a bad parameter. Thus no request goes to the sink. */
|
|
85
|
+
export class SinkInputError extends Error {
|
|
86
|
+
override readonly name = "SinkInputError";
|
|
87
|
+
readonly problems: readonly string[];
|
|
88
|
+
|
|
89
|
+
constructor(problems: readonly string[]) {
|
|
90
|
+
super(`The query of the sink has problems: ${problems.join(". ")}`);
|
|
91
|
+
this.problems = problems;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The extractor throws this error when the sink refuses a query with 400 and its problems. */
|
|
96
|
+
export class SinkQueryError extends Error {
|
|
97
|
+
override readonly name = "SinkQueryError";
|
|
98
|
+
readonly problems: readonly string[];
|
|
99
|
+
|
|
100
|
+
constructor(path: string, problems: readonly string[]) {
|
|
101
|
+
super(`The sink refused the query ${path}: ${problems.join(". ")}`);
|
|
102
|
+
this.problems = problems;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The extractor throws this error for another status, or for a body with a shape that it does not know. */
|
|
107
|
+
export class SinkAnswerError extends Error {
|
|
108
|
+
override readonly name = "SinkAnswerError";
|
|
109
|
+
readonly status: number;
|
|
110
|
+
|
|
111
|
+
constructor(message: string, status: number) {
|
|
112
|
+
super(message);
|
|
113
|
+
this.status = status;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** This function gives the problems of a window, with the rules of the sink. An empty list is a good window. */
|
|
118
|
+
export function windowProblems(window: SinkWindow): string[] {
|
|
119
|
+
const problems: string[] = [];
|
|
120
|
+
const { since, limit, service } = window;
|
|
121
|
+
if (since !== undefined && (!Number.isFinite(since) || since < 0)) problems.push("since must be a time in milliseconds from 0");
|
|
122
|
+
if (limit !== undefined && (!Number.isSafeInteger(limit) || limit < 1 || limit > MAX_LIMIT)) problems.push(`limit must be a whole number from 1 to ${MAX_LIMIT}`);
|
|
123
|
+
if (service === "") problems.push("service must not be empty");
|
|
124
|
+
return problems;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** This function gives the base URL without a final slash. It throws a `SinkInputError` for a bad URL. */
|
|
128
|
+
function baseOf(baseUrl: string): string {
|
|
129
|
+
let url: URL;
|
|
130
|
+
try {
|
|
131
|
+
url = new URL(baseUrl);
|
|
132
|
+
} catch {
|
|
133
|
+
throw new SinkInputError([`baseUrl ${JSON.stringify(baseUrl)} is not a URL`]);
|
|
134
|
+
}
|
|
135
|
+
const problems: string[] = [];
|
|
136
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") problems.push("baseUrl must be an http: or https: URL");
|
|
137
|
+
if (url.search !== "" || url.hash !== "") problems.push("baseUrl must have no query and no fragment");
|
|
138
|
+
if (url.username !== "" || url.password !== "") problems.push("baseUrl must have no user name and no password");
|
|
139
|
+
if (problems.length > 0) throw new SinkInputError(problems);
|
|
140
|
+
return `${url.origin}${url.pathname.replace(/\/+$/, "")}`;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* This function gives the request of one query. It throws a `SinkInputError` for a bad window, a bad trace id or a
|
|
145
|
+
* bad base URL. The default base URL is `DEFAULT_SINK_URL`.
|
|
146
|
+
*/
|
|
147
|
+
export function sinkRequest(query: SinkQuery, baseUrl: string = DEFAULT_SINK_URL): SinkRequest {
|
|
148
|
+
const params = new URLSearchParams();
|
|
149
|
+
if (query.route === "trace") {
|
|
150
|
+
if (!TRACE_ID_INPUT.test(query.id)) throw new SinkInputError(["id must be a trace id of 32 hex digits"]);
|
|
151
|
+
params.set("id", query.id.toLowerCase());
|
|
152
|
+
} else if (query.route !== "stats") {
|
|
153
|
+
const window = query.window ?? {};
|
|
154
|
+
const problems = windowProblems(window);
|
|
155
|
+
if (problems.length > 0) throw new SinkInputError(problems);
|
|
156
|
+
if (window.since !== undefined) params.set("since", String(window.since));
|
|
157
|
+
if (window.limit !== undefined) params.set("limit", String(window.limit));
|
|
158
|
+
if (window.service !== undefined) params.set("service", window.service);
|
|
159
|
+
}
|
|
160
|
+
const search = params.toString();
|
|
161
|
+
return { url: `${baseOf(baseUrl)}${SINK_ROUTES[query.route]}${search === "" ? "" : `?${search}`}`, headers: { accept: "application/json" } };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const spans = z.array(SpanViewSchema);
|
|
165
|
+
const services = z.array(ServiceViewSchema);
|
|
166
|
+
const problemsBody = z.object({ problems: z.array(z.string()).min(1) });
|
|
167
|
+
|
|
168
|
+
/** The reader of the body of each route: the body of the sink to the output of the procedure */
|
|
169
|
+
const READERS: { readonly [R in SinkRoute]: (body: unknown) => SinkAnswers[R] } = {
|
|
170
|
+
slowest: (body) => ({ spans: spans.parse(body) }),
|
|
171
|
+
errors: (body) => SinkErrorsOutputSchema.parse(body),
|
|
172
|
+
services: (body) => ({ services: services.parse(body) }),
|
|
173
|
+
trace: (body) => SinkTraceOutputSchema.parse(body),
|
|
174
|
+
stats: (body) => SinkStatsOutputSchema.parse(body),
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
/** This function gives the first characters of a body for a message. */
|
|
178
|
+
function excerpt(text: string): string {
|
|
179
|
+
const flat = text.replace(/\s+/g, " ").trim();
|
|
180
|
+
return flat.length > 200 ? `${flat.slice(0, 200)}...` : flat;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** This function parses JSON text, or gives `undefined` for text that is not JSON. */
|
|
184
|
+
function parseJson(text: string): unknown {
|
|
185
|
+
try {
|
|
186
|
+
return JSON.parse(text) as unknown;
|
|
187
|
+
} catch {
|
|
188
|
+
return undefined;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* This function reads the answer of the sink to a query of one route: its HTTP status and its body text.
|
|
194
|
+
*
|
|
195
|
+
* - A 200 with a body of the known shape gives the output of the procedure.
|
|
196
|
+
* - A 400 with problems throws a `SinkQueryError` with the problems of the sink.
|
|
197
|
+
* - Another status, or a body of an unknown shape, throws a `SinkAnswerError`.
|
|
198
|
+
*/
|
|
199
|
+
export function readSinkAnswer<R extends SinkRoute>(route: R, status: number, text: string): SinkAnswers[R] {
|
|
200
|
+
const path = `GET ${SINK_ROUTES[route]}`;
|
|
201
|
+
const body = parseJson(text);
|
|
202
|
+
if (status === 400) {
|
|
203
|
+
const refused = problemsBody.safeParse(body);
|
|
204
|
+
if (refused.success) throw new SinkQueryError(path, refused.data.problems);
|
|
205
|
+
}
|
|
206
|
+
if (status !== 200) {
|
|
207
|
+
const hint = status === 404 ? " The sink has no such route: it is an older sink, or another server is on the port." : "";
|
|
208
|
+
throw new SinkAnswerError(`The sink gave HTTP ${status} for ${path}: ${excerpt(text)}.${hint}`, status);
|
|
209
|
+
}
|
|
210
|
+
if (body === undefined) throw new SinkAnswerError(`The answer of the sink for ${path} is not JSON: ${excerpt(text)}`, status);
|
|
211
|
+
try {
|
|
212
|
+
return READERS[route](body);
|
|
213
|
+
} catch (error) {
|
|
214
|
+
const detail = error instanceof z.ZodError ? z.prettifyError(error).replace(/\s+/g, " ") : String(error);
|
|
215
|
+
throw new SinkAnswerError(`The answer of the sink for ${path} has an unknown shape: ${detail}`, status);
|
|
216
|
+
}
|
|
217
|
+
}
|
package/src/call.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
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
|
+
|
|
6
|
+
import { createHostFetch, isHostNotAllowed } from "@procedures/http";
|
|
7
|
+
import { readSinkAnswer, sinkRequest, type SinkAnswers, type SinkQuery, type SinkRoute, type SinkWindow } from "./api.js";
|
|
8
|
+
import { DEFAULT_SINK_URL, type SinkWindowInput } from "./types.js";
|
|
9
|
+
|
|
10
|
+
/** The part of `fetch` that the procedures use. `createHostFetch()` of `@procedures/http` fits this type. */
|
|
11
|
+
export type SinkFetch = (
|
|
12
|
+
url: string,
|
|
13
|
+
init: { readonly headers: Readonly<Record<string, string>>; readonly signal: AbortSignal },
|
|
14
|
+
) => Promise<{ readonly status: number; text(): Promise<string> }>;
|
|
15
|
+
|
|
16
|
+
/** The hosts of the default fetch: the sink on the loopback addresses of this computer, at its port 4328 */
|
|
17
|
+
export const SINK_HOSTS: readonly string[] = ["127.0.0.1:4328", "localhost:4328", "[::1]:4328"];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* This function gives the default fetch of the procedures. The host policy of `@procedures/http` lets it reach only
|
|
21
|
+
* `SINK_HOSTS`, and no public host. Thus the input `baseUrl` cannot send a request to another server.
|
|
22
|
+
*/
|
|
23
|
+
export function defaultSinkFetch(): SinkFetch {
|
|
24
|
+
return createHostFetch({ allow: SINK_HOSTS, publicHosts: false });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The context of a handler: the fetch of the host and the signal of the call */
|
|
28
|
+
export interface SinkContext {
|
|
29
|
+
readonly fetch: SinkFetch;
|
|
30
|
+
readonly signal?: AbortSignal | undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** This function gives the window of the input of a window procedure. */
|
|
34
|
+
export function windowOf(input: SinkWindowInput): SinkWindow {
|
|
35
|
+
return { since: input.since, limit: input.limit, service: input.service };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The settings of one query: the base URL, the time limit and the signal of the caller */
|
|
39
|
+
export interface SinkCallOptions {
|
|
40
|
+
readonly baseUrl?: string | undefined;
|
|
41
|
+
readonly timeoutMs: number;
|
|
42
|
+
readonly signal?: AbortSignal | undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* This function sends one query to the sink and reads its answer. The builder examines the input before the
|
|
47
|
+
* request. A refusal of the host policy keeps its code and its reason.
|
|
48
|
+
*/
|
|
49
|
+
export async function querySink<R extends SinkRoute>(
|
|
50
|
+
fetch: SinkFetch,
|
|
51
|
+
query: SinkQuery & { readonly route: R },
|
|
52
|
+
options: SinkCallOptions,
|
|
53
|
+
): Promise<SinkAnswers[R]> {
|
|
54
|
+
const baseUrl = options.baseUrl ?? DEFAULT_SINK_URL;
|
|
55
|
+
const request = sinkRequest(query, baseUrl);
|
|
56
|
+
const signals = [AbortSignal.timeout(options.timeoutMs), ...(options.signal ? [options.signal] : [])];
|
|
57
|
+
let status: number;
|
|
58
|
+
let text: string;
|
|
59
|
+
try {
|
|
60
|
+
const response = await fetch(request.url, { headers: request.headers, signal: AbortSignal.any(signals) });
|
|
61
|
+
status = response.status;
|
|
62
|
+
text = await response.text();
|
|
63
|
+
} catch (error) {
|
|
64
|
+
if (isHostNotAllowed(error)) throw error;
|
|
65
|
+
const reason = (error as { name?: string }).name === "TimeoutError" ? `no answer in ${options.timeoutMs} ms` : "the request failed";
|
|
66
|
+
throw new Error(`The sink at ${baseUrl} did not answer: ${reason}. Is the sink started?`, { cause: error });
|
|
67
|
+
}
|
|
68
|
+
return readSinkAnswer(query.route, status, text);
|
|
69
|
+
}
|
package/src/index.ts
ADDED
package/src/module.ts
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
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
|
+
|
|
7
|
+
import {
|
|
8
|
+
createProcedure,
|
|
9
|
+
defineModule,
|
|
10
|
+
registerModules,
|
|
11
|
+
PROCEDURE_REGISTRY,
|
|
12
|
+
type AnyProcedure,
|
|
13
|
+
type Effects,
|
|
14
|
+
type ProcedureContext,
|
|
15
|
+
type ProcedureModule,
|
|
16
|
+
type ProcedureRegistry,
|
|
17
|
+
} from "@procedures/client";
|
|
18
|
+
import { defaultSinkFetch, type SinkContext, type SinkFetch } from "./call.js";
|
|
19
|
+
import { sinkErrors } from "./procedures/sink/errors.js";
|
|
20
|
+
import { sinkServices } from "./procedures/sink/services.js";
|
|
21
|
+
import { sinkSlowest } from "./procedures/sink/slowest.js";
|
|
22
|
+
import { sinkStats } from "./procedures/sink/stats.js";
|
|
23
|
+
import { sinkTrace } from "./procedures/sink/trace.js";
|
|
24
|
+
import {
|
|
25
|
+
SinkErrorsInputSchema,
|
|
26
|
+
SinkErrorsOutputSchema,
|
|
27
|
+
SinkServicesInputSchema,
|
|
28
|
+
SinkServicesOutputSchema,
|
|
29
|
+
SinkSlowestInputSchema,
|
|
30
|
+
SinkSlowestOutputSchema,
|
|
31
|
+
SinkStatsInputSchema,
|
|
32
|
+
SinkStatsOutputSchema,
|
|
33
|
+
SinkTraceInputSchema,
|
|
34
|
+
SinkTraceOutputSchema,
|
|
35
|
+
type SinkErrorsInput,
|
|
36
|
+
type SinkErrorsOutput,
|
|
37
|
+
type SinkServicesInput,
|
|
38
|
+
type SinkServicesOutput,
|
|
39
|
+
type SinkSlowestInput,
|
|
40
|
+
type SinkSlowestOutput,
|
|
41
|
+
type SinkStatsInput,
|
|
42
|
+
type SinkStatsOutput,
|
|
43
|
+
type SinkTraceInput,
|
|
44
|
+
type SinkTraceOutput,
|
|
45
|
+
} from "./types.js";
|
|
46
|
+
|
|
47
|
+
/** The name of the module */
|
|
48
|
+
const NAME = "@procedures/sink";
|
|
49
|
+
|
|
50
|
+
/** The effects of each procedure: a read over the network, and no write. The sink is idempotent for each query. */
|
|
51
|
+
export const SINK_EFFECTS: Effects = { reads: ["network"], writes: [], idempotent: true };
|
|
52
|
+
|
|
53
|
+
/** The settings of the host for the procedures of this package. The procedure input cannot change them. */
|
|
54
|
+
export interface SinkProceduresOptions {
|
|
55
|
+
/**
|
|
56
|
+
* The function that sends the request. The default is `defaultSinkFetch()`: only the sink on the loopback
|
|
57
|
+
* addresses, at port 4328. A host with a sink on another port gives `createHostFetch({ allow: ["127.0.0.1:4400"],
|
|
58
|
+
* publicHosts: false })`.
|
|
59
|
+
*/
|
|
60
|
+
readonly fetch?: SinkFetch | undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** This function gives the procedures of this package with the settings of the host. */
|
|
64
|
+
export function createSinkProcedures(options: SinkProceduresOptions = {}): AnyProcedure[] {
|
|
65
|
+
const fetch = options.fetch ?? defaultSinkFetch();
|
|
66
|
+
const context = (ctx: ProcedureContext | undefined): SinkContext => ({ fetch, signal: ctx?.signal });
|
|
67
|
+
|
|
68
|
+
const slowest = createProcedure()
|
|
69
|
+
.path(["sink", "slowest"])
|
|
70
|
+
.input(SinkSlowestInputSchema)
|
|
71
|
+
.output(SinkSlowestOutputSchema)
|
|
72
|
+
.meta({
|
|
73
|
+
description: "Give the slowest spans of a window from the OTLP sink, the slowest first",
|
|
74
|
+
output: "json",
|
|
75
|
+
effects: SINK_EFFECTS,
|
|
76
|
+
})
|
|
77
|
+
.handler(async (input: SinkSlowestInput, ctx?: ProcedureContext): Promise<SinkSlowestOutput> => sinkSlowest(input, context(ctx)))
|
|
78
|
+
.build();
|
|
79
|
+
|
|
80
|
+
const errors = createProcedure()
|
|
81
|
+
.path(["sink", "errors"])
|
|
82
|
+
.input(SinkErrorsInputSchema)
|
|
83
|
+
.output(SinkErrorsOutputSchema)
|
|
84
|
+
.meta({
|
|
85
|
+
description: "Give the failed spans and the error logs of a window from the OTLP sink, the newest first",
|
|
86
|
+
output: "json",
|
|
87
|
+
effects: SINK_EFFECTS,
|
|
88
|
+
})
|
|
89
|
+
.handler(async (input: SinkErrorsInput, ctx?: ProcedureContext): Promise<SinkErrorsOutput> => sinkErrors(input, context(ctx)))
|
|
90
|
+
.build();
|
|
91
|
+
|
|
92
|
+
const services = createProcedure()
|
|
93
|
+
.path(["sink", "services"])
|
|
94
|
+
.input(SinkServicesInputSchema)
|
|
95
|
+
.output(SinkServicesOutputSchema)
|
|
96
|
+
.meta({
|
|
97
|
+
description: "Give the span counts and the error rate of each service in a window from the OTLP sink",
|
|
98
|
+
output: "json",
|
|
99
|
+
effects: SINK_EFFECTS,
|
|
100
|
+
})
|
|
101
|
+
.handler(async (input: SinkServicesInput, ctx?: ProcedureContext): Promise<SinkServicesOutput> => sinkServices(input, context(ctx)))
|
|
102
|
+
.build();
|
|
103
|
+
|
|
104
|
+
const trace = createProcedure()
|
|
105
|
+
.path(["sink", "trace"])
|
|
106
|
+
.input(SinkTraceInputSchema)
|
|
107
|
+
.output(SinkTraceOutputSchema)
|
|
108
|
+
.meta({
|
|
109
|
+
description: "Give the spans and the logs of one trace from the OTLP sink, in the order of their times",
|
|
110
|
+
args: ["id"],
|
|
111
|
+
output: "json",
|
|
112
|
+
effects: SINK_EFFECTS,
|
|
113
|
+
})
|
|
114
|
+
.handler(async (input: SinkTraceInput, ctx?: ProcedureContext): Promise<SinkTraceOutput> => sinkTrace(input, context(ctx)))
|
|
115
|
+
.build();
|
|
116
|
+
|
|
117
|
+
const stats = createProcedure()
|
|
118
|
+
.path(["sink", "stats"])
|
|
119
|
+
.input(SinkStatsInputSchema)
|
|
120
|
+
.output(SinkStatsOutputSchema)
|
|
121
|
+
.meta({
|
|
122
|
+
description: "Give how much telemetry the OTLP sink stores: the rows and bytes of each signal, the file size and the retention rules",
|
|
123
|
+
output: "json",
|
|
124
|
+
effects: SINK_EFFECTS,
|
|
125
|
+
})
|
|
126
|
+
.handler(async (input: SinkStatsInput, ctx?: ProcedureContext): Promise<SinkStatsOutput> => sinkStats(input, context(ctx)))
|
|
127
|
+
.build();
|
|
128
|
+
|
|
129
|
+
return [slowest, errors, services, trace, stats];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export const sinkModule: ProcedureModule = defineModule(NAME, createSinkProcedures());
|
|
133
|
+
|
|
134
|
+
export default sinkModule;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* This function gives the module with the settings of the host: the `fetch` of the requests. Without options, it
|
|
138
|
+
* gives `sinkModule`.
|
|
139
|
+
*/
|
|
140
|
+
export function createSinkModule(options: SinkProceduresOptions = {}): ProcedureModule {
|
|
141
|
+
if (options.fetch === undefined) return sinkModule;
|
|
142
|
+
return defineModule(NAME, createSinkProcedures(options));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* This function registers the procedures of @procedures/sink in a registry. The default is the global registry.
|
|
147
|
+
*/
|
|
148
|
+
export function registerSinkProcedures(registry: ProcedureRegistry = PROCEDURE_REGISTRY): void {
|
|
149
|
+
registerModules(registry, [sinkModule]);
|
|
150
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The procedure `sink.errors`: the query `GET /api/errors` of the sink.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { querySink, windowOf, type SinkContext } from "../../call.js";
|
|
6
|
+
import type { SinkErrorsInput, SinkErrorsOutput } from "../../types.js";
|
|
7
|
+
|
|
8
|
+
/** This function gives the spans with the status error and the error logs of the window, the newest first. */
|
|
9
|
+
export async function sinkErrors(input: SinkErrorsInput, context: SinkContext): Promise<SinkErrorsOutput> {
|
|
10
|
+
return querySink(context.fetch, { route: "errors", window: windowOf(input) }, { ...input, signal: context.signal });
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The procedure `sink.services`: the query `GET /api/services` of the sink.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { querySink, windowOf, type SinkContext } from "../../call.js";
|
|
6
|
+
import type { SinkServicesInput, SinkServicesOutput } from "../../types.js";
|
|
7
|
+
|
|
8
|
+
/** This function gives the span counts of each service in the window, the most failed spans first. */
|
|
9
|
+
export async function sinkServices(input: SinkServicesInput, context: SinkContext): Promise<SinkServicesOutput> {
|
|
10
|
+
return querySink(context.fetch, { route: "services", window: windowOf(input) }, { ...input, signal: context.signal });
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The procedure `sink.slowest`: the query `GET /api/slowest` of the sink.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { querySink, windowOf, type SinkContext } from "../../call.js";
|
|
6
|
+
import type { SinkSlowestInput, SinkSlowestOutput } from "../../types.js";
|
|
7
|
+
|
|
8
|
+
/** This function gives the slowest spans that start in the window, the slowest first. */
|
|
9
|
+
export async function sinkSlowest(input: SinkSlowestInput, context: SinkContext): Promise<SinkSlowestOutput> {
|
|
10
|
+
return querySink(context.fetch, { route: "slowest", window: windowOf(input) }, { ...input, signal: context.signal });
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The procedure `sink.stats`: the query `GET /api/stats` of the sink.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { querySink, type SinkContext } from "../../call.js";
|
|
6
|
+
import type { SinkStatsInput, SinkStatsOutput } from "../../types.js";
|
|
7
|
+
|
|
8
|
+
/** This function gives the rows and the bytes of each signal, the bytes of the file and the retention rules. */
|
|
9
|
+
export async function sinkStats(input: SinkStatsInput, context: SinkContext): Promise<SinkStatsOutput> {
|
|
10
|
+
return querySink(context.fetch, { route: "stats" }, { ...input, signal: context.signal });
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The procedure `sink.trace`: the query `GET /api/trace` of the sink.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { querySink, type SinkContext } from "../../call.js";
|
|
6
|
+
import type { SinkTraceInput, SinkTraceOutput } from "../../types.js";
|
|
7
|
+
|
|
8
|
+
/** This function gives the spans and the logs of one trace, in the order of their times. */
|
|
9
|
+
export async function sinkTrace(input: SinkTraceInput, context: SinkContext): Promise<SinkTraceOutput> {
|
|
10
|
+
return querySink(context.fetch, { route: "trace", id: input.id }, { ...input, signal: context.signal });
|
|
11
|
+
}
|
package/src/register.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An import of this file registers the procedures of @procedures/sink in the global registry:
|
|
3
|
+
* `import "@procedures/sink/register"`. The package root registers nothing.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { registerSinkProcedures } from "./module.js";
|
|
7
|
+
|
|
8
|
+
export * from "./module.js";
|
|
9
|
+
|
|
10
|
+
registerSinkProcedures();
|