@mark1russell7/otel-sink 0.0.0-stage → 0.1.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/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ ## 0.1.1
4
+
5
+ ### Fixed
6
+
7
+ - Version 0.1.0 has no `dist` folder, so its entry points and the command `otel-sink` do not work. The build of the repository did not start the build of this package. Use 0.1.1 or later.
8
+
9
+ ## 0.1.0
10
+
11
+ ### Added
12
+
13
+ - The first release. `@mark1russell7/otel-sink` is an OTLP/HTTP receiver for one computer with no collector. It keeps spans, logs and metric points in one SQLite file.
14
+ - The receiver: `POST /v1/traces`, `/v1/logs` and `/v1/metrics` take OTLP JSON, with gzip or without it. A protobuf body gets 415, a body larger than 8 MiB gets 413, and a body that is not OTLP gets 400.
15
+ - The retention rules (`RetentionRules`): a maximum age and a maximum size for each signal, and a hard limit of 1 GiB on the sum. The store caps a signal at each write, and a worker prunes in bounded parts.
16
+ - The queries: `GET /api/slowest`, `/api/errors`, `/api/trace`, `/api/services` and `/api/stats`.
17
+ - The command `otel-sink`, with the environment variables `PORT`, `PROBE_PORT`, `SERVICE_PROFILE` and `SINK_DB`. It refuses to start when its own OTLP endpoint is its receiver.
18
+ - The plain functions `openStore`, `spanRows`, `logRows`, `pointRows`, `slowest`, `errors`, `trace` and `services`, and `declare(options)` for the service.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 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,67 @@
1
- # Temporary Holding Version
1
+ # @mark1russell7/otel-sink
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A small OTLP/HTTP receiver for one computer. It keeps spans, logs and metric points in one SQLite file, with retention rules. It answers questions about them over HTTP: the slowest spans, the errors, one trace with its logs, and the counts of each service.
4
+
5
+ The sink is for a development computer or a small host with no collector. It is not a replacement for a collector and a store, for example Tempo, Loki or Prometheus.
6
+
7
+ ## Start it
8
+
9
+ ```sh
10
+ npm install --global @mark1russell7/otel-sink
11
+ SINK_DB=~/otel-sink.db PORT=4318 otel-sink
12
+ ```
13
+
14
+ The environment gives these values. The value in parentheses is the value without the variable.
15
+
16
+ - `PORT` (4318): the port of the receiver and of the queries.
17
+ - `PROBE_PORT` (9090): the port of `/startup`, `/readiness`, `/liveness` and `/report`.
18
+ - `SERVICE_PROFILE` (`local`): the profile of the shutdown, `local` or `pod`.
19
+ - `SINK_DB` (`otel-sink.db` in the home folder): the SQLite file. The sink makes its folder when the folder is not there.
20
+ - `OTEL_EXPORTER_OTLP_ENDPOINT`: the endpoint of the telemetry of the sink itself. The sink refuses to start when it is its own receiver, because each export then makes a new export.
21
+
22
+ Point a sender at the sink:
23
+
24
+ ```sh
25
+ OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318
26
+ OTEL_EXPORTER_OTLP_PROTOCOL=http/json
27
+ ```
28
+
29
+ ## The receiver
30
+
31
+ - `POST /v1/traces`, `POST /v1/logs` and `POST /v1/metrics` take a body of OTLP JSON, with gzip or without it.
32
+ - **Only JSON.** The default protocol of the OTLP exporters is `http/protobuf`. The sink answers it with 415, and the body of the answer names the setting `OTEL_EXPORTER_OTLP_PROTOCOL=http/json`.
33
+ - **The limits.** A body larger than 8 MiB gets 413. A body that is not OTLP JSON gets 400. The sink reads and drops the rest of a refused body, thus the sender reads the answer and gets no reset.
34
+ - **The metrics.** The sink keeps the points of sums, gauges and histograms. It keeps the count, the sum and the buckets of each histogram point.
35
+
36
+ ## The queries
37
+
38
+ Each query takes `since` (a time in milliseconds since the epoch, the last hour by default), `limit` (from 1 to 1000, 50 by default) and `service`. Each time in a result is in milliseconds since the epoch. A bad parameter gets 400 with a list of the problems.
39
+
40
+ | Route | Result |
41
+ | --- | --- |
42
+ | `GET /api/slowest` | The slowest spans of the window, the slowest first |
43
+ | `GET /api/errors` | The spans with the status error and the logs from the severity ERROR (17), the newest first |
44
+ | `GET /api/trace?id=<trace id>` | The spans and the logs of one trace, in the order of their times |
45
+ | `GET /api/services` | For each service: the count of spans, the count of failed spans, the error rate and the slowest span |
46
+ | `GET /api/stats` | The rows and the bytes of each signal, the size of the file, and the retention rules |
47
+
48
+ ## Retention
49
+
50
+ The retention rules are data (`RetentionRules`). Each signal has a maximum age and a maximum size.
51
+
52
+ - **The defaults.** Spans and logs: 3 days and 256 MiB each. Metric points: 7 days and 128 MiB.
53
+ - **The hard limit.** The sum of the sizes must be 1 GiB or less. `openStore` refuses rules with problems, and `retentionProblems` gives those problems.
54
+ - **The size of a row.** The store counts the bytes of the text of each row, 8 bytes for each number and 32 bytes for the row. Triggers keep the sum of each signal in the table `sizes`.
55
+ - **The cap at each write.** After each export, the store deletes the oldest rows of a signal that is past its size. Thus a signal goes past its size by one export at most.
56
+ - **The prune.** A worker deletes the rows past their age each minute, and the oldest rows past each size. Each delete is one transaction of at most 5000 rows. Each part of a prune does at most 20 deletes, then the intake continues.
57
+ - **The file.** The file uses `auto_vacuum = INCREMENTAL`, and each prune gives the free pages back to the disk.
58
+
59
+ ## As a library
60
+
61
+ `openStore`, the decoders (`spanRows`, `logRows`, `pointRows`) and the queries (`slowest`, `errors`, `trace`, `services`) are plain functions. `declare(options)` gives the service with its receiver, its queries and its retention worker, and it starts nothing.
62
+
63
+ ```ts
64
+ import { declare, PROFILES } from "@mark1russell7/otel-sink";
65
+
66
+ const life = await declare({ port: 4318, probePort: 9090, profile: PROFILES["local"]!, path: "sink.db" }).run();
67
+ ```
package/dist/api.d.ts ADDED
@@ -0,0 +1,37 @@
1
+ import type { ServiceBuilder } from "@mark1russell7/service";
2
+ import { type RequestHandler } from "@mark1russell7/service/http";
3
+ import { type Window } from "./query.ts";
4
+ import type { Store } from "./store.ts";
5
+ /** These are the options of the API of the service. */
6
+ export interface ApiOptions {
7
+ /** This number is the port of the API. */
8
+ readonly port: number;
9
+ /** This function gives the store. It throws before the store opens. */
10
+ readonly store: () => Store;
11
+ /** This function gives the time in milliseconds since the epoch. The default is `Date.now`. */
12
+ readonly now?: (() => number) | undefined;
13
+ }
14
+ /** The most bytes of the body of one export, after gzip: 8 MiB */
15
+ export declare const MAX_BODY_BYTES: number;
16
+ /** The window of a query without `since`: the last hour */
17
+ export declare const DEFAULT_WINDOW_MS: number;
18
+ /** The most rows of a query without `limit` */
19
+ export declare const DEFAULT_LIMIT = 50;
20
+ /** The routes of the queries. Each one is idempotent. */
21
+ export declare const QUERY_ROUTES: readonly ["GET /api/slowest", "GET /api/errors", "GET /api/services", "GET /api/trace", "GET /api/stats"];
22
+ /** The most bytes that the receiver reads and drops after a refusal. Past it, the receiver closes the connection. */
23
+ export declare const MAX_DISCARD_BYTES: number;
24
+ /**
25
+ * This function gives the handler of the OTLP/HTTP receiver: `POST /v1/traces`, `/v1/logs` and `/v1/metrics` with a
26
+ * body of OTLP JSON, with gzip or without it. It answers 415 to another type, 413 to a large body and 400 to a body
27
+ * that is not OTLP JSON. The handler writes its own status, so it is a mount of the transport, not a route of `handle`.
28
+ */
29
+ export declare const receiver: (store: () => Store) => RequestHandler;
30
+ /** This function gives the window of a query from the query string of its target. */
31
+ export declare const windowOf: (url: string | undefined, nowMs: number) => Window;
32
+ /**
33
+ * This function gives the API to the service of the entry: an HTTP server with the OTLP/HTTP receiver and the
34
+ * queries. The API is a component. It does not start the server, and it does not use `run()`.
35
+ */
36
+ export declare const attachApi: (svc: ServiceBuilder, options: ApiOptions) => ServiceBuilder;
37
+ //# sourceMappingURL=api.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC7D,OAAO,EAAiC,KAAK,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAEjG,OAAO,EAA8D,KAAK,MAAM,EAAE,MAAM,YAAY,CAAC;AACrG,OAAO,KAAK,EAAQ,KAAK,EAAE,MAAM,YAAY,CAAC;AAI9C,uDAAuD;AACvD,MAAM,WAAW,UAAU;IACzB,0CAA0C;IAC1C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,uEAAuE;IACvE,QAAQ,CAAC,KAAK,EAAE,MAAM,KAAK,CAAC;IAC5B,+FAA+F;IAC/F,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,MAAM,MAAM,CAAC,GAAG,SAAS,CAAC;CAC3C;AAED,kEAAkE;AAClE,eAAO,MAAM,cAAc,EAAE,MAAwB,CAAC;AAStD,2DAA2D;AAC3D,eAAO,MAAM,iBAAiB,EAAE,MAAuB,CAAC;AACxD,+CAA+C;AAC/C,eAAO,MAAM,aAAa,KAAK,CAAC;AAEhC,yDAAyD;AACzD,eAAO,MAAM,YAAY,2GAA4G,CAAC;AAatI,qHAAqH;AACrH,eAAO,MAAM,iBAAiB,EAAE,MAA2B,CAAC;AA8C5D;;;;GAIG;AACH,eAAO,MAAM,QAAQ,GAAI,OAAO,MAAM,KAAK,KAAG,cA+B7C,CAAC;AAEF,qFAAqF;AACrF,eAAO,MAAM,QAAQ,GAAI,KAAK,MAAM,GAAG,SAAS,EAAE,OAAO,MAAM,KAAG,MAUjE,CAAC;AAmBF;;;GAGG;AACH,eAAO,MAAM,SAAS,GAAI,KAAK,cAAc,EAAE,SAAS,UAAU,KAAG,cAyBpE,CAAC"}
package/dist/api.js ADDED
@@ -0,0 +1,169 @@
1
+ import { createServer } from "node:http";
2
+ import { gunzip } from "node:zlib";
3
+ import { promisify } from "node:util";
4
+ import { handle, httpTransport, pathOf } from "@mark1russell7/service/http";
5
+ import { logRows, OtlpShapeError, pointRows, spanRows } from "./otlp.js";
6
+ import { errors, services, slowest, trace, TRACE_ID, windowProblems } from "./query.js";
7
+ const unzip = promisify(gunzip);
8
+ /** The most bytes of the body of one export, after gzip: 8 MiB */
9
+ export const MAX_BODY_BYTES = 8 * 1024 * 1024;
10
+ /** The paths of the OTLP/HTTP receiver, and the rows of each body */
11
+ const RECEIVERS = {
12
+ "/v1/traces": (body) => ({ spans: spanRows(body) }),
13
+ "/v1/logs": (body) => ({ logs: logRows(body) }),
14
+ "/v1/metrics": (body) => ({ points: pointRows(body) }),
15
+ };
16
+ /** The window of a query without `since`: the last hour */
17
+ export const DEFAULT_WINDOW_MS = 60 * 60 * 1000;
18
+ /** The most rows of a query without `limit` */
19
+ export const DEFAULT_LIMIT = 50;
20
+ /** The routes of the queries. Each one is idempotent. */
21
+ export const QUERY_ROUTES = ["GET /api/slowest", "GET /api/errors", "GET /api/services", "GET /api/trace", "GET /api/stats"];
22
+ const s = (value) => ({ value, unit: "s" });
23
+ const reply = (res, status, body) => {
24
+ const text = JSON.stringify(body);
25
+ res.writeHead(status, { "content-type": "application/json", "content-length": Buffer.byteLength(text) });
26
+ res.end(text);
27
+ return true;
28
+ };
29
+ class TooLargeError extends Error {
30
+ }
31
+ /** The most bytes that the receiver reads and drops after a refusal. Past it, the receiver closes the connection. */
32
+ export const MAX_DISCARD_BYTES = 8 * MAX_BODY_BYTES;
33
+ /**
34
+ * This function reads the rest of a body and drops it, up to `MAX_DISCARD_BYTES`. Thus the client reads the refusal
35
+ * before the connection closes, and it does not get a reset. Past that size, it destroys the connection.
36
+ */
37
+ const discard = (req, already = 0) => new Promise((resolve) => {
38
+ if (req.readableEnded)
39
+ return resolve();
40
+ let size = already;
41
+ req.removeAllListeners("data");
42
+ req.on("data", (part) => {
43
+ size += part.length;
44
+ if (size > MAX_DISCARD_BYTES) {
45
+ req.destroy();
46
+ resolve();
47
+ }
48
+ });
49
+ req.once("end", () => resolve());
50
+ req.once("close", () => resolve());
51
+ req.once("error", () => resolve());
52
+ });
53
+ /** This function reads a body up to `MAX_BODY_BYTES`. Past that size, it drops the rest and rejects with a `TooLargeError`. */
54
+ const readBody = (req) => new Promise((resolve, reject) => {
55
+ const declared = Number(req.headers["content-length"] ?? 0);
56
+ if (declared > MAX_DISCARD_BYTES) {
57
+ req.destroy();
58
+ return reject(new TooLargeError());
59
+ }
60
+ if (declared > MAX_BODY_BYTES)
61
+ return void discard(req).then(() => reject(new TooLargeError()));
62
+ const parts = [];
63
+ let size = 0;
64
+ const onData = (part) => {
65
+ size += part.length;
66
+ if (size <= MAX_BODY_BYTES)
67
+ return void parts.push(part);
68
+ req.off("end", onEnd);
69
+ void discard(req, size).then(() => reject(new TooLargeError()));
70
+ };
71
+ const onEnd = () => resolve(Buffer.concat(parts));
72
+ req.on("data", onData);
73
+ req.once("end", onEnd);
74
+ req.once("error", reject);
75
+ });
76
+ /**
77
+ * This function gives the handler of the OTLP/HTTP receiver: `POST /v1/traces`, `/v1/logs` and `/v1/metrics` with a
78
+ * body of OTLP JSON, with gzip or without it. It answers 415 to another type, 413 to a large body and 400 to a body
79
+ * that is not OTLP JSON. The handler writes its own status, so it is a mount of the transport, not a route of `handle`.
80
+ */
81
+ export const receiver = (store) => {
82
+ const handler = async (req, res) => {
83
+ const toRows = req.method === "POST" ? RECEIVERS[pathOf(req.url)] : undefined;
84
+ if (toRows === undefined)
85
+ return false;
86
+ const type = String(req.headers["content-type"] ?? "").split(";")[0]?.trim().toLowerCase();
87
+ if (type !== "application/json") {
88
+ await discard(req);
89
+ return reply(res, 415, { error: "the sink takes only OTLP JSON. Set OTEL_EXPORTER_OTLP_PROTOCOL=http/json in the sender." });
90
+ }
91
+ const encoding = String(req.headers["content-encoding"] ?? "identity").toLowerCase();
92
+ if (encoding !== "identity" && encoding !== "gzip") {
93
+ await discard(req);
94
+ return reply(res, 415, { error: `the sink takes no content encoding ${encoding}: only gzip or none` });
95
+ }
96
+ let rows;
97
+ try {
98
+ const raw = await readBody(req);
99
+ const text = encoding === "gzip" ? await unzip(raw, { maxOutputLength: MAX_BODY_BYTES }) : raw;
100
+ rows = toRows(JSON.parse(text.toString("utf8")));
101
+ }
102
+ catch (error) {
103
+ if (error instanceof TooLargeError || error.code === "ERR_BUFFER_TOO_LARGE")
104
+ return reply(res, 413, { error: `the body is larger than ${MAX_BODY_BYTES} bytes` });
105
+ if (error instanceof SyntaxError || error instanceof OtlpShapeError || error.code === "Z_DATA_ERROR")
106
+ return reply(res, 400, { error: error instanceof Error ? error.message : String(error) });
107
+ throw error;
108
+ }
109
+ store().insert(rows);
110
+ // The success of OTLP/HTTP JSON: an empty response message, thus no partial success
111
+ return reply(res, 200, {});
112
+ };
113
+ return handler;
114
+ };
115
+ /** This function gives the window of a query from the query string of its target. */
116
+ export const windowOf = (url, nowMs) => {
117
+ const query = new URL(url ?? "/", "http://sink").searchParams;
118
+ const since = query.get("since");
119
+ const limit = query.get("limit");
120
+ const service = query.get("service");
121
+ return {
122
+ sinceMs: since === null ? nowMs - DEFAULT_WINDOW_MS : Number(since),
123
+ limit: limit === null ? DEFAULT_LIMIT : Number(limit),
124
+ ...(service === null || service === "" ? {} : { service }),
125
+ };
126
+ };
127
+ const traceIdOf = (url) => new URL(url ?? "/", "http://sink").searchParams.get("id")?.toLowerCase() ?? "";
128
+ /**
129
+ * This function gives the handler that answers 400 to a query with bad parameters. It gives false for each other
130
+ * request, so the next handler gets it.
131
+ */
132
+ const queryCheck = (now) => {
133
+ const handler = (req, res) => {
134
+ if (req.method !== "GET")
135
+ return false;
136
+ const path = pathOf(req.url);
137
+ if (!QUERY_ROUTES.includes(`GET ${path}`))
138
+ return false;
139
+ const problems = path === "/api/trace" ? (TRACE_ID.test(traceIdOf(req.url)) ? [] : ["id must be a trace id of 32 hex digits"]) : windowProblems(windowOf(req.url, now()));
140
+ return problems.length === 0 ? false : reply(res, 400, { problems });
141
+ };
142
+ return handler;
143
+ };
144
+ /**
145
+ * This function gives the API to the service of the entry: an HTTP server with the OTLP/HTTP receiver and the
146
+ * queries. The API is a component. It does not start the server, and it does not use `run()`.
147
+ */
148
+ export const attachApi = (svc, options) => {
149
+ const now = options.now ?? Date.now;
150
+ const store = options.store;
151
+ // The transport has no defaults: the idle close, the sweep of idle connections and the Retry-After of each 503.
152
+ const http = httpTransport(createServer(), { server_idle_close: s(65), server_idle_sweep_interval: s(1), server_retry_after: s(1) }, { listen: { port: options.port } });
153
+ http.mount(receiver(store));
154
+ http.mount(queryCheck(now));
155
+ // Each query goes through the service. Thus the drain waits for it, and the shutdown can cut it.
156
+ http.mount(handle("api", "GET /api/slowest", (req) => slowest(store().db, windowOf(req.url, now()))));
157
+ http.mount(handle("api", "GET /api/errors", (req) => errors(store().db, windowOf(req.url, now()))));
158
+ http.mount(handle("api", "GET /api/services", (req) => services(store().db, windowOf(req.url, now()))));
159
+ http.mount(handle("api", "GET /api/trace", (req) => trace(store().db, traceIdOf(req.url))));
160
+ http.mount(handle("api", "GET /api/stats", () => {
161
+ const opened = store();
162
+ return { sizes: opened.sizes(), fileBytes: opened.fileBytes(), rules: opened.rules };
163
+ }));
164
+ const api = svc.controller("api").requires("store");
165
+ for (const route of QUERY_ROUTES)
166
+ api.route(route, { idempotent: true });
167
+ return svc.server("http", http.knobs, http);
168
+ };
169
+ //# sourceMappingURL=api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAA6C,MAAM,WAAW,CAAC;AACpF,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAuB,MAAM,6BAA6B,CAAC;AACjG,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,QAAQ,EAA0D,MAAM,WAAW,CAAC;AACjI,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,cAAc,EAAe,MAAM,YAAY,CAAC;AAGrG,MAAM,KAAK,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC;AAYhC,kEAAkE;AAClE,MAAM,CAAC,MAAM,cAAc,GAAW,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;AAEtD,qEAAqE;AACrE,MAAM,SAAS,GAAsD;IACnE,YAAY,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,QAAQ,CAAC,IAAoB,CAAC,EAAE,CAAC;IACnE,UAAU,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,IAAkB,CAAC,EAAE,CAAC;IAC7D,aAAa,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,IAAqB,CAAC,EAAE,CAAC;CACxE,CAAC;AAEF,2DAA2D;AAC3D,MAAM,CAAC,MAAM,iBAAiB,GAAW,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AACxD,+CAA+C;AAC/C,MAAM,CAAC,MAAM,aAAa,GAAG,EAAE,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KAAK,EAAE,CAAC;IACvF,CAAC,CAAC,CACH,CAAC;IACF,MAAM,GAAG,GAAG,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACpD,KAAK,MAAM,KAAK,IAAI,YAAY;QAAE,GAAG,CAAC,KAAK,CAAC,KAAK,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC;IACzE,OAAO,GAAG,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;AAC9C,CAAC,CAAC"}
@@ -0,0 +1,46 @@
1
+ import type { Profile, ServiceBuilder } from "@mark1russell7/service";
2
+ import type { RetentionRules } from "./retention.ts";
3
+ export { attachApi, receiver, windowOf, MAX_BODY_BYTES, QUERY_ROUTES, type ApiOptions } from "./api.ts";
4
+ export * from "./otlp.ts";
5
+ export * from "./query.ts";
6
+ export * from "./retention.ts";
7
+ export * from "./store.ts";
8
+ /**
9
+ * These are the profiles of the shutdown, by name. `local` is for the computer of a developer.
10
+ * `pod` is for a pod of Kubernetes with a grace of 30 s and no preStop hook. Make `grace` equal to
11
+ * the `terminationGracePeriodSeconds` of the pod.
12
+ */
13
+ export declare const PROFILES: Readonly<Record<string, Profile>>;
14
+ /**
15
+ * This function tells if an OTLP endpoint is the receiver of this sink. A sink that exports its own spans to itself
16
+ * makes new spans at each export, without an end.
17
+ */
18
+ export declare const isOwnEndpoint: (endpoint: string | undefined, port: number) => boolean;
19
+ /** The most deletes of one iteration of the retention worker. Each delete is one transaction of at most 5000 rows. */
20
+ export declare const PRUNE_BATCHES = 20;
21
+ /** These are the options of the service. */
22
+ export interface Options {
23
+ /** This number is the port of the API. */
24
+ readonly port: number;
25
+ /** This number is the port of the probes: `/startup`, `/readiness`, `/liveness` and `/report`. */
26
+ readonly probePort: number;
27
+ /** This profile gives the phases of the shutdown and the effect of a second signal. */
28
+ readonly profile: Profile;
29
+ /** This path is the SQLite file of the store, or `:memory:`. */
30
+ readonly path: string;
31
+ /** These rules give the age and the size of each signal. The default is `DEFAULT_RETENTION`. */
32
+ readonly rules?: RetentionRules | undefined;
33
+ /** This time is the sleep of the retention worker between two prunes, in seconds. The default is 60. */
34
+ readonly pruneEverySeconds?: number | undefined;
35
+ /** This function gives the time in milliseconds since the epoch. The default is `Date.now`. */
36
+ readonly now?: (() => number) | undefined;
37
+ /** This function gets each failure of a prune. The default does nothing: the next prune tries again after its backoff. */
38
+ readonly onPruneError?: ((error: unknown, failures: number) => void) | undefined;
39
+ }
40
+ /**
41
+ * This function declares the one service of the process. It starts nothing. `describe()` gives the
42
+ * declaration as data, and `lint()` examines it. Only the entry (`main.ts`) uses `run()`.
43
+ * The store is a dependency: it opens before the servers start, and it closes after they stop.
44
+ */
45
+ export declare const declare: (options: Options) => ServiceBuilder;
46
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAGtE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAGrD,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,cAAc,EAAE,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,UAAU,CAAC;AACxG,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAK3B;;;;GAIG;AACH,eAAO,MAAM,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAGtD,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,aAAa,GAAI,UAAU,MAAM,GAAG,SAAS,EAAE,MAAM,MAAM,KAAG,OAW1E,CAAC;AAEF,sHAAsH;AACtH,eAAO,MAAM,aAAa,KAAK,CAAC;AAEhC,4CAA4C;AAC5C,MAAM,WAAW,OAAO;IACtB,0CAA0C;IAC1C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,kGAAkG;IAClG,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,uFAAuF;IACvF,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,gEAAgE;IAChE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,gGAAgG;IAChG,QAAQ,CAAC,KAAK,CAAC,EAAE,cAAc,GAAG,SAAS,CAAC;IAC5C,wGAAwG;IACxG,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAChD,+FAA+F;IAC/F,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,MAAM,MAAM,CAAC,GAAG,SAAS,CAAC;IAC1C,0HAA0H;IAC1H,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAC,GAAG,SAAS,CAAC;CAClF;AAED;;;;GAIG;AACH,eAAO,MAAM,OAAO,GAAI,SAAS,OAAO,KAAG,cA6C1C,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,84 @@
1
+ import { kubernetes, localDev, service, workerLoop } from "@mark1russell7/service";
2
+ import { probeServer } from "@mark1russell7/service/http";
3
+ import { attachApi } from "./api.js";
4
+ import { openStore } from "./store.js";
5
+ export { attachApi, receiver, windowOf, MAX_BODY_BYTES, QUERY_ROUTES } from "./api.js";
6
+ export * from "./otlp.js";
7
+ export * from "./query.js";
8
+ export * from "./retention.js";
9
+ export * from "./store.js";
10
+ const s = (value) => ({ value, unit: "s" });
11
+ const count = (value) => ({ value, unit: "count" });
12
+ /**
13
+ * These are the profiles of the shutdown, by name. `local` is for the computer of a developer.
14
+ * `pod` is for a pod of Kubernetes with a grace of 30 s and no preStop hook. Make `grace` equal to
15
+ * the `terminationGracePeriodSeconds` of the pod.
16
+ */
17
+ export const PROFILES = {
18
+ local: localDev,
19
+ pod: kubernetes({ grace: s(30), preStop: s(0) }),
20
+ };
21
+ /**
22
+ * This function tells if an OTLP endpoint is the receiver of this sink. A sink that exports its own spans to itself
23
+ * makes new spans at each export, without an end.
24
+ */
25
+ export const isOwnEndpoint = (endpoint, port) => {
26
+ if (endpoint === undefined || endpoint === "")
27
+ return false;
28
+ let url;
29
+ try {
30
+ url = new URL(endpoint);
31
+ }
32
+ catch {
33
+ return false;
34
+ }
35
+ const local = ["localhost", "127.0.0.1", "[::1]", "0.0.0.0"].includes(url.hostname);
36
+ const target = url.port === "" ? (url.protocol === "https:" ? 443 : 80) : Number(url.port);
37
+ return local && target === port;
38
+ };
39
+ /** The most deletes of one iteration of the retention worker. Each delete is one transaction of at most 5000 rows. */
40
+ export const PRUNE_BATCHES = 20;
41
+ /**
42
+ * This function declares the one service of the process. It starts nothing. `describe()` gives the
43
+ * declaration as data, and `lint()` examines it. Only the entry (`main.ts`) uses `run()`.
44
+ * The store is a dependency: it opens before the servers start, and it closes after they stop.
45
+ */
46
+ export const declare = (options) => {
47
+ const now = options.now ?? Date.now;
48
+ let opened;
49
+ const store = () => {
50
+ if (opened === undefined)
51
+ throw new Error("the store is not open");
52
+ return opened;
53
+ };
54
+ // The prune of the store, on a loop. A throw is a failure, and the loop waits its backoff. Each iteration does at most
55
+ // PRUNE_BATCHES deletes, and gives "more" when the prune is not done. Thus the intake goes on between two parts, and
56
+ // each part ends in its bound, before the cap of the drain of each profile.
57
+ const retention = workerLoop({
58
+ worker_sleep: s(options.pruneEverySeconds ?? 60),
59
+ unit_processing_time: s(3),
60
+ retry_base: s(5),
61
+ retry_cap: s(300),
62
+ close_bound: s(0.5),
63
+ concurrency: count(1),
64
+ }, { iteration: () => (store().prune(now(), PRUNE_BATCHES).done ? undefined : "more"), onError: options.onPruneError });
65
+ const svc = service("otel-sink").dependency("store", { system: null, knobs: {}, operations: {} }, {
66
+ connect: () => {
67
+ opened = openStore(options.path, { rules: options.rules });
68
+ },
69
+ close: () => {
70
+ opened?.close();
71
+ opened = undefined;
72
+ },
73
+ });
74
+ return (attachApi(svc, { port: options.port, store, now })
75
+ .worker("retention", retention.knobs, retention)
76
+ // The probes have a port of their own. Thus they answer through the whole shutdown.
77
+ .healthServer(probeServer({ port: options.probePort, server_retry_after: s(1) }))
78
+ // The policies apply to the checks of the service. Add a check with `check(check, run)`.
79
+ .readiness({ type: "debounce", interval: s(5), failAfter: 3, recoverAfter: 1 })
80
+ .liveness({ type: "window", failAfter: s(45) })
81
+ .startup(s(30))
82
+ .profile(options.profile));
83
+ };
84
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAEnF,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAErC,OAAO,EAAE,SAAS,EAAc,MAAM,YAAY,CAAC;AAEnD,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,cAAc,EAAE,YAAY,EAAmB,MAAM,UAAU,CAAC;AACxG,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAE3B,MAAM,CAAC,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC;AACpD,MAAM,KAAK,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;AAE5D;;;;GAIG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAsC;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M,CAAC,WAAW,EAAE,SAAS,CAAC,KAAK,EAAE,SAAS,CAAC;QAChD,oFAAoF;SACnF,YAAY,CAAC,WAAW,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,SAAS,EAAE,kBAAkB,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QACjF,yFAAyF;SACxF,SAAS,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,CAAC;SAC9E,QAAQ,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC;SAC9C,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;SACd,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAC5B,CAAC;AACJ,CAAC,CAAC"}
package/dist/main.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=main.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":""}
package/dist/main.js ADDED
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * This file is the entry of the process, and the only file that uses `run()`. The lifecycle owns the
4
+ * signals of the process. After SIGTERM or Ctrl-C, the service stops in its phases, and the process
5
+ * exits with 0 or 1.
6
+ *
7
+ * The environment gives `PORT` (4318), `PROBE_PORT` (9090), `SERVICE_PROFILE` (`local`) and `SINK_DB`
8
+ * (`otel-sink.db` in the home folder). The value in parentheses is the value without the variable.
9
+ *
10
+ * The run starts the context of otel-ts, so each log and each span of a request has its context. With a value in
11
+ * `OTEL_EXPORTER_OTLP_ENDPOINT`, the run also exports the spans.
12
+ */
13
+ import { homedir } from "node:os";
14
+ import { join } from "node:path";
15
+ import { PROFILES, declare, isOwnEndpoint } from "./index.js";
16
+ /** This function gives a port from the environment, or the fallback when the variable is not set. */
17
+ const port = (name, fallback) => {
18
+ const text = process.env[name];
19
+ const value = text === undefined ? fallback : Number(text);
20
+ if (!Number.isInteger(value) || value < 0 || value > 65535)
21
+ throw new Error(`${name} is not a port: ${text}`);
22
+ return value;
23
+ };
24
+ const name = process.env["SERVICE_PROFILE"] ?? "local";
25
+ const profile = PROFILES[name];
26
+ if (profile === undefined)
27
+ throw new Error(`SERVICE_PROFILE is "${name}". The profiles are: ${Object.keys(PROFILES).join(", ")}.`);
28
+ const path = process.env["SINK_DB"] ?? join(homedir(), "otel-sink.db");
29
+ const api = port("PORT", 4318);
30
+ for (const name of ["OTEL_EXPORTER_OTLP_ENDPOINT", "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT", "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT"])
31
+ if (isOwnEndpoint(process.env[name], api))
32
+ throw new Error(`${name} is the receiver of this sink. The sink must not export its own telemetry to itself.`);
33
+ await declare({ port: api, probePort: port("PROBE_PORT", 9090), profile, path }).run();
34
+ //# sourceMappingURL=main.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":";AACA;;;;;;;;;;GAUG;AACH,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE9D,qGAAqG;AACrG,MAAM,IAAI,GAAG,CAAC,IAAY,EAAE,QAAgB,EAAU,EAAE;IACtD,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC/B,MAAM,KAAK,GAAG,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC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