@latitude-data/telemetry 2.0.2 → 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.
@@ -0,0 +1,123 @@
1
+ import * as otel from '@opentelemetry/api';
2
+ import { TextMapPropagator } from '@opentelemetry/api';
3
+ import { SpanProcessor, Span, ReadableSpan, SpanExporter } from '@opentelemetry/sdk-trace-node';
4
+ import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
5
+
6
+ interface BaseInstrumentation {
7
+ isEnabled(): boolean;
8
+ enable(): void;
9
+ disable(): void;
10
+ }
11
+
12
+ type CaptureOptions = {
13
+ tags?: string[];
14
+ metadata?: Record<string, unknown>;
15
+ sessionId?: string;
16
+ userId?: string;
17
+ };
18
+ declare class ManualInstrumentation implements BaseInstrumentation {
19
+ private enabled;
20
+ private readonly _tracer;
21
+ constructor(tracer: otel.Tracer);
22
+ get tracer(): otel.Tracer;
23
+ isEnabled(): boolean;
24
+ enable(): void;
25
+ disable(): void;
26
+ resume(ctx: {
27
+ traceparent: string;
28
+ baggage?: string;
29
+ }): otel.Context;
30
+ }
31
+
32
+ interface RedactSpanProcessorOptions {
33
+ attributes: (string | RegExp)[];
34
+ mask?: (attribute: string, value: unknown) => string;
35
+ }
36
+ declare class RedactSpanProcessor implements SpanProcessor {
37
+ private options;
38
+ constructor(options: RedactSpanProcessorOptions);
39
+ onStart(_span: Span, _context: otel.Context): void;
40
+ onEnd(span: ReadableSpan): void;
41
+ forceFlush(): Promise<void>;
42
+ shutdown(): Promise<void>;
43
+ private shouldRedact;
44
+ private redactAttributes;
45
+ }
46
+ declare const DEFAULT_REDACT_SPAN_PROCESSOR: () => RedactSpanProcessor;
47
+
48
+ type TelemetryContext = otel.Context;
49
+ declare class ContextManager {
50
+ private readonly telemetry;
51
+ constructor(telemetry: ManualInstrumentation);
52
+ resume(ctx: {
53
+ traceparent: string;
54
+ baggage?: string;
55
+ }): otel.Context;
56
+ active(): otel.Context;
57
+ with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(ctx: TelemetryContext, fn: F, thisArg?: ThisParameterType<F>, ...args: A): ReturnType<F>;
58
+ }
59
+ declare class InstrumentationManager {
60
+ private readonly instrumentations;
61
+ constructor(instrumentations: BaseInstrumentation[]);
62
+ enable(): void;
63
+ disable(): void;
64
+ }
65
+ declare const DEFAULT_SPAN_EXPORTER: (apiKey: string, projectSlug: string) => OTLPTraceExporter;
66
+ declare enum Instrumentation {
67
+ Anthropic = "anthropic",
68
+ AIPlatform = "aiplatform",
69
+ Bedrock = "bedrock",
70
+ Cohere = "cohere",
71
+ Langchain = "langchain",
72
+ LlamaIndex = "llamaindex",
73
+ Manual = "manual",
74
+ OpenAI = "openai",
75
+ TogetherAI = "togetherai",
76
+ VertexAI = "vertexai"
77
+ }
78
+ type TelemetryOptions = {
79
+ serviceName?: string;
80
+ disableBatch?: boolean;
81
+ exporter?: SpanExporter;
82
+ processors?: SpanProcessor[];
83
+ propagators?: TextMapPropagator[];
84
+ instrumentations?: {
85
+ [Instrumentation.AIPlatform]?: unknown;
86
+ [Instrumentation.Anthropic]?: unknown;
87
+ [Instrumentation.Bedrock]?: unknown;
88
+ [Instrumentation.Cohere]?: unknown;
89
+ [Instrumentation.OpenAI]?: unknown;
90
+ [Instrumentation.LlamaIndex]?: unknown;
91
+ [Instrumentation.TogetherAI]?: unknown;
92
+ [Instrumentation.VertexAI]?: unknown;
93
+ [Instrumentation.Langchain]?: {
94
+ callbackManagerModule?: unknown;
95
+ };
96
+ };
97
+ };
98
+ declare class LatitudeTelemetry {
99
+ private options;
100
+ private nodeProvider;
101
+ private instrumentationsList;
102
+ /** OpenTelemetry tracer for creating custom spans. */
103
+ readonly tracer: otel.Tracer;
104
+ readonly context: ContextManager;
105
+ readonly instrumentation: InstrumentationManager;
106
+ private readonly lifecycle;
107
+ constructor(apiKey: string, projectSlug: string, options?: TelemetryOptions);
108
+ flush(): Promise<void>;
109
+ shutdown(): Promise<void>;
110
+ private initProviderInstrumentations;
111
+ /**
112
+ * Wrap a block of code with trace-wide context attributes.
113
+ * Baggage entries (tags, metadata, sessionId, userId) are propagated
114
+ * to all spans created within the callback via BaggageSpanProcessor.
115
+ *
116
+ * If there is no active span, a root span is created so all child spans
117
+ * are grouped under a single trace. If a span is already active, only
118
+ * baggage is set without creating an extra wrapper span.
119
+ */
120
+ capture<T>(options: CaptureOptions, fn: (ctx: TelemetryContext) => T | Promise<T>): Promise<T>;
121
+ }
122
+
123
+ export { type CaptureOptions, DEFAULT_REDACT_SPAN_PROCESSOR, DEFAULT_SPAN_EXPORTER, Instrumentation, LatitudeTelemetry, RedactSpanProcessor, type RedactSpanProcessorOptions, type TelemetryContext, type TelemetryOptions };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,6 @@
1
- import { z } from 'zod';
2
1
  import * as otel from '@opentelemetry/api';
3
2
  import { TextMapPropagator } from '@opentelemetry/api';
4
- import { Provider } from 'rosetta-ai';
5
- import { SpanProcessor, ReadableSpan, SpanExporter, NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
6
- import * as latitude from '@latitude-data/sdk';
7
- import { Latitude } from '@latitude-data/sdk';
3
+ import { SpanProcessor, Span, ReadableSpan, SpanExporter } from '@opentelemetry/sdk-trace-node';
8
4
  import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
9
5
 
10
6
  interface BaseInstrumentation {
@@ -13,195 +9,34 @@ interface BaseInstrumentation {
13
9
  disable(): void;
14
10
  }
15
11
 
16
- type LatitudeInstrumentationOptions = {
17
- module: typeof Latitude;
18
- completions?: boolean;
19
- };
20
-
21
- declare enum LogSources {
22
- API = "api",
23
- AgentAsTool = "agent_as_tool",
24
- Copilot = "copilot",
25
- EmailTrigger = "email_trigger",
26
- Evaluation = "evaluation",
27
- Experiment = "experiment",
28
- IntegrationTrigger = "integration_trigger",
29
- Playground = "playground",
30
- ScheduledTrigger = "scheduled_trigger",
31
- SharedPrompt = "shared_prompt",
32
- ShadowTest = "shadow_test",
33
- ABTestChallenger = "ab_test_challenger",
34
- User = "user",
35
- Optimization = "optimization"
36
- }
37
-
38
- declare const traceContextSchema: z.ZodObject<{
39
- traceparent: z.ZodString;
40
- tracestate: z.ZodOptional<z.ZodString>;
41
- baggage: z.ZodOptional<z.ZodString>;
42
- }, z.core.$strip>;
43
- type TraceContext = z.infer<typeof traceContextSchema>;
44
-
45
- type StartSpanOptions = {
46
- name?: string;
47
- attributes?: otel.Attributes;
48
- };
49
- type EndSpanOptions = {
50
- attributes?: otel.Attributes;
51
- };
52
- type ErrorOptions = {
53
- attributes?: otel.Attributes;
54
- };
55
- type StartToolSpanOptions = StartSpanOptions & {
56
- name: string;
57
- call: {
58
- id: string;
59
- arguments: Record<string, unknown>;
60
- };
61
- };
62
- type EndToolSpanOptions = EndSpanOptions & {
63
- result: {
64
- value: unknown;
65
- isError: boolean;
66
- };
67
- };
68
- type StartCompletionSpanOptions = StartSpanOptions & {
69
- provider: string;
70
- model: string;
71
- configuration?: Record<string, unknown>;
72
- input?: string | Record<string, unknown>[];
73
- versionUuid?: string;
74
- promptUuid?: string;
75
- experimentUuid?: string;
76
- };
77
- type EndCompletionSpanOptions = EndSpanOptions & {
78
- output?: string | Record<string, unknown>[];
79
- tokens?: {
80
- prompt?: number;
81
- cached?: number;
82
- reasoning?: number;
83
- completion?: number;
84
- };
85
- finishReason?: string;
86
- };
87
- type StartHttpSpanOptions = StartSpanOptions & {
88
- request: {
89
- method: string;
90
- url: string;
91
- headers: Record<string, string>;
92
- body: string | Record<string, unknown>;
93
- };
94
- };
95
- type EndHttpSpanOptions = EndSpanOptions & {
96
- response: {
97
- status: number;
98
- headers: Record<string, string>;
99
- body: string | Record<string, unknown>;
100
- };
101
- };
102
- type PromptSpanOptions = StartSpanOptions & {
103
- documentLogUuid: string;
104
- versionUuid?: string;
105
- promptUuid: string;
106
- projectId?: number;
107
- experimentUuid?: string;
108
- testDeploymentId?: number;
109
- externalId?: string;
110
- template: string;
111
- parameters?: Record<string, unknown>;
112
- source?: LogSources;
113
- };
114
- type ChatSpanOptions = StartSpanOptions & {
115
- documentLogUuid: string;
116
- previousTraceId: string;
117
- source?: LogSources;
118
- versionUuid?: string;
119
- promptUuid?: string;
120
- };
121
- type ExternalSpanOptions = StartSpanOptions & {
122
- promptUuid: string;
123
- documentLogUuid: string;
124
- source?: LogSources;
125
- versionUuid?: string;
126
- externalId?: string;
127
- };
128
- type CaptureOptions = StartSpanOptions & {
129
- path: string;
130
- projectId: number;
131
- versionUuid?: string;
132
- conversationUuid?: string;
133
- };
134
- type ManualInstrumentationOptions = {
135
- provider?: Provider;
12
+ type CaptureOptions = {
13
+ tags?: string[];
14
+ metadata?: Record<string, unknown>;
15
+ sessionId?: string;
16
+ userId?: string;
136
17
  };
137
18
  declare class ManualInstrumentation implements BaseInstrumentation {
138
19
  private enabled;
139
- private readonly tracer;
140
- private readonly options;
141
- constructor(tracer: otel.Tracer, options?: ManualInstrumentationOptions);
20
+ private readonly _tracer;
21
+ constructor(tracer: otel.Tracer);
22
+ get tracer(): otel.Tracer;
142
23
  isEnabled(): boolean;
143
24
  enable(): void;
144
25
  disable(): void;
145
- resume(ctx: TraceContext): otel.Context;
146
- private error;
147
- private span;
148
- unknown(ctx: otel.Context, options?: StartSpanOptions): {
149
- context: otel.Context;
150
- end: (_options?: EndSpanOptions) => void;
151
- fail: (_error: Error, _options?: ErrorOptions) => void;
152
- };
153
- tool(ctx: otel.Context, options: StartToolSpanOptions): {
154
- end: (options: EndToolSpanOptions) => void;
155
- context: otel.Context;
156
- fail: (_error: Error, _options?: ErrorOptions) => void;
157
- };
158
- private attribifyConfiguration;
159
- completion(ctx: otel.Context, options: StartCompletionSpanOptions): {
160
- end: (options?: EndCompletionSpanOptions) => void;
161
- context: otel.Context;
162
- fail: (_error: Error, _options?: ErrorOptions) => void;
163
- };
164
- embedding(ctx: otel.Context, options?: StartSpanOptions): {
165
- context: otel.Context;
166
- end: (_options?: EndSpanOptions) => void;
167
- fail: (_error: Error, _options?: ErrorOptions) => void;
168
- };
169
- private attribifyHeaders;
170
- http(ctx: otel.Context, options: StartHttpSpanOptions): {
171
- end: (options: EndHttpSpanOptions) => void;
172
- context: otel.Context;
173
- fail: (_error: Error, _options?: ErrorOptions) => void;
174
- };
175
- prompt(ctx: otel.Context, { documentLogUuid, versionUuid, promptUuid, projectId, experimentUuid, testDeploymentId, externalId, template, parameters, name, source, ...rest }: PromptSpanOptions): {
176
- context: otel.Context;
177
- end: (_options?: EndSpanOptions) => void;
178
- fail: (_error: Error, _options?: ErrorOptions) => void;
179
- };
180
- chat(ctx: otel.Context, { documentLogUuid, previousTraceId, source, name, versionUuid, promptUuid, ...rest }: ChatSpanOptions): {
181
- context: otel.Context;
182
- end: (_options?: EndSpanOptions) => void;
183
- fail: (_error: Error, _options?: ErrorOptions) => void;
184
- };
185
- external(ctx: otel.Context, { promptUuid, documentLogUuid, source, versionUuid, externalId, name, ...rest }: ExternalSpanOptions): {
186
- context: otel.Context;
187
- end: (_options?: EndSpanOptions) => void;
188
- fail: (_error: Error, _options?: ErrorOptions) => void;
189
- };
190
- unresolvedExternal(ctx: otel.Context, { path, projectId, versionUuid, conversationUuid, name, ...rest }: CaptureOptions): {
191
- context: otel.Context;
192
- end: (_options?: EndSpanOptions) => void;
193
- fail: (_error: Error, _options?: ErrorOptions) => void;
194
- };
26
+ resume(ctx: {
27
+ traceparent: string;
28
+ baggage?: string;
29
+ }): otel.Context;
195
30
  }
196
31
 
197
32
  interface RedactSpanProcessorOptions {
198
33
  attributes: (string | RegExp)[];
199
- mask?: (attribute: string, value: any) => string;
34
+ mask?: (attribute: string, value: unknown) => string;
200
35
  }
201
36
  declare class RedactSpanProcessor implements SpanProcessor {
202
37
  private options;
203
38
  constructor(options: RedactSpanProcessorOptions);
204
- onStart(_span: ReadableSpan, _context: otel.Context): void;
39
+ onStart(_span: Span, _context: otel.Context): void;
205
40
  onEnd(span: ReadableSpan): void;
206
41
  forceFlush(): Promise<void>;
207
42
  shutdown(): Promise<void>;
@@ -211,55 +46,13 @@ declare class RedactSpanProcessor implements SpanProcessor {
211
46
  declare const DEFAULT_REDACT_SPAN_PROCESSOR: () => RedactSpanProcessor;
212
47
 
213
48
  type TelemetryContext = otel.Context;
214
- declare const BACKGROUND: () => otel.Context;
215
- declare class SpanFactory {
216
- private readonly telemetry;
217
- constructor(telemetry: ManualInstrumentation);
218
- span(options?: StartSpanOptions, ctx?: TelemetryContext): {
219
- context: otel.Context;
220
- end: (_options?: EndSpanOptions) => void;
221
- fail: (_error: Error, _options?: ErrorOptions) => void;
222
- };
223
- tool(options: StartToolSpanOptions, ctx?: TelemetryContext): {
224
- end: (options: EndToolSpanOptions) => void;
225
- context: otel.Context;
226
- fail: (_error: Error, _options?: ErrorOptions) => void;
227
- };
228
- completion(options: StartCompletionSpanOptions, ctx?: TelemetryContext): {
229
- end: (options?: EndCompletionSpanOptions) => void;
230
- context: otel.Context;
231
- fail: (_error: Error, _options?: ErrorOptions) => void;
232
- };
233
- embedding(options?: StartSpanOptions, ctx?: TelemetryContext): {
234
- context: otel.Context;
235
- end: (_options?: EndSpanOptions) => void;
236
- fail: (_error: Error, _options?: ErrorOptions) => void;
237
- };
238
- http(options: StartHttpSpanOptions, ctx?: TelemetryContext): {
239
- end: (options: EndHttpSpanOptions) => void;
240
- context: otel.Context;
241
- fail: (_error: Error, _options?: ErrorOptions) => void;
242
- };
243
- prompt(options: PromptSpanOptions, ctx?: TelemetryContext): {
244
- context: otel.Context;
245
- end: (_options?: EndSpanOptions) => void;
246
- fail: (_error: Error, _options?: ErrorOptions) => void;
247
- };
248
- chat(options: ChatSpanOptions, ctx?: TelemetryContext): {
249
- context: otel.Context;
250
- end: (_options?: EndSpanOptions) => void;
251
- fail: (_error: Error, _options?: ErrorOptions) => void;
252
- };
253
- external(options: ExternalSpanOptions, ctx?: TelemetryContext): {
254
- context: otel.Context;
255
- end: (_options?: EndSpanOptions) => void;
256
- fail: (_error: Error, _options?: ErrorOptions) => void;
257
- };
258
- }
259
49
  declare class ContextManager {
260
50
  private readonly telemetry;
261
51
  constructor(telemetry: ManualInstrumentation);
262
- resume(ctx: TraceContext): otel.Context;
52
+ resume(ctx: {
53
+ traceparent: string;
54
+ baggage?: string;
55
+ }): otel.Context;
263
56
  active(): otel.Context;
264
57
  with<A extends unknown[], F extends (...args: A) => ReturnType<F>>(ctx: TelemetryContext, fn: F, thisArg?: ThisParameterType<F>, ...args: A): ReturnType<F>;
265
58
  }
@@ -269,28 +62,13 @@ declare class InstrumentationManager {
269
62
  enable(): void;
270
63
  disable(): void;
271
64
  }
272
- declare class TracerManager {
273
- private readonly nodeProvider;
274
- private readonly scopeVersion;
275
- constructor(nodeProvider: NodeTracerProvider, scopeVersion: string);
276
- get(scope: Instrumentation): otel.Tracer;
277
- provider(scope: Instrumentation): ScopedTracerProvider;
278
- }
279
- declare class ScopedTracerProvider implements otel.TracerProvider {
280
- private readonly scope;
281
- private readonly version;
282
- private readonly provider;
283
- constructor(scope: string, version: string, provider: otel.TracerProvider);
284
- getTracer(_name: string, _version?: string, options?: otel.TracerOptions): otel.Tracer;
285
- }
286
- declare const DEFAULT_SPAN_EXPORTER: (apiKey: string) => OTLPTraceExporter;
65
+ declare const DEFAULT_SPAN_EXPORTER: (apiKey: string, projectSlug: string) => OTLPTraceExporter;
287
66
  declare enum Instrumentation {
288
67
  Anthropic = "anthropic",
289
68
  AIPlatform = "aiplatform",
290
69
  Bedrock = "bedrock",
291
70
  Cohere = "cohere",
292
71
  Langchain = "langchain",
293
- Latitude = "latitude",
294
72
  LlamaIndex = "llamaindex",
295
73
  Manual = "manual",
296
74
  OpenAI = "openai",
@@ -298,42 +76,48 @@ declare enum Instrumentation {
298
76
  VertexAI = "vertexai"
299
77
  }
300
78
  type TelemetryOptions = {
79
+ serviceName?: string;
301
80
  disableBatch?: boolean;
302
81
  exporter?: SpanExporter;
303
82
  processors?: SpanProcessor[];
304
83
  propagators?: TextMapPropagator[];
305
84
  instrumentations?: {
306
- [Instrumentation.Latitude]?: typeof latitude.Latitude | LatitudeInstrumentationOptions;
307
- [Instrumentation.AIPlatform]?: any;
308
- [Instrumentation.Anthropic]?: any;
309
- [Instrumentation.Bedrock]?: any;
310
- [Instrumentation.Cohere]?: any;
311
- [Instrumentation.OpenAI]?: any;
312
- [Instrumentation.LlamaIndex]?: any;
313
- [Instrumentation.TogetherAI]?: any;
314
- [Instrumentation.VertexAI]?: any;
85
+ [Instrumentation.AIPlatform]?: unknown;
86
+ [Instrumentation.Anthropic]?: unknown;
87
+ [Instrumentation.Bedrock]?: unknown;
88
+ [Instrumentation.Cohere]?: unknown;
89
+ [Instrumentation.OpenAI]?: unknown;
90
+ [Instrumentation.LlamaIndex]?: unknown;
91
+ [Instrumentation.TogetherAI]?: unknown;
92
+ [Instrumentation.VertexAI]?: unknown;
315
93
  [Instrumentation.Langchain]?: {
316
- callbackManagerModule?: any;
94
+ callbackManagerModule?: unknown;
317
95
  };
318
- [Instrumentation.Manual]?: ManualInstrumentationOptions;
319
96
  };
320
97
  };
321
98
  declare class LatitudeTelemetry {
322
99
  private options;
323
100
  private nodeProvider;
324
- private manualInstrumentation;
325
101
  private instrumentationsList;
326
- readonly span: SpanFactory;
102
+ /** OpenTelemetry tracer for creating custom spans. */
103
+ readonly tracer: otel.Tracer;
327
104
  readonly context: ContextManager;
328
105
  readonly instrumentation: InstrumentationManager;
329
- readonly tracer: TracerManager;
330
106
  private readonly lifecycle;
331
- constructor(apiKey: string, options?: TelemetryOptions);
107
+ constructor(apiKey: string, projectSlug: string, options?: TelemetryOptions);
332
108
  flush(): Promise<void>;
333
109
  shutdown(): Promise<void>;
334
- private initInstrumentations;
110
+ private initProviderInstrumentations;
111
+ /**
112
+ * Wrap a block of code with trace-wide context attributes.
113
+ * Baggage entries (tags, metadata, sessionId, userId) are propagated
114
+ * to all spans created within the callback via BaggageSpanProcessor.
115
+ *
116
+ * If there is no active span, a root span is created so all child spans
117
+ * are grouped under a single trace. If a span is already active, only
118
+ * baggage is set without creating an extra wrapper span.
119
+ */
335
120
  capture<T>(options: CaptureOptions, fn: (ctx: TelemetryContext) => T | Promise<T>): Promise<T>;
336
121
  }
337
122
 
338
- export { BACKGROUND, DEFAULT_REDACT_SPAN_PROCESSOR, DEFAULT_SPAN_EXPORTER, Instrumentation, LatitudeTelemetry, RedactSpanProcessor };
339
- export type { CaptureOptions, ChatSpanOptions, EndCompletionSpanOptions, EndHttpSpanOptions, EndSpanOptions, EndToolSpanOptions, ErrorOptions, ExternalSpanOptions, PromptSpanOptions as PromptSegmentOptions, RedactSpanProcessorOptions, StartCompletionSpanOptions, StartHttpSpanOptions, StartSpanOptions, StartToolSpanOptions, TelemetryContext, TelemetryOptions };
123
+ export { type CaptureOptions, DEFAULT_REDACT_SPAN_PROCESSOR, DEFAULT_SPAN_EXPORTER, Instrumentation, LatitudeTelemetry, RedactSpanProcessor, type RedactSpanProcessorOptions, type TelemetryContext, type TelemetryOptions };