@probelabs/probe 0.6.0-rc134 → 0.6.0-rc136
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/build/agent/ProbeAgent.js +15 -15
- package/build/agent/index.js +82362 -224
- package/build/agent/schemaUtils.js +5 -2
- package/build/agent/telemetry.js +3 -4
- package/build/index.js +7 -1
- package/cjs/agent/ProbeAgent.cjs +798 -740
- package/cjs/agent/simpleTelemetry.cjs +250 -0
- package/cjs/agent/telemetry.cjs +373 -0
- package/cjs/index.cjs +1375 -691
- package/index.d.ts +155 -0
- package/package.json +5 -1
- package/src/agent/ProbeAgent.js +15 -15
- package/src/agent/schemaUtils.js +5 -2
- package/src/agent/telemetry.js +3 -4
- package/src/index.js +7 -1
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/agent/simpleTelemetry.js
|
|
21
|
+
var simpleTelemetry_exports = {};
|
|
22
|
+
__export(simpleTelemetry_exports, {
|
|
23
|
+
SimpleAppTracer: () => SimpleAppTracer,
|
|
24
|
+
SimpleTelemetry: () => SimpleTelemetry,
|
|
25
|
+
initializeSimpleTelemetryFromOptions: () => initializeSimpleTelemetryFromOptions
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(simpleTelemetry_exports);
|
|
28
|
+
var import_fs = require("fs");
|
|
29
|
+
var import_path = require("path");
|
|
30
|
+
var SimpleTelemetry = class {
|
|
31
|
+
constructor(options = {}) {
|
|
32
|
+
this.serviceName = options.serviceName || "probe-agent";
|
|
33
|
+
this.enableFile = options.enableFile || false;
|
|
34
|
+
this.enableConsole = options.enableConsole || false;
|
|
35
|
+
this.filePath = options.filePath || "./traces.jsonl";
|
|
36
|
+
this.stream = null;
|
|
37
|
+
if (this.enableFile) {
|
|
38
|
+
this.initializeFileExporter();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
initializeFileExporter() {
|
|
42
|
+
try {
|
|
43
|
+
const dir = (0, import_path.dirname)(this.filePath);
|
|
44
|
+
if (!(0, import_fs.existsSync)(dir)) {
|
|
45
|
+
(0, import_fs.mkdirSync)(dir, { recursive: true });
|
|
46
|
+
}
|
|
47
|
+
this.stream = (0, import_fs.createWriteStream)(this.filePath, { flags: "a" });
|
|
48
|
+
this.stream.on("error", (error) => {
|
|
49
|
+
console.error(`[SimpleTelemetry] Stream error: ${error.message}`);
|
|
50
|
+
});
|
|
51
|
+
console.log(`[SimpleTelemetry] File exporter initialized: ${this.filePath}`);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error(`[SimpleTelemetry] Failed to initialize file exporter: ${error.message}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
createSpan(name, attributes = {}) {
|
|
57
|
+
const span = {
|
|
58
|
+
traceId: this.generateTraceId(),
|
|
59
|
+
spanId: this.generateSpanId(),
|
|
60
|
+
name,
|
|
61
|
+
startTime: Date.now(),
|
|
62
|
+
attributes: { ...attributes, service: this.serviceName },
|
|
63
|
+
events: [],
|
|
64
|
+
status: "OK"
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
...span,
|
|
68
|
+
addEvent: (eventName, eventAttributes = {}) => {
|
|
69
|
+
span.events.push({
|
|
70
|
+
name: eventName,
|
|
71
|
+
time: Date.now(),
|
|
72
|
+
attributes: eventAttributes
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
setAttributes: (attrs) => {
|
|
76
|
+
Object.assign(span.attributes, attrs);
|
|
77
|
+
},
|
|
78
|
+
setStatus: (status) => {
|
|
79
|
+
span.status = status;
|
|
80
|
+
},
|
|
81
|
+
end: () => {
|
|
82
|
+
span.endTime = Date.now();
|
|
83
|
+
span.duration = span.endTime - span.startTime;
|
|
84
|
+
this.exportSpan(span);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
exportSpan(span) {
|
|
89
|
+
const spanData = {
|
|
90
|
+
...span,
|
|
91
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
92
|
+
};
|
|
93
|
+
if (this.enableConsole) {
|
|
94
|
+
console.log("[Trace]", JSON.stringify(spanData, null, 2));
|
|
95
|
+
}
|
|
96
|
+
if (this.enableFile && this.stream) {
|
|
97
|
+
this.stream.write(JSON.stringify(spanData) + "\n");
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
generateTraceId() {
|
|
101
|
+
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
102
|
+
}
|
|
103
|
+
generateSpanId() {
|
|
104
|
+
return Math.random().toString(36).substring(2, 10);
|
|
105
|
+
}
|
|
106
|
+
async flush() {
|
|
107
|
+
if (this.stream) {
|
|
108
|
+
return new Promise((resolve) => {
|
|
109
|
+
this.stream.once("drain", resolve);
|
|
110
|
+
if (!this.stream.writableNeedDrain) {
|
|
111
|
+
resolve();
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
async shutdown() {
|
|
117
|
+
if (this.stream) {
|
|
118
|
+
return new Promise((resolve) => {
|
|
119
|
+
this.stream.end(() => {
|
|
120
|
+
console.log(`[SimpleTelemetry] File stream closed: ${this.filePath}`);
|
|
121
|
+
resolve();
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
var SimpleAppTracer = class {
|
|
128
|
+
constructor(telemetry, sessionId = null) {
|
|
129
|
+
this.telemetry = telemetry;
|
|
130
|
+
this.sessionId = sessionId || this.generateSessionId();
|
|
131
|
+
}
|
|
132
|
+
generateSessionId() {
|
|
133
|
+
return Math.random().toString(36).substring(2, 15);
|
|
134
|
+
}
|
|
135
|
+
isEnabled() {
|
|
136
|
+
return this.telemetry !== null;
|
|
137
|
+
}
|
|
138
|
+
createSessionSpan(attributes = {}) {
|
|
139
|
+
if (!this.isEnabled()) return null;
|
|
140
|
+
return this.telemetry.createSpan("agent.session", {
|
|
141
|
+
"session.id": this.sessionId,
|
|
142
|
+
...attributes
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
createAISpan(modelName, provider, attributes = {}) {
|
|
146
|
+
if (!this.isEnabled()) return null;
|
|
147
|
+
return this.telemetry.createSpan("ai.request", {
|
|
148
|
+
"ai.model": modelName,
|
|
149
|
+
"ai.provider": provider,
|
|
150
|
+
"session.id": this.sessionId,
|
|
151
|
+
...attributes
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
createToolSpan(toolName, attributes = {}) {
|
|
155
|
+
if (!this.isEnabled()) return null;
|
|
156
|
+
return this.telemetry.createSpan("tool.call", {
|
|
157
|
+
"tool.name": toolName,
|
|
158
|
+
"session.id": this.sessionId,
|
|
159
|
+
...attributes
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
addEvent(name, attributes = {}) {
|
|
163
|
+
if (this.telemetry && this.telemetry.enableConsole) {
|
|
164
|
+
console.log("[Event]", name, attributes);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Record delegation events
|
|
169
|
+
*/
|
|
170
|
+
recordDelegationEvent(eventType, data = {}) {
|
|
171
|
+
if (!this.isEnabled()) return;
|
|
172
|
+
this.addEvent(`delegation.${eventType}`, {
|
|
173
|
+
"session.id": this.sessionId,
|
|
174
|
+
...data
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Record JSON validation events
|
|
179
|
+
*/
|
|
180
|
+
recordJsonValidationEvent(eventType, data = {}) {
|
|
181
|
+
if (!this.isEnabled()) return;
|
|
182
|
+
this.addEvent(`json_validation.${eventType}`, {
|
|
183
|
+
"session.id": this.sessionId,
|
|
184
|
+
...data
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Record Mermaid validation events
|
|
189
|
+
*/
|
|
190
|
+
recordMermaidValidationEvent(eventType, data = {}) {
|
|
191
|
+
if (!this.isEnabled()) return;
|
|
192
|
+
this.addEvent(`mermaid_validation.${eventType}`, {
|
|
193
|
+
"session.id": this.sessionId,
|
|
194
|
+
...data
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
setAttributes(attributes) {
|
|
198
|
+
if (this.telemetry && this.telemetry.enableConsole) {
|
|
199
|
+
console.log("[Attributes]", attributes);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
async withSpan(spanName, fn, attributes = {}) {
|
|
203
|
+
if (!this.isEnabled()) {
|
|
204
|
+
return fn();
|
|
205
|
+
}
|
|
206
|
+
const span = this.telemetry.createSpan(spanName, {
|
|
207
|
+
"session.id": this.sessionId,
|
|
208
|
+
...attributes
|
|
209
|
+
});
|
|
210
|
+
try {
|
|
211
|
+
const result = await fn();
|
|
212
|
+
span.setStatus("OK");
|
|
213
|
+
return result;
|
|
214
|
+
} catch (error) {
|
|
215
|
+
span.setStatus("ERROR");
|
|
216
|
+
span.addEvent("exception", {
|
|
217
|
+
"exception.message": error.message,
|
|
218
|
+
"exception.stack": error.stack
|
|
219
|
+
});
|
|
220
|
+
throw error;
|
|
221
|
+
} finally {
|
|
222
|
+
span.end();
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
async flush() {
|
|
226
|
+
if (this.telemetry) {
|
|
227
|
+
await this.telemetry.flush();
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
async shutdown() {
|
|
231
|
+
if (this.telemetry) {
|
|
232
|
+
await this.telemetry.shutdown();
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
function initializeSimpleTelemetryFromOptions(options) {
|
|
237
|
+
const telemetry = new SimpleTelemetry({
|
|
238
|
+
serviceName: "probe-agent",
|
|
239
|
+
enableFile: options.traceFile !== void 0,
|
|
240
|
+
enableConsole: options.traceConsole,
|
|
241
|
+
filePath: options.traceFile || "./traces.jsonl"
|
|
242
|
+
});
|
|
243
|
+
return telemetry;
|
|
244
|
+
}
|
|
245
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
246
|
+
0 && (module.exports = {
|
|
247
|
+
SimpleAppTracer,
|
|
248
|
+
SimpleTelemetry,
|
|
249
|
+
initializeSimpleTelemetryFromOptions
|
|
250
|
+
});
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/agent/telemetry.js
|
|
31
|
+
var telemetry_exports = {};
|
|
32
|
+
__export(telemetry_exports, {
|
|
33
|
+
TelemetryConfig: () => TelemetryConfig,
|
|
34
|
+
initializeTelemetryFromOptions: () => initializeTelemetryFromOptions
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(telemetry_exports);
|
|
37
|
+
var import_sdk_node = __toESM(require("@opentelemetry/sdk-node"), 1);
|
|
38
|
+
var import_resources = __toESM(require("@opentelemetry/resources"), 1);
|
|
39
|
+
var import_semantic_conventions = require("@opentelemetry/semantic-conventions");
|
|
40
|
+
var import_api = require("@opentelemetry/api");
|
|
41
|
+
var import_exporter_trace_otlp_http = __toESM(require("@opentelemetry/exporter-trace-otlp-http"), 1);
|
|
42
|
+
var import_sdk_trace_base = __toESM(require("@opentelemetry/sdk-trace-base"), 1);
|
|
43
|
+
var import_fs2 = require("fs");
|
|
44
|
+
var import_path = require("path");
|
|
45
|
+
|
|
46
|
+
// src/agent/fileSpanExporter.js
|
|
47
|
+
var import_fs = require("fs");
|
|
48
|
+
var import_core = __toESM(require("@opentelemetry/core"), 1);
|
|
49
|
+
var { ExportResultCode } = import_core.default;
|
|
50
|
+
var FileSpanExporter = class {
|
|
51
|
+
constructor(filePath = "./traces.jsonl") {
|
|
52
|
+
this.filePath = filePath;
|
|
53
|
+
this.stream = (0, import_fs.createWriteStream)(filePath, { flags: "a" });
|
|
54
|
+
this.stream.on("error", (error) => {
|
|
55
|
+
console.error(`[FileSpanExporter] Stream error: ${error.message}`);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Export spans to file
|
|
60
|
+
* @param {ReadableSpan[]} spans - Array of spans to export
|
|
61
|
+
* @param {function} resultCallback - Callback to call with the export result
|
|
62
|
+
*/
|
|
63
|
+
export(spans, resultCallback) {
|
|
64
|
+
if (!spans || spans.length === 0) {
|
|
65
|
+
resultCallback({ code: ExportResultCode.SUCCESS });
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
const timestamp = Date.now();
|
|
70
|
+
spans.forEach((span, index) => {
|
|
71
|
+
let parentSpanId = void 0;
|
|
72
|
+
if (span.parentSpanContext) {
|
|
73
|
+
parentSpanId = span.parentSpanContext.spanId;
|
|
74
|
+
} else if (span._parentSpanContext) {
|
|
75
|
+
parentSpanId = span._parentSpanContext.spanId;
|
|
76
|
+
} else if (span.parent) {
|
|
77
|
+
parentSpanId = span.parent.spanId;
|
|
78
|
+
} else if (span._parent) {
|
|
79
|
+
parentSpanId = span._parent.spanId;
|
|
80
|
+
} else if (span._parentId) {
|
|
81
|
+
parentSpanId = span._parentId;
|
|
82
|
+
} else if (span.parentSpanId) {
|
|
83
|
+
parentSpanId = span.parentSpanId;
|
|
84
|
+
}
|
|
85
|
+
const spanData = {
|
|
86
|
+
traceId: span.spanContext().traceId,
|
|
87
|
+
spanId: span.spanContext().spanId,
|
|
88
|
+
parentSpanId,
|
|
89
|
+
name: span.name,
|
|
90
|
+
kind: span.kind,
|
|
91
|
+
startTimeUnixNano: span.startTime[0] * 1e9 + span.startTime[1],
|
|
92
|
+
endTimeUnixNano: span.endTime[0] * 1e9 + span.endTime[1],
|
|
93
|
+
attributes: this.convertAttributes(span.attributes),
|
|
94
|
+
status: span.status,
|
|
95
|
+
events: span.events?.map((event) => ({
|
|
96
|
+
timeUnixNano: event.time[0] * 1e9 + event.time[1],
|
|
97
|
+
name: event.name,
|
|
98
|
+
attributes: this.convertAttributes(event.attributes)
|
|
99
|
+
})) || [],
|
|
100
|
+
links: span.links?.map((link) => ({
|
|
101
|
+
traceId: link.context.traceId,
|
|
102
|
+
spanId: link.context.spanId,
|
|
103
|
+
attributes: this.convertAttributes(link.attributes)
|
|
104
|
+
})) || [],
|
|
105
|
+
resource: {
|
|
106
|
+
attributes: this.convertAttributes(span.resource?.attributes || {})
|
|
107
|
+
},
|
|
108
|
+
instrumentationLibrary: {
|
|
109
|
+
name: span.instrumentationLibrary?.name || "unknown",
|
|
110
|
+
version: span.instrumentationLibrary?.version || "unknown"
|
|
111
|
+
},
|
|
112
|
+
timestamp
|
|
113
|
+
};
|
|
114
|
+
this.stream.write(JSON.stringify(spanData) + "\n");
|
|
115
|
+
});
|
|
116
|
+
resultCallback({ code: ExportResultCode.SUCCESS });
|
|
117
|
+
} catch (error) {
|
|
118
|
+
console.error(`[FileSpanExporter] Export error: ${error.message}`);
|
|
119
|
+
resultCallback({
|
|
120
|
+
code: ExportResultCode.FAILED,
|
|
121
|
+
error
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Convert OpenTelemetry attributes to plain object
|
|
127
|
+
* @param {Object} attributes - OpenTelemetry attributes
|
|
128
|
+
* @returns {Object} Plain object with string values
|
|
129
|
+
*/
|
|
130
|
+
convertAttributes(attributes) {
|
|
131
|
+
if (!attributes) return {};
|
|
132
|
+
const result = {};
|
|
133
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
134
|
+
if (typeof value === "object" && value !== null) {
|
|
135
|
+
result[key] = JSON.stringify(value);
|
|
136
|
+
} else {
|
|
137
|
+
result[key] = String(value);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return result;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Shutdown the exporter
|
|
144
|
+
* @returns {Promise<void>}
|
|
145
|
+
*/
|
|
146
|
+
async shutdown() {
|
|
147
|
+
return new Promise((resolve) => {
|
|
148
|
+
if (this.stream) {
|
|
149
|
+
this.stream.end(() => {
|
|
150
|
+
console.log(`[FileSpanExporter] File stream closed: ${this.filePath}`);
|
|
151
|
+
resolve();
|
|
152
|
+
});
|
|
153
|
+
} else {
|
|
154
|
+
resolve();
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Force flush any pending spans
|
|
160
|
+
* @returns {Promise<void>}
|
|
161
|
+
*/
|
|
162
|
+
async forceFlush() {
|
|
163
|
+
return new Promise((resolve, reject) => {
|
|
164
|
+
if (this.stream) {
|
|
165
|
+
const flushTimeout = setTimeout(() => {
|
|
166
|
+
console.warn("[FileSpanExporter] Flush timeout after 5 seconds");
|
|
167
|
+
resolve();
|
|
168
|
+
}, 5e3);
|
|
169
|
+
if (this.stream.writableCorked) {
|
|
170
|
+
this.stream.uncork();
|
|
171
|
+
}
|
|
172
|
+
if (this.stream.writableNeedDrain) {
|
|
173
|
+
this.stream.once("drain", () => {
|
|
174
|
+
clearTimeout(flushTimeout);
|
|
175
|
+
resolve();
|
|
176
|
+
});
|
|
177
|
+
} else {
|
|
178
|
+
setImmediate(() => {
|
|
179
|
+
clearTimeout(flushTimeout);
|
|
180
|
+
resolve();
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
} else {
|
|
184
|
+
resolve();
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
// src/agent/telemetry.js
|
|
191
|
+
var { NodeSDK } = import_sdk_node.default;
|
|
192
|
+
var { Resource } = import_resources.default;
|
|
193
|
+
var { OTLPTraceExporter } = import_exporter_trace_otlp_http.default;
|
|
194
|
+
var { BatchSpanProcessor, ConsoleSpanExporter } = import_sdk_trace_base.default;
|
|
195
|
+
var TelemetryConfig = class {
|
|
196
|
+
constructor(options = {}) {
|
|
197
|
+
this.serviceName = options.serviceName || "probe-agent";
|
|
198
|
+
this.serviceVersion = options.serviceVersion || "1.0.0";
|
|
199
|
+
this.enableFile = options.enableFile || false;
|
|
200
|
+
this.enableRemote = options.enableRemote || false;
|
|
201
|
+
this.enableConsole = options.enableConsole || false;
|
|
202
|
+
this.filePath = options.filePath || "./traces.jsonl";
|
|
203
|
+
this.remoteEndpoint = options.remoteEndpoint || "http://localhost:4318/v1/traces";
|
|
204
|
+
this.sdk = null;
|
|
205
|
+
this.tracer = null;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Initialize OpenTelemetry SDK
|
|
209
|
+
*/
|
|
210
|
+
initialize() {
|
|
211
|
+
if (this.sdk) {
|
|
212
|
+
console.warn("Telemetry already initialized");
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
const resource = new Resource({
|
|
216
|
+
[import_semantic_conventions.ATTR_SERVICE_NAME]: this.serviceName,
|
|
217
|
+
[import_semantic_conventions.ATTR_SERVICE_VERSION]: this.serviceVersion
|
|
218
|
+
});
|
|
219
|
+
const spanProcessors = [];
|
|
220
|
+
if (this.enableFile) {
|
|
221
|
+
try {
|
|
222
|
+
const dir = (0, import_path.dirname)(this.filePath);
|
|
223
|
+
if (!(0, import_fs2.existsSync)(dir)) {
|
|
224
|
+
(0, import_fs2.mkdirSync)(dir, { recursive: true });
|
|
225
|
+
}
|
|
226
|
+
const fileExporter = new FileSpanExporter(this.filePath);
|
|
227
|
+
spanProcessors.push(new BatchSpanProcessor(fileExporter, {
|
|
228
|
+
maxQueueSize: 2048,
|
|
229
|
+
maxExportBatchSize: 512,
|
|
230
|
+
scheduledDelayMillis: 500,
|
|
231
|
+
exportTimeoutMillis: 3e4
|
|
232
|
+
}));
|
|
233
|
+
console.log(`[Telemetry] File exporter enabled, writing to: ${this.filePath}`);
|
|
234
|
+
} catch (error) {
|
|
235
|
+
console.error(`[Telemetry] Failed to initialize file exporter: ${error.message}`);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (this.enableRemote) {
|
|
239
|
+
try {
|
|
240
|
+
const remoteExporter = new OTLPTraceExporter({
|
|
241
|
+
url: this.remoteEndpoint
|
|
242
|
+
});
|
|
243
|
+
spanProcessors.push(new BatchSpanProcessor(remoteExporter, {
|
|
244
|
+
maxQueueSize: 2048,
|
|
245
|
+
maxExportBatchSize: 512,
|
|
246
|
+
scheduledDelayMillis: 500,
|
|
247
|
+
exportTimeoutMillis: 3e4
|
|
248
|
+
}));
|
|
249
|
+
console.log(`[Telemetry] Remote exporter enabled, endpoint: ${this.remoteEndpoint}`);
|
|
250
|
+
} catch (error) {
|
|
251
|
+
console.error(`[Telemetry] Failed to initialize remote exporter: ${error.message}`);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (this.enableConsole) {
|
|
255
|
+
const consoleExporter = new ConsoleSpanExporter();
|
|
256
|
+
spanProcessors.push(new BatchSpanProcessor(consoleExporter, {
|
|
257
|
+
maxQueueSize: 2048,
|
|
258
|
+
maxExportBatchSize: 512,
|
|
259
|
+
scheduledDelayMillis: 500,
|
|
260
|
+
exportTimeoutMillis: 3e4
|
|
261
|
+
}));
|
|
262
|
+
console.log(`[Telemetry] Console exporter enabled`);
|
|
263
|
+
}
|
|
264
|
+
if (spanProcessors.length === 0) {
|
|
265
|
+
console.log("[Telemetry] No exporters configured, telemetry will not be collected");
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
this.sdk = new NodeSDK({
|
|
269
|
+
resource,
|
|
270
|
+
spanProcessors
|
|
271
|
+
});
|
|
272
|
+
try {
|
|
273
|
+
this.sdk.start();
|
|
274
|
+
this.tracer = import_api.trace.getTracer(this.serviceName, this.serviceVersion);
|
|
275
|
+
console.log(`[Telemetry] OpenTelemetry SDK initialized successfully`);
|
|
276
|
+
} catch (error) {
|
|
277
|
+
console.error(`[Telemetry] Failed to start OpenTelemetry SDK: ${error.message}`);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Get the tracer instance
|
|
282
|
+
*/
|
|
283
|
+
getTracer() {
|
|
284
|
+
return this.tracer;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Create a span with the given name and attributes
|
|
288
|
+
*/
|
|
289
|
+
createSpan(name, attributes = {}) {
|
|
290
|
+
if (!this.tracer) {
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
return this.tracer.startSpan(name, {
|
|
294
|
+
attributes
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Wrap a function to automatically create spans
|
|
299
|
+
*/
|
|
300
|
+
wrapFunction(name, fn, attributes = {}) {
|
|
301
|
+
if (!this.tracer) {
|
|
302
|
+
return fn;
|
|
303
|
+
}
|
|
304
|
+
return async (...args) => {
|
|
305
|
+
const span = this.createSpan(name, attributes);
|
|
306
|
+
if (!span) {
|
|
307
|
+
return fn(...args);
|
|
308
|
+
}
|
|
309
|
+
try {
|
|
310
|
+
const result = await import_api.context.with(import_api.trace.setSpan(import_api.context.active(), span), () => fn(...args));
|
|
311
|
+
span.setStatus({ code: import_api.SpanStatusCode.OK });
|
|
312
|
+
return result;
|
|
313
|
+
} catch (error) {
|
|
314
|
+
span.setStatus({
|
|
315
|
+
code: import_api.SpanStatusCode.ERROR,
|
|
316
|
+
message: error.message
|
|
317
|
+
});
|
|
318
|
+
span.recordException(error);
|
|
319
|
+
throw error;
|
|
320
|
+
} finally {
|
|
321
|
+
span.end();
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Force flush all pending spans
|
|
327
|
+
*/
|
|
328
|
+
async forceFlush() {
|
|
329
|
+
if (this.sdk) {
|
|
330
|
+
try {
|
|
331
|
+
const tracerProvider = import_api.trace.getTracerProvider();
|
|
332
|
+
if (tracerProvider && typeof tracerProvider.forceFlush === "function") {
|
|
333
|
+
await tracerProvider.forceFlush();
|
|
334
|
+
}
|
|
335
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
336
|
+
console.log("[Telemetry] OpenTelemetry spans flushed successfully");
|
|
337
|
+
} catch (error) {
|
|
338
|
+
console.error(`[Telemetry] Failed to flush OpenTelemetry spans: ${error.message}`);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Shutdown telemetry
|
|
344
|
+
*/
|
|
345
|
+
async shutdown() {
|
|
346
|
+
if (this.sdk) {
|
|
347
|
+
try {
|
|
348
|
+
await this.sdk.shutdown();
|
|
349
|
+
console.log("[Telemetry] OpenTelemetry SDK shutdown successfully");
|
|
350
|
+
} catch (error) {
|
|
351
|
+
console.error(`[Telemetry] Failed to shutdown OpenTelemetry SDK: ${error.message}`);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
function initializeTelemetryFromOptions(options) {
|
|
357
|
+
const config = new TelemetryConfig({
|
|
358
|
+
serviceName: "probe-agent",
|
|
359
|
+
serviceVersion: "1.0.0",
|
|
360
|
+
enableFile: options.traceFile !== void 0,
|
|
361
|
+
enableRemote: options.traceRemote !== void 0,
|
|
362
|
+
enableConsole: options.traceConsole,
|
|
363
|
+
filePath: options.traceFile || "./traces.jsonl",
|
|
364
|
+
remoteEndpoint: options.traceRemote || "http://localhost:4318/v1/traces"
|
|
365
|
+
});
|
|
366
|
+
config.initialize();
|
|
367
|
+
return config;
|
|
368
|
+
}
|
|
369
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
370
|
+
0 && (module.exports = {
|
|
371
|
+
TelemetryConfig,
|
|
372
|
+
initializeTelemetryFromOptions
|
|
373
|
+
});
|