@latitude-data/telemetry 3.0.0-alpha.0 → 3.0.0-alpha.1

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/index.d.ts CHANGED
@@ -1,33 +1,56 @@
1
+ import { SpanProcessor, Span, ReadableSpan, SpanExporter, NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
1
2
  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';
3
+ import { TracerProvider, Context } from '@opentelemetry/api';
5
4
 
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;
5
+ /**
6
+ * Supported LLM instrumentation types.
7
+ * Use these string identifiers to enable auto-instrumentation.
8
+ */
9
+ type InstrumentationType = "openai" | "anthropic" | "bedrock" | "cohere" | "langchain" | "llamaindex" | "togetherai" | "vertexai" | "aiplatform";
10
+ /**
11
+ * Options for creating LLM instrumentations.
12
+ */
13
+ interface CreateInstrumentationsOptions {
14
+ /** List of instrumentation types to enable. */
15
+ instrumentations: InstrumentationType[];
16
+ /**
17
+ * Optional module references for auto-instrumentation.
18
+ * If not provided, the instrumentation will attempt to require the module.
19
+ * Used for Traceloop-based instrumentations.
20
+ */
21
+ modules?: Partial<Record<InstrumentationType, unknown>>;
22
+ /**
23
+ * Per-instrumentation token enrichment settings.
24
+ * @default { openai: true }
25
+ */
26
+ enrichTokens?: Partial<Record<InstrumentationType, boolean>>;
30
27
  }
28
+ /**
29
+ * Registers LLM instrumentations with the global OpenTelemetry instrumentation registry.
30
+ *
31
+ * This is a convenience wrapper around `createLatitudeInstrumentations` and
32
+ * `@opentelemetry/instrumentation`'s `registerInstrumentations`.
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node"
37
+ * import { registerLatitudeInstrumentations, LatitudeSpanProcessor } from "@latitude-data/telemetry"
38
+ *
39
+ * const provider = new NodeTracerProvider({
40
+ * spanProcessors: [new LatitudeSpanProcessor(apiKey, projectSlug)],
41
+ * })
42
+ *
43
+ * await registerLatitudeInstrumentations({
44
+ * instrumentations: ["openai", "anthropic"],
45
+ * tracerProvider: provider,
46
+ * })
47
+ *
48
+ * provider.register()
49
+ * ```
50
+ */
51
+ declare function registerLatitudeInstrumentations(options: CreateInstrumentationsOptions & {
52
+ tracerProvider: TracerProvider;
53
+ }): Promise<void>;
31
54
 
32
55
  interface RedactSpanProcessorOptions {
33
56
  attributes: (string | RegExp)[];
@@ -43,81 +66,104 @@ declare class RedactSpanProcessor implements SpanProcessor {
43
66
  private shouldRedact;
44
67
  private redactAttributes;
45
68
  }
46
- declare const DEFAULT_REDACT_SPAN_PROCESSOR: () => RedactSpanProcessor;
47
69
 
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;
70
+ type SmartFilterOptions = {
71
+ /**
72
+ * When true, all spans are exported (legacy behavior).
73
+ * Default false — only LLM-relevant spans are exported.
74
+ */
75
+ disableSmartFilter?: boolean;
76
+ /**
77
+ * When smart filter is on, also export spans for which this returns true
78
+ * (in addition to {@link isDefaultExportSpan}).
79
+ */
80
+ shouldExportSpan?: (span: ReadableSpan) => boolean;
81
+ /** Instrumentation scope names to drop (exact match) even if they pass the default predicate. */
82
+ blockedInstrumentationScopes?: string[];
83
+ };
84
+ /** Input for {@link buildShouldExportSpanFromFields}; allows `undefined` field values for ergonomics. */
85
+ type SmartFilterFieldsInput = {
86
+ disableSmartFilter?: boolean | undefined;
87
+ shouldExportSpan?: ((span: ReadableSpan) => boolean) | undefined;
88
+ blockedInstrumentationScopes?: string[] | undefined;
89
+ };
90
+ /**
91
+ * Builds the export predicate from loose option fields (`exactOptionalPropertyTypes`-safe call sites).
92
+ */
93
+ declare function buildShouldExportSpanFromFields(fields: SmartFilterFieldsInput): (span: ReadableSpan) => boolean;
94
+ /** True if the span uses OpenTelemetry GenAI semantic conventions or common LLM attribute namespaces. */
95
+ declare function isGenAiOrLlmAttributeSpan(span: ReadableSpan): boolean;
96
+ /** True if the span was created with Latitude's tracer scopes. */
97
+ declare function isLatitudeInstrumentationSpan(span: ReadableSpan): boolean;
98
+ /**
99
+ * Default export predicate (smart filter): Latitude scopes, GenAI / LLM attributes,
100
+ * or known LLM instrumentation scopes.
101
+ */
102
+ declare function isDefaultExportSpan(span: ReadableSpan): boolean;
103
+ declare function buildShouldExportSpan(options: SmartFilterOptions): (span: ReadableSpan) => boolean;
104
+ /**
105
+ * Drops spans that fail the export predicate before passing them to the inner processor.
106
+ * Inner processor should perform redaction and export.
107
+ */
108
+ declare class ExportFilterSpanProcessor implements SpanProcessor {
109
+ private readonly shouldExport;
110
+ private readonly inner;
111
+ constructor(shouldExport: (span: ReadableSpan) => boolean, inner: SpanProcessor);
112
+ onStart(span: Span, parentContext: Context): void;
113
+ onEnd(span: ReadableSpan): void;
114
+ forceFlush(): Promise<void>;
115
+ shutdown(): Promise<void>;
64
116
  }
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"
117
+ /** Runs optional redaction then the export processor (batch/simple). */
118
+ declare class RedactThenExportSpanProcessor implements SpanProcessor {
119
+ private readonly redact;
120
+ private readonly exportProcessor;
121
+ constructor(redact: SpanProcessor | null, exportProcessor: SpanProcessor);
122
+ onStart(span: Span, parentContext: Context): void;
123
+ onEnd(span: ReadableSpan): void;
124
+ forceFlush(): Promise<void>;
125
+ shutdown(): Promise<void>;
77
126
  }
78
- type TelemetryOptions = {
79
- serviceName?: string;
127
+
128
+ type ContextOptions = {
129
+ name?: string;
130
+ tags?: string[];
131
+ metadata?: Record<string, unknown>;
132
+ sessionId?: string;
133
+ userId?: string;
134
+ };
135
+ type InitLatitudeOptions = SmartFilterOptions & {
136
+ apiKey: string;
137
+ projectSlug: string;
138
+ instrumentations?: InstrumentationType[];
139
+ disableRedact?: boolean;
140
+ redact?: RedactSpanProcessorOptions;
80
141
  disableBatch?: boolean;
81
142
  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
143
  };
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>;
144
+ type LatitudeSpanProcessorOptions = SmartFilterOptions & {
145
+ disableRedact?: boolean;
146
+ redact?: RedactSpanProcessorOptions;
147
+ disableBatch?: boolean;
148
+ exporter?: SpanExporter;
149
+ };
150
+
151
+ declare function capture<T>(name: string, fn: () => T | Promise<T>, options?: ContextOptions): T | Promise<T>;
152
+
153
+ declare function initLatitude(options: InitLatitudeOptions): {
154
+ provider: NodeTracerProvider;
155
+ flush: () => Promise<void>;
156
+ shutdown: () => Promise<void>;
157
+ ready: Promise<void>;
158
+ };
159
+
160
+ declare class LatitudeSpanProcessor implements SpanProcessor {
161
+ private readonly tail;
162
+ constructor(apiKey: string, projectSlug: string, options?: LatitudeSpanProcessorOptions);
163
+ onStart(span: Span, parentContext: Context): void;
164
+ onEnd(span: ReadableSpan): void;
165
+ forceFlush(): Promise<void>;
109
166
  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
167
  }
122
168
 
123
- export { type CaptureOptions, DEFAULT_REDACT_SPAN_PROCESSOR, DEFAULT_SPAN_EXPORTER, Instrumentation, LatitudeTelemetry, RedactSpanProcessor, type RedactSpanProcessorOptions, type TelemetryContext, type TelemetryOptions };
169
+ export { type ContextOptions, ExportFilterSpanProcessor, type InitLatitudeOptions, type InstrumentationType, LatitudeSpanProcessor, type LatitudeSpanProcessorOptions, RedactSpanProcessor, type RedactSpanProcessorOptions, RedactThenExportSpanProcessor, type SmartFilterFieldsInput, type SmartFilterOptions, buildShouldExportSpan, buildShouldExportSpanFromFields, capture, initLatitude, isDefaultExportSpan, isGenAiOrLlmAttributeSpan, isLatitudeInstrumentationSpan, registerLatitudeInstrumentations };