@agentuity/telemetry 3.0.0-alpha.0
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/dist/console.d.ts +33 -0
- package/dist/console.d.ts.map +1 -0
- package/dist/console.js +86 -0
- package/dist/console.js.map +1 -0
- package/dist/exporters/index.d.ts +4 -0
- package/dist/exporters/index.d.ts.map +1 -0
- package/dist/exporters/index.js +4 -0
- package/dist/exporters/index.js.map +1 -0
- package/dist/exporters/jsonl-log-exporter.d.ts +36 -0
- package/dist/exporters/jsonl-log-exporter.d.ts.map +1 -0
- package/dist/exporters/jsonl-log-exporter.js +103 -0
- package/dist/exporters/jsonl-log-exporter.js.map +1 -0
- package/dist/exporters/jsonl-metric-exporter.d.ts +40 -0
- package/dist/exporters/jsonl-metric-exporter.d.ts.map +1 -0
- package/dist/exporters/jsonl-metric-exporter.js +104 -0
- package/dist/exporters/jsonl-metric-exporter.js.map +1 -0
- package/dist/exporters/jsonl-trace-exporter.d.ts +36 -0
- package/dist/exporters/jsonl-trace-exporter.d.ts.map +1 -0
- package/dist/exporters/jsonl-trace-exporter.js +111 -0
- package/dist/exporters/jsonl-trace-exporter.js.map +1 -0
- package/dist/fetch.d.ts +12 -0
- package/dist/fetch.d.ts.map +1 -0
- package/dist/fetch.js +82 -0
- package/dist/fetch.js.map +1 -0
- package/dist/globals.d.ts +9 -0
- package/dist/globals.d.ts.map +1 -0
- package/dist/globals.js +13 -0
- package/dist/globals.js.map +1 -0
- package/dist/http.d.ts +16 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +44 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +62 -0
- package/dist/index.js.map +1 -0
- package/dist/logger/console.d.ts +69 -0
- package/dist/logger/console.d.ts.map +1 -0
- package/dist/logger/console.js +278 -0
- package/dist/logger/console.js.map +1 -0
- package/dist/logger/index.d.ts +4 -0
- package/dist/logger/index.d.ts.map +1 -0
- package/dist/logger/index.js +3 -0
- package/dist/logger/index.js.map +1 -0
- package/dist/logger/internal.d.ts +79 -0
- package/dist/logger/internal.d.ts.map +1 -0
- package/dist/logger/internal.js +133 -0
- package/dist/logger/internal.js.map +1 -0
- package/dist/logger/user.d.ts +8 -0
- package/dist/logger/user.d.ts.map +1 -0
- package/dist/logger/user.js +7 -0
- package/dist/logger/user.js.map +1 -0
- package/dist/logger/util.d.ts +11 -0
- package/dist/logger/util.d.ts.map +1 -0
- package/dist/logger/util.js +77 -0
- package/dist/logger/util.js.map +1 -0
- package/dist/logger.d.ts +40 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +259 -0
- package/dist/logger.js.map +1 -0
- package/dist/telemetry.d.ts +71 -0
- package/dist/telemetry.d.ts.map +1 -0
- package/dist/telemetry.js +274 -0
- package/dist/telemetry.js.map +1 -0
- package/dist/tracestate.d.ts +44 -0
- package/dist/tracestate.d.ts.map +1 -0
- package/dist/tracestate.js +84 -0
- package/dist/tracestate.js.map +1 -0
- package/package.json +58 -0
- package/src/console.ts +91 -0
- package/src/exporters/README.md +217 -0
- package/src/exporters/index.ts +3 -0
- package/src/exporters/jsonl-log-exporter.ts +113 -0
- package/src/exporters/jsonl-metric-exporter.ts +120 -0
- package/src/exporters/jsonl-trace-exporter.ts +121 -0
- package/src/fetch.ts +105 -0
- package/src/globals.ts +18 -0
- package/src/http.ts +53 -0
- package/src/index.ts +82 -0
- package/src/logger/console.ts +322 -0
- package/src/logger/index.ts +3 -0
- package/src/logger/internal.ts +165 -0
- package/src/logger/user.ts +15 -0
- package/src/logger/util.ts +80 -0
- package/src/logger.ts +285 -0
- package/src/telemetry.ts +403 -0
- package/src/tracestate.ts +108 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type Context } from '@opentelemetry/api';
|
|
2
|
+
/**
|
|
3
|
+
* Entries to set on the W3C tracestate header. Each key-value pair is added
|
|
4
|
+
* to the parent context's existing traceState (if any). Values that are
|
|
5
|
+
* `undefined` or empty strings are skipped.
|
|
6
|
+
*/
|
|
7
|
+
export type TraceStateEntries = Record<string, string | undefined>;
|
|
8
|
+
/**
|
|
9
|
+
* Build a context whose span context carries an enriched W3C traceState.
|
|
10
|
+
*
|
|
11
|
+
* The returned context is intended to be passed as the **parent context**
|
|
12
|
+
* to `tracer.startActiveSpan(name, opts, enrichedCtx, fn)` or
|
|
13
|
+
* `tracer.startSpan(name, opts, enrichedCtx)`. Because the OTel SDK
|
|
14
|
+
* copies `traceState` from a *valid* parent span context into every new
|
|
15
|
+
* child span, the recording span that gets exported to OTLP will carry the
|
|
16
|
+
* enriched traceState — making it visible in backends like ClickHouse.
|
|
17
|
+
*
|
|
18
|
+
* ### How it works
|
|
19
|
+
*
|
|
20
|
+
* 1. If the supplied `parentContext` already contains a span with a valid
|
|
21
|
+
* span context (e.g. from an incoming `traceparent` header), we enrich
|
|
22
|
+
* that span context's traceState and wrap it in a `NonRecordingSpan`.
|
|
23
|
+
*
|
|
24
|
+
* 2. If there is **no** valid parent span (e.g. no incoming `traceparent`),
|
|
25
|
+
* we synthesise a minimal remote span context with a freshly generated
|
|
26
|
+
* traceId. The OTel SDK will treat this as a valid remote parent,
|
|
27
|
+
* inherit both the traceId **and** the traceState, and mark the new
|
|
28
|
+
* span as a continuation of that trace.
|
|
29
|
+
*
|
|
30
|
+
* @param parentContext The context to enrich (typically `context.active()`).
|
|
31
|
+
* @param entries Key-value pairs to merge into the traceState.
|
|
32
|
+
* @returns A new `Context` ready to be used as a parent for span creation.
|
|
33
|
+
*/
|
|
34
|
+
export declare function enrichContextWithTraceState(parentContext: Context, entries: TraceStateEntries): Context;
|
|
35
|
+
/**
|
|
36
|
+
* Generate a random 32-hex-char trace ID (16 bytes).
|
|
37
|
+
* Uses the Web Crypto API which is available in Bun and Node 20+.
|
|
38
|
+
*/
|
|
39
|
+
export declare function generateTraceId(): string;
|
|
40
|
+
/**
|
|
41
|
+
* Generate a random 16-hex-char span ID (8 bytes).
|
|
42
|
+
*/
|
|
43
|
+
export declare function generateSpanId(): string;
|
|
44
|
+
//# sourceMappingURL=tracestate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tracestate.d.ts","sourceRoot":"","sources":["../src/tracestate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAGrE;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;AAEnE;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,2BAA2B,CAC1C,aAAa,EAAE,OAAO,EACtB,OAAO,EAAE,iBAAiB,GACxB,OAAO,CAuCT;AAID;;;GAGG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAIxC;AAED;;GAEG;AACH,wBAAgB,cAAc,IAAI,MAAM,CAIvC"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { trace, TraceFlags } from '@opentelemetry/api';
|
|
2
|
+
import { TraceState } from '@opentelemetry/core';
|
|
3
|
+
/**
|
|
4
|
+
* Build a context whose span context carries an enriched W3C traceState.
|
|
5
|
+
*
|
|
6
|
+
* The returned context is intended to be passed as the **parent context**
|
|
7
|
+
* to `tracer.startActiveSpan(name, opts, enrichedCtx, fn)` or
|
|
8
|
+
* `tracer.startSpan(name, opts, enrichedCtx)`. Because the OTel SDK
|
|
9
|
+
* copies `traceState` from a *valid* parent span context into every new
|
|
10
|
+
* child span, the recording span that gets exported to OTLP will carry the
|
|
11
|
+
* enriched traceState — making it visible in backends like ClickHouse.
|
|
12
|
+
*
|
|
13
|
+
* ### How it works
|
|
14
|
+
*
|
|
15
|
+
* 1. If the supplied `parentContext` already contains a span with a valid
|
|
16
|
+
* span context (e.g. from an incoming `traceparent` header), we enrich
|
|
17
|
+
* that span context's traceState and wrap it in a `NonRecordingSpan`.
|
|
18
|
+
*
|
|
19
|
+
* 2. If there is **no** valid parent span (e.g. no incoming `traceparent`),
|
|
20
|
+
* we synthesise a minimal remote span context with a freshly generated
|
|
21
|
+
* traceId. The OTel SDK will treat this as a valid remote parent,
|
|
22
|
+
* inherit both the traceId **and** the traceState, and mark the new
|
|
23
|
+
* span as a continuation of that trace.
|
|
24
|
+
*
|
|
25
|
+
* @param parentContext The context to enrich (typically `context.active()`).
|
|
26
|
+
* @param entries Key-value pairs to merge into the traceState.
|
|
27
|
+
* @returns A new `Context` ready to be used as a parent for span creation.
|
|
28
|
+
*/
|
|
29
|
+
export function enrichContextWithTraceState(parentContext, entries) {
|
|
30
|
+
const parentSpan = trace.getSpan(parentContext);
|
|
31
|
+
const parentSctx = parentSpan?.spanContext();
|
|
32
|
+
// Start from any existing traceState on the parent, or a fresh one.
|
|
33
|
+
let traceState = parentSctx?.traceState ?? new TraceState();
|
|
34
|
+
// Merge caller-supplied entries.
|
|
35
|
+
for (const [key, value] of Object.entries(entries)) {
|
|
36
|
+
if (value !== undefined && value !== '') {
|
|
37
|
+
traceState = traceState.set(key, value);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (parentSctx && trace.isSpanContextValid(parentSctx)) {
|
|
41
|
+
// The parent already has a valid traceId/spanId (e.g. from an
|
|
42
|
+
// incoming request with `traceparent`). We just need to update
|
|
43
|
+
// its traceState.
|
|
44
|
+
return trace.setSpan(parentContext, trace.wrapSpanContext({
|
|
45
|
+
...parentSctx,
|
|
46
|
+
traceState,
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
// No valid parent — synthesise a remote parent so the OTel SDK
|
|
50
|
+
// considers the span context valid and copies traceState to the child.
|
|
51
|
+
return trace.setSpan(parentContext, trace.wrapSpanContext({
|
|
52
|
+
traceId: generateTraceId(),
|
|
53
|
+
spanId: generateSpanId(),
|
|
54
|
+
traceFlags: TraceFlags.SAMPLED,
|
|
55
|
+
isRemote: true,
|
|
56
|
+
traceState,
|
|
57
|
+
}));
|
|
58
|
+
}
|
|
59
|
+
// ── ID generation helpers ────────────────────────────────────────────
|
|
60
|
+
/**
|
|
61
|
+
* Generate a random 32-hex-char trace ID (16 bytes).
|
|
62
|
+
* Uses the Web Crypto API which is available in Bun and Node 20+.
|
|
63
|
+
*/
|
|
64
|
+
export function generateTraceId() {
|
|
65
|
+
const bytes = new Uint8Array(16);
|
|
66
|
+
crypto.getRandomValues(bytes);
|
|
67
|
+
return hexFromBytes(bytes);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Generate a random 16-hex-char span ID (8 bytes).
|
|
71
|
+
*/
|
|
72
|
+
export function generateSpanId() {
|
|
73
|
+
const bytes = new Uint8Array(8);
|
|
74
|
+
crypto.getRandomValues(bytes);
|
|
75
|
+
return hexFromBytes(bytes);
|
|
76
|
+
}
|
|
77
|
+
function hexFromBytes(bytes) {
|
|
78
|
+
let hex = '';
|
|
79
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
80
|
+
hex += bytes[i].toString(16).padStart(2, '0');
|
|
81
|
+
}
|
|
82
|
+
return hex;
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=tracestate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tracestate.js","sourceRoot":"","sources":["../src/tracestate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,UAAU,EAAgB,MAAM,oBAAoB,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AASjD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,2BAA2B,CAC1C,aAAsB,EACtB,OAA0B;IAE1B,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;IAChD,MAAM,UAAU,GAAG,UAAU,EAAE,WAAW,EAAE,CAAC;IAE7C,oEAAoE;IACpE,IAAI,UAAU,GAAG,UAAU,EAAE,UAAU,IAAI,IAAI,UAAU,EAAE,CAAC;IAE5D,iCAAiC;IACjC,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QACpD,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,EAAE,EAAE,CAAC;YACzC,UAAU,GAAG,UAAU,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACzC,CAAC;IACF,CAAC;IAED,IAAI,UAAU,IAAI,KAAK,CAAC,kBAAkB,CAAC,UAAU,CAAC,EAAE,CAAC;QACxD,8DAA8D;QAC9D,gEAAgE;QAChE,kBAAkB;QAClB,OAAO,KAAK,CAAC,OAAO,CACnB,aAAa,EACb,KAAK,CAAC,eAAe,CAAC;YACrB,GAAG,UAAU;YACb,UAAU;SACV,CAAC,CACF,CAAC;IACH,CAAC;IAED,+DAA+D;IAC/D,uEAAuE;IACvE,OAAO,KAAK,CAAC,OAAO,CACnB,aAAa,EACb,KAAK,CAAC,eAAe,CAAC;QACrB,OAAO,EAAE,eAAe,EAAE;QAC1B,MAAM,EAAE,cAAc,EAAE;QACxB,UAAU,EAAE,UAAU,CAAC,OAAO;QAC9B,QAAQ,EAAE,IAAI;QACd,UAAU;KACV,CAAC,CACF,CAAC;AACH,CAAC;AAED,wEAAwE;AAExE;;;GAGG;AACH,MAAM,UAAU,eAAe;IAC9B,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IACjC,MAAM,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;IAC9B,OAAO,YAAY,CAAC,KAAK,CAAC,CAAC;AAC5B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,cAAc;IAC7B,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC;IAChC,MAAM,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;IAC9B,OAAO,YAAY,CAAC,KAAK,CAAC,CAAC;AAC5B,CAAC;AAED,SAAS,YAAY,CAAC,KAAiB;IACtC,IAAI,GAAG,GAAG,EAAE,CAAC;IACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACvC,GAAG,IAAK,KAAK,CAAC,CAAC,CAAY,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IAC3D,CAAC;IACD,OAAO,GAAG,CAAC;AACZ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@agentuity/telemetry",
|
|
3
|
+
"version": "3.0.0-alpha.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"author": "Agentuity employees and contributors",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"src",
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"clean": "rm -rf dist tsconfig.tsbuildinfo",
|
|
21
|
+
"build": "bunx tsc --build",
|
|
22
|
+
"typecheck": "bunx tsc --noEmit",
|
|
23
|
+
"prepublishOnly": "bun run clean && bun run build"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@agentuity/core": "3.0.0-alpha.0",
|
|
27
|
+
"@agentuity/server": "3.0.0-alpha.0",
|
|
28
|
+
"@opentelemetry/api": "^1.9.0",
|
|
29
|
+
"@opentelemetry/api-logs": "^0.207.0",
|
|
30
|
+
"@opentelemetry/auto-instrumentations-node": "^0.66.0",
|
|
31
|
+
"@opentelemetry/core": "^2.2.0",
|
|
32
|
+
"@opentelemetry/exporter-logs-otlp-http": "^0.207.0",
|
|
33
|
+
"@opentelemetry/exporter-metrics-otlp-http": "^0.207.0",
|
|
34
|
+
"@opentelemetry/exporter-trace-otlp-http": "^0.207.0",
|
|
35
|
+
"@opentelemetry/host-metrics": "^0.36.2",
|
|
36
|
+
"@opentelemetry/otlp-exporter-base": "^0.207.0",
|
|
37
|
+
"@opentelemetry/resources": "^2.2.0",
|
|
38
|
+
"@opentelemetry/sdk-logs": "^0.207.0",
|
|
39
|
+
"@opentelemetry/sdk-metrics": "^2.2.0",
|
|
40
|
+
"@opentelemetry/sdk-node": "^0.207.0",
|
|
41
|
+
"@opentelemetry/sdk-trace-base": "^2.2.0",
|
|
42
|
+
"@opentelemetry/semantic-conventions": "^1.37.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/bun": "latest",
|
|
46
|
+
"bun-types": "latest",
|
|
47
|
+
"typescript": "^5.9.0"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
},
|
|
52
|
+
"sideEffects": true,
|
|
53
|
+
"repository": {
|
|
54
|
+
"type": "git",
|
|
55
|
+
"url": "https://github.com/agentuity/sdk.git",
|
|
56
|
+
"directory": "packages/telemetry"
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/console.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { SeverityNumber } from '@opentelemetry/api-logs';
|
|
2
|
+
import { type ExportResult, ExportResultCode } from '@opentelemetry/core';
|
|
3
|
+
import type { LogRecordExporter, ReadableLogRecord } from '@opentelemetry/sdk-logs';
|
|
4
|
+
import type { SpanExporter, ReadableSpan } from '@opentelemetry/sdk-trace-base';
|
|
5
|
+
import { __originalConsole } from './logger';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Console implementation of the LogRecordExporter interface
|
|
9
|
+
* Uses __originalConsole to avoid infinite loop when console is patched
|
|
10
|
+
*/
|
|
11
|
+
export class ConsoleLogRecordExporter implements LogRecordExporter {
|
|
12
|
+
private dumpRecords = false;
|
|
13
|
+
|
|
14
|
+
constructor(dumpRecords: boolean) {
|
|
15
|
+
this.dumpRecords = dumpRecords;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Exports log records to the console
|
|
19
|
+
*
|
|
20
|
+
* @param logs - The log records to export
|
|
21
|
+
* @param resultCallback - Callback function to report the export result
|
|
22
|
+
*/
|
|
23
|
+
export(logs: ReadableLogRecord[], resultCallback: (result: ExportResult) => void): void {
|
|
24
|
+
for (const log of logs) {
|
|
25
|
+
if (this.dumpRecords) {
|
|
26
|
+
__originalConsole.log('[LOG]', {
|
|
27
|
+
body: log.body,
|
|
28
|
+
severityNumber: log.severityNumber,
|
|
29
|
+
severityText: log.severityText,
|
|
30
|
+
timestamp: log.hrTime,
|
|
31
|
+
attributes: log.attributes,
|
|
32
|
+
resource: log.resource.attributes,
|
|
33
|
+
});
|
|
34
|
+
} else {
|
|
35
|
+
const severity = log.severityNumber ? SeverityNumber[log.severityNumber] : 'INFO';
|
|
36
|
+
const msg = `[${severity}] ${log.body}`;
|
|
37
|
+
switch (log.severityNumber) {
|
|
38
|
+
case SeverityNumber.DEBUG:
|
|
39
|
+
__originalConsole.debug(msg);
|
|
40
|
+
break;
|
|
41
|
+
case SeverityNumber.INFO:
|
|
42
|
+
__originalConsole.info(msg);
|
|
43
|
+
break;
|
|
44
|
+
case SeverityNumber.WARN:
|
|
45
|
+
__originalConsole.warn(msg);
|
|
46
|
+
break;
|
|
47
|
+
case SeverityNumber.ERROR:
|
|
48
|
+
__originalConsole.error(msg);
|
|
49
|
+
break;
|
|
50
|
+
default:
|
|
51
|
+
__originalConsole.log(msg);
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
resultCallback({ code: ExportResultCode.SUCCESS });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Shuts down the exporter
|
|
61
|
+
*
|
|
62
|
+
* @returns A promise that resolves when shutdown is complete
|
|
63
|
+
*/
|
|
64
|
+
shutdown(): Promise<void> {
|
|
65
|
+
return Promise.resolve();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Console implementation of the SpanExporter interface
|
|
71
|
+
* Uses __originalConsole to avoid infinite loop when console is patched
|
|
72
|
+
*/
|
|
73
|
+
export class DebugSpanExporter implements SpanExporter {
|
|
74
|
+
export(spans: ReadableSpan[], resultCallback: (result: ExportResult) => void): void {
|
|
75
|
+
for (const span of spans) {
|
|
76
|
+
__originalConsole.log('[SPAN]', {
|
|
77
|
+
name: span.name,
|
|
78
|
+
traceId: span.spanContext().traceId,
|
|
79
|
+
spanId: span.spanContext().spanId,
|
|
80
|
+
duration: span.duration,
|
|
81
|
+
status: span.status,
|
|
82
|
+
attributes: span.attributes,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
resultCallback({ code: ExportResultCode.SUCCESS });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
shutdown(): Promise<void> {
|
|
89
|
+
return Promise.resolve();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
# JSONL Exporters
|
|
2
|
+
|
|
3
|
+
Custom OpenTelemetry exporters that write telemetry data (logs, traces, metrics) to JSONL (JSON Lines) files instead of sending directly to an OTLP endpoint.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
These exporters write telemetry data to local files in JSONL format. Each line in the file represents a single telemetry item in JSON format. This allows for:
|
|
8
|
+
|
|
9
|
+
1. **Decoupled Processing**: Telemetry data is buffered locally and processed separately
|
|
10
|
+
2. **Reliability**: Data persists even if the OTLP endpoint is temporarily unavailable
|
|
11
|
+
3. **Batch Processing**: A separate cron job can read and send data in batches
|
|
12
|
+
4. **Easy Debugging**: JSONL files can be inspected directly
|
|
13
|
+
|
|
14
|
+
## How It Works
|
|
15
|
+
|
|
16
|
+
### 1. Writing Telemetry Data
|
|
17
|
+
|
|
18
|
+
The exporters write telemetry data to timestamped JSONL files:
|
|
19
|
+
|
|
20
|
+
- **Logs**: `./otel-data/logs-<timestamp>.jsonl`
|
|
21
|
+
- **Traces**: `./otel-data/traces-<timestamp>.jsonl`
|
|
22
|
+
- **Metrics**: `./otel-data/metrics-<timestamp>.jsonl`
|
|
23
|
+
|
|
24
|
+
Files are named with an ISO timestamp (with colons and periods replaced by hyphens) to ensure uniqueness. The exporters will continue writing to the same file as long as it exists.
|
|
25
|
+
|
|
26
|
+
### 2. Reading and Forwarding Data (External Process)
|
|
27
|
+
|
|
28
|
+
A separate cron job (recommended: every 30 seconds) should:
|
|
29
|
+
|
|
30
|
+
1. Read the JSONL files
|
|
31
|
+
2. Parse each line as a JSON object
|
|
32
|
+
3. Send the telemetry data to your OTLP endpoint
|
|
33
|
+
4. Delete the file after successful transmission
|
|
34
|
+
|
|
35
|
+
This decouples the application from the OTLP endpoint and provides resilience.
|
|
36
|
+
|
|
37
|
+
## Configuration
|
|
38
|
+
|
|
39
|
+
### Enabling JSONL Exporters
|
|
40
|
+
|
|
41
|
+
By default, JSONL exporters are enabled. You can configure them via the `OtelConfig`:
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { registerOtel } from '@agentuity/runtime/otel';
|
|
45
|
+
|
|
46
|
+
registerOtel({
|
|
47
|
+
name: 'my-app',
|
|
48
|
+
version: '1.0.0',
|
|
49
|
+
url: 'https://otel.example.com',
|
|
50
|
+
useJsonlExporter: true, // Enable JSONL exporters (default: true)
|
|
51
|
+
jsonlBasePath: './.agentuity/otel-data', // Directory for JSONL files
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Disabling JSONL Exporters
|
|
56
|
+
|
|
57
|
+
To use the original OTLP exporters (direct network calls):
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
registerOtel({
|
|
61
|
+
name: 'my-app',
|
|
62
|
+
version: '1.0.0',
|
|
63
|
+
url: 'https://otel.example.com',
|
|
64
|
+
useJsonlExporter: false, // Disable JSONL, use OTLP directly
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## File Format
|
|
69
|
+
|
|
70
|
+
### Logs
|
|
71
|
+
|
|
72
|
+
Each log entry contains:
|
|
73
|
+
|
|
74
|
+
```json
|
|
75
|
+
{
|
|
76
|
+
"timestamp": [seconds, nanoseconds],
|
|
77
|
+
"observedTimestamp": [seconds, nanoseconds],
|
|
78
|
+
"severityNumber": 9,
|
|
79
|
+
"severityText": "INFO",
|
|
80
|
+
"body": "Log message",
|
|
81
|
+
"attributes": { "key": "value" },
|
|
82
|
+
"resource": { "@agentuity/orgId": "...", ... },
|
|
83
|
+
"instrumentationScope": { "name": "...", "version": "..." },
|
|
84
|
+
"spanContext": { "traceId": "...", "spanId": "...", ... }
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Traces
|
|
89
|
+
|
|
90
|
+
Each span contains:
|
|
91
|
+
|
|
92
|
+
```json
|
|
93
|
+
{
|
|
94
|
+
"traceId": "...",
|
|
95
|
+
"spanId": "...",
|
|
96
|
+
"traceState": "...",
|
|
97
|
+
"name": "operation-name",
|
|
98
|
+
"kind": 1,
|
|
99
|
+
"startTime": [seconds, nanoseconds],
|
|
100
|
+
"endTime": [seconds, nanoseconds],
|
|
101
|
+
"attributes": { "key": "value" },
|
|
102
|
+
"status": { "code": 0 },
|
|
103
|
+
"events": [],
|
|
104
|
+
"links": [],
|
|
105
|
+
"resource": { "@agentuity/orgId": "...", ... },
|
|
106
|
+
"droppedAttributesCount": 0,
|
|
107
|
+
"droppedEventsCount": 0,
|
|
108
|
+
"droppedLinksCount": 0,
|
|
109
|
+
"duration": [seconds, nanoseconds],
|
|
110
|
+
"ended": true
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Metrics
|
|
115
|
+
|
|
116
|
+
Each metric batch contains:
|
|
117
|
+
|
|
118
|
+
```json
|
|
119
|
+
{
|
|
120
|
+
"resource": { "@agentuity/orgId": "...", ... },
|
|
121
|
+
"scopeMetrics": [
|
|
122
|
+
{
|
|
123
|
+
"scope": { "name": "...", "version": "..." },
|
|
124
|
+
"metrics": [
|
|
125
|
+
{
|
|
126
|
+
"descriptor": { "name": "...", "description": "...", ... },
|
|
127
|
+
"dataPointType": 0,
|
|
128
|
+
"dataPoints": [...],
|
|
129
|
+
"aggregationTemporality": 1
|
|
130
|
+
}
|
|
131
|
+
]
|
|
132
|
+
}
|
|
133
|
+
]
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Example Cron Job
|
|
138
|
+
|
|
139
|
+
Here's an example script that reads JSONL files and forwards them to OTLP:
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
#!/usr/bin/env bun
|
|
143
|
+
|
|
144
|
+
import { readdir, readFile, unlink } from 'node:fs/promises';
|
|
145
|
+
import { join } from 'node:path';
|
|
146
|
+
|
|
147
|
+
const OTEL_ENDPOINT = process.env.OTEL_ENDPOINT || 'https://otel.agentuity.cloud';
|
|
148
|
+
const OTEL_TOKEN = process.env.OTEL_TOKEN;
|
|
149
|
+
const DATA_DIR = process.env.DATA_DIR || './.agentuity/otel-data';
|
|
150
|
+
|
|
151
|
+
async function processFiles() {
|
|
152
|
+
const files = await readdir(DATA_DIR);
|
|
153
|
+
|
|
154
|
+
for (const file of files) {
|
|
155
|
+
if (file.endsWith('.jsonl')) {
|
|
156
|
+
const filePath = join(DATA_DIR, file);
|
|
157
|
+
const content = await readFile(filePath, 'utf-8');
|
|
158
|
+
const lines = content.trim().split('\n');
|
|
159
|
+
|
|
160
|
+
const type = file.startsWith('logs-')
|
|
161
|
+
? 'logs'
|
|
162
|
+
: file.startsWith('traces-')
|
|
163
|
+
? 'traces'
|
|
164
|
+
: 'metrics';
|
|
165
|
+
|
|
166
|
+
try {
|
|
167
|
+
// Send to OTLP endpoint
|
|
168
|
+
await fetch(`${OTEL_ENDPOINT}/v1/${type}`, {
|
|
169
|
+
method: 'POST',
|
|
170
|
+
headers: {
|
|
171
|
+
'Content-Type': 'application/json',
|
|
172
|
+
Authorization: `Bearer ${OTEL_TOKEN}`,
|
|
173
|
+
},
|
|
174
|
+
body: JSON.stringify({ [type]: lines.map((line) => JSON.parse(line)) }),
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// Delete file after successful transmission
|
|
178
|
+
await unlink(filePath);
|
|
179
|
+
console.log(`Processed and deleted ${file}`);
|
|
180
|
+
} catch (error) {
|
|
181
|
+
console.error(`Failed to process ${file}:`, error);
|
|
182
|
+
// Don't delete the file on error, will retry next time
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
processFiles().catch(console.error);
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Add this to your crontab to run every 30 seconds:
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
* * * * * /path/to/process-otel-data.ts
|
|
195
|
+
* * * * * sleep 30 && /path/to/process-otel-data.ts
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## Exporters
|
|
199
|
+
|
|
200
|
+
### JSONLLogExporter
|
|
201
|
+
|
|
202
|
+
Implements `LogRecordExporter` interface.
|
|
203
|
+
|
|
204
|
+
### JSONLTraceExporter
|
|
205
|
+
|
|
206
|
+
Implements `SpanExporter` interface.
|
|
207
|
+
|
|
208
|
+
### JSONLMetricExporter
|
|
209
|
+
|
|
210
|
+
Implements `PushMetricExporter` interface.
|
|
211
|
+
|
|
212
|
+
## Notes
|
|
213
|
+
|
|
214
|
+
- Files are written synchronously using `appendFileSync` for simplicity and reliability
|
|
215
|
+
- File existence is checked before each write, creating a new file if necessary
|
|
216
|
+
- Timestamps in filenames use ISO format with special characters replaced by hyphens
|
|
217
|
+
- The exporters handle errors gracefully and report them via the callback mechanism
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { type ExportResult, ExportResultCode } from '@opentelemetry/core';
|
|
2
|
+
import type { LogRecordExporter, ReadableLogRecord } from '@opentelemetry/sdk-logs';
|
|
3
|
+
import { existsSync, appendFileSync, mkdirSync } from 'node:fs';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { randomUUID } from 'node:crypto';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* JSONL implementation of the LogRecordExporter interface
|
|
9
|
+
* Writes logs to a timestamped JSONL file
|
|
10
|
+
*/
|
|
11
|
+
export class JSONLLogExporter implements LogRecordExporter {
|
|
12
|
+
private currentFile: string | null = null;
|
|
13
|
+
private readonly basePath: string;
|
|
14
|
+
private readonly filePrefix: string;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Creates a new JSONL log record exporter
|
|
18
|
+
* @param basePath - Directory to store the JSONL files
|
|
19
|
+
*/
|
|
20
|
+
constructor(basePath: string) {
|
|
21
|
+
this.basePath = basePath;
|
|
22
|
+
this.filePrefix = 'otel-log';
|
|
23
|
+
this.ensureDirectory();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private ensureDirectory(): void {
|
|
27
|
+
if (!existsSync(this.basePath)) {
|
|
28
|
+
mkdirSync(this.basePath, { recursive: true });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
private getOrCreateFile(): string {
|
|
33
|
+
// If current file exists, use it
|
|
34
|
+
if (this.currentFile && existsSync(this.currentFile)) {
|
|
35
|
+
return this.currentFile;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
this.currentFile = join(
|
|
39
|
+
this.basePath,
|
|
40
|
+
`${this.filePrefix}-${Date.now()}.${randomUUID()}.jsonl`
|
|
41
|
+
);
|
|
42
|
+
return this.currentFile;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Exports log records to a JSONL file
|
|
47
|
+
*
|
|
48
|
+
* @param logs - The log records to export
|
|
49
|
+
* @param resultCallback - Callback function to report the export result
|
|
50
|
+
*/
|
|
51
|
+
export(logs: ReadableLogRecord[], resultCallback: (result: ExportResult) => void): void {
|
|
52
|
+
try {
|
|
53
|
+
if (logs.length === 0) {
|
|
54
|
+
resultCallback({ code: ExportResultCode.SUCCESS });
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const file = this.getOrCreateFile();
|
|
58
|
+
const lines: string[] = [];
|
|
59
|
+
for (const log of logs) {
|
|
60
|
+
const record = {
|
|
61
|
+
timestamp: log.hrTime,
|
|
62
|
+
observedTimestamp: log.hrTimeObserved,
|
|
63
|
+
severityNumber: log.severityNumber,
|
|
64
|
+
severityText: log.severityText,
|
|
65
|
+
body: log.body,
|
|
66
|
+
attributes: log.attributes,
|
|
67
|
+
resource: log.resource.attributes,
|
|
68
|
+
instrumentationScope: log.instrumentationScope,
|
|
69
|
+
spanContext: log.spanContext,
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
lines.push(JSON.stringify(record));
|
|
73
|
+
}
|
|
74
|
+
const payload = `${lines.join('\n')}\n`;
|
|
75
|
+
try {
|
|
76
|
+
appendFileSync(file, payload, 'utf-8');
|
|
77
|
+
} catch (err) {
|
|
78
|
+
// File may have been deleted, reset and retry once
|
|
79
|
+
const code = (err as NodeJS.ErrnoException).code;
|
|
80
|
+
if (code === 'ENOENT') {
|
|
81
|
+
this.currentFile = null;
|
|
82
|
+
const newFile = this.getOrCreateFile();
|
|
83
|
+
appendFileSync(newFile, payload, 'utf-8');
|
|
84
|
+
} else {
|
|
85
|
+
throw err;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
resultCallback({ code: ExportResultCode.SUCCESS });
|
|
90
|
+
} catch (error) {
|
|
91
|
+
resultCallback({
|
|
92
|
+
code: ExportResultCode.FAILED,
|
|
93
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Shuts down the exporter
|
|
100
|
+
*
|
|
101
|
+
* @returns A promise that resolves when shutdown is complete
|
|
102
|
+
*/
|
|
103
|
+
async shutdown(): Promise<void> {
|
|
104
|
+
this.currentFile = null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Forces a flush of any pending data
|
|
109
|
+
*/
|
|
110
|
+
async forceFlush(): Promise<void> {
|
|
111
|
+
// No-op for file-based exporter as writes are synchronous
|
|
112
|
+
}
|
|
113
|
+
}
|