@cat-factory/observability-otel 0.2.10

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.
@@ -0,0 +1,73 @@
1
+ import { ATTR, DEFAULT_SERVICE_NAME, SCOPE_NAME, mapPlatformMetrics, toUnixNano, } from './mapping.js';
2
+ import { keyValues, postOtlp } from './otlp.js';
3
+ /** Encode one {@link MappedGauge} as an OTLP metric with a `gauge` data-point list. */
4
+ function encodeGauge(gauge, timeUnixNano) {
5
+ return {
6
+ name: gauge.name,
7
+ unit: gauge.unit,
8
+ gauge: {
9
+ dataPoints: gauge.points.map((point) => ({
10
+ attributes: keyValues(point.attributes),
11
+ timeUnixNano,
12
+ ...(point.isInt ? { asInt: String(Math.round(point.value)) } : { asDouble: point.value }),
13
+ })),
14
+ },
15
+ };
16
+ }
17
+ export class PlatformMetricsOtelExporter {
18
+ metricsEndpoint;
19
+ headers;
20
+ serviceName;
21
+ logger;
22
+ fetchImpl;
23
+ constructor(config) {
24
+ const base = config.endpoint.replace(/\/+$/, '');
25
+ this.metricsEndpoint = `${base}/v1/metrics`;
26
+ this.headers = { 'content-type': 'application/json', ...config.headers };
27
+ this.serviceName = config.serviceName || DEFAULT_SERVICE_NAME;
28
+ this.logger = config.logger;
29
+ this.fetchImpl = config.fetchImpl ?? fetch;
30
+ }
31
+ resourceAttributes() {
32
+ return [{ key: ATTR.serviceName, value: { stringValue: this.serviceName } }];
33
+ }
34
+ /**
35
+ * Export one account's platform-observability snapshot as OTLP gauge metrics. Gauges are
36
+ * stamped with the snapshot's `generatedAt` (the clock the projection was computed at), so
37
+ * no wall-clock read is needed here. In practice a snapshot always yields the run-count and
38
+ * live-depth gauges (even all-zero — a zero-valued gauge is meaningful, not misleading), so
39
+ * a POST is always sent; the empty-batch guard below is defensive only, in case the mapping
40
+ * ever becomes fully conditional. Best-effort — see the file header.
41
+ */
42
+ async export(snapshot, dims) {
43
+ const gauges = mapPlatformMetrics(snapshot, dims);
44
+ if (gauges.length === 0)
45
+ return;
46
+ const timeUnixNano = toUnixNano(snapshot.generatedAt);
47
+ const payload = {
48
+ resourceMetrics: [
49
+ {
50
+ resource: { attributes: this.resourceAttributes() },
51
+ scopeMetrics: [
52
+ {
53
+ scope: { name: SCOPE_NAME },
54
+ metrics: gauges.map((gauge) => encodeGauge(gauge, timeUnixNano)),
55
+ },
56
+ ],
57
+ },
58
+ ],
59
+ };
60
+ await postOtlp({
61
+ fetchImpl: this.fetchImpl,
62
+ endpoint: this.metricsEndpoint,
63
+ headers: this.headers,
64
+ payload,
65
+ logger: this.logger,
66
+ });
67
+ }
68
+ }
69
+ /** Build a fetch-based {@link PlatformMetricsOtelExporter}. The workerd-safe opt-in exporter. */
70
+ export function createPlatformMetricsOtelExporter(config) {
71
+ return new PlatformMetricsOtelExporter(config);
72
+ }
73
+ //# sourceMappingURL=platform.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"platform.js","sourceRoot":"","sources":["../src/platform.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,IAAI,EACJ,oBAAoB,EACpB,UAAU,EACV,kBAAkB,EAClB,UAAU,GACX,MAAM,cAAc,CAAA;AACrB,OAAO,EAAkC,SAAS,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAiC/E,uFAAuF;AACvF,SAAS,WAAW,CAAC,KAAkB,EAAE,YAAoB;IAC3D,OAAO;QACL,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,KAAK,EAAE;YACL,UAAU,EAAE,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;gBACvC,UAAU,EAAE,SAAS,CAAC,KAAK,CAAC,UAAU,CAAC;gBACvC,YAAY;gBACZ,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KA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AI,CAAC,kBAAkB,EAAE,EAAE;oBACnD,YAAY,EAAE;wBACZ;4BACE,KAAK,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE;4BAC3B,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,WAAW,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;yBACjE;qBACF;iBACF;aACF;SACF,CAAA;QACD,MAAM,QAAQ,CAAC;YACb,SAAS,EAAE,IAAI,CAAC,SAAS;YACzB,QAAQ,EAAE,IAAI,CAAC,eAAe;YAC9B,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,OAAO;YACP,MAAM,EAAE,IAAI,CAAC,MAAM;SACpB,CAAC,CAAA;IACJ,CAAC;CACF;AAED,iGAAiG;AACjG,MAAM,UAAU,iCAAiC,CAC/C,MAAyC;IAEzC,OAAO,IAAI,2BAA2B,CAAC,MAAM,CAAC,CAAA;AAChD,CAAC"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@cat-factory/observability-otel",
3
+ "version": "0.2.10",
4
+ "description": "Opt-in OpenTelemetry (OTLP) trace + metrics publisher for the Agent Architecture Board. Streams LLM generations and container tool spans to any OTLP/HTTP backend — the workerd-safe fetch exporter on the Cloudflare Worker facade, the official @opentelemetry/* SDK on Node — kept conformant by a shared mapping layer + tests. Also publishes deployment-level (platform-operator) run-health aggregates as OTLP gauge metrics.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/kibertoad/cat-factory.git",
8
+ "directory": "backend/packages/observability-otel"
9
+ },
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "type": "module",
14
+ "main": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "default": "./dist/index.js"
20
+ },
21
+ "./node": {
22
+ "types": "./dist/node.d.ts",
23
+ "default": "./dist/node.js"
24
+ },
25
+ "./package.json": "./package.json"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "dependencies": {
31
+ "@opentelemetry/api": "^1.9.1",
32
+ "@opentelemetry/exporter-metrics-otlp-http": "^0.220.0",
33
+ "@opentelemetry/exporter-trace-otlp-http": "^0.220.0",
34
+ "@opentelemetry/resources": "^2.9.0",
35
+ "@opentelemetry/sdk-metrics": "^2.9.0",
36
+ "@opentelemetry/sdk-trace-base": "^2.9.0",
37
+ "@opentelemetry/sdk-trace-node": "^2.9.0",
38
+ "@cat-factory/contracts": "0.148.1",
39
+ "@cat-factory/kernel": "0.140.1"
40
+ },
41
+ "devDependencies": {
42
+ "typescript": "7.0.2",
43
+ "vitest": "^4.1.10"
44
+ },
45
+ "scripts": {
46
+ "build": "tsc -b tsconfig.build.json",
47
+ "typecheck": "tsc -p tsconfig.json --noEmit",
48
+ "test:run": "vitest run"
49
+ }
50
+ }