@sentienguard/apm 1.0.16 → 1.0.18
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/package.json +1 -1
- package/src/traceSpanExporter.js +43 -1
- package/src/tracing.js +25 -0
package/package.json
CHANGED
package/src/traceSpanExporter.js
CHANGED
|
@@ -10,6 +10,45 @@ import { SpanStatusCode } from '@opentelemetry/api';
|
|
|
10
10
|
import { enqueueSpans } from './traceTransport.js';
|
|
11
11
|
import { getConfig } from './config.js';
|
|
12
12
|
|
|
13
|
+
let debugLogged = 0;
|
|
14
|
+
const DEBUG_LOG_LIMIT = 10;
|
|
15
|
+
|
|
16
|
+
function debugSpanShape(span, serialized, cfg) {
|
|
17
|
+
try {
|
|
18
|
+
if (!cfg?.debug) return;
|
|
19
|
+
if (debugLogged >= DEBUG_LOG_LIMIT) return;
|
|
20
|
+
debugLogged++;
|
|
21
|
+
|
|
22
|
+
const attrObj = serialized?.attributes && typeof serialized.attributes === 'object' ? serialized.attributes : {};
|
|
23
|
+
const keys = Object.keys(attrObj);
|
|
24
|
+
const sample = keys.slice(0, 12).reduce((acc, k) => {
|
|
25
|
+
acc[k] = attrObj[k];
|
|
26
|
+
return acc;
|
|
27
|
+
}, {});
|
|
28
|
+
|
|
29
|
+
// eslint-disable-next-line no-console
|
|
30
|
+
console.log(
|
|
31
|
+
'[SentienGuard APM][trace-debug]',
|
|
32
|
+
JSON.stringify(
|
|
33
|
+
{
|
|
34
|
+
name: serialized?.name,
|
|
35
|
+
kind: serialized?.kind,
|
|
36
|
+
status: serialized?.status?.code,
|
|
37
|
+
trace_id: serialized?.trace_id,
|
|
38
|
+
span_id: serialized?.span_id,
|
|
39
|
+
parent_span_id: serialized?.parent_span_id,
|
|
40
|
+
attributes_count: keys.length,
|
|
41
|
+
attributes_sample: sample
|
|
42
|
+
},
|
|
43
|
+
null,
|
|
44
|
+
0
|
|
45
|
+
)
|
|
46
|
+
);
|
|
47
|
+
} catch {
|
|
48
|
+
// never break the app
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
13
52
|
function hrTimeToUnixNanoString(hrTime) {
|
|
14
53
|
// hrTime is [seconds, nanoseconds]
|
|
15
54
|
if (!Array.isArray(hrTime) || hrTime.length !== 2) return '';
|
|
@@ -98,7 +137,10 @@ export class SentienGuardTraceSpanExporter {
|
|
|
98
137
|
for (const span of spans) {
|
|
99
138
|
try {
|
|
100
139
|
const s = serializeSpan(span);
|
|
101
|
-
if (s && shouldSampleTraceId(s.trace_id, rate))
|
|
140
|
+
if (s && shouldSampleTraceId(s.trace_id, rate)) {
|
|
141
|
+
debugSpanShape(span, s, cfg);
|
|
142
|
+
serialized.push(s);
|
|
143
|
+
}
|
|
102
144
|
} catch {
|
|
103
145
|
// ignore
|
|
104
146
|
}
|
package/src/tracing.js
CHANGED
|
@@ -141,6 +141,31 @@ export function startTracing() {
|
|
|
141
141
|
setAttrNumber(span, 'http.status_code', response.statusCode);
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
|
+
,
|
|
145
|
+
/**
|
|
146
|
+
* More reliable cross-version hook that runs with both request+response context.
|
|
147
|
+
* Used as a safety net to guarantee basic attributes exist on spans.
|
|
148
|
+
*/
|
|
149
|
+
applyCustomAttributesOnSpan: (span, request, response) => {
|
|
150
|
+
// Incoming request (SERVER)
|
|
151
|
+
if (request && typeof request === 'object' && 'headers' in request && 'method' in request) {
|
|
152
|
+
setAttr(span, 'http.method', request.method);
|
|
153
|
+
setAttr(span, 'http.target', request.url);
|
|
154
|
+
setAttr(span, 'http.host', safeHostFromIncoming(request));
|
|
155
|
+
} else {
|
|
156
|
+
// Outgoing request (CLIENT)
|
|
157
|
+
setAttr(span, 'http.method', request?.method);
|
|
158
|
+
const url = safeUrlFromOutgoingRequest(request);
|
|
159
|
+
if (url) setAttr(span, 'http.url', url);
|
|
160
|
+
const host = request?.hostname || (request?.host ? String(request.host).split(':')[0] : '');
|
|
161
|
+
if (host) setAttr(span, 'net.peer.name', host);
|
|
162
|
+
if (request?.port) setAttrNumber(span, 'net.peer.port', request.port);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (response && typeof response === 'object' && 'statusCode' in response) {
|
|
166
|
+
setAttrNumber(span, 'http.status_code', response.statusCode);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
144
169
|
});
|
|
145
170
|
|
|
146
171
|
const expressInstrumentation = new ExpressInstrumentation();
|