intellim-node-sdk 1.0.0 → 1.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.
- package/index.js +68 -11
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -112,6 +112,39 @@ function patchOutgoingRequests() {
|
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
+
// Best-effort human-readable reason for a failed network call. Axios errors
|
|
116
|
+
// normally have a real .message ("Request failed with status code 401",
|
|
117
|
+
// "connect ECONNREFUSED ..."), but Node's dual-stack "happy eyeballs"
|
|
118
|
+
// connection attempt (used for a bare hostname like "localhost", which
|
|
119
|
+
// resolves to both ::1 and 127.0.0.1) can produce an AggregateError whose
|
|
120
|
+
// own top-level .message is blank by design — the real per-address reasons
|
|
121
|
+
// live in .errors (or .cause.errors) instead. Without this, a debug log
|
|
122
|
+
// silently prints "failed: " with nothing after it, which is exactly what
|
|
123
|
+
// happened when a backendUrl was left pointing at localhost with nothing
|
|
124
|
+
// listening there.
|
|
125
|
+
const describeError = (err) => {
|
|
126
|
+
if (err.message) return err.message;
|
|
127
|
+
if (err.code) return err.code;
|
|
128
|
+
const sub = err.errors || (err.cause && err.cause.errors);
|
|
129
|
+
if (sub && sub.length) return sub.map(e => e.message || e.code).join(", ");
|
|
130
|
+
return "unknown error";
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// One-time reachability check right at startup — separate from the ongoing
|
|
134
|
+
// per-call debug logs below, which stay silent unless debug:true. A wrong
|
|
135
|
+
// or unreachable backendUrl otherwise fails completely silently in a real
|
|
136
|
+
// deployment (debug is off by default), so a client could have zero data
|
|
137
|
+
// arriving for weeks with no indication anything is wrong. This is always
|
|
138
|
+
// printed, regardless of the debug flag, specifically so a misconfigured
|
|
139
|
+
// backendUrl is never invisible.
|
|
140
|
+
const checkBackendReachable = async () => {
|
|
141
|
+
try {
|
|
142
|
+
await axios.get(config.backendUrl, { timeout: 5000, validateStatus: () => true });
|
|
143
|
+
} catch (err) {
|
|
144
|
+
log(`WARNING: could not reach backend at ${config.backendUrl} (${describeError(err)}) — no data will be sent until this is fixed. Check the backendUrl passed to init().`);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
115
148
|
// Announces "the process I'm running in (this PID) calls itself
|
|
116
149
|
// `serviceName` in its own trace data" — lets the backend link this SDK's
|
|
117
150
|
// traced name to whatever name Service Discovery's process scanner
|
|
@@ -121,14 +154,20 @@ function patchOutgoingRequests() {
|
|
|
121
154
|
// side) might not have found this PID yet at the exact moment the SDK
|
|
122
155
|
// boots — retrying avoids depending on any particular startup order.
|
|
123
156
|
const ANNOUNCE_INTERVAL_MS = 60000;
|
|
157
|
+
// This SDK's own package version, not the instrumented app's version --
|
|
158
|
+
// shown on the Services card as a "what's actually running" signal
|
|
159
|
+
// (approximates "last deployed" too, paired with process uptime, since a
|
|
160
|
+
// real deploy-tracking pipeline is a separate, bigger feature).
|
|
161
|
+
const SDK_VERSION = require("./package.json").version;
|
|
124
162
|
const announceIdentity = async () => {
|
|
125
163
|
try {
|
|
126
164
|
await axios.post(`${config.backendUrl}/api/discovery/identify`, {
|
|
127
165
|
pid: process.pid,
|
|
128
166
|
tracedServiceName: config.serviceName,
|
|
167
|
+
sdkVersion: SDK_VERSION,
|
|
129
168
|
}, { headers: authHeaders() });
|
|
130
169
|
} catch (err) {
|
|
131
|
-
if (config.debug) log(`identity announce failed: ${err
|
|
170
|
+
if (config.debug) log(`identity announce failed: ${describeError(err)}`);
|
|
132
171
|
}
|
|
133
172
|
};
|
|
134
173
|
|
|
@@ -138,6 +177,12 @@ const init = (userConfig = {}) => {
|
|
|
138
177
|
backendUrl: userConfig.backendUrl || "http://localhost:3001",
|
|
139
178
|
apiKey: userConfig.apiKey || process.env.INTELLIM_API_KEY || "",
|
|
140
179
|
debug: userConfig.debug || false,
|
|
180
|
+
// Neither is required -- an app that doesn't set them just sends "" for
|
|
181
|
+
// both, same as every existing integration before this. Read from
|
|
182
|
+
// common env var names as a fallback so a CI/CD pipeline that already
|
|
183
|
+
// sets APP_VERSION/NODE_ENV doesn't need a code change to pick this up.
|
|
184
|
+
version: userConfig.version || process.env.APP_VERSION || "",
|
|
185
|
+
environment: userConfig.environment || process.env.NODE_ENV || "",
|
|
141
186
|
};
|
|
142
187
|
|
|
143
188
|
if (!config.apiKey && config.debug) {
|
|
@@ -145,6 +190,7 @@ const init = (userConfig = {}) => {
|
|
|
145
190
|
}
|
|
146
191
|
|
|
147
192
|
log(`initializing for service: ${config.serviceName}`);
|
|
193
|
+
checkBackendReachable();
|
|
148
194
|
|
|
149
195
|
patchOutgoingRequests();
|
|
150
196
|
|
|
@@ -203,20 +249,20 @@ const middleware = (req, res, next) => {
|
|
|
203
249
|
await sendMetric("response_time", duration, "ms");
|
|
204
250
|
|
|
205
251
|
if (res.statusCode >= 500) {
|
|
206
|
-
await sendLog("ERROR", `${req.method} ${req.path} returned ${res.statusCode} in ${duration}ms
|
|
252
|
+
await sendLog("ERROR", `${req.method} ${req.path} returned ${res.statusCode} in ${duration}ms`, traceId, spanId);
|
|
207
253
|
} else if (res.statusCode === 401 || res.statusCode === 403) {
|
|
208
|
-
await sendLog("WARN", `Auth failed: ${req.method} ${req.path} returned ${res.statusCode} in ${duration}ms
|
|
254
|
+
await sendLog("WARN", `Auth failed: ${req.method} ${req.path} returned ${res.statusCode} in ${duration}ms`, traceId, spanId);
|
|
209
255
|
} else if (res.statusCode === 429) {
|
|
210
|
-
await sendLog("WARN", `Rate limited: ${req.method} ${req.path} returned 429 in ${duration}ms
|
|
256
|
+
await sendLog("WARN", `Rate limited: ${req.method} ${req.path} returned 429 in ${duration}ms`, traceId, spanId);
|
|
211
257
|
} else if (res.statusCode >= 400) {
|
|
212
|
-
await sendLog("WARN", `${req.method} ${req.path} returned ${res.statusCode} in ${duration}ms
|
|
258
|
+
await sendLog("WARN", `${req.method} ${req.path} returned ${res.statusCode} in ${duration}ms`, traceId, spanId);
|
|
213
259
|
}
|
|
214
260
|
// Two-tier slow-request logging — a 1.2s request and an 8s request are
|
|
215
261
|
// very different severities of problem, so don't bucket them the same.
|
|
216
262
|
if (duration > 3000) {
|
|
217
|
-
await sendLog("ERROR", `Very slow request: ${req.method} ${req.path} took ${duration}ms
|
|
263
|
+
await sendLog("ERROR", `Very slow request: ${req.method} ${req.path} took ${duration}ms`, traceId, spanId);
|
|
218
264
|
} else if (duration > 1000) {
|
|
219
|
-
await sendLog("WARN", `Slow request: ${req.method} ${req.path} took ${duration}ms
|
|
265
|
+
await sendLog("WARN", `Slow request: ${req.method} ${req.path} took ${duration}ms`, traceId, spanId);
|
|
220
266
|
}
|
|
221
267
|
});
|
|
222
268
|
|
|
@@ -239,33 +285,44 @@ const sendMetric = async (name, value, unit = "") => {
|
|
|
239
285
|
value,
|
|
240
286
|
unit,
|
|
241
287
|
timestamp: new Date().toISOString(),
|
|
288
|
+
version: config.version,
|
|
289
|
+
environment: config.environment,
|
|
242
290
|
}, { headers: authHeaders() });
|
|
243
291
|
} catch (err) {
|
|
244
|
-
if (config.debug) log(`metric ${name} failed: ${err
|
|
292
|
+
if (config.debug) log(`metric ${name} failed: ${describeError(err)}`);
|
|
245
293
|
}
|
|
246
294
|
};
|
|
247
295
|
|
|
248
|
-
|
|
296
|
+
// traceId/spanId are optional -- a log sent outside a traced request
|
|
297
|
+
// (startup/shutdown messages) has neither, and the backend already
|
|
298
|
+
// defaults them to "" (see toLogDoc in the backend's kafka consumer).
|
|
299
|
+
const sendLog = async (level, message, traceId, spanId) => {
|
|
249
300
|
try {
|
|
250
301
|
await axios.post(`${config.backendUrl}/api/logs`, {
|
|
251
302
|
service: config.serviceName,
|
|
252
303
|
level,
|
|
253
304
|
message,
|
|
254
305
|
timestamp: new Date().toISOString(),
|
|
306
|
+
version: config.version,
|
|
307
|
+
environment: config.environment,
|
|
308
|
+
...(traceId ? { traceId } : {}),
|
|
309
|
+
...(spanId ? { spanId } : {}),
|
|
255
310
|
}, { headers: authHeaders() });
|
|
256
311
|
} catch (err) {
|
|
257
|
-
if (config.debug) log(`log failed: ${err
|
|
312
|
+
if (config.debug) log(`log failed: ${describeError(err)}`);
|
|
258
313
|
}
|
|
259
314
|
};
|
|
260
315
|
|
|
261
316
|
const sendTrace = async (trace) => {
|
|
262
317
|
try {
|
|
263
318
|
await axios.post(`${config.backendUrl}/api/traces`, {
|
|
319
|
+
version: config.version,
|
|
320
|
+
environment: config.environment,
|
|
264
321
|
...trace,
|
|
265
322
|
timestamp: new Date().toISOString(),
|
|
266
323
|
}, { headers: authHeaders() });
|
|
267
324
|
} catch (err) {
|
|
268
|
-
if (config.debug) log(`trace failed: ${err
|
|
325
|
+
if (config.debug) log(`trace failed: ${describeError(err)}`);
|
|
269
326
|
}
|
|
270
327
|
};
|
|
271
328
|
|