@oconde/radar 0.3.0 → 0.4.2

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.
@@ -303,6 +303,12 @@ function requestPath(req) {
303
303
  const end = url.indexOf("?");
304
304
  return end >= 0 ? url.slice(0, end) : url;
305
305
  }
306
+ function matchesRequest(pattern, path, route) {
307
+ const candidates = route === void 0 ? [path] : [path, route];
308
+ if (pattern instanceof RegExp) return candidates.some((value) => value.search(pattern) !== -1);
309
+ const prefix = pattern.endsWith("/") ? pattern : `${pattern}/`;
310
+ return candidates.some((value) => value === pattern || value.startsWith(prefix));
311
+ }
306
312
  function requestRoute(req, route) {
307
313
  if (route) return route;
308
314
  if (typeof req.route?.path !== "string") return void 0;
@@ -442,11 +448,24 @@ function createMiddleware(logger) {
442
448
  // src/options.ts
443
449
  var DEFAULT_ENDPOINT = "https://radar-ingest.oconde.dev";
444
450
  var LEVELS = ["debug", "info", "warn", "error"];
451
+ function patterns(value) {
452
+ if (!Array.isArray(value)) return [];
453
+ return value.flatMap((item) => {
454
+ if (item instanceof RegExp) return [item];
455
+ if (typeof item === "string" && item.trim() !== "") return [item.trim()];
456
+ return [];
457
+ });
458
+ }
445
459
  function resolveTraceFetch(value) {
446
- if (!value) return { enabled: false, propagateTo: [] };
447
- if (value === true) return { enabled: true, propagateTo: [] };
448
- const targets2 = Array.isArray(value.propagateTo) ? value.propagateTo : [];
449
- return { enabled: true, propagateTo: targets2.filter((target) => typeof target === "string" ? target.trim() !== "" : target instanceof RegExp) };
460
+ if (!value) return { enabled: false, propagateTo: [], ignore: [] };
461
+ if (value === true) return { enabled: true, propagateTo: [], ignore: [] };
462
+ return { enabled: true, propagateTo: patterns(value.propagateTo), ignore: patterns(value.ignore) };
463
+ }
464
+ function resolveLogRequests(value) {
465
+ if (value === false) return { enabled: false, include: [], exclude: [], sample: 1 };
466
+ if (value === void 0 || value === true || typeof value !== "object" || value === null) return { enabled: true, include: [], exclude: [], sample: 1 };
467
+ const sample = typeof value.sample === "number" && Number.isFinite(value.sample) && value.sample >= 0 && value.sample <= 1 ? value.sample : 1;
468
+ return { enabled: true, include: patterns(value.include), exclude: patterns(value.exclude), sample };
450
469
  }
451
470
  function resolveOptions(options = {}, env = process.env) {
452
471
  const key = options.key?.trim() || void 0;
@@ -460,7 +479,7 @@ function resolveOptions(options = {}, env = process.env) {
460
479
  minLevel: options.minLevel && LEVELS.includes(options.minLevel) ? options.minLevel : "info",
461
480
  redact: options.redact === "none" ? "none" : "mask",
462
481
  requestDetail: options.requestDetail === "route" ? "route" : "full",
463
- logRequests: options.logRequests ?? true,
482
+ logRequests: resolveLogRequests(options.logRequests),
464
483
  ignorePaths: options.ignorePaths ?? ["/healthz", "/health"],
465
484
  captureUnhandled: options.captureUnhandled ?? true,
466
485
  traceFetch: resolveTraceFetch(options.traceFetch),
@@ -614,8 +633,8 @@ function headerOf(code, index, prefix) {
614
633
  return header;
615
634
  }
616
635
  function nameIn(header) {
617
- const patterns = ANONYMOUS_FUNCTION.test(header) ? NAME_PATTERNS : [...NAME_PATTERNS, METHOD_PATTERN];
618
- for (const pattern of patterns) {
636
+ const patterns2 = ANONYMOUS_FUNCTION.test(header) ? NAME_PATTERNS : [...NAME_PATTERNS, METHOD_PATTERN];
637
+ for (const pattern of patterns2) {
619
638
  const name = pattern.exec(header)?.[1];
620
639
  if (name && !KEYWORDS.has(name)) return name.slice(0, MAX_NAME_LENGTH);
621
640
  }
@@ -935,12 +954,19 @@ function originOf(value) {
935
954
  return null;
936
955
  }
937
956
  }
938
- function targets(url, target) {
939
- if (target instanceof RegExp) return target.test(url.host);
940
- const wanted = target.trim().toLowerCase();
957
+ function hostMatches(url, host) {
958
+ const wanted = host.trim().toLowerCase();
941
959
  if (wanted.startsWith(".")) return url.hostname.toLowerCase().endsWith(wanted);
942
960
  return url.host.toLowerCase() === wanted || url.hostname.toLowerCase() === wanted;
943
961
  }
962
+ function targets(url, target) {
963
+ if (target instanceof RegExp) return url.host.search(target) !== -1;
964
+ return hostMatches(url, target);
965
+ }
966
+ function ignored(url, target) {
967
+ if (target instanceof RegExp) return `${url.host}${url.pathname}`.search(target) !== -1;
968
+ return hostMatches(url, target);
969
+ }
944
970
  function isPlainObject(value) {
945
971
  if (value === null || typeof value !== "object") return false;
946
972
  const prototype = Object.getPrototypeOf(value);
@@ -964,6 +990,7 @@ function traceFetch(config) {
964
990
  const method = (init?.method ?? (input instanceof Request ? input.method : "GET")).toUpperCase();
965
991
  const context = currentContext();
966
992
  const [nextInput, nextInit] = context && config.propagateTo.some((target) => targets(url, target)) ? withRequestId(input, init, context.requestId) : [input, init];
993
+ if (config.ignore?.some((target) => ignored(url, target))) return original(nextInput, nextInit);
967
994
  const attrs = { method, host: url.host, path: outboundPath(url.pathname, config.redact) };
968
995
  const started = performance.now();
969
996
  try {
@@ -1212,7 +1239,7 @@ function withTimeout(promise, ms) {
1212
1239
 
1213
1240
  // src/version.ts
1214
1241
  var SDK_NAME = "@oconde/radar";
1215
- var SDK_VERSION = true ? "0.3.0" : "0.0.0-dev";
1242
+ var SDK_VERSION = true ? "0.4.2" : "0.0.0-dev";
1216
1243
 
1217
1244
  // src/client.ts
1218
1245
  var MAX_CHECK_IN_DURATION_MS = 7 * 864e5;
@@ -1256,6 +1283,7 @@ var RadarClient = class {
1256
1283
  this.restoreFetch = traceFetch({
1257
1284
  endpoint: this.options.endpoint,
1258
1285
  propagateTo: this.options.traceFetch.propagateTo,
1286
+ ignore: this.options.traceFetch.ignore,
1259
1287
  redact: this.options.redact,
1260
1288
  record: (level, attrs) => this.safely(() => this.emitLog(level, "http.client", attrs))
1261
1289
  });
@@ -1366,22 +1394,28 @@ var RadarClient = class {
1366
1394
  return createMiddleware(this);
1367
1395
  }
1368
1396
  shouldLogRequest(req) {
1369
- return this.options.logRequests && !this.options.ignorePaths.includes(requestPath(req));
1397
+ return this.options.logRequests.enabled && !this.options.ignorePaths.includes(requestPath(req));
1370
1398
  }
1371
1399
  logHttpRequest(context, status) {
1372
1400
  this.safely(() => {
1373
1401
  const req = context.req;
1374
1402
  if (!req) return;
1375
- const level = status >= 500 ? "error" : status >= 400 ? "warn" : "info";
1376
1403
  const route = requestRoute(req, context.route);
1404
+ const path = requestPath(req);
1405
+ const { include, exclude, sample } = this.options.logRequests;
1406
+ if (include.length > 0 && !include.some((pattern) => matchesRequest(pattern, path, route))) return;
1407
+ if (exclude.some((pattern) => matchesRequest(pattern, path, route))) return;
1408
+ if (sample < 1 && Math.random() >= sample) return;
1409
+ const level = status >= 500 ? "error" : status >= 400 ? "warn" : "info";
1377
1410
  runWithContext(
1378
1411
  context,
1379
1412
  () => this.emitLog(level, "http.request", {
1380
1413
  method: (req.method ?? "GET").toUpperCase(),
1381
1414
  ...route ? { route } : {},
1382
- ...this.options.requestDetail === "full" ? { path: redactPath(requestPath(req), pathParams(req, requestPath(req)), this.options.redact) } : {},
1415
+ ...this.options.requestDetail === "full" ? { path: redactPath(path, pathParams(req, path), this.options.redact) } : {},
1383
1416
  status,
1384
- durationMs: elapsedSince(context.startedAt)
1417
+ durationMs: elapsedSince(context.startedAt),
1418
+ ...sample < 1 ? { sampleRate: sample } : {}
1385
1419
  })
1386
1420
  );
1387
1421
  });