@telemetry-dev/eve 0.1.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.
@@ -0,0 +1,82 @@
1
+ import type {
2
+ InstrumentationDefinition,
3
+ InstrumentationEvents,
4
+ InstrumentationRuntimeContext,
5
+ } from "eve/instrumentation";
6
+
7
+ import { ensureInit, type ClientOverrides, type TelemetryDevEveOptions } from "./config.ts";
8
+
9
+ export interface TelemetryDevInstrumentationOptions extends TelemetryDevEveOptions {
10
+ /** ai.telemetry.functionId override; eve defaults it to the agent name. */
11
+ functionId?: string;
12
+ /** Record model inputs on spans. Default: captureInput ?? true. */
13
+ recordInputs?: boolean;
14
+ /** Record model outputs on spans. Default: captureOutput ?? true. */
15
+ recordOutputs?: boolean;
16
+ /** Static runtime context merged into every model-call span. */
17
+ runtimeContext?: InstrumentationRuntimeContext;
18
+ /** User step.started callback, composed with the integration's own. */
19
+ stepStarted?: InstrumentationEvents["step.started"];
20
+ }
21
+
22
+ const envServiceName = (): string | undefined =>
23
+ typeof process !== "undefined" ? process.env.OTEL_SERVICE_NAME : undefined;
24
+ function reportError(onError: ((error: unknown) => void) | undefined, error: unknown): void {
25
+ try {
26
+ onError?.(error);
27
+ } catch {
28
+ // instrumentation callbacks must never fail an Eve turn.
29
+ }
30
+ }
31
+
32
+ export function telemetryDevInstrumentation(
33
+ options: TelemetryDevInstrumentationOptions = {},
34
+ overrides?: ClientOverrides,
35
+ ): InstrumentationDefinition {
36
+ const { functionId, recordInputs, recordOutputs, runtimeContext, stepStarted, ...sdkOptions } =
37
+ options;
38
+
39
+ const definition: InstrumentationDefinition = {
40
+ recordInputs: recordInputs ?? sdkOptions.captureInput ?? true,
41
+ recordOutputs: recordOutputs ?? sdkOptions.captureOutput ?? true,
42
+ setup({ agentName }) {
43
+ try {
44
+ ensureInit(
45
+ {
46
+ ...sdkOptions,
47
+ serviceName: sdkOptions.serviceName ?? envServiceName() ?? agentName,
48
+ },
49
+ overrides,
50
+ );
51
+ } catch (error) {
52
+ reportError(sdkOptions.onError, error);
53
+ }
54
+ },
55
+ events: {
56
+ "step.started"(input) {
57
+ const ctx: Record<string, unknown> = { ...runtimeContext };
58
+ const userId =
59
+ input.session.auth.initiator?.principalId ?? input.session.auth.current?.principalId;
60
+ if (userId) ctx["user.id"] = userId;
61
+
62
+ try {
63
+ const userResult = stepStarted?.(input);
64
+ if (userResult?.runtimeContext) {
65
+ Object.assign(ctx, userResult.runtimeContext);
66
+ }
67
+ } catch (error) {
68
+ reportError(sdkOptions.onError, error);
69
+ }
70
+
71
+ return Object.keys(ctx).length > 0
72
+ ? { runtimeContext: ctx as InstrumentationRuntimeContext }
73
+ : undefined;
74
+ },
75
+ },
76
+ };
77
+
78
+ if (functionId !== undefined) {
79
+ return { ...definition, functionId };
80
+ }
81
+ return definition;
82
+ }