@sentienguard/apm 1.0.8 → 1.0.9

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.
@@ -53,9 +53,14 @@ function instrumentFetch() {
53
53
  originalFetch = window.fetch;
54
54
 
55
55
  window.fetch = function (input, init) {
56
- const url = typeof input === 'string'
57
- ? input
58
- : (input instanceof Request ? input.url : String(input));
56
+ let url;
57
+ if (typeof input === 'string') {
58
+ url = input;
59
+ } else if (input instanceof Request) {
60
+ url = input.url;
61
+ } else {
62
+ url = String(input);
63
+ }
59
64
 
60
65
  if (shouldExclude(url)) {
61
66
  return originalFetch.apply(this, arguments);
package/src/config.js CHANGED
@@ -36,6 +36,14 @@ const config = {
36
36
  trackTokens: true,
37
37
  trackCosts: true,
38
38
  slowCallMs: 5000
39
+ },
40
+ /** OpenTelemetry + W3C trace context; set SENTIENGUARD_TRACING=false for legacy HTTP patches only */
41
+ tracing: {
42
+ enabled: true,
43
+ /** When true, outgoing HTTP to localhost is traced (for multi-service dev). Default false. */
44
+ traceLocalHttp: false,
45
+ /** Port -> display name for local peers, from SENTIENGUARD_PEER_SERVICE_MAP */
46
+ peerServiceMap: {}
39
47
  }
40
48
  };
41
49
 
@@ -72,6 +80,38 @@ export function loadConfig({ force = false } = {}) {
72
80
  config.openai.trackTokens = process.env.SENTIENGUARD_OPENAI_TRACK_TOKENS !== 'false';
73
81
  config.openai.trackCosts = process.env.SENTIENGUARD_OPENAI_TRACK_COSTS !== 'false';
74
82
  config.openai.slowCallMs = parseInt(process.env.SENTIENGUARD_OPENAI_SLOW_CALL_MS, 10) || 5000;
83
+
84
+ config.tracing.enabled = process.env.SENTIENGUARD_TRACING !== 'false';
85
+ // Default behavior:
86
+ // - production: do NOT record localhost dependency edges (noise + self-calls)
87
+ // - non-production: DO record localhost edges (local multi-service dev "just works")
88
+ // Can always be overridden explicitly via SENTIENGUARD_TRACE_LOCAL_HTTP=true/false.
89
+ const explicitLocal =
90
+ process.env.SENTIENGUARD_TRACE_LOCAL_HTTP === 'true'
91
+ ? true
92
+ : process.env.SENTIENGUARD_TRACE_LOCAL_HTTP === 'false'
93
+ ? false
94
+ : undefined;
95
+ const isProd = (config.environment || '').toLowerCase() === 'production' ||
96
+ (process.env.NODE_ENV || '').toLowerCase() === 'production';
97
+ config.tracing.traceLocalHttp = explicitLocal ?? !isProd;
98
+ config.tracing.peerServiceMap = parsePeerServiceMap(process.env.SENTIENGUARD_PEER_SERVICE_MAP || '');
99
+ }
100
+
101
+ /** "3001:payments-api,3002:auth-api" -> { "3001": "payments-api", ... } */
102
+ function parsePeerServiceMap(raw) {
103
+ const map = Object.create(null);
104
+ if (!raw || typeof raw !== 'string') return map;
105
+ for (const part of raw.split(',')) {
106
+ const trimmed = part.trim();
107
+ if (!trimmed) continue;
108
+ const idx = trimmed.indexOf(':');
109
+ if (idx === -1) continue;
110
+ const port = trimmed.slice(0, idx).trim();
111
+ const name = trimmed.slice(idx + 1).trim();
112
+ if (port && name) map[port] = name;
113
+ }
114
+ return map;
75
115
  }
76
116
 
77
117
  /**