@keemakr/observability 0.2.0-dev-20261012160211 → 0.2.0-dev-20261124171750
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/README.md +19 -24
- package/dist/content-policy-processor.d.ts +5 -1
- package/dist/content-policy-processor.js +30 -4
- package/dist/eve-session.d.ts +2 -1
- package/dist/eve-session.js +2 -1
- package/dist/execution-context.d.ts +5 -0
- package/dist/execution-context.js +19 -0
- package/dist/host.d.ts +43 -18
- package/dist/host.js +54 -51
- package/dist/index.d.ts +7 -13
- package/dist/index.js +23 -22
- package/dist/policy.d.ts +17 -9
- package/dist/policy.js +48 -25
- package/package.json +1 -5
package/README.md
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
Keemakr telemetry, in two entry points. One process registers the host once. Everything else traces
|
|
4
4
|
through the runtime and never learns which backend is behind it.
|
|
5
5
|
|
|
6
|
-
| Entry | Who imports it | What it does
|
|
7
|
-
| ----------------------------- | ---------------------------------------------------------------------------------------- |
|
|
8
|
-
| `@keemakr/observability/host` | The one instrumentation file of a process: Core, Operator, Task Operator, a Pack Runtime | Registers OpenTelemetry via `@vercel/otel
|
|
9
|
-
| `@keemakr/observability` | Core libraries, `@keemakr/agent-sdk`, Pack code | Execution
|
|
6
|
+
| Entry | Who imports it | What it does |
|
|
7
|
+
| ----------------------------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ |
|
|
8
|
+
| `@keemakr/observability/host` | The one instrumentation file of a process: Core, Operator, Task Operator, a Pack Runtime | Registers OpenTelemetry via `@vercel/otel` (Vercel tracing on Vercel), applies the sampler and the content policy. |
|
|
9
|
+
| `@keemakr/observability` | Core libraries, `@keemakr/agent-sdk`, Pack code | Execution identity on spans, trace-context helpers, span names. Plain OpenTelemetry; a no-op with no host. |
|
|
10
10
|
|
|
11
11
|
This follows the Observability PRD: eve's tracing is the agent span model, the host owns the
|
|
12
12
|
provider and exporter, SDKs only participate, metadata is the default and content is an explicit
|
|
@@ -19,26 +19,21 @@ decision, W3C `traceparent` is the propagation contract.
|
|
|
19
19
|
import { registerKeemakrTelemetry } from '@keemakr/observability/host';
|
|
20
20
|
|
|
21
21
|
const telemetry = await registerKeemakrTelemetry({ service: 'keemakr-core' });
|
|
22
|
-
// telemetry.destination '
|
|
22
|
+
// telemetry.destination 'vercel' | 'otlp' | 'none'
|
|
23
23
|
// telemetry.recordContent the content decision — hand it to eve / the AI SDK
|
|
24
|
-
// telemetry.flush() before a serverless function returns
|
|
25
24
|
```
|
|
26
25
|
|
|
27
|
-
| Variable
|
|
28
|
-
|
|
|
29
|
-
|
|
|
30
|
-
| `OTEL_EXPORTER_OTLP_ENDPOINT`
|
|
31
|
-
| `OTEL_RECORD_CONTENT`
|
|
32
|
-
| `VERCEL_GIT_COMMIT_REF
|
|
26
|
+
| Variable | Effect |
|
|
27
|
+
| ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
28
|
+
| Running on Vercel | Spans go to Vercel traces; the project's sampling rules (`vercel traces config`) decide what is kept. |
|
|
29
|
+
| `OTEL_EXPORTER_OTLP_ENDPOINT` | Any OTLP backend, read by `@vercel/otel`. |
|
|
30
|
+
| `OTEL_RECORD_CONTENT` | `0`, `false`, `off` or `no` turns content off. Unset or anything else: on, on every tier. |
|
|
31
|
+
| `VERCEL_GIT_COMMIT_REF` | Becomes `deployment.environment.name`. The branch maps `main`→`production`, `stage`→`staging`, `dev`→`dev`; other branches `preview`; a laptop `development`. `VERCEL_ENV` is `production` on all three projects, so it is not used. |
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
any `keemakr-*` tracer, and the root request span so a trace has a root. Framework internals and
|
|
36
|
-
outbound `fetch` spans do not. Root spans for `/api/internal/*`, middleware, `/_next/*` and
|
|
37
|
-
`/api/health` are not sampled at all.
|
|
33
|
+
Root spans for `/api/internal/*`, middleware, `/_next/*` and `/api/health` are not sampled.
|
|
38
34
|
|
|
39
|
-
|
|
40
|
-
results
|
|
41
|
-
whichever library produced the span.
|
|
35
|
+
In a `registerKeemakrTelemetry` host, when content capture is off, `gen_ai.input.messages`, `gen_ai.output.messages`, tool arguments and
|
|
36
|
+
results leave as `[redacted: content capture off]`, whichever library produced the span. A query string is dropped from every request URL either way. In an eve process, eve's Agent Runs exporter sees spans before these processors run, so eve's `tracePolicy` is the content ceiling there.
|
|
42
37
|
|
|
43
38
|
## Runtime
|
|
44
39
|
|
|
@@ -51,9 +46,9 @@ await withExecutionContext(
|
|
|
51
46
|
);
|
|
52
47
|
```
|
|
53
48
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
49
|
+
The active span and every span started inside it, by Core, eve, the AI SDK or a library, carry the
|
|
50
|
+
safe `keemakr.*` identifiers (`keemakr.task_id`, `keemakr.conversation_id`, …). The tenant is a
|
|
51
|
+
12-hex hash, never the id. Nest freely. `executionAttributes(identity)` returns the same set for a
|
|
52
|
+
span you start yourself.
|
|
57
53
|
|
|
58
|
-
|
|
59
|
-
target names, so a rename is a breaking change.
|
|
54
|
+
Span names are constants in `OBSERVATION`. Anything that filters traces by span name breaks on a rename.
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { SpanProcessor } from '@opentelemetry/sdk-trace-base';
|
|
2
|
+
/** HTTP span attributes that carry the request URL with its query string. */
|
|
3
|
+
export declare const URL_ATTRIBUTES: string[];
|
|
2
4
|
/**
|
|
3
|
-
* Redact native GenAI
|
|
5
|
+
* Redact native GenAI content before the exporters that follow this processor receive it. In an
|
|
6
|
+
* eve host, eve's `agent-runs` slot exports first, so that destination needs
|
|
7
|
+
* `agentRunsExportPolicy()` from the host as well.
|
|
4
8
|
*
|
|
5
9
|
* A query string is dropped from every span whatever the content decision: it is
|
|
6
10
|
* where bearer values ride when a header is not possible (a provider webhook
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { isContentAttribute, REDACTED } from './policy.js';
|
|
1
|
+
import { isContentAttribute, isModelCallSpan, REDACTED, REDACTED_QUERY } from './policy.js';
|
|
2
2
|
/** HTTP span attributes that carry the request URL with its query string. */
|
|
3
|
-
const URL_ATTRIBUTES = ['http.target', 'http.url', 'url.full'];
|
|
3
|
+
export const URL_ATTRIBUTES = ['http.target', 'http.url', 'url.full'];
|
|
4
4
|
/**
|
|
5
|
-
* Redact native GenAI
|
|
5
|
+
* Redact native GenAI content before the exporters that follow this processor receive it. In an
|
|
6
|
+
* eve host, eve's `agent-runs` slot exports first, so that destination needs
|
|
7
|
+
* `agentRunsExportPolicy()` from the host as well.
|
|
6
8
|
*
|
|
7
9
|
* A query string is dropped from every span whatever the content decision: it is
|
|
8
10
|
* where bearer values ride when a header is not possible (a provider webhook
|
|
@@ -20,15 +22,39 @@ export function contentPolicyProcessor(recordContent) {
|
|
|
20
22
|
}
|
|
21
23
|
}
|
|
22
24
|
if ('url.query' in span.attributes)
|
|
23
|
-
span.attributes['url.query'] =
|
|
25
|
+
span.attributes['url.query'] = REDACTED_QUERY;
|
|
24
26
|
if (recordContent)
|
|
25
27
|
return;
|
|
26
28
|
for (const key of Object.keys(span.attributes)) {
|
|
27
29
|
if (isContentAttribute(key))
|
|
28
30
|
span.attributes[key] = REDACTED;
|
|
29
31
|
}
|
|
32
|
+
if (isModelCallSpan(span.name, span.attributes))
|
|
33
|
+
keepOnlyErrorNames(span);
|
|
30
34
|
},
|
|
31
35
|
async forceFlush() { },
|
|
32
36
|
async shutdown() { },
|
|
33
37
|
};
|
|
34
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* The AI SDK writes a failed model call's `error.message` and stack onto the span status and its
|
|
41
|
+
* exception event whatever `recordInputs` says, and a provider's error can quote the prompt
|
|
42
|
+
* back. Only the error's name is kept, which still says what kind of failure it was.
|
|
43
|
+
*/
|
|
44
|
+
function keepOnlyErrorNames(span) {
|
|
45
|
+
let errorName;
|
|
46
|
+
for (const event of span.events) {
|
|
47
|
+
if (event.name !== 'exception' || !event.attributes)
|
|
48
|
+
continue;
|
|
49
|
+
const type = event.attributes['exception.type'];
|
|
50
|
+
if (typeof type === 'string')
|
|
51
|
+
errorName = type;
|
|
52
|
+
if ('exception.message' in event.attributes)
|
|
53
|
+
event.attributes['exception.message'] = REDACTED;
|
|
54
|
+
if ('exception.stacktrace' in event.attributes) {
|
|
55
|
+
event.attributes['exception.stacktrace'] = REDACTED;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (span.status.message)
|
|
59
|
+
span.status.message = errorName ?? REDACTED;
|
|
60
|
+
}
|
package/dist/eve-session.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import type { SpanProcessor } from '@opentelemetry/sdk-trace-base';
|
|
2
|
-
/** AI SDK v7 prefixes
|
|
2
|
+
/** AI SDK v7 prefixes eve's runtime context, so the conversation is copied to the
|
|
3
|
+
* OpenTelemetry `session.id` and inherited by child spans. */
|
|
3
4
|
export declare function eveSessionProcessor(): SpanProcessor;
|
package/dist/eve-session.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { trace } from '@opentelemetry/api';
|
|
2
|
-
/** AI SDK v7 prefixes
|
|
2
|
+
/** AI SDK v7 prefixes eve's runtime context, so the conversation is copied to the
|
|
3
|
+
* OpenTelemetry `session.id` and inherited by child spans. */
|
|
3
4
|
export function eveSessionProcessor() {
|
|
4
5
|
return {
|
|
5
6
|
onStart(span, parentContext) {
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { SpanProcessor } from '@opentelemetry/sdk-trace-base';
|
|
2
|
+
export declare const EXECUTION: symbol;
|
|
3
|
+
/** The host side of `withExecutionContext`. A span's own attributes win: a span that names its
|
|
4
|
+
* task or component knows better than the context around it. */
|
|
5
|
+
export declare function executionContextProcessor(): SpanProcessor;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { createContextKey } from '@opentelemetry/api';
|
|
2
|
+
export const EXECUTION = createContextKey('keemakr.execution');
|
|
3
|
+
/** The host side of `withExecutionContext`. A span's own attributes win: a span that names its
|
|
4
|
+
* task or component knows better than the context around it. */
|
|
5
|
+
export function executionContextProcessor() {
|
|
6
|
+
return {
|
|
7
|
+
onStart(span, parentContext) {
|
|
8
|
+
const attrs = parentContext.getValue(EXECUTION);
|
|
9
|
+
if (!attrs)
|
|
10
|
+
return;
|
|
11
|
+
for (const [key, value] of Object.entries(attrs))
|
|
12
|
+
if (!(key in span.attributes) && value !== undefined)
|
|
13
|
+
span.setAttribute(key, value);
|
|
14
|
+
},
|
|
15
|
+
onEnd() { },
|
|
16
|
+
async forceFlush() { },
|
|
17
|
+
async shutdown() { },
|
|
18
|
+
};
|
|
19
|
+
}
|
package/dist/host.d.ts
CHANGED
|
@@ -1,45 +1,70 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { SpanProcessor } from '@opentelemetry/sdk-trace-base';
|
|
2
2
|
import { keemakrSampler } from './sampler.js';
|
|
3
3
|
export interface TelemetryHostOptions {
|
|
4
4
|
/** `service.name` on every span. */
|
|
5
5
|
service: string;
|
|
6
6
|
/** Defaults to `telemetryEnvironment()`. */
|
|
7
7
|
environment?: string;
|
|
8
|
-
/** Defaults to `telemetryRelease()`. */
|
|
9
|
-
release?: string;
|
|
10
8
|
/** Defaults to `contentCaptureAllowed()`. */
|
|
11
9
|
recordContent?: boolean;
|
|
12
10
|
env?: NodeJS.ProcessEnv;
|
|
13
11
|
}
|
|
14
12
|
export interface TelemetryHost {
|
|
15
13
|
exporting: boolean;
|
|
16
|
-
destination: '
|
|
14
|
+
destination: 'vercel' | 'otlp' | 'none';
|
|
17
15
|
environment: string;
|
|
18
16
|
/** Hand this to eve and the AI SDK so every producer obeys one content decision. */
|
|
19
17
|
recordContent: boolean;
|
|
20
|
-
/** Serverless handlers call this before they return. */
|
|
21
|
-
flush(): Promise<void>;
|
|
22
18
|
}
|
|
23
|
-
export declare function langfuseConfigured(env?: NodeJS.ProcessEnv): boolean;
|
|
24
19
|
export declare function otlpConfigured(env?: NodeJS.ProcessEnv): boolean;
|
|
25
|
-
/**
|
|
26
|
-
* On top of the Langfuse SDK default (its own spans, `gen_ai.*`, known LLM
|
|
27
|
-
* scopes): eve's spans, durable workflow spans, Keemakr's tracers, and the host framework's ROOT span
|
|
28
|
-
* so the trace has a root and its children are not orphans in the UI.
|
|
29
|
-
* Framework internals (rendering, fetch, DB) stay out.
|
|
30
|
-
*/
|
|
31
|
-
export declare function shouldExportToLangfuse(span: ReadableSpan): boolean;
|
|
32
20
|
/**
|
|
33
21
|
* Keemakr's span pipeline without registering a provider, for a host that
|
|
34
|
-
* owns OpenTelemetry itself (eve's `otelIntegration`).
|
|
35
|
-
*
|
|
22
|
+
* owns OpenTelemetry itself (eve's `otelIntegration`). eve exports through its
|
|
23
|
+
* own Agent Runs slot, so `destination` describes a `registerKeemakrTelemetry`
|
|
24
|
+
* host only.
|
|
36
25
|
*/
|
|
37
26
|
export declare function keemakrTelemetry(opts: TelemetryHostOptions): TelemetryHost & {
|
|
38
27
|
spanProcessors: SpanProcessor[];
|
|
39
28
|
};
|
|
29
|
+
/** The shape of eve's `SpanExportPolicy`, kept structural so this package does not depend on eve. */
|
|
30
|
+
export interface AgentRunsExportPolicy {
|
|
31
|
+
span: (span: {
|
|
32
|
+
name: string;
|
|
33
|
+
attributes: Readonly<Record<string, unknown>>;
|
|
34
|
+
}) => {
|
|
35
|
+
emit: true;
|
|
36
|
+
} | {
|
|
37
|
+
redact: true;
|
|
38
|
+
inputs: true;
|
|
39
|
+
outputs: true;
|
|
40
|
+
};
|
|
41
|
+
attribute: (input: {
|
|
42
|
+
key: string;
|
|
43
|
+
value: unknown;
|
|
44
|
+
}) => {
|
|
45
|
+
emit: true;
|
|
46
|
+
} | {
|
|
47
|
+
replace: true;
|
|
48
|
+
value: string;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The content policy for eve's Agent Runs destination:
|
|
53
|
+
* `export default agentRuns({ exportPolicy: agentRunsExportPolicy() })` in
|
|
54
|
+
* `agent/instrumentation/agent-runs.ts`.
|
|
55
|
+
*
|
|
56
|
+
* eve orders instrumentation slots by name, so its `agent-runs` exporter receives
|
|
57
|
+
* a span before the `keemakrTelemetry()` processors in any later slot can change
|
|
58
|
+
* it. eve's `tracePolicy` keeps prompts and replies from being recorded, but a
|
|
59
|
+
* failed model call's error message (which can quote the prompt) and a request
|
|
60
|
+
* URL's query string still reach Vercel unless this destination redacts them.
|
|
61
|
+
* Every other span keeps its error message: tool and Core errors are what a
|
|
62
|
+
* developer debugs from.
|
|
63
|
+
*/
|
|
64
|
+
export declare function agentRunsExportPolicy(recordContent?: boolean): AgentRunsExportPolicy;
|
|
40
65
|
/**
|
|
41
|
-
* Idempotent per process (`@vercel/otel` guards). With
|
|
42
|
-
* provider is still installed so context propagates
|
|
66
|
+
* Idempotent per process (`@vercel/otel` guards). With no backend configured
|
|
67
|
+
* the provider is still installed so context propagates.
|
|
43
68
|
*/
|
|
44
69
|
export declare function registerKeemakrTelemetry(opts: TelemetryHostOptions): Promise<TelemetryHost>;
|
|
45
70
|
export { keemakrSampler };
|
package/dist/host.js
CHANGED
|
@@ -1,87 +1,90 @@
|
|
|
1
1
|
// The one module per process that owns OpenTelemetry (Observability PRD: the
|
|
2
2
|
// host owns provider, sampling, exporters and shutdown; libraries participate).
|
|
3
|
-
|
|
4
|
-
import { LangfuseSpanProcessor, isDefaultExportSpan } from '@langfuse/otel';
|
|
5
|
-
import { contentCaptureAllowed, telemetryEnvironment, telemetryRelease } from './policy.js';
|
|
3
|
+
import { contentCaptureAllowed, isContentAttribute, isModelCallSpan, REDACTED, REDACTED_QUERY, telemetryEnvironment, } from './policy.js';
|
|
6
4
|
import { keemakrSampler } from './sampler.js';
|
|
7
|
-
import { KEEMAKR_TRACER_PREFIX } from './index.js';
|
|
8
5
|
import { eveSessionProcessor } from './eve-session.js';
|
|
9
|
-
import { contentPolicyProcessor } from './content-policy-processor.js';
|
|
10
|
-
|
|
11
|
-
return Boolean(env.LANGFUSE_PUBLIC_KEY?.trim() && env.LANGFUSE_SECRET_KEY?.trim());
|
|
12
|
-
}
|
|
6
|
+
import { contentPolicyProcessor, URL_ATTRIBUTES } from './content-policy-processor.js';
|
|
7
|
+
import { executionContextProcessor } from './execution-context.js';
|
|
13
8
|
export function otlpConfigured(env = process.env) {
|
|
14
9
|
return Boolean(env.OTEL_EXPORTER_OTLP_ENDPOINT || env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT);
|
|
15
10
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (isDefaultExportSpan(span))
|
|
24
|
-
return true;
|
|
25
|
-
const scope = span.instrumentationScope.name;
|
|
26
|
-
// Eve's durable workflow steps are ancestors of ai.eve.turn.
|
|
27
|
-
if (scope === 'workflow')
|
|
28
|
-
return true;
|
|
29
|
-
if (scope === 'eve' || scope.startsWith('eve/'))
|
|
30
|
-
return true;
|
|
31
|
-
if (scope.startsWith(KEEMAKR_TRACER_PREFIX))
|
|
32
|
-
return true;
|
|
33
|
-
return span.parentSpanContext === undefined;
|
|
11
|
+
// On Vercel, @vercel/otel's 'auto' exporter reports spans to the platform through the request
|
|
12
|
+
// context, and the project's tracing config decides what is kept. Elsewhere only an OTLP
|
|
13
|
+
// endpoint can receive them.
|
|
14
|
+
function destinationOf(env) {
|
|
15
|
+
if (env.VERCEL)
|
|
16
|
+
return 'vercel';
|
|
17
|
+
return otlpConfigured(env) ? 'otlp' : 'none';
|
|
34
18
|
}
|
|
35
19
|
/**
|
|
36
20
|
* Keemakr's span pipeline without registering a provider, for a host that
|
|
37
|
-
* owns OpenTelemetry itself (eve's `otelIntegration`).
|
|
38
|
-
*
|
|
21
|
+
* owns OpenTelemetry itself (eve's `otelIntegration`). eve exports through its
|
|
22
|
+
* own Agent Runs slot, so `destination` describes a `registerKeemakrTelemetry`
|
|
23
|
+
* host only.
|
|
39
24
|
*/
|
|
40
25
|
export function keemakrTelemetry(opts) {
|
|
41
26
|
const env = opts.env ?? process.env;
|
|
42
|
-
const environment = opts.environment ?? telemetryEnvironment(env);
|
|
43
27
|
const recordContent = opts.recordContent ?? contentCaptureAllowed(env);
|
|
44
|
-
const
|
|
45
|
-
const otlp = otlpConfigured(env);
|
|
46
|
-
const processor = langfuse
|
|
47
|
-
? new LangfuseSpanProcessor({
|
|
48
|
-
environment,
|
|
49
|
-
release: opts.release ?? telemetryRelease(env),
|
|
50
|
-
shouldExportSpan: ({ otelSpan }) => shouldExportToLangfuse(otelSpan),
|
|
51
|
-
// A serverless function can be frozen right after it responds.
|
|
52
|
-
exportMode: env.VERCEL ? 'immediate' : 'batched',
|
|
53
|
-
})
|
|
54
|
-
: null;
|
|
28
|
+
const destination = destinationOf(env);
|
|
55
29
|
return {
|
|
56
30
|
spanProcessors: [
|
|
57
31
|
contentPolicyProcessor(recordContent),
|
|
58
32
|
eveSessionProcessor(),
|
|
59
|
-
|
|
33
|
+
executionContextProcessor(),
|
|
60
34
|
],
|
|
61
|
-
exporting:
|
|
62
|
-
destination
|
|
63
|
-
environment,
|
|
35
|
+
exporting: destination !== 'none',
|
|
36
|
+
destination,
|
|
37
|
+
environment: opts.environment ?? telemetryEnvironment(env),
|
|
64
38
|
recordContent,
|
|
65
|
-
|
|
66
|
-
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The content policy for eve's Agent Runs destination:
|
|
43
|
+
* `export default agentRuns({ exportPolicy: agentRunsExportPolicy() })` in
|
|
44
|
+
* `agent/instrumentation/agent-runs.ts`.
|
|
45
|
+
*
|
|
46
|
+
* eve orders instrumentation slots by name, so its `agent-runs` exporter receives
|
|
47
|
+
* a span before the `keemakrTelemetry()` processors in any later slot can change
|
|
48
|
+
* it. eve's `tracePolicy` keeps prompts and replies from being recorded, but a
|
|
49
|
+
* failed model call's error message (which can quote the prompt) and a request
|
|
50
|
+
* URL's query string still reach Vercel unless this destination redacts them.
|
|
51
|
+
* Every other span keeps its error message: tool and Core errors are what a
|
|
52
|
+
* developer debugs from.
|
|
53
|
+
*/
|
|
54
|
+
export function agentRunsExportPolicy(recordContent = contentCaptureAllowed()) {
|
|
55
|
+
return {
|
|
56
|
+
// eve's output redaction is what drops the status message and exception events; the AI SDK
|
|
57
|
+
// puts the error name only on that event, so Agent Runs keeps the status code alone.
|
|
58
|
+
span: ({ name, attributes }) => recordContent || !isModelCallSpan(name, attributes)
|
|
59
|
+
? { emit: true }
|
|
60
|
+
: { redact: true, inputs: true, outputs: true },
|
|
61
|
+
attribute: ({ key, value }) => {
|
|
62
|
+
// eve's own redaction matches exact keys only and misses some AI SDK prefixes.
|
|
63
|
+
if (!recordContent && isContentAttribute(key))
|
|
64
|
+
return { replace: true, value: REDACTED };
|
|
65
|
+
if (key === 'url.query')
|
|
66
|
+
return { replace: true, value: REDACTED_QUERY };
|
|
67
|
+
if (URL_ATTRIBUTES.includes(key) && typeof value === 'string' && value.includes('?')) {
|
|
68
|
+
return { replace: true, value: value.slice(0, value.indexOf('?')) };
|
|
69
|
+
}
|
|
70
|
+
return { emit: true };
|
|
67
71
|
},
|
|
68
72
|
};
|
|
69
73
|
}
|
|
70
74
|
/**
|
|
71
|
-
* Idempotent per process (`@vercel/otel` guards). With
|
|
72
|
-
* provider is still installed so context propagates
|
|
75
|
+
* Idempotent per process (`@vercel/otel` guards). With no backend configured
|
|
76
|
+
* the provider is still installed so context propagates.
|
|
73
77
|
*/
|
|
74
78
|
export async function registerKeemakrTelemetry(opts) {
|
|
75
79
|
const { spanProcessors, ...host } = keemakrTelemetry(opts);
|
|
76
|
-
const [contentPolicy, eveSession, ...exporters] = spanProcessors;
|
|
77
80
|
// `@vercel/otel` is an optional peer and its Node bundle must load as ESM.
|
|
78
81
|
const { registerOTel } = await import('@vercel/otel');
|
|
79
82
|
registerOTel({
|
|
80
83
|
serviceName: opts.service,
|
|
81
84
|
attributes: { 'deployment.environment.name': host.environment },
|
|
82
85
|
traceSampler: keemakrSampler(),
|
|
83
|
-
// 'auto'
|
|
84
|
-
spanProcessors: [
|
|
86
|
+
// The content policy runs before 'auto' so no exporter sees what it redacts.
|
|
87
|
+
spanProcessors: [...spanProcessors, 'auto'],
|
|
85
88
|
});
|
|
86
89
|
return host;
|
|
87
90
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import { type Attributes } from '@opentelemetry/api';
|
|
2
2
|
export { contentCaptureAllowed, telemetryEnvironment } from './policy.js';
|
|
3
|
-
/** Every Keemakr tracer name starts with this, so a host can allowlist them. */
|
|
4
3
|
export declare const KEEMAKR_TRACER_PREFIX = "keemakr";
|
|
5
4
|
export declare function keemakrTracer(component: string): import("@opentelemetry/api").Tracer;
|
|
6
5
|
export type ExecutionLane = 'inline' | 'agent_task' | 'workflow_task' | 'delegation' | 'planning';
|
|
@@ -24,17 +23,12 @@ export interface ExecutionIdentity {
|
|
|
24
23
|
}
|
|
25
24
|
/** A tenant is never exported raw (PRD: opaque identifiers for third parties). */
|
|
26
25
|
export declare function opaqueTenant(tenantId: string): string;
|
|
27
|
-
|
|
28
|
-
export declare function
|
|
29
|
-
sessionId: string | undefined;
|
|
30
|
-
tags: string[];
|
|
31
|
-
metadata: Record<string, string>;
|
|
32
|
-
version: string | undefined;
|
|
33
|
-
};
|
|
26
|
+
/** The `keemakr.*` span attributes for an execution: the ids a trace search filters on. */
|
|
27
|
+
export declare function executionAttributes(id: ExecutionIdentity): Attributes;
|
|
34
28
|
/**
|
|
35
|
-
* Put this execution's identity on
|
|
36
|
-
*
|
|
37
|
-
*
|
|
29
|
+
* Put this execution's identity on the active span and on every span started
|
|
30
|
+
* beneath `fn`, whoever creates it: a trace search on a child span only sees
|
|
31
|
+
* the child's own attributes. Nested contexts add to the outer one.
|
|
38
32
|
*/
|
|
39
33
|
export declare function withExecutionContext<T>(id: ExecutionIdentity, fn: () => T): T;
|
|
40
34
|
export declare function isTraceId(id: string | null | undefined): id is string;
|
|
@@ -42,7 +36,7 @@ export declare function isTraceId(id: string | null | undefined): id is string;
|
|
|
42
36
|
export declare function activeTraceId(): string | null;
|
|
43
37
|
/** Add `traceparent` and `tracestate` to headers for a call leaving this process. */
|
|
44
38
|
export declare function injectTraceparent(headers: Record<string, string>): Record<string, string>;
|
|
45
|
-
/**
|
|
39
|
+
/** Anything that filters traces by span name breaks when one of these is renamed. */
|
|
46
40
|
export declare const OBSERVATION: {
|
|
47
41
|
readonly capabilityInvoke: "capability.invoke";
|
|
48
42
|
readonly workflowDispatch: "workflow.dispatch";
|
package/dist/index.js
CHANGED
|
@@ -2,10 +2,8 @@
|
|
|
2
2
|
// no-op, and nothing here may throw into the caller.
|
|
3
3
|
import { createHash } from 'node:crypto';
|
|
4
4
|
import { context, propagation, trace } from '@opentelemetry/api';
|
|
5
|
-
import {
|
|
6
|
-
export { getActiveSpanId, getActiveTraceId, observe, startActiveObservation, startObservation, updateActiveObservation, } from '@langfuse/tracing';
|
|
5
|
+
import { EXECUTION } from './execution-context.js';
|
|
7
6
|
export { contentCaptureAllowed, telemetryEnvironment } from './policy.js';
|
|
8
|
-
/** Every Keemakr tracer name starts with this, so a host can allowlist them. */
|
|
9
7
|
export const KEEMAKR_TRACER_PREFIX = 'keemakr';
|
|
10
8
|
export function keemakrTracer(component) {
|
|
11
9
|
return trace.getTracer(`${KEEMAKR_TRACER_PREFIX}-${component}`);
|
|
@@ -14,14 +12,12 @@ export function keemakrTracer(component) {
|
|
|
14
12
|
export function opaqueTenant(tenantId) {
|
|
15
13
|
return createHash('sha256').update(tenantId).digest('hex').slice(0, 12);
|
|
16
14
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
export function propagatedAttributes(id) {
|
|
21
|
-
const metadata = { component: id.component };
|
|
15
|
+
/** The `keemakr.*` span attributes for an execution: the ids a trace search filters on. */
|
|
16
|
+
export function executionAttributes(id) {
|
|
17
|
+
const attrs = { 'keemakr.component': id.component };
|
|
22
18
|
const put = (k, v) => {
|
|
23
19
|
if (v)
|
|
24
|
-
|
|
20
|
+
attrs[`keemakr.${k}`] = v;
|
|
25
21
|
};
|
|
26
22
|
put('lane', id.lane);
|
|
27
23
|
put('tenant', id.tenantId ? opaqueTenant(id.tenantId) : undefined);
|
|
@@ -35,22 +31,27 @@ export function propagatedAttributes(id) {
|
|
|
35
31
|
put('workflow_id', id.workflowId);
|
|
36
32
|
put('delegation_id', id.delegationId);
|
|
37
33
|
put('caller_kind', id.callerKind);
|
|
38
|
-
|
|
39
|
-
return
|
|
40
|
-
sessionId: sessionIdFor(id),
|
|
41
|
-
tags,
|
|
42
|
-
metadata,
|
|
43
|
-
version: id.version ?? undefined,
|
|
44
|
-
};
|
|
34
|
+
put('version', id.version);
|
|
35
|
+
return attrs;
|
|
45
36
|
}
|
|
46
37
|
/**
|
|
47
|
-
* Put this execution's identity on
|
|
48
|
-
*
|
|
49
|
-
*
|
|
38
|
+
* Put this execution's identity on the active span and on every span started
|
|
39
|
+
* beneath `fn`, whoever creates it: a trace search on a child span only sees
|
|
40
|
+
* the child's own attributes. Nested contexts add to the outer one.
|
|
50
41
|
*/
|
|
51
42
|
export function withExecutionContext(id, fn) {
|
|
52
|
-
|
|
53
|
-
|
|
43
|
+
const outer = context.active();
|
|
44
|
+
const attrs = {
|
|
45
|
+
...outer.getValue(EXECUTION),
|
|
46
|
+
...executionAttributes(id),
|
|
47
|
+
};
|
|
48
|
+
try {
|
|
49
|
+
trace.getActiveSpan()?.setAttributes(attrs);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// telemetry never fails the caller
|
|
53
|
+
}
|
|
54
|
+
return context.with(outer.setValue(EXECUTION, attrs), fn);
|
|
54
55
|
}
|
|
55
56
|
const W3C_TRACE_ID = /^[0-9a-f]{32}$/;
|
|
56
57
|
const NO_TRACE = '0'.repeat(32);
|
|
@@ -73,7 +74,7 @@ export function injectTraceparent(headers) {
|
|
|
73
74
|
}
|
|
74
75
|
return out;
|
|
75
76
|
}
|
|
76
|
-
/**
|
|
77
|
+
/** Anything that filters traces by span name breaks when one of these is renamed. */
|
|
77
78
|
export const OBSERVATION = {
|
|
78
79
|
capabilityInvoke: 'capability.invoke',
|
|
79
80
|
workflowDispatch: 'workflow.dispatch',
|
package/dist/policy.d.ts
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
export type TelemetryEnvironment = 'development' | 'dev' | 'staging' | 'production' | 'preview' | (string & {});
|
|
2
|
-
/** The
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
/** The `deployment.environment.name` of this process: the tier from the
|
|
3
|
+
* deployed branch; a feature-branch deploy is `preview`; a machine with no
|
|
4
|
+
* Vercel context is `development`. */
|
|
5
5
|
export declare function telemetryEnvironment(env?: NodeJS.ProcessEnv): TelemetryEnvironment;
|
|
6
|
-
export declare function telemetryRelease(env?: NodeJS.ProcessEnv): string | undefined;
|
|
7
6
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
7
|
+
* Content is recorded unless a deployment turns it off with `OTEL_RECORD_CONTENT=0`. Visibility
|
|
8
|
+
* by default was a product decision (18 Nov 2026): a developer debugging a run needs the prompt,
|
|
9
|
+
* the reply and the error. A process that exports to another OTLP endpoint sends it there too,
|
|
10
|
+
* so turn it off for one.
|
|
11
11
|
*/
|
|
12
12
|
export declare function contentCaptureAllowed(env?: NodeJS.ProcessEnv): boolean;
|
|
13
13
|
/** A marker, so a reader knows content existed and was withheld. */
|
|
14
14
|
export declare const REDACTED = "[redacted: content capture off]";
|
|
15
|
-
/**
|
|
16
|
-
|
|
15
|
+
/** A query string carries credentials (webhook secrets, OAuth codes), so it is withheld always. */
|
|
16
|
+
export declare const REDACTED_QUERY = "[redacted: query string]";
|
|
17
|
+
/** `gen_ai.*` is the OpenTelemetry GenAI convention (eve, the AI SDK), `ai.*`
|
|
18
|
+
* the AI SDK's own names. */
|
|
17
19
|
export declare const CONTENT_ATTRIBUTE_PREFIXES: readonly string[];
|
|
18
20
|
export declare function isContentAttribute(key: string): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* A span for a model call, not a tool's execution or eve's own workflow. A provider's error on one
|
|
23
|
+
* can quote the prompt back; any other error is what a developer debugs from, so it stays. Both
|
|
24
|
+
* attributes are set when the span starts, which is when eve's export policy decides.
|
|
25
|
+
*/
|
|
26
|
+
export declare function isModelCallSpan(name: string, attributes: Readonly<Record<string, unknown>>): boolean;
|
package/dist/policy.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
// The one decision on whether model and tool content may leave a process
|
|
2
|
-
// (Observability PRD: metadata by default, content by explicit destination decision).
|
|
1
|
+
// The one decision on whether model and tool content may leave a process.
|
|
3
2
|
/** Keemakr runs three Vercel projects, one per branch, and each is that
|
|
4
3
|
* project's "production" deployment — so `VERCEL_ENV` is `production` on all
|
|
5
4
|
* three and cannot tell them apart. The branch can (docs/prod-git-rules.md). */
|
|
@@ -8,13 +7,10 @@ const BRANCH_ENVIRONMENT = {
|
|
|
8
7
|
stage: 'staging',
|
|
9
8
|
dev: 'dev',
|
|
10
9
|
};
|
|
11
|
-
/** The
|
|
12
|
-
*
|
|
13
|
-
*
|
|
10
|
+
/** The `deployment.environment.name` of this process: the tier from the
|
|
11
|
+
* deployed branch; a feature-branch deploy is `preview`; a machine with no
|
|
12
|
+
* Vercel context is `development`. */
|
|
14
13
|
export function telemetryEnvironment(env = process.env) {
|
|
15
|
-
const explicit = env.LANGFUSE_TRACING_ENVIRONMENT?.trim();
|
|
16
|
-
if (explicit)
|
|
17
|
-
return explicit;
|
|
18
14
|
const branch = env.VERCEL_GIT_COMMIT_REF?.trim();
|
|
19
15
|
if (branch)
|
|
20
16
|
return BRANCH_ENVIRONMENT[branch] ?? 'preview';
|
|
@@ -22,31 +18,24 @@ export function telemetryEnvironment(env = process.env) {
|
|
|
22
18
|
return 'preview';
|
|
23
19
|
return 'development';
|
|
24
20
|
}
|
|
25
|
-
|
|
26
|
-
return env.LANGFUSE_RELEASE?.trim() || env.VERCEL_GIT_COMMIT_SHA?.trim() || undefined;
|
|
27
|
-
}
|
|
21
|
+
const CONTENT_OFF = new Set(['0', 'false', 'off', 'no']);
|
|
28
22
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
23
|
+
* Content is recorded unless a deployment turns it off with `OTEL_RECORD_CONTENT=0`. Visibility
|
|
24
|
+
* by default was a product decision (18 Nov 2026): a developer debugging a run needs the prompt,
|
|
25
|
+
* the reply and the error. A process that exports to another OTLP endpoint sends it there too,
|
|
26
|
+
* so turn it off for one.
|
|
32
27
|
*/
|
|
33
28
|
export function contentCaptureAllowed(env = process.env) {
|
|
34
29
|
const flag = env.OTEL_RECORD_CONTENT?.trim().toLowerCase();
|
|
35
|
-
|
|
36
|
-
return true;
|
|
37
|
-
if (flag === '0' || flag === 'false')
|
|
38
|
-
return false;
|
|
39
|
-
return telemetryEnvironment(env) === 'development';
|
|
30
|
+
return flag === undefined || !CONTENT_OFF.has(flag);
|
|
40
31
|
}
|
|
41
32
|
/** A marker, so a reader knows content existed and was withheld. */
|
|
42
33
|
export const REDACTED = '[redacted: content capture off]';
|
|
43
|
-
/**
|
|
44
|
-
|
|
34
|
+
/** A query string carries credentials (webhook secrets, OAuth codes), so it is withheld always. */
|
|
35
|
+
export const REDACTED_QUERY = '[redacted: query string]';
|
|
36
|
+
/** `gen_ai.*` is the OpenTelemetry GenAI convention (eve, the AI SDK), `ai.*`
|
|
37
|
+
* the AI SDK's own names. */
|
|
45
38
|
export const CONTENT_ATTRIBUTE_PREFIXES = [
|
|
46
|
-
'langfuse.observation.input',
|
|
47
|
-
'langfuse.observation.output',
|
|
48
|
-
'langfuse.trace.input',
|
|
49
|
-
'langfuse.trace.output',
|
|
50
39
|
'gen_ai.input.messages',
|
|
51
40
|
'gen_ai.output.messages',
|
|
52
41
|
'gen_ai.prompt',
|
|
@@ -54,13 +43,47 @@ export const CONTENT_ATTRIBUTE_PREFIXES = [
|
|
|
54
43
|
'gen_ai.system_instructions',
|
|
55
44
|
'gen_ai.tool.call.arguments',
|
|
56
45
|
'gen_ai.tool.call.result',
|
|
46
|
+
'gen_ai.tool.definitions',
|
|
57
47
|
'ai.prompt',
|
|
58
48
|
'ai.response.text',
|
|
59
49
|
'ai.response.object',
|
|
60
50
|
'ai.response.toolCalls',
|
|
51
|
+
'ai.response.reasoning',
|
|
52
|
+
'ai.response.files',
|
|
61
53
|
'ai.toolCall.args',
|
|
62
54
|
'ai.toolCall.result',
|
|
55
|
+
'ai.documents',
|
|
56
|
+
'ai.value',
|
|
57
|
+
'ai.values',
|
|
58
|
+
'ai.embedding',
|
|
59
|
+
'ai.embeddings',
|
|
60
|
+
'ai.ranking',
|
|
61
|
+
'ai.decision',
|
|
63
62
|
];
|
|
64
63
|
export function isContentAttribute(key) {
|
|
65
64
|
return CONTENT_ATTRIBUTE_PREFIXES.some((p) => key === p || key.startsWith(`${p}.`));
|
|
66
65
|
}
|
|
66
|
+
/** `gen_ai.operation.name` values whose failure carries a provider's error (inference, an agent
|
|
67
|
+
* turn or step that wraps one, an embedding, a rerank, a decision). eve's `workflow` and memory
|
|
68
|
+
* operations, and `execute_tool`, are not among them. */
|
|
69
|
+
const MODEL_CALL_OPERATIONS = new Set([
|
|
70
|
+
'chat',
|
|
71
|
+
'text_completion',
|
|
72
|
+
'generate_content',
|
|
73
|
+
'invoke_agent',
|
|
74
|
+
'agent_step',
|
|
75
|
+
'embeddings',
|
|
76
|
+
'rerank',
|
|
77
|
+
'decide',
|
|
78
|
+
]);
|
|
79
|
+
/**
|
|
80
|
+
* A span for a model call, not a tool's execution or eve's own workflow. A provider's error on one
|
|
81
|
+
* can quote the prompt back; any other error is what a developer debugs from, so it stays. Both
|
|
82
|
+
* attributes are set when the span starts, which is when eve's export policy decides.
|
|
83
|
+
*/
|
|
84
|
+
export function isModelCallSpan(name, attributes) {
|
|
85
|
+
const operation = attributes['gen_ai.operation.name'];
|
|
86
|
+
if (typeof operation === 'string')
|
|
87
|
+
return MODEL_CALL_OPERATIONS.has(operation);
|
|
88
|
+
return 'ai.operationId' in attributes && name !== 'ai.toolCall';
|
|
89
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keemakr/observability",
|
|
3
|
-
"version": "0.2.0-dev-
|
|
3
|
+
"version": "0.2.0-dev-20261124171750",
|
|
4
4
|
"description": "Keemakr telemetry: the host registration for a process (Core, Operator, Task Operator, Pack Runtime) and the exporter-neutral runtime that Core libraries, the Agents SDK and Pack code trace with.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -35,10 +35,6 @@
|
|
|
35
35
|
"typecheck": "tsc --noEmit",
|
|
36
36
|
"prepack": "npm run build"
|
|
37
37
|
},
|
|
38
|
-
"dependencies": {
|
|
39
|
-
"@langfuse/otel": "^5.11.1",
|
|
40
|
-
"@langfuse/tracing": "^5.11.1"
|
|
41
|
-
},
|
|
42
38
|
"peerDependencies": {
|
|
43
39
|
"@opentelemetry/api": "^1.9.0",
|
|
44
40
|
"@opentelemetry/core": "^2.0.1",
|