@oconde/radar 0.3.0 → 0.4.1

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.
@@ -1,5 +1,5 @@
1
1
  import { RequestEvent, RequestHandler, Handle, HandleServerError } from '@sveltejs/kit';
2
- import { R as RadarClient } from '../client-BB-to8cr.cjs';
2
+ import { R as RadarClient } from '../client-DRZ7htMJ.cjs';
3
3
  import '../protocol/index.cjs';
4
4
 
5
5
  declare function moveClientSourceMaps(buildDir?: string): number;
@@ -1,5 +1,5 @@
1
1
  import { RequestEvent, RequestHandler, Handle, HandleServerError } from '@sveltejs/kit';
2
- import { R as RadarClient } from '../client-BS798K3S.js';
2
+ import { R as RadarClient } from '../client-FkXWx8sJ.js';
3
3
  import '../protocol/index.js';
4
4
 
5
5
  declare function moveClientSourceMaps(buildDir?: string): number;
@@ -258,6 +258,12 @@ function requestPath(req) {
258
258
  const end = url.indexOf("?");
259
259
  return end >= 0 ? url.slice(0, end) : url;
260
260
  }
261
+ function matchesRequest(pattern, path, route) {
262
+ const candidates = route === void 0 ? [path] : [path, route];
263
+ if (pattern instanceof RegExp) return candidates.some((value) => value.search(pattern) !== -1);
264
+ const prefix = pattern.endsWith("/") ? pattern : `${pattern}/`;
265
+ return candidates.some((value) => value === pattern || value.startsWith(prefix));
266
+ }
261
267
  function requestRoute(req, route) {
262
268
  if (route) return route;
263
269
  if (typeof req.route?.path !== "string") return void 0;
@@ -397,11 +403,24 @@ function createMiddleware(logger) {
397
403
  // src/options.ts
398
404
  var DEFAULT_ENDPOINT = "https://radar-ingest.oconde.dev";
399
405
  var LEVELS = ["debug", "info", "warn", "error"];
406
+ function patterns(value) {
407
+ if (!Array.isArray(value)) return [];
408
+ return value.flatMap((item) => {
409
+ if (item instanceof RegExp) return [item];
410
+ if (typeof item === "string" && item.trim() !== "") return [item.trim()];
411
+ return [];
412
+ });
413
+ }
400
414
  function resolveTraceFetch(value) {
401
- if (!value) return { enabled: false, propagateTo: [] };
402
- if (value === true) return { enabled: true, propagateTo: [] };
403
- const targets2 = Array.isArray(value.propagateTo) ? value.propagateTo : [];
404
- return { enabled: true, propagateTo: targets2.filter((target) => typeof target === "string" ? target.trim() !== "" : target instanceof RegExp) };
415
+ if (!value) return { enabled: false, propagateTo: [], ignore: [] };
416
+ if (value === true) return { enabled: true, propagateTo: [], ignore: [] };
417
+ return { enabled: true, propagateTo: patterns(value.propagateTo), ignore: patterns(value.ignore) };
418
+ }
419
+ function resolveLogRequests(value) {
420
+ if (value === false) return { enabled: false, include: [], exclude: [], sample: 1 };
421
+ if (value === void 0 || value === true || typeof value !== "object" || value === null) return { enabled: true, include: [], exclude: [], sample: 1 };
422
+ const sample = typeof value.sample === "number" && Number.isFinite(value.sample) && value.sample >= 0 && value.sample <= 1 ? value.sample : 1;
423
+ return { enabled: true, include: patterns(value.include), exclude: patterns(value.exclude), sample };
405
424
  }
406
425
  function resolveOptions(options = {}, env = process.env) {
407
426
  const key = options.key?.trim() || void 0;
@@ -415,7 +434,7 @@ function resolveOptions(options = {}, env = process.env) {
415
434
  minLevel: options.minLevel && LEVELS.includes(options.minLevel) ? options.minLevel : "info",
416
435
  redact: options.redact === "none" ? "none" : "mask",
417
436
  requestDetail: options.requestDetail === "route" ? "route" : "full",
418
- logRequests: options.logRequests ?? true,
437
+ logRequests: resolveLogRequests(options.logRequests),
419
438
  ignorePaths: options.ignorePaths ?? ["/healthz", "/health"],
420
439
  captureUnhandled: options.captureUnhandled ?? true,
421
440
  traceFetch: resolveTraceFetch(options.traceFetch),
@@ -569,8 +588,8 @@ function headerOf(code, index, prefix) {
569
588
  return header;
570
589
  }
571
590
  function nameIn(header) {
572
- const patterns = ANONYMOUS_FUNCTION.test(header) ? NAME_PATTERNS : [...NAME_PATTERNS, METHOD_PATTERN];
573
- for (const pattern of patterns) {
591
+ const patterns2 = ANONYMOUS_FUNCTION.test(header) ? NAME_PATTERNS : [...NAME_PATTERNS, METHOD_PATTERN];
592
+ for (const pattern of patterns2) {
574
593
  const name = pattern.exec(header)?.[1];
575
594
  if (name && !KEYWORDS.has(name)) return name.slice(0, MAX_NAME_LENGTH);
576
595
  }
@@ -913,12 +932,19 @@ function originOf(value) {
913
932
  return null;
914
933
  }
915
934
  }
916
- function targets(url, target) {
917
- if (target instanceof RegExp) return target.test(url.host);
918
- const wanted = target.trim().toLowerCase();
935
+ function hostMatches(url, host) {
936
+ const wanted = host.trim().toLowerCase();
919
937
  if (wanted.startsWith(".")) return url.hostname.toLowerCase().endsWith(wanted);
920
938
  return url.host.toLowerCase() === wanted || url.hostname.toLowerCase() === wanted;
921
939
  }
940
+ function targets(url, target) {
941
+ if (target instanceof RegExp) return url.host.search(target) !== -1;
942
+ return hostMatches(url, target);
943
+ }
944
+ function ignored(url, target) {
945
+ if (target instanceof RegExp) return `${url.host}${url.pathname}`.search(target) !== -1;
946
+ return hostMatches(url, target);
947
+ }
922
948
  function isPlainObject(value) {
923
949
  if (value === null || typeof value !== "object") return false;
924
950
  const prototype = Object.getPrototypeOf(value);
@@ -942,6 +968,7 @@ function traceFetch(config) {
942
968
  const method = (init?.method ?? (input instanceof Request ? input.method : "GET")).toUpperCase();
943
969
  const context = currentContext();
944
970
  const [nextInput, nextInit] = context && config.propagateTo.some((target) => targets(url, target)) ? withRequestId(input, init, context.requestId) : [input, init];
971
+ if (config.ignore?.some((target) => ignored(url, target))) return original(nextInput, nextInit);
945
972
  const attrs = { method, host: url.host, path: outboundPath(url.pathname, config.redact) };
946
973
  const started = performance.now();
947
974
  try {
@@ -1190,7 +1217,7 @@ function withTimeout(promise, ms) {
1190
1217
 
1191
1218
  // src/version.ts
1192
1219
  var SDK_NAME = "@oconde/radar";
1193
- var SDK_VERSION = true ? "0.3.0" : "0.0.0-dev";
1220
+ var SDK_VERSION = true ? "0.4.1" : "0.0.0-dev";
1194
1221
 
1195
1222
  // src/client.ts
1196
1223
  var MAX_CHECK_IN_DURATION_MS = 7 * 864e5;
@@ -1234,6 +1261,7 @@ var RadarClient = class {
1234
1261
  this.restoreFetch = traceFetch({
1235
1262
  endpoint: this.options.endpoint,
1236
1263
  propagateTo: this.options.traceFetch.propagateTo,
1264
+ ignore: this.options.traceFetch.ignore,
1237
1265
  redact: this.options.redact,
1238
1266
  record: (level, attrs) => this.safely(() => this.emitLog(level, "http.client", attrs))
1239
1267
  });
@@ -1344,22 +1372,28 @@ var RadarClient = class {
1344
1372
  return createMiddleware(this);
1345
1373
  }
1346
1374
  shouldLogRequest(req) {
1347
- return this.options.logRequests && !this.options.ignorePaths.includes(requestPath(req));
1375
+ return this.options.logRequests.enabled && !this.options.ignorePaths.includes(requestPath(req));
1348
1376
  }
1349
1377
  logHttpRequest(context, status) {
1350
1378
  this.safely(() => {
1351
1379
  const req = context.req;
1352
1380
  if (!req) return;
1353
- const level = status >= 500 ? "error" : status >= 400 ? "warn" : "info";
1354
1381
  const route = requestRoute(req, context.route);
1382
+ const path = requestPath(req);
1383
+ const { include, exclude, sample } = this.options.logRequests;
1384
+ if (include.length > 0 && !include.some((pattern) => matchesRequest(pattern, path, route))) return;
1385
+ if (exclude.some((pattern) => matchesRequest(pattern, path, route))) return;
1386
+ if (sample < 1 && Math.random() >= sample) return;
1387
+ const level = status >= 500 ? "error" : status >= 400 ? "warn" : "info";
1355
1388
  runWithContext(
1356
1389
  context,
1357
1390
  () => this.emitLog(level, "http.request", {
1358
1391
  method: (req.method ?? "GET").toUpperCase(),
1359
1392
  ...route ? { route } : {},
1360
- ...this.options.requestDetail === "full" ? { path: redactPath(requestPath(req), pathParams(req, requestPath(req)), this.options.redact) } : {},
1393
+ ...this.options.requestDetail === "full" ? { path: redactPath(path, pathParams(req, path), this.options.redact) } : {},
1361
1394
  status,
1362
- durationMs: elapsedSince(context.startedAt)
1395
+ durationMs: elapsedSince(context.startedAt),
1396
+ ...sample < 1 ? { sampleRate: sample } : {}
1363
1397
  })
1364
1398
  );
1365
1399
  });