@elasticdash/otel 0.0.1 → 0.0.3

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,234 @@
1
+ import { Context } from '@opentelemetry/api';
2
+ import { ReadableSpan, SpanExporter, SpanProcessor, Span } from '@opentelemetry/sdk-trace-base';
3
+
4
+ /**
5
+ * Function type for masking sensitive data in spans before export.
6
+ *
7
+ * @param params - Object containing the data to be masked
8
+ * @param params.data - The data that should be masked
9
+ * @returns The masked data (can be of any type)
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * const maskFunction: MaskFunction = ({ data }) => {
14
+ * if (typeof data === 'string') {
15
+ * return data.replace(/password=\w+/g, 'password=***');
16
+ * }
17
+ * return data;
18
+ * };
19
+ * ```
20
+ *
21
+ * @public
22
+ */
23
+ type MaskFunction = (params: {
24
+ data: any;
25
+ }) => any;
26
+ /**
27
+ * Function type for determining whether a span should be exported to Langfuse.
28
+ *
29
+ * @param params - Object containing the span to evaluate
30
+ * @param params.otelSpan - The OpenTelemetry span to evaluate
31
+ * @returns `true` if the span should be exported, `false` otherwise
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * const shouldExportSpan: ShouldExportSpan = ({ otelSpan }) => {
36
+ * // Only export spans that took longer than 100ms
37
+ * return otelSpan.duration[0] * 1000 + otelSpan.duration[1] / 1000000 > 100;
38
+ * };
39
+ * ```
40
+ *
41
+ * @public
42
+ */
43
+ type ShouldExportSpan = (params: {
44
+ otelSpan: ReadableSpan;
45
+ }) => boolean;
46
+ /**
47
+ * Configuration parameters for the LangfuseSpanProcessor.
48
+ *
49
+ * @public
50
+ */
51
+ interface LangfuseSpanProcessorParams {
52
+ /**
53
+ * Custom OpenTelemetry span exporter. If not provided, a default OTLP exporter will be used.
54
+ */
55
+ exporter?: SpanExporter;
56
+ /**
57
+ * Langfuse public API key. Can also be set via LANGFUSE_PUBLIC_KEY environment variable.
58
+ */
59
+ publicKey?: string;
60
+ /**
61
+ * Langfuse secret API key. Can also be set via LANGFUSE_SECRET_KEY environment variable.
62
+ */
63
+ secretKey?: string;
64
+ /**
65
+ * Langfuse instance base URL. Can also be set via LANGFUSE_BASE_URL environment variable.
66
+ * @defaultValue "https://cloud.langfuse.com"
67
+ */
68
+ baseUrl?: string;
69
+ /**
70
+ * Number of spans to batch before flushing. Can also be set via LANGFUSE_FLUSH_AT environment variable.
71
+ */
72
+ flushAt?: number;
73
+ /**
74
+ * Flush interval in seconds. Can also be set via LANGFUSE_FLUSH_INTERVAL environment variable.
75
+ */
76
+ flushInterval?: number;
77
+ /**
78
+ * Function to mask sensitive data in spans before export.
79
+ */
80
+ mask?: MaskFunction;
81
+ /**
82
+ * Function to determine whether a span should be exported to Langfuse.
83
+ */
84
+ shouldExportSpan?: ShouldExportSpan;
85
+ /**
86
+ * Environment identifier for the traces. Can also be set via LANGFUSE_TRACING_ENVIRONMENT environment variable.
87
+ */
88
+ environment?: string;
89
+ /**
90
+ * Release identifier for the traces. Can also be set via LANGFUSE_RELEASE environment variable.
91
+ */
92
+ release?: string;
93
+ /**
94
+ * Request timeout in seconds. Can also be set via LANGFUSE_TIMEOUT environment variable.
95
+ * @defaultValue 5
96
+ */
97
+ timeout?: number;
98
+ /**
99
+ * Additional HTTP headers to include with requests.
100
+ */
101
+ additionalHeaders?: Record<string, string>;
102
+ /**
103
+ * Span export mode to use.
104
+ *
105
+ * - **batched**: Recommended for production environments with long-running processes.
106
+ * Spans are batched and exported in groups for optimal performance.
107
+ * - **immediate**: Recommended for short-lived environments such as serverless functions.
108
+ * Spans are exported immediately to prevent data loss when the process terminates / is frozen.
109
+ *
110
+ * @defaultValue "batched"
111
+ */
112
+ exportMode?: "immediate" | "batched";
113
+ }
114
+ /**
115
+ * OpenTelemetry span processor for sending spans to Langfuse.
116
+ *
117
+ * This processor extends the standard BatchSpanProcessor to provide:
118
+ * - Automatic batching and flushing of spans to Langfuse
119
+ * - Media content extraction and upload from base64 data URIs
120
+ * - Data masking capabilities for sensitive information
121
+ * - Conditional span export based on custom logic
122
+ * - Environment and release tagging
123
+ *
124
+ * @example
125
+ * ```typescript
126
+ * import { NodeSDK } from '@opentelemetry/sdk-node';
127
+ * import { LangfuseSpanProcessor } from '@elasticdash/otel';
128
+ *
129
+ * const sdk = new NodeSDK({
130
+ * spanProcessors: [
131
+ * new LangfuseSpanProcessor({
132
+ * publicKey: 'pk_...',
133
+ * secretKey: 'sk_...',
134
+ * baseUrl: 'https://cloud.langfuse.com',
135
+ * environment: 'production',
136
+ * mask: ({ data }) => {
137
+ * // Mask sensitive data
138
+ * return data.replace(/api_key=\w+/g, 'api_key=***');
139
+ * }
140
+ * })
141
+ * ]
142
+ * });
143
+ *
144
+ * sdk.start();
145
+ * ```
146
+ *
147
+ * @public
148
+ */
149
+ declare class LangfuseSpanProcessor implements SpanProcessor {
150
+ private pendingEndedSpans;
151
+ private publicKey?;
152
+ private baseUrl?;
153
+ private environment?;
154
+ private release?;
155
+ private mask?;
156
+ private shouldExportSpan?;
157
+ private apiClient;
158
+ private processor;
159
+ private mediaService;
160
+ /**
161
+ * Creates a new LangfuseSpanProcessor instance.
162
+ *
163
+ * @param params - Configuration parameters for the processor
164
+ *
165
+ * @example
166
+ * ```typescript
167
+ * const processor = new LangfuseSpanProcessor({
168
+ * publicKey: 'pk_...',
169
+ * secretKey: 'sk_...',
170
+ * environment: 'staging',
171
+ * flushAt: 10,
172
+ * flushInterval: 2,
173
+ * mask: ({ data }) => {
174
+ * // Custom masking logic
175
+ * return typeof data === 'string'
176
+ * ? data.replace(/secret_\w+/g, 'secret_***')
177
+ * : data;
178
+ * },
179
+ * shouldExportSpan: ({ otelSpan }) => {
180
+ * // Only export spans from specific services
181
+ * return otelSpan.name.startsWith('my-service');
182
+ * }
183
+ * });
184
+ * ```
185
+ */
186
+ constructor(params?: LangfuseSpanProcessorParams);
187
+ private get logger();
188
+ /**
189
+ * Called when a span is started. Adds environment, release, and propagated attributes to the span.
190
+ *
191
+ * @param span - The span that was started
192
+ * @param parentContext - The parent context
193
+ *
194
+ * @override
195
+ */
196
+ onStart(span: Span, parentContext: Context): void;
197
+ /**
198
+ * Called when a span ends. Processes the span for export to Langfuse.
199
+ *
200
+ * This method:
201
+ * 1. Checks if the span should be exported using the shouldExportSpan function
202
+ * 2. Applies data masking to sensitive attributes
203
+ * 3. Handles media content extraction and upload
204
+ * 4. Logs span details in debug mode
205
+ * 5. Passes the span to the parent processor for export
206
+ *
207
+ * @param span - The span that ended
208
+ *
209
+ * @override
210
+ */
211
+ onEnd(span: ReadableSpan): void;
212
+ private flush;
213
+ /**
214
+ * Forces an immediate flush of all pending spans and media uploads.
215
+ *
216
+ * @returns Promise that resolves when all pending operations are complete
217
+ *
218
+ * @override
219
+ */
220
+ forceFlush(): Promise<void>;
221
+ /**
222
+ * Gracefully shuts down the processor, ensuring all pending operations are completed.
223
+ *
224
+ * @returns Promise that resolves when shutdown is complete
225
+ *
226
+ * @override
227
+ */
228
+ shutdown(): Promise<void>;
229
+ private processEndedSpan;
230
+ private applyMaskInPlace;
231
+ private applyMask;
232
+ }
233
+
234
+ export { LangfuseSpanProcessor, type LangfuseSpanProcessorParams, type MaskFunction, type ShouldExportSpan };
@@ -0,0 +1,234 @@
1
+ import { Context } from '@opentelemetry/api';
2
+ import { ReadableSpan, SpanExporter, SpanProcessor, Span } from '@opentelemetry/sdk-trace-base';
3
+
4
+ /**
5
+ * Function type for masking sensitive data in spans before export.
6
+ *
7
+ * @param params - Object containing the data to be masked
8
+ * @param params.data - The data that should be masked
9
+ * @returns The masked data (can be of any type)
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * const maskFunction: MaskFunction = ({ data }) => {
14
+ * if (typeof data === 'string') {
15
+ * return data.replace(/password=\w+/g, 'password=***');
16
+ * }
17
+ * return data;
18
+ * };
19
+ * ```
20
+ *
21
+ * @public
22
+ */
23
+ type MaskFunction = (params: {
24
+ data: any;
25
+ }) => any;
26
+ /**
27
+ * Function type for determining whether a span should be exported to Langfuse.
28
+ *
29
+ * @param params - Object containing the span to evaluate
30
+ * @param params.otelSpan - The OpenTelemetry span to evaluate
31
+ * @returns `true` if the span should be exported, `false` otherwise
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * const shouldExportSpan: ShouldExportSpan = ({ otelSpan }) => {
36
+ * // Only export spans that took longer than 100ms
37
+ * return otelSpan.duration[0] * 1000 + otelSpan.duration[1] / 1000000 > 100;
38
+ * };
39
+ * ```
40
+ *
41
+ * @public
42
+ */
43
+ type ShouldExportSpan = (params: {
44
+ otelSpan: ReadableSpan;
45
+ }) => boolean;
46
+ /**
47
+ * Configuration parameters for the LangfuseSpanProcessor.
48
+ *
49
+ * @public
50
+ */
51
+ interface LangfuseSpanProcessorParams {
52
+ /**
53
+ * Custom OpenTelemetry span exporter. If not provided, a default OTLP exporter will be used.
54
+ */
55
+ exporter?: SpanExporter;
56
+ /**
57
+ * Langfuse public API key. Can also be set via LANGFUSE_PUBLIC_KEY environment variable.
58
+ */
59
+ publicKey?: string;
60
+ /**
61
+ * Langfuse secret API key. Can also be set via LANGFUSE_SECRET_KEY environment variable.
62
+ */
63
+ secretKey?: string;
64
+ /**
65
+ * Langfuse instance base URL. Can also be set via LANGFUSE_BASE_URL environment variable.
66
+ * @defaultValue "https://cloud.langfuse.com"
67
+ */
68
+ baseUrl?: string;
69
+ /**
70
+ * Number of spans to batch before flushing. Can also be set via LANGFUSE_FLUSH_AT environment variable.
71
+ */
72
+ flushAt?: number;
73
+ /**
74
+ * Flush interval in seconds. Can also be set via LANGFUSE_FLUSH_INTERVAL environment variable.
75
+ */
76
+ flushInterval?: number;
77
+ /**
78
+ * Function to mask sensitive data in spans before export.
79
+ */
80
+ mask?: MaskFunction;
81
+ /**
82
+ * Function to determine whether a span should be exported to Langfuse.
83
+ */
84
+ shouldExportSpan?: ShouldExportSpan;
85
+ /**
86
+ * Environment identifier for the traces. Can also be set via LANGFUSE_TRACING_ENVIRONMENT environment variable.
87
+ */
88
+ environment?: string;
89
+ /**
90
+ * Release identifier for the traces. Can also be set via LANGFUSE_RELEASE environment variable.
91
+ */
92
+ release?: string;
93
+ /**
94
+ * Request timeout in seconds. Can also be set via LANGFUSE_TIMEOUT environment variable.
95
+ * @defaultValue 5
96
+ */
97
+ timeout?: number;
98
+ /**
99
+ * Additional HTTP headers to include with requests.
100
+ */
101
+ additionalHeaders?: Record<string, string>;
102
+ /**
103
+ * Span export mode to use.
104
+ *
105
+ * - **batched**: Recommended for production environments with long-running processes.
106
+ * Spans are batched and exported in groups for optimal performance.
107
+ * - **immediate**: Recommended for short-lived environments such as serverless functions.
108
+ * Spans are exported immediately to prevent data loss when the process terminates / is frozen.
109
+ *
110
+ * @defaultValue "batched"
111
+ */
112
+ exportMode?: "immediate" | "batched";
113
+ }
114
+ /**
115
+ * OpenTelemetry span processor for sending spans to Langfuse.
116
+ *
117
+ * This processor extends the standard BatchSpanProcessor to provide:
118
+ * - Automatic batching and flushing of spans to Langfuse
119
+ * - Media content extraction and upload from base64 data URIs
120
+ * - Data masking capabilities for sensitive information
121
+ * - Conditional span export based on custom logic
122
+ * - Environment and release tagging
123
+ *
124
+ * @example
125
+ * ```typescript
126
+ * import { NodeSDK } from '@opentelemetry/sdk-node';
127
+ * import { LangfuseSpanProcessor } from '@elasticdash/otel';
128
+ *
129
+ * const sdk = new NodeSDK({
130
+ * spanProcessors: [
131
+ * new LangfuseSpanProcessor({
132
+ * publicKey: 'pk_...',
133
+ * secretKey: 'sk_...',
134
+ * baseUrl: 'https://cloud.langfuse.com',
135
+ * environment: 'production',
136
+ * mask: ({ data }) => {
137
+ * // Mask sensitive data
138
+ * return data.replace(/api_key=\w+/g, 'api_key=***');
139
+ * }
140
+ * })
141
+ * ]
142
+ * });
143
+ *
144
+ * sdk.start();
145
+ * ```
146
+ *
147
+ * @public
148
+ */
149
+ declare class LangfuseSpanProcessor implements SpanProcessor {
150
+ private pendingEndedSpans;
151
+ private publicKey?;
152
+ private baseUrl?;
153
+ private environment?;
154
+ private release?;
155
+ private mask?;
156
+ private shouldExportSpan?;
157
+ private apiClient;
158
+ private processor;
159
+ private mediaService;
160
+ /**
161
+ * Creates a new LangfuseSpanProcessor instance.
162
+ *
163
+ * @param params - Configuration parameters for the processor
164
+ *
165
+ * @example
166
+ * ```typescript
167
+ * const processor = new LangfuseSpanProcessor({
168
+ * publicKey: 'pk_...',
169
+ * secretKey: 'sk_...',
170
+ * environment: 'staging',
171
+ * flushAt: 10,
172
+ * flushInterval: 2,
173
+ * mask: ({ data }) => {
174
+ * // Custom masking logic
175
+ * return typeof data === 'string'
176
+ * ? data.replace(/secret_\w+/g, 'secret_***')
177
+ * : data;
178
+ * },
179
+ * shouldExportSpan: ({ otelSpan }) => {
180
+ * // Only export spans from specific services
181
+ * return otelSpan.name.startsWith('my-service');
182
+ * }
183
+ * });
184
+ * ```
185
+ */
186
+ constructor(params?: LangfuseSpanProcessorParams);
187
+ private get logger();
188
+ /**
189
+ * Called when a span is started. Adds environment, release, and propagated attributes to the span.
190
+ *
191
+ * @param span - The span that was started
192
+ * @param parentContext - The parent context
193
+ *
194
+ * @override
195
+ */
196
+ onStart(span: Span, parentContext: Context): void;
197
+ /**
198
+ * Called when a span ends. Processes the span for export to Langfuse.
199
+ *
200
+ * This method:
201
+ * 1. Checks if the span should be exported using the shouldExportSpan function
202
+ * 2. Applies data masking to sensitive attributes
203
+ * 3. Handles media content extraction and upload
204
+ * 4. Logs span details in debug mode
205
+ * 5. Passes the span to the parent processor for export
206
+ *
207
+ * @param span - The span that ended
208
+ *
209
+ * @override
210
+ */
211
+ onEnd(span: ReadableSpan): void;
212
+ private flush;
213
+ /**
214
+ * Forces an immediate flush of all pending spans and media uploads.
215
+ *
216
+ * @returns Promise that resolves when all pending operations are complete
217
+ *
218
+ * @override
219
+ */
220
+ forceFlush(): Promise<void>;
221
+ /**
222
+ * Gracefully shuts down the processor, ensuring all pending operations are completed.
223
+ *
224
+ * @returns Promise that resolves when shutdown is complete
225
+ *
226
+ * @override
227
+ */
228
+ shutdown(): Promise<void>;
229
+ private processEndedSpan;
230
+ private applyMaskInPlace;
231
+ private applyMask;
232
+ }
233
+
234
+ export { LangfuseSpanProcessor, type LangfuseSpanProcessorParams, type MaskFunction, type ShouldExportSpan };