@observyze/sdk 0.1.4 → 0.1.5

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.
@@ -1,2 +1 @@
1
- export { a as ObservyzeExporterConfig, b as ObservyzeSpanExporter } from '../index-IZdiaORv.mjs';
2
- import '@observyze/types';
1
+ export { f as ObservyzeExporterConfig, g as ObservyzeSpanExporter } from '../index-ClE8H5jj.mjs';
@@ -1,2 +1 @@
1
- export { a as ObservyzeExporterConfig, b as ObservyzeSpanExporter } from '../index-IZdiaORv.js';
2
- import '@observyze/types';
1
+ export { f as ObservyzeExporterConfig, g as ObservyzeSpanExporter } from '../index-ClE8H5jj.js';
@@ -24,23 +24,43 @@ __export(opentelemetry_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(opentelemetry_exports);
26
26
 
27
- // src/types.ts
28
- var import_types = require("@observyze/types");
29
-
30
27
  // src/trace.ts
31
28
  var import_crypto = require("crypto");
32
29
  function generateId() {
33
30
  return `${Date.now()}-${(0, import_crypto.randomUUID)().substring(0, 8)}`;
34
31
  }
32
+ var SAFE_OPERATIONAL_METADATA = /* @__PURE__ */ new Set([
33
+ "provider",
34
+ "model",
35
+ "temperature",
36
+ "max_tokens",
37
+ "max_completion_tokens",
38
+ "max_output_tokens",
39
+ "latency_ms",
40
+ "streaming",
41
+ "stream_completed",
42
+ "output_truncated",
43
+ "token_usage_source",
44
+ "cost_source",
45
+ "known_pricing",
46
+ "source",
47
+ "lifecycle",
48
+ "invocation_type"
49
+ ]);
50
+ function isSafeOperationalValue(value) {
51
+ return value === null || ["string", "number", "boolean"].includes(typeof value);
52
+ }
35
53
  var Span = class {
36
54
  data;
37
55
  startTime;
38
- constructor(name, type, parentSpanId) {
56
+ captureContent;
57
+ constructor(name, type, parentSpanId, captureContent = true) {
39
58
  this.startTime = Date.now();
59
+ this.captureContent = captureContent;
40
60
  this.data = {
41
61
  span_id: generateId(),
42
62
  parent_span_id: parentSpanId,
43
- name,
63
+ name: captureContent ? name : "[CONTENT_CAPTURE_DISABLED]",
44
64
  type,
45
65
  start_time: new Date(this.startTime),
46
66
  end_time: new Date(this.startTime),
@@ -55,14 +75,14 @@ var Span = class {
55
75
  * Set the input data for this span
56
76
  */
57
77
  setInput(input) {
58
- this.data.input = input;
78
+ if (this.captureContent) this.data.input = input;
59
79
  return this;
60
80
  }
61
81
  /**
62
82
  * Set the output data for this span
63
83
  */
64
84
  setOutput(output) {
65
- this.data.output = output;
85
+ if (this.captureContent) this.data.output = output;
66
86
  return this;
67
87
  }
68
88
  /**
@@ -70,9 +90,9 @@ var Span = class {
70
90
  */
71
91
  setError(error) {
72
92
  this.data.error = {
73
- message: error.message,
74
- stack: error.stack,
75
- code: error.code
93
+ message: this.captureContent ? error.message : "Error details omitted because content capture is disabled",
94
+ stack: this.captureContent ? error.stack : void 0,
95
+ code: this.captureContent ? error.code : void 0
76
96
  };
77
97
  return this;
78
98
  }
@@ -80,14 +100,16 @@ var Span = class {
80
100
  * Set metadata for this span
81
101
  */
82
102
  setMetadata(key, value) {
83
- this.data.metadata[key] = value;
103
+ if (this.captureContent || SAFE_OPERATIONAL_METADATA.has(key) && isSafeOperationalValue(value)) {
104
+ this.data.metadata[key] = value;
105
+ }
84
106
  return this;
85
107
  }
86
108
  /**
87
109
  * Set multiple metadata fields at once
88
110
  */
89
111
  setMetadataAll(metadata) {
90
- this.data.metadata = { ...this.data.metadata, ...metadata };
112
+ for (const [key, value] of Object.entries(metadata)) this.setMetadata(key, value);
91
113
  return this;
92
114
  }
93
115
  /**
@@ -123,14 +145,16 @@ var Trace = class {
123
145
  startTime;
124
146
  spans = [];
125
147
  ended = false;
126
- constructor(name, organizationId, projectId) {
148
+ captureContent;
149
+ constructor(name, organizationId, projectId, captureContent = true) {
127
150
  this.startTime = Date.now();
151
+ this.captureContent = captureContent;
128
152
  this.data = {
129
153
  trace_id: generateId(),
130
154
  organization_id: organizationId,
131
155
  project_id: projectId,
132
- name,
133
- status: import_types.TraceStatus.RUNNING,
156
+ name: captureContent ? name : "[CONTENT_CAPTURE_DISABLED]",
157
+ status: "running" /* RUNNING */,
134
158
  start_time: new Date(this.startTime),
135
159
  end_time: new Date(this.startTime),
136
160
  // Will be updated on end()
@@ -147,7 +171,7 @@ var Trace = class {
147
171
  if (this.ended) {
148
172
  throw new Error("Cannot start span on an ended trace");
149
173
  }
150
- const span = new Span(name, type, parentSpanId);
174
+ const span = new Span(name, type, parentSpanId, this.captureContent);
151
175
  this.spans.push(span);
152
176
  return span;
153
177
  }
@@ -155,20 +179,23 @@ var Trace = class {
155
179
  * Add metadata to the trace
156
180
  */
157
181
  setMetadata(key, value) {
158
- this.data.metadata[key] = value;
182
+ if (this.captureContent || SAFE_OPERATIONAL_METADATA.has(key) && isSafeOperationalValue(value)) {
183
+ this.data.metadata[key] = value;
184
+ }
159
185
  return this;
160
186
  }
161
187
  /**
162
188
  * Set multiple metadata fields at once
163
189
  */
164
190
  setMetadataAll(metadata) {
165
- this.data.metadata = { ...this.data.metadata, ...metadata };
191
+ for (const [key, value] of Object.entries(metadata)) this.setMetadata(key, value);
166
192
  return this;
167
193
  }
168
194
  /**
169
195
  * Add tags to the trace
170
196
  */
171
197
  addTag(tag) {
198
+ if (!this.captureContent) return this;
172
199
  if (!this.data.tags.includes(tag)) {
173
200
  this.data.tags.push(tag);
174
201
  }
@@ -185,20 +212,20 @@ var Trace = class {
185
212
  * Set the user ID associated with this trace
186
213
  */
187
214
  setUserId(userId) {
188
- this.data.user_id = userId;
215
+ if (this.captureContent) this.data.user_id = userId;
189
216
  return this;
190
217
  }
191
218
  /**
192
219
  * Set the session ID associated with this trace
193
220
  */
194
221
  setSessionId(sessionId) {
195
- this.data.session_id = sessionId;
222
+ if (this.captureContent) this.data.session_id = sessionId;
196
223
  return this;
197
224
  }
198
225
  /**
199
226
  * End the trace with a final status
200
227
  */
201
- end(status = import_types.TraceStatus.SUCCESS) {
228
+ end(status = "success" /* SUCCESS */) {
202
229
  if (this.ended) {
203
230
  return;
204
231
  }
@@ -299,9 +326,9 @@ var ObservyzeSpanExporter = class {
299
326
  const statusCode = status?.code;
300
327
  if (statusCode === 2) {
301
328
  oSpan.setError(new Error(status?.message || "OTel span error"));
302
- trace.end(import_types.TraceStatus.ERROR);
329
+ trace.end("error" /* ERROR */);
303
330
  } else {
304
- trace.end(import_types.TraceStatus.SUCCESS);
331
+ trace.end("success" /* SUCCESS */);
305
332
  }
306
333
  }
307
334
  resultCallback({ code: 0 });
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  ObservyzeSpanExporter
3
- } from "../chunk-FQBYUOJB.mjs";
3
+ } from "../chunk-53PSEBAD.mjs";
4
4
  export {
5
5
  ObservyzeSpanExporter
6
6
  };
package/package.json CHANGED
@@ -1,57 +1,60 @@
1
- {
2
- "name": "@observyze/sdk",
3
- "version": "0.1.4",
4
- "description": "Node.js SDK for Observyze AI Observability Platform",
5
- "files": [
6
- "dist"
7
- ],
8
- "main": "./dist/index.js",
9
- "types": "./dist/index.d.ts",
10
- "exports": {
11
- ".": {
12
- "types": "./dist/index.d.ts",
13
- "require": "./dist/index.js",
14
- "import": "./dist/index.mjs"
15
- },
16
- "./opentelemetry": {
17
- "types": "./dist/opentelemetry/index.d.ts",
18
- "require": "./dist/opentelemetry/index.js",
19
- "import": "./dist/opentelemetry/index.mjs"
20
- }
21
- },
22
- "scripts": {
23
- "build": "tsup src/index.ts src/opentelemetry/index.ts --format cjs,esm --dts",
24
- "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
25
- "lint": "eslint src --ext .ts",
26
- "test": "vitest",
27
- "typecheck": "tsc --noEmit"
28
- },
29
- "keywords": [
30
- "Observyze",
31
- "ai",
32
- "observability",
33
- "tracing",
34
- "monitoring",
35
- "llm"
36
- ],
37
- "author": "Observyze",
38
- "license": "MIT",
39
- "dependencies": {
40
- "@observyze/types": "*",
41
- "debug": "4.4.3"
42
- },
43
- "optionalDependencies": {
44
- "@opentelemetry/api": "^1.9.0",
45
- "@opentelemetry/sdk-trace-base": "^1.30.0"
46
- },
47
- "devDependencies": {
48
- "@types/debug": "4.1.13",
49
- "@types/node": "^20.0.0",
50
- "tsup": "^8.0.0",
51
- "typescript": "^5.3.0",
52
- "vitest": "^1.0.0"
53
- },
54
- "engines": {
55
- "node": ">=20.0.0"
56
- }
57
- }
1
+ {
2
+ "name": "@observyze/sdk",
3
+ "version": "0.1.5",
4
+ "description": "Node.js SDK for Observyze AI Observability Platform",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "require": "./dist/index.js",
11
+ "import": "./dist/index.mjs"
12
+ },
13
+ "./opentelemetry": {
14
+ "types": "./dist/opentelemetry/index.d.ts",
15
+ "require": "./dist/opentelemetry/index.js",
16
+ "import": "./dist/opentelemetry/index.mjs"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "scripts": {
23
+ "build": "tsup src/index.ts src/opentelemetry/index.ts --format cjs,esm --dts",
24
+ "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
25
+ "test": "vitest run",
26
+ "typecheck": "tsc --noEmit"
27
+ },
28
+ "keywords": [
29
+ "Observyze",
30
+ "ai",
31
+ "observability",
32
+ "tracing",
33
+ "monitoring",
34
+ "llm",
35
+ "openai",
36
+ "anthropic",
37
+ "gemini",
38
+ "langchain",
39
+ "vercel"
40
+ ],
41
+ "author": "Observyze",
42
+ "license": "MIT",
43
+ "dependencies": {
44
+ "debug": "4.4.3"
45
+ },
46
+ "optionalDependencies": {
47
+ "@opentelemetry/api": "^1.9.0",
48
+ "@opentelemetry/sdk-trace-base": "^2.10.0"
49
+ },
50
+ "devDependencies": {
51
+ "@types/debug": "4.1.13",
52
+ "@types/node": "^20.0.0",
53
+ "tsup": "^8.0.0",
54
+ "typescript": "^5.3.0",
55
+ "vitest": "^4.1.11"
56
+ },
57
+ "engines": {
58
+ "node": ">=20.0.0"
59
+ }
60
+ }
@@ -1,357 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
6
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
7
- }) : x)(function(x) {
8
- if (typeof require !== "undefined") return require.apply(this, arguments);
9
- throw Error('Dynamic require of "' + x + '" is not supported');
10
- });
11
- var __esm = (fn, res) => function __init() {
12
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
13
- };
14
- var __export = (target, all) => {
15
- for (var name in all)
16
- __defProp(target, name, { get: all[name], enumerable: true });
17
- };
18
- var __copyProps = (to, from, except, desc) => {
19
- if (from && typeof from === "object" || typeof from === "function") {
20
- for (let key of __getOwnPropNames(from))
21
- if (!__hasOwnProp.call(to, key) && key !== except)
22
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
- }
24
- return to;
25
- };
26
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
-
28
- // src/types.ts
29
- import { SpanType, TraceStatus } from "@observyze/types";
30
- var init_types = __esm({
31
- "src/types.ts"() {
32
- "use strict";
33
- }
34
- });
35
-
36
- // src/opentelemetry/exporter.ts
37
- init_types();
38
-
39
- // src/trace.ts
40
- init_types();
41
- import { randomUUID } from "crypto";
42
- function generateId() {
43
- return `${Date.now()}-${randomUUID().substring(0, 8)}`;
44
- }
45
- var Span = class {
46
- data;
47
- startTime;
48
- constructor(name, type, parentSpanId) {
49
- this.startTime = Date.now();
50
- this.data = {
51
- span_id: generateId(),
52
- parent_span_id: parentSpanId,
53
- name,
54
- type,
55
- start_time: new Date(this.startTime),
56
- end_time: new Date(this.startTime),
57
- // Will be updated on end()
58
- duration_ms: 0,
59
- input: null,
60
- output: null,
61
- metadata: {}
62
- };
63
- }
64
- /**
65
- * Set the input data for this span
66
- */
67
- setInput(input) {
68
- this.data.input = input;
69
- return this;
70
- }
71
- /**
72
- * Set the output data for this span
73
- */
74
- setOutput(output) {
75
- this.data.output = output;
76
- return this;
77
- }
78
- /**
79
- * Record an error that occurred during span execution
80
- */
81
- setError(error) {
82
- this.data.error = {
83
- message: error.message,
84
- stack: error.stack,
85
- code: error.code
86
- };
87
- return this;
88
- }
89
- /**
90
- * Set metadata for this span
91
- */
92
- setMetadata(key, value) {
93
- this.data.metadata[key] = value;
94
- return this;
95
- }
96
- /**
97
- * Set multiple metadata fields at once
98
- */
99
- setMetadataAll(metadata) {
100
- this.data.metadata = { ...this.data.metadata, ...metadata };
101
- return this;
102
- }
103
- /**
104
- * Set token usage information
105
- */
106
- setTokens(tokens) {
107
- this.data.tokens = tokens;
108
- return this;
109
- }
110
- /**
111
- * End the span and calculate duration
112
- */
113
- end() {
114
- const endTime = Date.now();
115
- this.data.end_time = new Date(endTime);
116
- this.data.duration_ms = endTime - this.startTime;
117
- }
118
- /**
119
- * Get the span ID
120
- */
121
- get id() {
122
- return this.data.span_id;
123
- }
124
- /**
125
- * Get the span data for serialization
126
- */
127
- toJSON() {
128
- return { ...this.data };
129
- }
130
- };
131
- var Trace = class {
132
- data;
133
- startTime;
134
- spans = [];
135
- ended = false;
136
- constructor(name, organizationId, projectId) {
137
- this.startTime = Date.now();
138
- this.data = {
139
- trace_id: generateId(),
140
- organization_id: organizationId,
141
- project_id: projectId,
142
- name,
143
- status: TraceStatus.RUNNING,
144
- start_time: new Date(this.startTime),
145
- end_time: new Date(this.startTime),
146
- // Will be updated on end()
147
- duration_ms: 0,
148
- metadata: {},
149
- spans: [],
150
- tags: []
151
- };
152
- }
153
- /**
154
- * Start a new span within this trace
155
- */
156
- startSpan(name, type, parentSpanId) {
157
- if (this.ended) {
158
- throw new Error("Cannot start span on an ended trace");
159
- }
160
- const span = new Span(name, type, parentSpanId);
161
- this.spans.push(span);
162
- return span;
163
- }
164
- /**
165
- * Add metadata to the trace
166
- */
167
- setMetadata(key, value) {
168
- this.data.metadata[key] = value;
169
- return this;
170
- }
171
- /**
172
- * Set multiple metadata fields at once
173
- */
174
- setMetadataAll(metadata) {
175
- this.data.metadata = { ...this.data.metadata, ...metadata };
176
- return this;
177
- }
178
- /**
179
- * Add tags to the trace
180
- */
181
- addTag(tag) {
182
- if (!this.data.tags.includes(tag)) {
183
- this.data.tags.push(tag);
184
- }
185
- return this;
186
- }
187
- /**
188
- * Add multiple tags at once
189
- */
190
- addTags(tags) {
191
- tags.forEach((tag) => this.addTag(tag));
192
- return this;
193
- }
194
- /**
195
- * Set the user ID associated with this trace
196
- */
197
- setUserId(userId) {
198
- this.data.user_id = userId;
199
- return this;
200
- }
201
- /**
202
- * Set the session ID associated with this trace
203
- */
204
- setSessionId(sessionId) {
205
- this.data.session_id = sessionId;
206
- return this;
207
- }
208
- /**
209
- * End the trace with a final status
210
- */
211
- end(status = TraceStatus.SUCCESS) {
212
- if (this.ended) {
213
- return;
214
- }
215
- const endTime = Date.now();
216
- this.data.end_time = new Date(endTime);
217
- this.data.duration_ms = endTime - this.startTime;
218
- this.data.status = status;
219
- this.data.spans = this.spans.map((span) => span.toJSON());
220
- this.ended = true;
221
- }
222
- /**
223
- * Get the trace ID
224
- */
225
- get id() {
226
- return this.data.trace_id;
227
- }
228
- /**
229
- * Check if the trace has ended
230
- */
231
- get isEnded() {
232
- return this.ended;
233
- }
234
- /**
235
- * Get the trace data for serialization
236
- */
237
- toJSON() {
238
- return { ...this.data };
239
- }
240
- };
241
-
242
- // src/opentelemetry/exporter.ts
243
- var ObservyzeSpanExporter = class {
244
- client;
245
- config;
246
- constructor(client, config) {
247
- this.client = client;
248
- this.config = {
249
- serviceName: config?.serviceName || "unknown-service",
250
- projectId: config?.projectId || "",
251
- defaultSpanType: config?.defaultSpanType || "llm",
252
- headers: config?.headers || {}
253
- };
254
- }
255
- /**
256
- * Export spans — called by OTel SDK when spans are ready.
257
- * Converts OTel spans to Observyze traces and buffers them.
258
- */
259
- async export(spans, resultCallback) {
260
- if (!spans || spans.length === 0) {
261
- resultCallback({ code: 0 });
262
- return;
263
- }
264
- try {
265
- const organizationId = this.client.config?.organizationId || "";
266
- const projectId = this.config.projectId || this.client.config?.projectId || "";
267
- for (const span of spans) {
268
- const spanContext = span.spanContext();
269
- const traceId = spanContext?.traceId || span.spanId();
270
- const trace = new Trace(
271
- span.name || "otel-span",
272
- organizationId,
273
- projectId
274
- );
275
- trace.setMetadata("source", "opentelemetry");
276
- trace.setMetadata("otel.trace_id", traceId);
277
- trace.setMetadata("otel.span_id", spanContext?.spanId || "");
278
- trace.setMetadata("service.name", this.config.serviceName);
279
- trace.setMetadataAll(span.attributes || {});
280
- if (span.resource?.attributes) {
281
- trace.setMetadataAll(span.resource.attributes);
282
- }
283
- const input = span.attributes?.["gen_ai.prompt.0.content"] || span.attributes?.["gen_ai.completion.0.content"] || span.attributes?.["llm.input"] || void 0;
284
- const output = span.attributes?.["gen_ai.completion.0.content"] || span.attributes?.["llm.output"] || void 0;
285
- const model = span.attributes?.["gen_ai.request.model"] || span.attributes?.["llm.model"] || void 0;
286
- const provider = span.attributes?.["gen_ai.request.provider"] || span.attributes?.["llm.provider"] || void 0;
287
- const inputTokens = typeof span.attributes?.["gen_ai.usage.input_tokens"] === "number" ? span.attributes["gen_ai.usage.input_tokens"] : void 0;
288
- const outputTokens = typeof span.attributes?.["gen_ai.usage.output_tokens"] === "number" ? span.attributes["gen_ai.usage.output_tokens"] : void 0;
289
- trace.setMetadata("provider", provider || "unknown");
290
- trace.setMetadata("model", model || "unknown");
291
- const nsDuration = span.duration;
292
- const durationMs = nsDuration ? Math.round(nsDuration / 1e6) : 0;
293
- trace.setMetadata("latency_ms", durationMs);
294
- trace.setMetadata("otel.duration_ns", nsDuration);
295
- const oSpan = trace.startSpan(span.name || "otel-operation", this.config.defaultSpanType);
296
- if (input) oSpan.setInput(input);
297
- if (output) oSpan.setOutput(output);
298
- if (inputTokens || outputTokens) {
299
- oSpan.setTokens({
300
- input: inputTokens || 0,
301
- output: outputTokens || 0,
302
- total: (inputTokens || 0) + (outputTokens || 0)
303
- });
304
- }
305
- if (model) oSpan.setMetadata("model", model);
306
- if (provider) oSpan.setMetadata("provider", provider);
307
- if (span.attributes) oSpan.setMetadataAll(span.attributes);
308
- const status = span.status;
309
- const statusCode = status?.code;
310
- if (statusCode === 2) {
311
- oSpan.setError(new Error(status?.message || "OTel span error"));
312
- trace.end(TraceStatus.ERROR);
313
- } else {
314
- trace.end(TraceStatus.SUCCESS);
315
- }
316
- }
317
- resultCallback({ code: 0 });
318
- } catch (error) {
319
- resultCallback({
320
- code: 1,
321
- error: error instanceof Error ? error : new Error(String(error))
322
- });
323
- }
324
- }
325
- /**
326
- * Called when the exporter is shut down.
327
- * Flushes any remaining buffered traces via the SDK client.
328
- */
329
- async shutdown() {
330
- try {
331
- await this.client.flush();
332
- } catch {
333
- }
334
- }
335
- /**
336
- * Called by the OTel SDK to force-export buffered spans.
337
- */
338
- async forceFlush() {
339
- try {
340
- await this.client.flush();
341
- } catch {
342
- }
343
- }
344
- };
345
-
346
- export {
347
- __require,
348
- __esm,
349
- __export,
350
- __toCommonJS,
351
- SpanType,
352
- TraceStatus,
353
- init_types,
354
- Span,
355
- Trace,
356
- ObservyzeSpanExporter
357
- };