@zharp/node 0.2.0 → 0.3.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/dist/index.mjs CHANGED
@@ -13,7 +13,7 @@ import {
13
13
  INVALID_SPAN_CONTEXT,
14
14
  logs,
15
15
  SpanStatusCode,
16
- trace as trace2
16
+ trace as trace3
17
17
  } from "@opentelemetry/api";
18
18
  import { CompositePropagator, W3CBaggagePropagator, W3CTraceContextPropagator } from "@opentelemetry/core";
19
19
  import { envDetector, hostDetector, resourceFromAttributes } from "@opentelemetry/resources";
@@ -196,24 +196,24 @@ function shorten(value) {
196
196
  if (BASE64.test(value) && /[0-9+/]/.test(value)) return `[base64 \xB7 ${formatSize(Math.floor(value.length * 3 / 4))}]`;
197
197
  return value;
198
198
  }
199
- function filterAll(value, depth) {
200
- if (depth > 20) return FILTERED;
199
+ function filterAll(value, depth2) {
200
+ if (depth2 > 20) return FILTERED;
201
201
  if (value === null || value === "") return value;
202
- if (Array.isArray(value)) return value.map((v) => filterAll(v, depth + 1));
202
+ if (Array.isArray(value)) return value.map((v) => filterAll(v, depth2 + 1));
203
203
  if (typeof value === "object") {
204
204
  const out = {};
205
- for (const [k, v] of Object.entries(value)) out[k] = filterAll(v, depth + 1);
205
+ for (const [k, v] of Object.entries(value)) out[k] = filterAll(v, depth2 + 1);
206
206
  return out;
207
207
  }
208
208
  return FILTERED;
209
209
  }
210
- function redactValue(value, extra, depth = 0) {
210
+ function redactValue(value, extra, depth2 = 0) {
211
211
  if (typeof value === "string") return shorten(value);
212
- if (depth > 20 || value === null || typeof value !== "object") return value;
213
- if (Array.isArray(value)) return value.map((v) => redactValue(v, extra, depth + 1));
212
+ if (depth2 > 20 || value === null || typeof value !== "object") return value;
213
+ if (Array.isArray(value)) return value.map((v) => redactValue(v, extra, depth2 + 1));
214
214
  const out = {};
215
215
  for (const [k, v] of Object.entries(value)) {
216
- out[k] = isSecretKey(k, extra) ? filterAll(v, depth + 1) : redactValue(v, extra, depth + 1);
216
+ out[k] = isSecretKey(k, extra) ? filterAll(v, depth2 + 1) : redactValue(v, extra, depth2 + 1);
217
217
  }
218
218
  return out;
219
219
  }
@@ -798,6 +798,44 @@ var ContextLogProcessor = class {
798
798
  // src/console.ts
799
799
  import { format } from "util";
800
800
  import { SeverityNumber } from "@opentelemetry/api";
801
+
802
+ // src/capture-state.ts
803
+ var depth = 0;
804
+ function quiet(fn) {
805
+ depth++;
806
+ try {
807
+ return fn();
808
+ } finally {
809
+ depth--;
810
+ }
811
+ }
812
+ var isQuiet = () => depth > 0;
813
+ var RECENT_MS = 3e3;
814
+ var RECENT_MAX = 500;
815
+ var recent = [];
816
+ var normalise = (text) => text.replace(/\s+/g, " ").trim();
817
+ function rememberSent(body) {
818
+ if (typeof body !== "string") return;
819
+ const text = normalise(body);
820
+ if (text.length < 4) return;
821
+ recent.push({ at: Date.now(), text });
822
+ if (recent.length > RECENT_MAX) recent.splice(0, recent.length - RECENT_MAX);
823
+ }
824
+ function alreadySent(line) {
825
+ const now = Date.now();
826
+ while (recent.length && now - recent[0].at > RECENT_MS) recent.shift();
827
+ if (!recent.length) return false;
828
+ const text = normalise(line);
829
+ for (let i = recent.length - 1; i >= 0; i--) {
830
+ if (text.includes(recent[i].text)) {
831
+ recent.splice(i, 1);
832
+ return true;
833
+ }
834
+ }
835
+ return false;
836
+ }
837
+
838
+ // src/console.ts
801
839
  var LEVELS = [
802
840
  ["debug", SeverityNumber.DEBUG, "DEBUG"],
803
841
  ["trace", SeverityNumber.TRACE, "TRACE"],
@@ -814,7 +852,7 @@ function captureConsole(logger) {
814
852
  if (typeof original !== "function" || original.__zharp) continue;
815
853
  originals.set(method, original);
816
854
  const patched = function(...args) {
817
- original.apply(this ?? console, args);
855
+ quiet(() => original.apply(this ?? console, args));
818
856
  if (emitting) return;
819
857
  emitting = true;
820
858
  try {
@@ -835,6 +873,207 @@ function captureConsole(logger) {
835
873
  };
836
874
  }
837
875
 
876
+ // src/stdout.ts
877
+ import { context as otelContext2, isSpanContextValid, SeverityNumber as SeverityNumber2, trace as trace2 } from "@opentelemetry/api";
878
+ var LEVELS2 = {
879
+ trace: [SeverityNumber2.TRACE, "TRACE"],
880
+ verbose: [SeverityNumber2.DEBUG, "DEBUG"],
881
+ debug: [SeverityNumber2.DEBUG, "DEBUG"],
882
+ log: [SeverityNumber2.INFO, "INFO"],
883
+ info: [SeverityNumber2.INFO, "INFO"],
884
+ notice: [SeverityNumber2.INFO, "INFO"],
885
+ warn: [SeverityNumber2.WARN, "WARN"],
886
+ warning: [SeverityNumber2.WARN, "WARN"],
887
+ error: [SeverityNumber2.ERROR, "ERROR"],
888
+ err: [SeverityNumber2.ERROR, "ERROR"],
889
+ crit: [SeverityNumber2.FATAL, "FATAL"],
890
+ critical: [SeverityNumber2.FATAL, "FATAL"],
891
+ fatal: [SeverityNumber2.FATAL, "FATAL"],
892
+ emerg: [SeverityNumber2.FATAL, "FATAL"],
893
+ alert: [SeverityNumber2.FATAL, "FATAL"]
894
+ };
895
+ var NUMERIC = [[60, "fatal"], [50, "error"], [40, "warn"], [30, "info"], [20, "debug"], [0, "trace"]];
896
+ var ANSI = /\u001b\[[0-9;?]*[ -/]*[@-~]/g;
897
+ var NEST = /^\[Nest\]\s+\d+\s+-\s+.*?\s+(LOG|ERROR|WARN|DEBUG|VERBOSE|FATAL)\s+(?:\[([^\]]+)\]\s+)?([\s\S]*)$/;
898
+ var WORD = /(?:^|[\s[(|:=])(trace|verbose|debug|info|notice|warn(?:ing)?|err(?:or)?|crit(?:ical)?|fatal|emerg|alert)(?=$|[\s\]):|,])/i;
899
+ var MAX_LINE = 32 * 1024;
900
+ var FLUSH_MS = 200;
901
+ function levelOf(value) {
902
+ if (typeof value === "number") {
903
+ const found = NUMERIC.find(([n]) => value >= n);
904
+ return found ? LEVELS2[found[1]] : null;
905
+ }
906
+ return typeof value === "string" ? LEVELS2[value.toLowerCase()] ?? null : null;
907
+ }
908
+ function parseLine(raw) {
909
+ const line = raw.replace(ANSI, "").replace(/\s+$/, "");
910
+ if (line.startsWith("{") && line.endsWith("}")) {
911
+ try {
912
+ const record = JSON.parse(line);
913
+ const message = record.msg ?? record.message ?? record.event;
914
+ return {
915
+ level: levelOf(record.level ?? record.severity ?? record.lvl),
916
+ body: typeof message === "string" ? message : line,
917
+ attributes: {}
918
+ };
919
+ } catch {
920
+ }
921
+ }
922
+ const nest = NEST.exec(line);
923
+ if (nest) {
924
+ return {
925
+ level: LEVELS2[nest[1].toLowerCase()],
926
+ body: nest[3],
927
+ attributes: nest[2] ? { "log.logger": nest[2] } : {}
928
+ };
929
+ }
930
+ const word = WORD.exec(line.slice(0, 120));
931
+ return { level: word ? LEVELS2[word[1].toLowerCase()] ?? null : null, body: line, attributes: {} };
932
+ }
933
+ var SentLineProcessor = class {
934
+ onEmit(record) {
935
+ const source = record.attributes?.["log.source"];
936
+ if (source !== "stdout" && source !== "stderr") rememberSent(record.body);
937
+ }
938
+ forceFlush() {
939
+ return Promise.resolve();
940
+ }
941
+ shutdown() {
942
+ return Promise.resolve();
943
+ }
944
+ };
945
+ function captureOutput(logger) {
946
+ const undo = [];
947
+ for (const [stream, name, fallback] of [
948
+ [process.stdout, "stdout", LEVELS2.info],
949
+ [process.stderr, "stderr", LEVELS2.warn]
950
+ ]) {
951
+ if (!stream || typeof stream.write !== "function" || stream.write.__zharp) continue;
952
+ const original = stream.write;
953
+ let pending = "";
954
+ let pendingContext;
955
+ let timer;
956
+ const emitLine = (line, ctx) => {
957
+ if (!line.trim() || line.includes("[Zharp]") || alreadySent(line)) return;
958
+ const parsed2 = parseLine(line.length > MAX_LINE ? line.slice(0, MAX_LINE) : line);
959
+ if (!parsed2.body.trim()) return;
960
+ const [severityNumber, severityText] = parsed2.level ?? fallback;
961
+ quiet(() => logger.emit({
962
+ severityNumber,
963
+ severityText,
964
+ body: parsed2.body,
965
+ attributes: { "log.source": name, "log.iostream": name, ...parsed2.attributes },
966
+ ...ctx ? { context: ctx } : {}
967
+ }));
968
+ };
969
+ const flush = () => {
970
+ timer = void 0;
971
+ const line = pending;
972
+ const ctx = pendingContext;
973
+ pending = "";
974
+ pendingContext = void 0;
975
+ emitLine(line, ctx);
976
+ };
977
+ const take = (chunk, encoding) => {
978
+ if (isQuiet()) return;
979
+ const text = typeof chunk === "string" ? chunk : chunk instanceof Uint8Array ? Buffer.from(chunk).toString(typeof encoding === "string" ? encoding : "utf8") : "";
980
+ if (!text) return;
981
+ if (!pending) pendingContext = otelContext2.active();
982
+ pending += text;
983
+ const lines = pending.split("\n");
984
+ pending = lines.pop() ?? "";
985
+ const ctx = pendingContext;
986
+ if (pending) pendingContext = otelContext2.active();
987
+ for (const line of lines) emitLine(line, ctx);
988
+ if (pending.length > MAX_LINE) flush();
989
+ else if (pending && !timer) {
990
+ timer = setTimeout(flush, FLUSH_MS);
991
+ timer.unref?.();
992
+ }
993
+ };
994
+ const patched = function(chunk, encoding, callback) {
995
+ try {
996
+ take(chunk, encoding);
997
+ } catch {
998
+ }
999
+ return original.call(this ?? stream, chunk, encoding, callback);
1000
+ };
1001
+ patched.__zharp = true;
1002
+ stream.write = patched;
1003
+ undo.push(() => {
1004
+ if (timer) clearTimeout(timer);
1005
+ if (pending) flush();
1006
+ stream.write = original;
1007
+ });
1008
+ }
1009
+ return () => undo.forEach((fn) => fn());
1010
+ }
1011
+ function stampOutput() {
1012
+ const undo = [];
1013
+ for (const stream of [process.stdout, process.stderr]) {
1014
+ if (!stream || typeof stream.write !== "function" || stream.write.__zharpStamp) continue;
1015
+ const original = stream.write;
1016
+ const patched = function(chunk, encoding, callback) {
1017
+ let out = chunk;
1018
+ try {
1019
+ out = stamped(chunk, encoding);
1020
+ } catch {
1021
+ }
1022
+ return original.call(this ?? stream, out, encoding, callback);
1023
+ };
1024
+ patched.__zharpStamp = true;
1025
+ stream.write = patched;
1026
+ undo.push(() => {
1027
+ stream.write = original;
1028
+ });
1029
+ }
1030
+ return () => undo.forEach((fn) => fn());
1031
+ }
1032
+ function stamped(chunk, encoding) {
1033
+ const span = trace2.getActiveSpan()?.spanContext();
1034
+ if (!span || !isSpanContextValid(span)) return chunk;
1035
+ const isBytes = chunk instanceof Uint8Array;
1036
+ if (typeof chunk !== "string" && !isBytes) return chunk;
1037
+ const enc = typeof encoding === "string" && Buffer.isEncoding(encoding) ? encoding : "utf8";
1038
+ if (isBytes && enc !== "utf8") return chunk;
1039
+ const text = isBytes ? Buffer.from(chunk).toString("utf8") : chunk;
1040
+ if (!text.includes("\n")) return chunk;
1041
+ const parts = text.split("\n");
1042
+ let last = -1;
1043
+ for (let i = parts.length - 2; i >= 0; i--) {
1044
+ if (parts[i].trim()) {
1045
+ last = i;
1046
+ break;
1047
+ }
1048
+ }
1049
+ let changed = false;
1050
+ for (let i = 0; i < parts.length - 1; i++) {
1051
+ if (i !== last && !parts[i].trimStart().startsWith("{")) continue;
1052
+ const next = stampLine(parts[i], span.traceId, span.spanId);
1053
+ if (next !== parts[i]) {
1054
+ parts[i] = next;
1055
+ changed = true;
1056
+ }
1057
+ }
1058
+ if (!changed) return chunk;
1059
+ const joined = parts.join("\n");
1060
+ return isBytes ? Buffer.from(joined, "utf8") : joined;
1061
+ }
1062
+ function stampLine(line, traceId, spanId) {
1063
+ const cr = line.endsWith("\r") ? "\r" : "";
1064
+ const body = cr ? line.slice(0, -1) : line;
1065
+ const trimmed = body.trimEnd();
1066
+ if (!trimmed.trim() || body.includes("trace_id") || body.includes("[Zharp]")) return line;
1067
+ if (trimmed.endsWith("}")) {
1068
+ if (!trimmed.trimStart().startsWith("{")) return line;
1069
+ const close = trimmed.lastIndexOf("}");
1070
+ const inner = trimmed.slice(trimmed.indexOf("{") + 1, close).trim();
1071
+ const fields = `"trace_id":"${traceId}","span_id":"${spanId}"`;
1072
+ return `${trimmed.slice(0, close)}${inner ? "," : ""}${fields}}${body.slice(trimmed.length)}${cr}`;
1073
+ }
1074
+ return `${trimmed} trace_id=${traceId} span_id=${spanId}${body.slice(trimmed.length)}${cr}`;
1075
+ }
1076
+
838
1077
  // ../../internal/src/debug-ids.ts
839
1078
  var DEBUG_IDS_ATTRIBUTE = "zharp.debug_ids";
840
1079
  var files = /* @__PURE__ */ new Map();
@@ -982,7 +1221,7 @@ Caused by: ${cause.stack}`;
982
1221
  }
983
1222
 
984
1223
  // src/version.ts
985
- var VERSION = "0.2.0";
1224
+ var VERSION = "0.3.0";
986
1225
 
987
1226
  // src/zharp.ts
988
1227
  var DEFAULT_ENDPOINT = "https://api.zharp.io/api/v1/otlp";
@@ -1043,6 +1282,9 @@ var ZharpNode = class {
1043
1282
  const endpoint = (pick(options.endpoint, nodeEnv("ZHARP_ENDPOINT")) ?? DEFAULT_ENDPOINT).replace(/\/+$/, "");
1044
1283
  const tracing = tracingEnabled(options.tracing);
1045
1284
  this.tracing = tracing;
1285
+ const sendLogs = switchOn(options.logs, "ZHARP_LOGS", false);
1286
+ const correlation = switchOn(options.logCorrelation, "ZHARP_LOG_CORRELATION", true);
1287
+ const bridges = logBridges(sendLogs, correlation, options.instrumentations);
1046
1288
  if (options.debug) {
1047
1289
  const out = { error: console.error, warn: console.warn, info: console.info, debug: console.debug, verbose: console.debug };
1048
1290
  const prefixed = Object.fromEntries(
@@ -1060,6 +1302,7 @@ var ZharpNode = class {
1060
1302
  new BatchSpanProcessor({ exporter: new OTLPTraceExporter({ ...exporterOptions, url: `${endpoint}/v1/traces` }) })
1061
1303
  ] : [new ContextSpanProcessor(this.store)];
1062
1304
  this.logProcessors = [
1305
+ new SentLineProcessor(),
1063
1306
  new ContextLogProcessor(this.store),
1064
1307
  new DebugIdLogProcessor(),
1065
1308
  new BatchLogRecordProcessor({ exporter: new OTLPLogExporter({ ...exporterOptions, url: `${endpoint}/v1/logs` }) })
@@ -1097,16 +1340,23 @@ var ZharpNode = class {
1097
1340
  "@opentelemetry/instrumentation-net": { enabled: false },
1098
1341
  "@opentelemetry/instrumentation-fs": { enabled: false },
1099
1342
  ...options.instrumentations,
1343
+ ...bridges,
1100
1344
  ...bodies && {
1101
1345
  "@opentelemetry/instrumentation-http": withBodyCapture(bodies, options.instrumentations?.["@opentelemetry/instrumentation-http"])
1102
1346
  }
1103
- }) : logsOnlyInstrumentations(options),
1347
+ }) : logsOnlyInstrumentations({ ...options, instrumentations: { ...options.instrumentations, ...bridges } }),
1104
1348
  ...options.extraInstrumentations ?? []
1105
1349
  ]
1106
1350
  });
1107
1351
  this.sdk.start();
1108
1352
  this.logger = logs.getLogger("@zharp/node", VERSION);
1109
- if (options.captureConsole !== false) this.teardowns.push(captureConsole(this.logger));
1353
+ if (correlation) this.teardowns.push(stampOutput());
1354
+ if (sendLogs) {
1355
+ if (options.captureConsole !== false) this.teardowns.push(captureConsole(this.logger));
1356
+ if (options.captureOutput !== false && nodeEnv("ZHARP_CAPTURE_OUTPUT") !== "false") {
1357
+ this.teardowns.push(captureOutput(this.logger));
1358
+ }
1359
+ }
1110
1360
  if (options.captureUncaught !== false) this.teardowns.push(this.captureProcessErrors());
1111
1361
  this.teardowns.push(this.flushOnExit());
1112
1362
  announce(`Application monitoring active${tracing ? "" : " (logs only)"} \u2014 ${describeTarget(service, environment)}`);
@@ -1147,8 +1397,8 @@ var ZharpNode = class {
1147
1397
  * throw or rejection marks the span as failed and is passed on unchanged.
1148
1398
  */
1149
1399
  startSpan(name, fn, options = {}) {
1150
- if (!this.tracing) return fn(trace2.wrapSpanContext(trace2.getActiveSpan()?.spanContext() ?? INVALID_SPAN_CONTEXT));
1151
- const tracer = trace2.getTracer("@zharp/node", VERSION);
1400
+ if (!this.tracing) return fn(trace3.wrapSpanContext(trace3.getActiveSpan()?.spanContext() ?? INVALID_SPAN_CONTEXT));
1401
+ const tracer = trace3.getTracer("@zharp/node", VERSION);
1152
1402
  return tracer.startActiveSpan(name, options, (span) => {
1153
1403
  const fail = (error) => {
1154
1404
  span.setStatus({ code: SpanStatusCode.ERROR, message: asError(error).message });
@@ -1228,7 +1478,7 @@ var ZharpNode = class {
1228
1478
  body: `${exception["exception.type"]}: ${exception["exception.message"]}`,
1229
1479
  attributes: { ...attributes, ...exception }
1230
1480
  });
1231
- if (!handled) trace2.getActiveSpan()?.setStatus({ code: SpanStatusCode.ERROR, message: error.message });
1481
+ if (!handled) trace3.getActiveSpan()?.setStatus({ code: SpanStatusCode.ERROR, message: error.message });
1232
1482
  } catch {
1233
1483
  }
1234
1484
  }
@@ -1332,6 +1582,25 @@ function registerEsmHook() {
1332
1582
  } catch {
1333
1583
  }
1334
1584
  }
1585
+ function switchOn(option, variable, fallback) {
1586
+ if (typeof option === "boolean") return option;
1587
+ const value = nodeEnv(variable);
1588
+ if (value && /^(true|1|yes|on)$/i.test(value)) return true;
1589
+ if (value && /^(false|0|no|off)$/i.test(value)) return false;
1590
+ return fallback;
1591
+ }
1592
+ function logBridges(sendLogs, correlation, own = {}) {
1593
+ const config = (name) => ({
1594
+ disableLogSending: !sendLogs,
1595
+ disableLogCorrelation: !correlation,
1596
+ ...own[name]
1597
+ });
1598
+ return {
1599
+ "@opentelemetry/instrumentation-pino": config("@opentelemetry/instrumentation-pino"),
1600
+ "@opentelemetry/instrumentation-winston": config("@opentelemetry/instrumentation-winston"),
1601
+ "@opentelemetry/instrumentation-bunyan": config("@opentelemetry/instrumentation-bunyan")
1602
+ };
1603
+ }
1335
1604
  function tracingEnabled(option) {
1336
1605
  if (typeof option === "boolean") return option;
1337
1606
  const value = nodeEnv("ZHARP_TRACING");