@pingops/otel 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.
Files changed (42) hide show
  1. package/README.md +93 -0
  2. package/dist/config.d.ts +75 -0
  3. package/dist/config.d.ts.map +1 -0
  4. package/dist/config.js +5 -0
  5. package/dist/config.js.map +1 -0
  6. package/dist/index.d.ts +10 -0
  7. package/dist/index.d.ts.map +1 -0
  8. package/dist/index.js +9 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/instrumentations/body-extractor.d.ts +48 -0
  11. package/dist/instrumentations/body-extractor.d.ts.map +1 -0
  12. package/dist/instrumentations/body-extractor.js +361 -0
  13. package/dist/instrumentations/body-extractor.js.map +1 -0
  14. package/dist/instrumentations/http.d.ts +12 -0
  15. package/dist/instrumentations/http.d.ts.map +1 -0
  16. package/dist/instrumentations/http.js +38 -0
  17. package/dist/instrumentations/http.js.map +1 -0
  18. package/dist/instrumentations/index.d.ts +17 -0
  19. package/dist/instrumentations/index.d.ts.map +1 -0
  20. package/dist/instrumentations/index.js +32 -0
  21. package/dist/instrumentations/index.js.map +1 -0
  22. package/dist/instrumentations/undici.d.ts +12 -0
  23. package/dist/instrumentations/undici.d.ts.map +1 -0
  24. package/dist/instrumentations/undici.js +38 -0
  25. package/dist/instrumentations/undici.js.map +1 -0
  26. package/dist/processor.d.ts +82 -0
  27. package/dist/processor.d.ts.map +1 -0
  28. package/dist/processor.js +264 -0
  29. package/dist/processor.js.map +1 -0
  30. package/dist/span-processor.d.ts +78 -0
  31. package/dist/span-processor.d.ts.map +1 -0
  32. package/dist/span-processor.js +272 -0
  33. package/dist/span-processor.js.map +1 -0
  34. package/dist/span-wrapper.d.ts +60 -0
  35. package/dist/span-wrapper.d.ts.map +1 -0
  36. package/dist/span-wrapper.js +118 -0
  37. package/dist/span-wrapper.js.map +1 -0
  38. package/dist/tracer-provider.d.ts +57 -0
  39. package/dist/tracer-provider.d.ts.map +1 -0
  40. package/dist/tracer-provider.js +182 -0
  41. package/dist/tracer-provider.js.map +1 -0
  42. package/package.json +43 -0
@@ -0,0 +1,272 @@
1
+ /**
2
+ * PingopsSpanProcessor - OpenTelemetry SpanProcessor implementation
3
+ * Observes finished spans and sends eligible ones to PingOps backend
4
+ *
5
+ * This processor provides:
6
+ * - Automatic filtering of spans (CLIENT spans with HTTP/GenAI attributes only)
7
+ * - Domain and header filtering based on configuration
8
+ * - Batched or immediate export modes using OTLP exporters
9
+ * - Fire-and-forget transport (never blocks application)
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * import { NodeSDK } from '@opentelemetry/sdk-node';
14
+ * import { PingopsSpanProcessor } from '@pingops/otel';
15
+ *
16
+ * const sdk = new NodeSDK({
17
+ * spanProcessors: [
18
+ * new PingopsSpanProcessor({
19
+ * apiKey: 'your-api-key',
20
+ * baseUrl: 'https://api.pingops.com',
21
+ * serviceName: 'my-service',
22
+ * exportMode: 'batched', // or 'immediate'
23
+ * domainAllowList: [
24
+ * { domain: 'api.example.com' }
25
+ * ]
26
+ * })
27
+ * ]
28
+ * });
29
+ *
30
+ * sdk.start();
31
+ * ```
32
+ */
33
+ import { BatchSpanProcessor, SimpleSpanProcessor, } from "@opentelemetry/sdk-trace-base";
34
+ import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
35
+ import { isSpanEligible, shouldCaptureSpan, createLogger, getPropagatedAttributesFromContext, extractSpanPayload, } from "@pingops/core";
36
+ const log = createLogger("[PingOps Processor]");
37
+ /**
38
+ * Creates a filtered span wrapper that applies header filtering to attributes
39
+ *
40
+ * This wrapper applies both domain-specific and global header filtering:
41
+ * - Uses domain allow list to determine domain-specific header rules
42
+ * - Applies global header allow/deny lists
43
+ * - Filters headers from http.request.header and http.response.header attributes
44
+ *
45
+ * Uses a Proxy to automatically forward all properties and methods to the original span,
46
+ * except for 'attributes' which returns the filtered version. This approach is future-proof
47
+ * and will work with any new methods or properties added to ReadableSpan.
48
+ *
49
+ * This allows us to filter headers before the span is serialized by OTLP exporter
50
+ */
51
+ function createFilteredSpan(span, domainAllowList, globalHeadersAllowList, globalHeadersDenyList) {
52
+ // Use extractSpanPayload to get filtered attributes
53
+ // This handles both domain-specific header rules and global header filtering
54
+ const payload = extractSpanPayload(span, domainAllowList, globalHeadersAllowList, globalHeadersDenyList);
55
+ const filteredAttributes = (payload?.attributes ??
56
+ span.attributes);
57
+ log.debug("Payload", { payload });
58
+ // Create a Proxy that intercepts 'attributes' access and forwards everything else
59
+ return new Proxy(span, {
60
+ get(target, prop) {
61
+ // Intercept 'attributes' to return filtered version
62
+ if (prop === "attributes") {
63
+ return filteredAttributes;
64
+ }
65
+ // Forward all other property/method access to the original span
66
+ const value = target[prop];
67
+ // If it's a function, bind it to the original target to preserve 'this' context
68
+ if (typeof value === "function") {
69
+ return value.bind(target);
70
+ }
71
+ return value;
72
+ },
73
+ });
74
+ }
75
+ /**
76
+ * OpenTelemetry span processor for sending spans to PingOps backend.
77
+ *
78
+ * This processor wraps OpenTelemetry's built-in processors (BatchSpanProcessor or SimpleSpanProcessor)
79
+ * and applies filtering before passing spans to the OTLP exporter.
80
+ */
81
+ export class PingopsSpanProcessor {
82
+ processor;
83
+ config;
84
+ /**
85
+ * Creates a new PingopsSpanProcessor instance.
86
+ *
87
+ * @param config - Configuration parameters for the processor
88
+ */
89
+ constructor(config) {
90
+ const exportMode = config.exportMode ?? "batched";
91
+ // Get API key from config or environment
92
+ const apiKey = config.apiKey || process.env.PINGOPS_API_KEY || "";
93
+ // Create OTLP exporter pointing to PingOps backend
94
+ const exporter = new OTLPTraceExporter({
95
+ url: `${config.baseUrl}/v1/traces`,
96
+ headers: {
97
+ Authorization: apiKey ? `Bearer ${apiKey}` : "",
98
+ "Content-Type": "application/json",
99
+ },
100
+ timeoutMillis: 5000,
101
+ });
102
+ // Create underlying processor based on export mode
103
+ if (exportMode === "immediate") {
104
+ this.processor = new SimpleSpanProcessor(exporter);
105
+ }
106
+ else {
107
+ this.processor = new BatchSpanProcessor(exporter, {
108
+ maxExportBatchSize: config.batchSize ?? 50,
109
+ scheduledDelayMillis: config.batchTimeout ?? 5000,
110
+ });
111
+ }
112
+ this.config = {
113
+ debug: config.debug ?? false,
114
+ headersAllowList: config.headersAllowList,
115
+ headersDenyList: config.headersDenyList,
116
+ domainAllowList: config.domainAllowList,
117
+ domainDenyList: config.domainDenyList,
118
+ };
119
+ log.info("Initialized PingopsSpanProcessor", {
120
+ baseUrl: config.baseUrl,
121
+ exportMode,
122
+ batchSize: config.batchSize,
123
+ batchTimeout: config.batchTimeout,
124
+ hasDomainAllowList: !!config.domainAllowList && config.domainAllowList.length > 0,
125
+ hasDomainDenyList: !!config.domainDenyList && config.domainDenyList.length > 0,
126
+ hasHeadersAllowList: !!config.headersAllowList && config.headersAllowList.length > 0,
127
+ hasHeadersDenyList: !!config.headersDenyList && config.headersDenyList.length > 0,
128
+ });
129
+ }
130
+ /**
131
+ * Called when a span starts - extracts parent attributes from context and adds them to the span
132
+ */
133
+ onStart(span, parentContext) {
134
+ const spanContext = span.spanContext();
135
+ log.debug("Span started", {
136
+ spanName: span.name,
137
+ spanId: spanContext.spanId,
138
+ traceId: spanContext.traceId,
139
+ });
140
+ // Extract propagated attributes from context and set them on the span
141
+ const propagatedAttributes = getPropagatedAttributesFromContext(parentContext);
142
+ if (Object.keys(propagatedAttributes).length > 0) {
143
+ for (const [key, value] of Object.entries(propagatedAttributes)) {
144
+ // Type guard: value must be string or string[] for OpenTelemetry attributes
145
+ if (typeof value === "string" || Array.isArray(value)) {
146
+ span.setAttribute(key, value);
147
+ }
148
+ }
149
+ log.debug("Set propagated attributes on span", {
150
+ spanName: span.name,
151
+ attributeKeys: Object.keys(propagatedAttributes),
152
+ });
153
+ }
154
+ this.processor.onStart(span, parentContext);
155
+ }
156
+ /**
157
+ * Called when a span ends. Filters the span and passes it to the underlying processor if eligible.
158
+ *
159
+ * This method:
160
+ * 1. Checks if the span is eligible (CLIENT + HTTP/GenAI attributes)
161
+ * 2. Applies domain filtering (determines if span should be exported)
162
+ * 3. Applies header filtering via FilteredSpan wrapper (domain-specific and global rules)
163
+ * 4. If eligible, passes filtered span to underlying OTLP processor for export
164
+ */
165
+ onEnd(span) {
166
+ const spanContext = span.spanContext();
167
+ log.debug("Span ended, processing", {
168
+ spanName: span.name,
169
+ spanId: spanContext.spanId,
170
+ traceId: spanContext.traceId,
171
+ spanKind: span.kind,
172
+ });
173
+ try {
174
+ // Step 1: Check if span is eligible (CLIENT + HTTP/GenAI attributes)
175
+ if (!isSpanEligible(span)) {
176
+ log.debug("Span not eligible, skipping", {
177
+ spanName: span.name,
178
+ spanId: spanContext.spanId,
179
+ reason: "not CLIENT or missing HTTP/GenAI attributes",
180
+ });
181
+ return;
182
+ }
183
+ // Step 2: Extract URL for domain filtering
184
+ const attributes = span.attributes;
185
+ const url = attributes["http.url"] ||
186
+ attributes["url.full"] ||
187
+ (attributes["server.address"]
188
+ ? `https://${String(attributes["server.address"])}`
189
+ : "");
190
+ log.debug("Extracted URL for domain filtering", {
191
+ spanName: span.name,
192
+ url,
193
+ hasHttpUrl: !!attributes["http.url"],
194
+ hasUrlFull: !!attributes["url.full"],
195
+ hasServerAddress: !!attributes["server.address"],
196
+ });
197
+ // Step 3: Apply domain filtering
198
+ if (url) {
199
+ const shouldCapture = shouldCaptureSpan(url, this.config.domainAllowList, this.config.domainDenyList);
200
+ if (!shouldCapture) {
201
+ log.info("Span filtered out by domain rules", {
202
+ spanName: span.name,
203
+ spanId: spanContext.spanId,
204
+ url,
205
+ });
206
+ return;
207
+ }
208
+ }
209
+ else {
210
+ log.debug("No URL found for domain filtering, proceeding", {
211
+ spanName: span.name,
212
+ });
213
+ }
214
+ // Step 4: Apply filtering (header filtering with domain-specific rules) by wrapping the span
215
+ const filteredSpan = createFilteredSpan(span, this.config.domainAllowList, this.config.headersAllowList, this.config.headersDenyList);
216
+ // Step 5: Span passed all filters, pass filtered span to underlying processor for export
217
+ this.processor.onEnd(filteredSpan);
218
+ log.info("Span passed all filters and queued for export", {
219
+ spanName: span.name,
220
+ spanId: spanContext.spanId,
221
+ traceId: spanContext.traceId,
222
+ url,
223
+ hasHeaderFiltering: !!(this.config.headersAllowList || this.config.headersDenyList),
224
+ });
225
+ }
226
+ catch (error) {
227
+ // Defensive error handling - never crash the app
228
+ log.error("Error processing span", {
229
+ spanName: span.name,
230
+ spanId: spanContext.spanId,
231
+ error: error instanceof Error ? error.message : String(error),
232
+ });
233
+ }
234
+ }
235
+ /**
236
+ * Forces an immediate flush of all pending spans.
237
+ *
238
+ * @returns Promise that resolves when all pending operations are complete
239
+ */
240
+ async forceFlush() {
241
+ log.info("Force flushing spans");
242
+ try {
243
+ await this.processor.forceFlush();
244
+ log.info("Force flush complete");
245
+ }
246
+ catch (error) {
247
+ log.error("Error during force flush", {
248
+ error: error instanceof Error ? error.message : String(error),
249
+ });
250
+ throw error;
251
+ }
252
+ }
253
+ /**
254
+ * Gracefully shuts down the processor, ensuring all pending operations are completed.
255
+ *
256
+ * @returns Promise that resolves when shutdown is complete
257
+ */
258
+ async shutdown() {
259
+ log.info("Shutting down processor");
260
+ try {
261
+ await this.processor.shutdown();
262
+ log.info("Processor shutdown complete");
263
+ }
264
+ catch (error) {
265
+ log.error("Error during processor shutdown", {
266
+ error: error instanceof Error ? error.message : String(error),
267
+ });
268
+ throw error;
269
+ }
270
+ }
271
+ }
272
+ //# sourceMappingURL=span-processor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"span-processor.js","sourceRoot":"","sources":["../src/span-processor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAOH,OAAO,EACL,kBAAkB,EAClB,mBAAmB,GACpB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,yCAAyC,CAAC;AAE5E,OAAO,EACL,cAAc,EACd,iBAAiB,EAEjB,YAAY,EACZ,kCAAkC,EAClC,kBAAkB,GACnB,MAAM,eAAe,CAAC;AAGvB,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C,MAAM,CAAC,KAAK,CAAC;aAC9D,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAC,UAAU;QACrB,GAAG,CAAC,IAAI,CAAC,sBAAsB,CAAC,CAAC;QACjC,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,SAAS,CAAC,UAAU,EAAE,CAAC;YAClC,GAAG,CAAC,IAAI,CAAC,sBAAsB,CAAC,CAAC;QACnC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,GAAG,CAAC,KAAK,CAAC,0BAA0B,EAAE;gBACpC,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;aAC9D,CAAC,CAAC;YACH,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAC,QAAQ;QACnB,GAAG,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC;QACpC,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC;YAChC,GAAG,CAAC,IAAI,CAAC,6BAA6B,CAAC,CAAC;QAC1C,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,GAAG,CAAC,KAAK,CAAC,iCAAiC,EAAE;gBAC3C,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;aAC9D,CAAC,CAAC;YACH,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Span wrapper for PingOps SDK
3
+ *
4
+ * Provides PingopsSpan class similar to LangfuseSpan for wrapping OpenTelemetry spans
5
+ * with PingOps-specific functionality.
6
+ */
7
+ import { TimeInput, Span, SpanContext } from '@opentelemetry/api';
8
+ import type { Attributes } from '@opentelemetry/api';
9
+ import type { SpanAttributes } from './tracer-provider';
10
+ /**
11
+ * Base class for PingOps span wrappers
12
+ *
13
+ * Provides common functionality for wrapping OpenTelemetry spans with PingOps-specific features.
14
+ */
15
+ declare abstract class PingopsBaseSpan {
16
+ /** The underlying OpenTelemetry span */
17
+ readonly otelSpan: Span;
18
+ /** The span ID from the OpenTelemetry span context */
19
+ id: string;
20
+ /** The trace ID from the OpenTelemetry span context */
21
+ traceId: string;
22
+ constructor(otelSpan: Span);
23
+ /**
24
+ * Ends the span, marking it as complete.
25
+ *
26
+ * @param endTime - Optional end time, defaults to current time
27
+ */
28
+ end(endTime?: TimeInput): void;
29
+ /**
30
+ * Updates the span with new attributes.
31
+ *
32
+ * @param attributes - Span attributes to set
33
+ * @returns This span for method chaining
34
+ */
35
+ update(attributes: SpanAttributes): PingopsSpan;
36
+ }
37
+ /**
38
+ * PingOps span wrapper for tracking operations
39
+ *
40
+ * Provides a convenient wrapper around OpenTelemetry spans with PingOps-specific
41
+ * attribute handling and lifecycle management.
42
+ */
43
+ export declare class PingopsSpan extends PingopsBaseSpan {
44
+ constructor(otelSpan: Span);
45
+ }
46
+ /**
47
+ * Creates an OpenTelemetry span with the PingOps tracer.
48
+ *
49
+ * @param params - Parameters for span creation
50
+ * @returns The created OpenTelemetry span
51
+ * @internal
52
+ */
53
+ export declare function createOtelSpan(params: {
54
+ name: string;
55
+ startTime?: TimeInput;
56
+ parentSpanContext?: SpanContext;
57
+ attributes?: Attributes;
58
+ }): Span;
59
+ export {};
60
+ //# sourceMappingURL=span-wrapper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"span-wrapper.d.ts","sourceRoot":"","sources":["../src/span-wrapper.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAkB,SAAS,EAAE,IAAI,EAAE,WAAW,EAAW,MAAM,oBAAoB,CAAC;AAC3F,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAErD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD;;;;GAIG;AACH,uBAAe,eAAe;IAC5B,wCAAwC;IACxC,SAAgB,QAAQ,EAAE,IAAI,CAAC;IAC/B,sDAAsD;IAC/C,EAAE,EAAE,MAAM,CAAC;IAClB,uDAAuD;IAChD,OAAO,EAAE,MAAM,CAAC;gBAEX,QAAQ,EAAE,IAAI;IAO1B;;;;OAIG;IACI,GAAG,CAAC,OAAO,CAAC,EAAE,SAAS,GAAG,IAAI;IAIrC;;;;;OAKG;IACI,MAAM,CAAC,UAAU,EAAE,cAAc,GAAG,WAAW;CA8CvD;AAED;;;;;GAKG;AACH,qBAAa,WAAY,SAAQ,eAAe;gBAClC,QAAQ,EAAE,IAAI;CAG3B;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE;IACrC,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,iBAAiB,CAAC,EAAE,WAAW,CAAC;IAChC,UAAU,CAAC,EAAE,UAAU,CAAC;CACzB,GAAG,IAAI,CAYP"}
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Span wrapper for PingOps SDK
3
+ *
4
+ * Provides PingopsSpan class similar to LangfuseSpan for wrapping OpenTelemetry spans
5
+ * with PingOps-specific functionality.
6
+ */
7
+ import { trace, context } from '@opentelemetry/api';
8
+ import { getPingopsTracerProvider } from './tracer-provider';
9
+ /**
10
+ * Base class for PingOps span wrappers
11
+ *
12
+ * Provides common functionality for wrapping OpenTelemetry spans with PingOps-specific features.
13
+ */
14
+ class PingopsBaseSpan {
15
+ /** The underlying OpenTelemetry span */
16
+ otelSpan;
17
+ /** The span ID from the OpenTelemetry span context */
18
+ id;
19
+ /** The trace ID from the OpenTelemetry span context */
20
+ traceId;
21
+ constructor(otelSpan) {
22
+ this.otelSpan = otelSpan;
23
+ const spanContext = otelSpan.spanContext();
24
+ this.id = spanContext.spanId;
25
+ this.traceId = spanContext.traceId;
26
+ }
27
+ /**
28
+ * Ends the span, marking it as complete.
29
+ *
30
+ * @param endTime - Optional end time, defaults to current time
31
+ */
32
+ end(endTime) {
33
+ this.otelSpan.end(endTime);
34
+ }
35
+ /**
36
+ * Updates the span with new attributes.
37
+ *
38
+ * @param attributes - Span attributes to set
39
+ * @returns This span for method chaining
40
+ */
41
+ update(attributes) {
42
+ const otelAttributes = {};
43
+ // Handle nested attributes object
44
+ if (attributes.attributes) {
45
+ Object.assign(otelAttributes, attributes.attributes);
46
+ }
47
+ // Handle input/output
48
+ if (attributes.input !== undefined) {
49
+ otelAttributes['span.input'] =
50
+ typeof attributes.input === 'string'
51
+ ? attributes.input
52
+ : JSON.stringify(attributes.input);
53
+ }
54
+ if (attributes.output !== undefined) {
55
+ otelAttributes['span.output'] =
56
+ typeof attributes.output === 'string'
57
+ ? attributes.output
58
+ : JSON.stringify(attributes.output);
59
+ }
60
+ // Handle metadata
61
+ if (attributes.metadata !== undefined) {
62
+ for (const [key, value] of Object.entries(attributes.metadata)) {
63
+ otelAttributes[`span.metadata.${key}`] =
64
+ typeof value === 'string' ? value : JSON.stringify(value);
65
+ }
66
+ }
67
+ // Add any other custom attributes (excluding the special keys)
68
+ for (const [key, value] of Object.entries(attributes)) {
69
+ if (key !== 'attributes' &&
70
+ key !== 'input' &&
71
+ key !== 'output' &&
72
+ key !== 'metadata') {
73
+ otelAttributes[key] = value;
74
+ }
75
+ }
76
+ this.otelSpan.setAttributes(otelAttributes);
77
+ return this;
78
+ }
79
+ }
80
+ /**
81
+ * PingOps span wrapper for tracking operations
82
+ *
83
+ * Provides a convenient wrapper around OpenTelemetry spans with PingOps-specific
84
+ * attribute handling and lifecycle management.
85
+ */
86
+ export class PingopsSpan extends PingopsBaseSpan {
87
+ constructor(otelSpan) {
88
+ super(otelSpan);
89
+ }
90
+ }
91
+ /**
92
+ * Creates an OpenTelemetry span with the PingOps tracer.
93
+ *
94
+ * @param params - Parameters for span creation
95
+ * @returns The created OpenTelemetry span
96
+ * @internal
97
+ */
98
+ export function createOtelSpan(params) {
99
+ const provider = getPingopsTracerProvider();
100
+ const tracer = provider.getTracer('@pingops/sdk', '0.1.0');
101
+ return tracer.startSpan(params.name, {
102
+ startTime: params.startTime,
103
+ attributes: params.attributes,
104
+ }, createParentContext(params.parentSpanContext));
105
+ }
106
+ /**
107
+ * Creates a parent context from a span context.
108
+ *
109
+ * @param parentSpanContext - The span context to use as parent
110
+ * @returns The created context or undefined if no parent provided
111
+ * @internal
112
+ */
113
+ function createParentContext(parentSpanContext) {
114
+ if (!parentSpanContext)
115
+ return undefined;
116
+ return trace.setSpanContext(context.active(), parentSpanContext);
117
+ }
118
+ //# sourceMappingURL=span-wrapper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"span-wrapper.js","sourceRoot":"","sources":["../src/span-wrapper.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,KAAK,EAAE,OAAO,EAAyC,MAAM,oBAAoB,CAAC;AAE3F,OAAO,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAC;AAG7D;;;;GAIG;AACH,MAAe,eAAe;IAC5B,wCAAwC;IACxB,QAAQ,CAAO;IAC/B,sDAAsD;IAC/C,EAAE,CAAS;IAClB,uDAAuD;IAChD,OAAO,CAAS;IAEvB,YAAY,QAAc;QACxB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,MAAM,WAAW,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;QAC3C,IAAI,CAAC,EAAE,GAAG,WAAW,CAAC,MAAM,CAAC;QAC7B,IAAI,CAAC,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC;IACrC,CAAC;IAED;;;;OAIG;IACI,GAAG,CAAC,OAAmB;QAC5B,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAC7B,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,UAA0B;QACtC,MAAM,cAAc,GAAe,EAAE,CA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;gBAChB,GAAG,KAAK,UAAU,EAClB,CAAC;gBACD,cAAc,CAAC,GAAG,CAAC,GAAG,KAAY,CAAC;YACrC,CAAC;QACH,CAAC;QAED,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,cAAc,CAAC,CAAC;QAC5C,OAAO,IAAmB,CAAC;IAC7B,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,OAAO,WAAY,SAAQ,eAAe;IAC9C,YAAY,QAAc;QACxB,KAAK,CAAC,QAAQ,CAAC,CAAC;IAClB,CAAC;CACF;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,MAK9B;IACC,MAAM,QAAQ,GAAG,wBAAwB,EAAE,CAAC;IAC5C,MAAM,MAAM,GAAG,QAAQ,CAAC,SAAS,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;IAE3D,OAAO,MAAM,CAAC,SAAS,CACrB,MAAM,CAAC,IAAI,EACX;QACE,SAAS,EAAE,MAAM,CAAC,SAAS;QAC3B,UAAU,EAAE,MAAM,CAAC,UAAU;KAC9B,EACD,mBAAmB,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAC9C,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,SAAS,mBAAmB,CAC1B,iBAA+B;IAE/B,IAAI,CAAC,iBAAiB;QAAE,OAAO,SAAS,CAAC;IAEzC,OAAO,KAAK,CAAC,cAAc,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,iBAAiB,CAAC,CAAC;AACnE,CAAC"}
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Tracer Provider with global state and isolated TracerProvider architecture
3
+ *
4
+ * This module provides an isolated TracerProvider that shares the same
5
+ * span processors (like PingopsSpanProcessor) with the main OpenTelemetry SDK.
6
+ * This ensures manual spans created via startSpan are properly processed.
7
+ *
8
+ * Architecture follows Langfuse's pattern with global state management.
9
+ */
10
+ import type { TracerProvider } from "@opentelemetry/api";
11
+ import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
12
+ import type { SpanProcessor } from "@opentelemetry/sdk-trace-base";
13
+ /**
14
+ * Sets an isolated TracerProvider for PingOps tracing operations.
15
+ *
16
+ * This allows PingOps to use its own TracerProvider instance, separate from
17
+ * the global OpenTelemetry TracerProvider. This is useful for avoiding conflicts
18
+ * with other OpenTelemetry instrumentation in the application.
19
+ *
20
+ * @param provider - The TracerProvider instance to use, or null to clear the isolated provider
21
+ * @public
22
+ */
23
+ export declare function setPingopsTracerProvider(provider: TracerProvider | null): void;
24
+ /**
25
+ * Gets the TracerProvider for PingOps tracing operations.
26
+ *
27
+ * Returns the isolated TracerProvider if one has been set via setPingopsTracerProvider(),
28
+ * otherwise falls back to the global OpenTelemetry TracerProvider.
29
+ *
30
+ * @returns The TracerProvider instance to use for PingOps tracing
31
+ * @public
32
+ */
33
+ export declare function getPingopsTracerProvider(): TracerProvider;
34
+ /**
35
+ * Initializes the isolated TracerProvider with the given span processors
36
+ *
37
+ * This creates a separate TracerProvider that shares the same span processors
38
+ * (like PingopsSpanProcessor) with the main SDK. This ensures manual spans
39
+ * are processed correctly.
40
+ *
41
+ * @param spanProcessors - Array of span processors to use (e.g., PingopsSpanProcessor)
42
+ * @param serviceName - Service name for resource attributes
43
+ * @deprecated Use setPingopsTracerProvider instead
44
+ */
45
+ export declare function initializeTracerProvider(spanProcessors: SpanProcessor[], serviceName: string): void;
46
+ /**
47
+ * Gets the isolated TracerProvider instance
48
+ *
49
+ * @returns The TracerProvider instance, or null if not initialized
50
+ * @deprecated Use getPingopsTracerProvider instead
51
+ */
52
+ export declare function getTracerProvider(): NodeTracerProvider | null;
53
+ /**
54
+ * Shuts down the TracerProvider and flushes remaining spans
55
+ */
56
+ export declare function shutdownTracerProvider(): Promise<void>;
57
+ //# sourceMappingURL=tracer-provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracer-provider.d.ts","sourceRoot":"","sources":["../src/tracer-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACnE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AA4EnE;;;;;;;;;GASG;AACH,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,cAAc,GAAG,IAAI,GAC9B,IAAI,CAcN;AAED;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,IAAI,cAAc,CAezD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,wBAAwB,CACtC,cAAc,EAAE,aAAa,EAAE,EAC/B,WAAW,EAAE,MAAM,GAClB,IAAI,CA4BN;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,IAAI,kBAAkB,GAAG,IAAI,CAG7D;AAED;;GAEG;AACH,wBAAsB,sBAAsB,IAAI,OAAO,CAAC,IAAI,CAAC,CA6B5D"}