agentrail-node 0.0.0 → 0.3.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/LICENSE +201 -0
- package/README.md +179 -0
- package/dist/client.d.ts +17 -0
- package/dist/client.js +116 -0
- package/dist/http.d.ts +10 -0
- package/dist/http.js +197 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +27 -0
- package/dist/init.d.ts +4 -0
- package/dist/init.js +80 -0
- package/dist/limits.d.ts +18 -0
- package/dist/limits.js +18 -0
- package/dist/mcp.d.ts +51 -0
- package/dist/mcp.js +712 -0
- package/dist/options.d.ts +101 -0
- package/dist/options.js +79 -0
- package/dist/performance.d.ts +4 -0
- package/dist/performance.js +37 -0
- package/dist/pino.d.ts +6 -0
- package/dist/pino.js +35 -0
- package/dist/privacy.d.ts +28 -0
- package/dist/privacy.js +338 -0
- package/dist/processors.d.ts +11 -0
- package/dist/processors.js +64 -0
- package/dist/queue.d.ts +54 -0
- package/dist/queue.js +179 -0
- package/dist/runtime.d.ts +88 -0
- package/dist/runtime.js +67 -0
- package/dist/suppression.d.ts +3 -0
- package/dist/suppression.js +14 -0
- package/package.json +70 -3
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { isCaptureSuppressed } from './suppression.js';
|
|
2
|
+
import { context } from '@opentelemetry/api';
|
|
3
|
+
import { SamplingDecision } from '@opentelemetry/sdk-trace';
|
|
4
|
+
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
|
|
5
|
+
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-proto';
|
|
6
|
+
import { ExportQueue } from './queue.js';
|
|
7
|
+
import { copyLog, copySpan, estimatedBytes, payloadBytes, withoutPayloads, } from './privacy.js';
|
|
8
|
+
export function processors(runtime) {
|
|
9
|
+
const options = runtime.options;
|
|
10
|
+
const config = {
|
|
11
|
+
headers: { Authorization: `Bearer ${options.input.collector.secret}` },
|
|
12
|
+
timeoutMillis: options.queue.exportTimeoutMs,
|
|
13
|
+
};
|
|
14
|
+
const spans = new ExportQueue('traces', new OTLPTraceExporter({ ...config, url: `${options.endpoint}/v1/traces` }), options, { bytes: estimatedBytes, payloadBytes, withoutPayloads });
|
|
15
|
+
const logs = new ExportQueue('logs', new OTLPLogExporter({ ...config, url: `${options.endpoint}/v1/logs` }), options);
|
|
16
|
+
const spanProcessor = {
|
|
17
|
+
onStart(span, parent) {
|
|
18
|
+
if (isCaptureSuppressed(parent))
|
|
19
|
+
return;
|
|
20
|
+
const state = runtime.state(parent);
|
|
21
|
+
if (state === undefined)
|
|
22
|
+
return;
|
|
23
|
+
const mark = runtime.mark(parent);
|
|
24
|
+
runtime.spans.set(span, {
|
|
25
|
+
state,
|
|
26
|
+
...(mark === undefined ? {} : { mark }),
|
|
27
|
+
});
|
|
28
|
+
state.entry.span ??= span;
|
|
29
|
+
},
|
|
30
|
+
onEnd(span) {
|
|
31
|
+
const copy = copySpan(span, runtime);
|
|
32
|
+
if (copy !== undefined)
|
|
33
|
+
spans.add(copy);
|
|
34
|
+
},
|
|
35
|
+
forceFlush: () => spans.flush(),
|
|
36
|
+
shutdown: () => spans.shutdown(),
|
|
37
|
+
};
|
|
38
|
+
const logProcessor = {
|
|
39
|
+
onEmit(record, ctx = context.active()) {
|
|
40
|
+
if (isCaptureSuppressed(ctx))
|
|
41
|
+
return;
|
|
42
|
+
// An unrelated request's log must never inherit a managed request's context.
|
|
43
|
+
if (runtime.state(ctx) === undefined &&
|
|
44
|
+
options.input.logs?.uncorrelated === false)
|
|
45
|
+
return;
|
|
46
|
+
const copy = copyLog(record, runtime);
|
|
47
|
+
if (copy !== undefined)
|
|
48
|
+
logs.add(copy);
|
|
49
|
+
},
|
|
50
|
+
forceFlush: () => logs.flush(),
|
|
51
|
+
shutdown: () => logs.shutdown(),
|
|
52
|
+
};
|
|
53
|
+
const sampler = (base) => ({
|
|
54
|
+
shouldSample(ctx, traceId, name, kind, attributes, links) {
|
|
55
|
+
const result = base.shouldSample(ctx, traceId, name, kind, attributes, links);
|
|
56
|
+
return result.decision === SamplingDecision.NOT_RECORD &&
|
|
57
|
+
runtime.state(ctx) !== undefined
|
|
58
|
+
? { ...result, decision: SamplingDecision.RECORD }
|
|
59
|
+
: result;
|
|
60
|
+
},
|
|
61
|
+
toString: () => `Agentrail(${base.toString()})`,
|
|
62
|
+
});
|
|
63
|
+
return { spanProcessor, logProcessor, sampler, spans, logs };
|
|
64
|
+
}
|
package/dist/queue.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { ExportResult } from '@opentelemetry/core';
|
|
2
|
+
import { type ResolvedOptions } from './options.js';
|
|
3
|
+
interface Exporter<T> {
|
|
4
|
+
export(records: T[], callback: (result: ExportResult) => void): void;
|
|
5
|
+
shutdown(): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
/** How a queue weighs its records; a queue without one counts records only. */
|
|
8
|
+
export interface Sizing<T> {
|
|
9
|
+
/** An upper estimate of the record's encoded size. */
|
|
10
|
+
bytes(record: T): number;
|
|
11
|
+
/** The payload text it holds, against the queue's payload budget. */
|
|
12
|
+
payloadBytes(record: T): number;
|
|
13
|
+
withoutPayloads(record: T): T;
|
|
14
|
+
}
|
|
15
|
+
export interface QueueStats {
|
|
16
|
+
accepted: number;
|
|
17
|
+
exported: number;
|
|
18
|
+
dropped: number;
|
|
19
|
+
pending: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* One in-flight batch; its records still consume capacity. A batch closes at
|
|
23
|
+
* `batchSize` records or before its estimated size passes what one export
|
|
24
|
+
* carries. The vendor transport owns OTLP retry policy.
|
|
25
|
+
*/
|
|
26
|
+
export declare class ExportQueue<T> {
|
|
27
|
+
private readonly signal;
|
|
28
|
+
private readonly exporter;
|
|
29
|
+
private readonly options;
|
|
30
|
+
private readonly sizing?;
|
|
31
|
+
private records;
|
|
32
|
+
private queuedBytes;
|
|
33
|
+
/** Payload text queued or in flight. */
|
|
34
|
+
private payloadHeld;
|
|
35
|
+
private active;
|
|
36
|
+
private inFlight;
|
|
37
|
+
private cancelActive;
|
|
38
|
+
private timer;
|
|
39
|
+
private closed;
|
|
40
|
+
private stop;
|
|
41
|
+
private readonly stats;
|
|
42
|
+
constructor(signal: 'traces' | 'logs', exporter: Exporter<T>, options: ResolvedOptions, sizing?: Sizing<T> | undefined);
|
|
43
|
+
diagnostics(): QueueStats;
|
|
44
|
+
add(given: T): void;
|
|
45
|
+
private drop;
|
|
46
|
+
private exportBatch;
|
|
47
|
+
/** At least one record, so one estimated over the limit still goes, alone. */
|
|
48
|
+
private nextBatchLength;
|
|
49
|
+
private drain;
|
|
50
|
+
private bounded;
|
|
51
|
+
flush(): Promise<void>;
|
|
52
|
+
shutdown(): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
export {};
|
package/dist/queue.js
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { ExportResultCode, suppressTracing } from '@opentelemetry/core';
|
|
2
|
+
import { context } from '@opentelemetry/api';
|
|
3
|
+
import { CaptureError } from './options.js';
|
|
4
|
+
import { EXPORT_BATCH_BYTES, QUEUE_PAYLOAD_BYTES } from './limits.js';
|
|
5
|
+
/**
|
|
6
|
+
* One in-flight batch; its records still consume capacity. A batch closes at
|
|
7
|
+
* `batchSize` records or before its estimated size passes what one export
|
|
8
|
+
* carries. The vendor transport owns OTLP retry policy.
|
|
9
|
+
*/
|
|
10
|
+
export class ExportQueue {
|
|
11
|
+
signal;
|
|
12
|
+
exporter;
|
|
13
|
+
options;
|
|
14
|
+
sizing;
|
|
15
|
+
records = [];
|
|
16
|
+
queuedBytes = 0;
|
|
17
|
+
/** Payload text queued or in flight. */
|
|
18
|
+
payloadHeld = 0;
|
|
19
|
+
active;
|
|
20
|
+
inFlight = 0;
|
|
21
|
+
cancelActive;
|
|
22
|
+
timer;
|
|
23
|
+
closed = false;
|
|
24
|
+
stop;
|
|
25
|
+
stats = { accepted: 0, exported: 0, dropped: 0 };
|
|
26
|
+
constructor(signal, exporter, options, sizing) {
|
|
27
|
+
this.signal = signal;
|
|
28
|
+
this.exporter = exporter;
|
|
29
|
+
this.options = options;
|
|
30
|
+
this.sizing = sizing;
|
|
31
|
+
}
|
|
32
|
+
diagnostics() {
|
|
33
|
+
return { ...this.stats, pending: this.records.length + this.inFlight };
|
|
34
|
+
}
|
|
35
|
+
add(given) {
|
|
36
|
+
if (this.closed ||
|
|
37
|
+
this.records.length + this.inFlight >= this.options.queue.maxRecords) {
|
|
38
|
+
this.drop(this.closed ? 'closed' : 'queue_full', 1);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
let record = given;
|
|
42
|
+
let payload = 0;
|
|
43
|
+
let bytes = 0;
|
|
44
|
+
const sizing = this.sizing;
|
|
45
|
+
if (sizing !== undefined) {
|
|
46
|
+
payload = sizing.payloadBytes(record);
|
|
47
|
+
if (payload > 0 && this.payloadHeld + payload > QUEUE_PAYLOAD_BYTES) {
|
|
48
|
+
// The span still goes; only its payloads are dropped.
|
|
49
|
+
record = sizing.withoutPayloads(record);
|
|
50
|
+
payload = 0;
|
|
51
|
+
this.options.report(new CaptureError('payload_budget', this.signal, 1));
|
|
52
|
+
}
|
|
53
|
+
bytes = sizing.bytes(record);
|
|
54
|
+
}
|
|
55
|
+
this.records.push({ record, bytes, payload });
|
|
56
|
+
this.queuedBytes += bytes;
|
|
57
|
+
this.payloadHeld += payload;
|
|
58
|
+
this.stats.accepted++;
|
|
59
|
+
if (this.records.length >= this.options.queue.batchSize ||
|
|
60
|
+
this.queuedBytes >= EXPORT_BATCH_BYTES)
|
|
61
|
+
void this.drain();
|
|
62
|
+
else if (this.timer === undefined) {
|
|
63
|
+
this.timer = setTimeout(() => {
|
|
64
|
+
this.timer = undefined;
|
|
65
|
+
void this.drain();
|
|
66
|
+
}, this.options.queue.scheduledDelayMs);
|
|
67
|
+
this.timer.unref();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
drop(code, count, cause) {
|
|
71
|
+
this.stats.dropped += count;
|
|
72
|
+
this.options.report(new CaptureError(code, this.signal, count, cause));
|
|
73
|
+
}
|
|
74
|
+
exportBatch(batch) {
|
|
75
|
+
return new Promise((resolve) => {
|
|
76
|
+
let finished = false;
|
|
77
|
+
const done = (result) => {
|
|
78
|
+
if (finished)
|
|
79
|
+
return;
|
|
80
|
+
finished = true;
|
|
81
|
+
clearTimeout(timer);
|
|
82
|
+
this.cancelActive = undefined;
|
|
83
|
+
try {
|
|
84
|
+
if (result.code === ExportResultCode.SUCCESS)
|
|
85
|
+
this.stats.exported += batch.length;
|
|
86
|
+
else
|
|
87
|
+
this.drop('export_failed', batch.length, result.error);
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
resolve();
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const timer = setTimeout(() => {
|
|
94
|
+
done({
|
|
95
|
+
code: ExportResultCode.FAILED,
|
|
96
|
+
error: new Error('OTLP export deadline exceeded.'),
|
|
97
|
+
});
|
|
98
|
+
}, this.options.queue.exportTimeoutMs);
|
|
99
|
+
this.cancelActive = () => {
|
|
100
|
+
done({
|
|
101
|
+
code: ExportResultCode.FAILED,
|
|
102
|
+
error: new Error('OTLP flush or shutdown deadline exceeded.'),
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
context.with(suppressTracing(context.active()), () => {
|
|
106
|
+
this.exporter.export(batch, done);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
/** At least one record, so one estimated over the limit still goes, alone. */
|
|
111
|
+
nextBatchLength() {
|
|
112
|
+
let count = 0;
|
|
113
|
+
let bytes = 0;
|
|
114
|
+
for (const entry of this.records) {
|
|
115
|
+
if (count === this.options.queue.batchSize ||
|
|
116
|
+
(count > 0 && bytes + entry.bytes > EXPORT_BATCH_BYTES))
|
|
117
|
+
break;
|
|
118
|
+
bytes += entry.bytes;
|
|
119
|
+
count++;
|
|
120
|
+
}
|
|
121
|
+
return count;
|
|
122
|
+
}
|
|
123
|
+
drain() {
|
|
124
|
+
if (this.active !== undefined)
|
|
125
|
+
return this.active;
|
|
126
|
+
clearTimeout(this.timer);
|
|
127
|
+
this.timer = undefined;
|
|
128
|
+
this.active = (async () => {
|
|
129
|
+
while (this.records.length > 0) {
|
|
130
|
+
const batch = this.records.splice(0, this.nextBatchLength());
|
|
131
|
+
this.inFlight = batch.length;
|
|
132
|
+
for (const entry of batch)
|
|
133
|
+
this.queuedBytes -= entry.bytes;
|
|
134
|
+
await this.exportBatch(batch.map((entry) => entry.record));
|
|
135
|
+
this.inFlight = 0;
|
|
136
|
+
for (const entry of batch)
|
|
137
|
+
this.payloadHeld -= entry.payload;
|
|
138
|
+
}
|
|
139
|
+
})().finally(() => {
|
|
140
|
+
this.active = undefined;
|
|
141
|
+
// A record can arrive after the loop ended but before this microtask.
|
|
142
|
+
// Chain its drain so flush/shutdown also wait for that delivery.
|
|
143
|
+
if (this.records.length > 0)
|
|
144
|
+
return this.drain();
|
|
145
|
+
});
|
|
146
|
+
return this.active;
|
|
147
|
+
}
|
|
148
|
+
async bounded(work, timeout) {
|
|
149
|
+
let timer;
|
|
150
|
+
await Promise.race([
|
|
151
|
+
work,
|
|
152
|
+
new Promise((resolve) => {
|
|
153
|
+
timer = setTimeout(() => {
|
|
154
|
+
const count = this.records.length;
|
|
155
|
+
for (const entry of this.records)
|
|
156
|
+
this.payloadHeld -= entry.payload;
|
|
157
|
+
this.records = [];
|
|
158
|
+
this.queuedBytes = 0;
|
|
159
|
+
if (count > 0)
|
|
160
|
+
this.drop('deadline', count);
|
|
161
|
+
this.cancelActive?.();
|
|
162
|
+
resolve();
|
|
163
|
+
}, timeout);
|
|
164
|
+
}),
|
|
165
|
+
]);
|
|
166
|
+
clearTimeout(timer);
|
|
167
|
+
}
|
|
168
|
+
async flush() {
|
|
169
|
+
await this.bounded(this.drain(), this.options.queue.flushTimeoutMs);
|
|
170
|
+
}
|
|
171
|
+
shutdown() {
|
|
172
|
+
this.stop ??= (async () => {
|
|
173
|
+
this.closed = true;
|
|
174
|
+
clearTimeout(this.timer);
|
|
175
|
+
await this.bounded(this.drain().then(() => this.exporter.shutdown()), this.options.queue.shutdownTimeoutMs);
|
|
176
|
+
})();
|
|
177
|
+
return this.stop;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { Attributes, Context, Span } from '@opentelemetry/api';
|
|
2
|
+
import { type MilestoneContext, type ResolvedOptions, type VerifiedIdentity } from './options.js';
|
|
3
|
+
export interface State {
|
|
4
|
+
location?: Omit<import('agentrail-core').IpLocation, 'observedAt'> | null;
|
|
5
|
+
identity?: Attributes;
|
|
6
|
+
readonly verifiedIngress: boolean;
|
|
7
|
+
readonly links: import('@opentelemetry/api').Link[];
|
|
8
|
+
readonly entry: {
|
|
9
|
+
span?: Span;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* The names of the incoming request's headers in the order and the case
|
|
13
|
+
* they arrived (Node's `rawHeaders`), never a value; undefined where the
|
|
14
|
+
* HTTP instrumentation did not see the request.
|
|
15
|
+
*/
|
|
16
|
+
headerNames?: readonly string[];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* What the HTTP request that carried an MCP message says of itself, as the
|
|
20
|
+
* contract's `mcpSpans.requestFacts` lists it. No header's value but these
|
|
21
|
+
* three, and the names of the rest.
|
|
22
|
+
*/
|
|
23
|
+
export interface McpRequestFacts {
|
|
24
|
+
readonly method: string;
|
|
25
|
+
readonly url: string;
|
|
26
|
+
readonly userAgent?: string;
|
|
27
|
+
/** Comma-joined, as received; undefined for a request that holds none. */
|
|
28
|
+
readonly headerNames?: string;
|
|
29
|
+
readonly accept?: string;
|
|
30
|
+
readonly acceptEncoding?: string;
|
|
31
|
+
}
|
|
32
|
+
/** What the MCP capture learns of one call, filled until its span ends. */
|
|
33
|
+
export interface McpFacts {
|
|
34
|
+
readonly layer: 'transport' | 'handler';
|
|
35
|
+
readonly method: string;
|
|
36
|
+
/** Set only for a message that came by HTTP. */
|
|
37
|
+
request?: McpRequestFacts;
|
|
38
|
+
/** The message's JSON-RPC id as text; set only with `request`, and never for a notification. */
|
|
39
|
+
jsonrpcId?: string;
|
|
40
|
+
/** The status the HTTP answer carried, where the capture saw it before the call ended. */
|
|
41
|
+
httpStatus?: number;
|
|
42
|
+
sessionHash?: string;
|
|
43
|
+
requestBytes?: number;
|
|
44
|
+
responseBytes?: number;
|
|
45
|
+
errorCode?: number;
|
|
46
|
+
isError?: boolean;
|
|
47
|
+
requestPayload?: string;
|
|
48
|
+
responsePayload?: string;
|
|
49
|
+
requestTruncated?: boolean;
|
|
50
|
+
responseTruncated?: boolean;
|
|
51
|
+
}
|
|
52
|
+
export interface Mark extends Omit<MilestoneContext, 'performance'> {
|
|
53
|
+
readonly performance?: Attributes;
|
|
54
|
+
readonly name: string;
|
|
55
|
+
readonly identity?: Attributes;
|
|
56
|
+
/** False for a span that is not a step: an MCP handler inside its transport span. */
|
|
57
|
+
readonly step?: false;
|
|
58
|
+
readonly mcpFacts?: McpFacts;
|
|
59
|
+
}
|
|
60
|
+
export declare class Runtime {
|
|
61
|
+
readonly options: ResolvedOptions;
|
|
62
|
+
readonly stateKey: symbol;
|
|
63
|
+
readonly markKey: symbol;
|
|
64
|
+
readonly spans: WeakMap<object, {
|
|
65
|
+
state: State;
|
|
66
|
+
mark?: Mark;
|
|
67
|
+
}>;
|
|
68
|
+
readonly keyId: string;
|
|
69
|
+
readonly resource: import("@opentelemetry/resources").Resource;
|
|
70
|
+
constructor(options: ResolvedOptions);
|
|
71
|
+
state(ctx: Context): State | undefined;
|
|
72
|
+
mark(ctx: Context): Mark | undefined;
|
|
73
|
+
newState(verifiedIngress?: boolean, links?: State['links']): State;
|
|
74
|
+
/** The keyed hash every principal and session ID is sent as: 32 lowercase hex. */
|
|
75
|
+
hash(id: string): string;
|
|
76
|
+
identity(identity: VerifiedIdentity): Attributes;
|
|
77
|
+
}
|
|
78
|
+
/** What the MCP adapter needs of a client, kept out of its public type. */
|
|
79
|
+
export interface Internal {
|
|
80
|
+
readonly runtime: Runtime;
|
|
81
|
+
/** Starts a marked span under the active context. */
|
|
82
|
+
start(name: string, details: MilestoneContext, extra?: Pick<Mark, 'step' | 'mcpFacts'>): {
|
|
83
|
+
span: Span;
|
|
84
|
+
ctx: Context;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
export declare function registerInternal(client: object, internal: Internal): void;
|
|
88
|
+
export declare function internalOf(client: object): Internal;
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { createHash, createHmac, randomUUID } from 'node:crypto';
|
|
2
|
+
import { createContextKey } from '@opentelemetry/api';
|
|
3
|
+
import { resourceFromAttributes } from '@opentelemetry/resources';
|
|
4
|
+
import { VERSION } from 'agentrail-core';
|
|
5
|
+
import { ConfigurationError, } from './options.js';
|
|
6
|
+
export class Runtime {
|
|
7
|
+
options;
|
|
8
|
+
stateKey = createContextKey(`agentrail.state.${randomUUID()}`);
|
|
9
|
+
markKey = createContextKey(`agentrail.milestone.${randomUUID()}`);
|
|
10
|
+
spans = new WeakMap();
|
|
11
|
+
keyId;
|
|
12
|
+
resource;
|
|
13
|
+
constructor(options) {
|
|
14
|
+
this.options = options;
|
|
15
|
+
this.keyId = createHash('sha256')
|
|
16
|
+
.update(options.input.hashKey)
|
|
17
|
+
.digest('hex')
|
|
18
|
+
.slice(0, 16);
|
|
19
|
+
this.resource = resourceFromAttributes({
|
|
20
|
+
'service.name': options.input.serviceName,
|
|
21
|
+
'service.instance.id': options.instance,
|
|
22
|
+
'agentrail.component.name': 'agentrail-node',
|
|
23
|
+
'agentrail.component.version': VERSION,
|
|
24
|
+
'agentrail.hash_key_id': this.keyId,
|
|
25
|
+
'agentrail.capture.surfaces': 'http,mcp,auth,custom',
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
state(ctx) {
|
|
29
|
+
return ctx.getValue(this.stateKey);
|
|
30
|
+
}
|
|
31
|
+
mark(ctx) {
|
|
32
|
+
return ctx.getValue(this.markKey);
|
|
33
|
+
}
|
|
34
|
+
newState(verifiedIngress = false, links = []) {
|
|
35
|
+
return { verifiedIngress, links, entry: {} };
|
|
36
|
+
}
|
|
37
|
+
/** The keyed hash every principal and session ID is sent as: 32 lowercase hex. */
|
|
38
|
+
hash(id) {
|
|
39
|
+
return createHmac('sha256', this.options.input.hashKey)
|
|
40
|
+
.update(id)
|
|
41
|
+
.digest('hex')
|
|
42
|
+
.slice(0, 32);
|
|
43
|
+
}
|
|
44
|
+
identity(identity) {
|
|
45
|
+
const hash = (id) => this.hash(id);
|
|
46
|
+
return {
|
|
47
|
+
'agentrail.identity.verified': true,
|
|
48
|
+
'agentrail.principal.kind': identity.kind,
|
|
49
|
+
'agentrail.principal.id_hash': hash(identity.id),
|
|
50
|
+
...(identity.actingUserId === undefined
|
|
51
|
+
? {}
|
|
52
|
+
: {
|
|
53
|
+
'agentrail.principal.acting_user_hash': hash(identity.actingUserId),
|
|
54
|
+
}),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const internals = new WeakMap();
|
|
59
|
+
export function registerInternal(client, internal) {
|
|
60
|
+
internals.set(client, internal);
|
|
61
|
+
}
|
|
62
|
+
export function internalOf(client) {
|
|
63
|
+
const internal = internals.get(client);
|
|
64
|
+
if (internal === undefined)
|
|
65
|
+
throw new ConfigurationError('Pass the client that init or createProcessors returned.');
|
|
66
|
+
return internal;
|
|
67
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { context, createContextKey } from '@opentelemetry/api';
|
|
3
|
+
import { suppressTracing } from '@opentelemetry/core';
|
|
4
|
+
// Shared by installations in this process; never derived from a request or baggage.
|
|
5
|
+
const diagnostic = createContextKey('agentrail.capture.suppressed');
|
|
6
|
+
const reporting = new AsyncLocalStorage();
|
|
7
|
+
export function isCaptureSuppressed(ctx) {
|
|
8
|
+
return reporting.getStore() === true || ctx.getValue(diagnostic) === true;
|
|
9
|
+
}
|
|
10
|
+
export function reportWithoutCapture(run) {
|
|
11
|
+
reporting.run(true, () => {
|
|
12
|
+
context.with(suppressTracing(context.active()).setValue(diagnostic, true), run);
|
|
13
|
+
});
|
|
14
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,78 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agentrail-node",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Backend HTTP traces, structured logs and MCP journey context for Agentrail.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/agentrail-co/agentrail-js.git",
|
|
9
9
|
"directory": "packages/node"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/agentrail-co/agentrail-js/tree/main/packages/node#readme",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./pino": {
|
|
19
|
+
"types": "./dist/pino.d.ts",
|
|
20
|
+
"import": "./dist/pino.js"
|
|
21
|
+
},
|
|
22
|
+
"./mcp": {
|
|
23
|
+
"types": "./dist/mcp.d.ts",
|
|
24
|
+
"import": "./dist/mcp.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"README.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=22"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@opentelemetry/api": "1.9.1",
|
|
37
|
+
"@opentelemetry/core": "2.10.0",
|
|
38
|
+
"@opentelemetry/context-async-hooks": "2.10.0",
|
|
39
|
+
"@opentelemetry/resources": "2.10.0",
|
|
40
|
+
"@opentelemetry/sdk-trace": "2.10.0",
|
|
41
|
+
"@opentelemetry/api-logs": "0.221.0",
|
|
42
|
+
"@opentelemetry/sdk-logs": "0.221.0",
|
|
43
|
+
"@opentelemetry/exporter-trace-otlp-proto": "0.221.0",
|
|
44
|
+
"@opentelemetry/exporter-logs-otlp-proto": "0.221.0",
|
|
45
|
+
"@opentelemetry/instrumentation": "0.221.0",
|
|
46
|
+
"@opentelemetry/instrumentation-http": "0.221.0",
|
|
47
|
+
"@opentelemetry/instrumentation-undici": "0.31.0",
|
|
48
|
+
"eventsource-parser": "3.1.1",
|
|
49
|
+
"agentrail-core": "0.3.0"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"pino": ">=9 <11",
|
|
53
|
+
"@modelcontextprotocol/server": ">=2.3.1 <3"
|
|
54
|
+
},
|
|
55
|
+
"peerDependenciesMeta": {
|
|
56
|
+
"pino": {
|
|
57
|
+
"optional": true
|
|
58
|
+
},
|
|
59
|
+
"@modelcontextprotocol/server": {
|
|
60
|
+
"optional": true
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"devDependencies": {
|
|
64
|
+
"pino": "10.3.1",
|
|
65
|
+
"protobufjs": "7.6.5",
|
|
66
|
+
"@modelcontextprotocol/server": "2.3.1",
|
|
67
|
+
"@modelcontextprotocol/client": "2.3.1",
|
|
68
|
+
"zod": "4.4.3",
|
|
69
|
+
"@sentry/node": "10.76.2",
|
|
70
|
+
"@sentry/opentelemetry": "10.76.2",
|
|
71
|
+
"agentrail-contract": "0.0.0"
|
|
72
|
+
},
|
|
73
|
+
"scripts": {
|
|
74
|
+
"build": "tsc -p tsconfig.build.json",
|
|
75
|
+
"typecheck": "tsc --noEmit",
|
|
76
|
+
"test": "vitest run"
|
|
10
77
|
}
|
|
11
|
-
}
|
|
78
|
+
}
|