@zintrust/trace 0.5.8 → 0.6.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/register.js CHANGED
@@ -117,11 +117,15 @@ const parseEnvBool = (rawValue) => {
117
117
  return false;
118
118
  return undefined;
119
119
  };
120
- const resolveTraceStartupOverrides = (core) => {
120
+ const resolveTraceStartupOverrides = async (core) => {
121
121
  const traceConfigFile = core.StartupConfigFile?.Trace;
122
122
  if (typeof traceConfigFile !== 'string' || traceConfigFile.trim() === '')
123
123
  return undefined;
124
- const overrides = core.StartupConfigFileRegistry?.get(traceConfigFile);
124
+ const registry = core.StartupConfigFileRegistry;
125
+ if (registry?.has?.(traceConfigFile) !== true && typeof registry?.preload === 'function') {
126
+ await registry.preload([traceConfigFile]);
127
+ }
128
+ const overrides = registry?.get(traceConfigFile);
125
129
  return isObjectValue(overrides) ? overrides : undefined;
126
130
  };
127
131
  const buildTraceRedactionOverrides = (input) => {
@@ -195,7 +199,7 @@ const assertTraceStorageReady = async (coreApi, db, connectionName) => {
195
199
  };
196
200
  const core = (await importCore());
197
201
  const Env = core.Env;
198
- const startupOverrides = resolveTraceStartupOverrides(core);
202
+ const startupOverrides = await resolveTraceStartupOverrides(core);
199
203
  if (!traceAlreadyInitialized && Env) {
200
204
  const enabled = startupOverrides?.enabled === true || Env.getBool('TRACE_ENABLED', false);
201
205
  if (enabled) {
@@ -1,9 +1,64 @@
1
+ import { TraceContext } from '../context.js';
2
+ import { EntryType } from '../types.js';
1
3
  import { TraceEntryFilter } from '../utils/entryFilter.js';
4
+ import { RequestFilter } from '../utils/requestFilter.js';
5
+ const MAX_IGNORED_BATCHES = 512;
6
+ const isObjectValue = (value) => {
7
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
8
+ };
9
+ const getEntryUri = (entry) => {
10
+ if (entry.type !== EntryType.REQUEST)
11
+ return undefined;
12
+ const content = isObjectValue(entry.content) ? entry.content : undefined;
13
+ const uri = content?.['uri'];
14
+ return typeof uri === 'string' && uri.trim() !== '' ? uri : undefined;
15
+ };
16
+ const createIgnoredBatchTracker = () => {
17
+ const ignoredBatchIds = new Set();
18
+ const ignoredBatchOrder = [];
19
+ const remember = (batchId) => {
20
+ if (ignoredBatchIds.has(batchId))
21
+ return;
22
+ ignoredBatchIds.add(batchId);
23
+ ignoredBatchOrder.push(batchId);
24
+ if (ignoredBatchOrder.length <= MAX_IGNORED_BATCHES)
25
+ return;
26
+ const evictedBatchId = ignoredBatchOrder.shift();
27
+ if (evictedBatchId !== undefined) {
28
+ ignoredBatchIds.delete(evictedBatchId);
29
+ }
30
+ };
31
+ const has = (batchId) => {
32
+ return ignoredBatchIds.has(batchId);
33
+ };
34
+ return Object.freeze({ has, remember });
35
+ };
36
+ const shouldDropForIgnoredRequest = (entry, config, tracker) => {
37
+ if (tracker.has(entry.batchId)) {
38
+ return true;
39
+ }
40
+ const currentPath = TraceContext.getRequestPath();
41
+ if (typeof currentPath === 'string' &&
42
+ currentPath !== '' &&
43
+ RequestFilter.matchesIgnoredPath(currentPath, config)) {
44
+ tracker.remember(entry.batchId);
45
+ return true;
46
+ }
47
+ const uri = getEntryUri(entry);
48
+ if (typeof uri === 'string' && RequestFilter.matchesIgnoredPath(uri, config)) {
49
+ tracker.remember(entry.batchId);
50
+ return true;
51
+ }
52
+ return false;
53
+ };
2
54
  export const TraceEntryFiltering = Object.freeze({
3
55
  wrapStorage(storage, config) {
56
+ const ignoredBatchTracker = createIgnoredBatchTracker();
4
57
  return Object.freeze({
5
58
  ...storage,
6
59
  async writeEntry(entry) {
60
+ if (shouldDropForIgnoredRequest(entry, config, ignoredBatchTracker))
61
+ return;
7
62
  if (!TraceEntryFilter.shouldCapture(entry, config))
8
63
  return;
9
64
  await storage.writeEntry(entry);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zintrust/trace",
3
- "version": "0.5.8",
3
+ "version": "0.6.0",
4
4
  "description": "Trace assistant for ZinTrust: logs requests, queries, exceptions, jobs, and more.",
5
5
  "private": false,
6
6
  "type": "module",
@@ -40,7 +40,7 @@
40
40
  "node": ">=20.0.0"
41
41
  },
42
42
  "peerDependencies": {
43
- "@zintrust/core": "^0.5.5"
43
+ "@zintrust/core": "^0.5.9"
44
44
  },
45
45
  "publishConfig": {
46
46
  "access": "public"
package/src/register.ts CHANGED
@@ -72,7 +72,9 @@ type CoreApi = {
72
72
  Trace?: string;
73
73
  };
74
74
  StartupConfigFileRegistry?: {
75
+ preload?(files: readonly string[]): Promise<void>;
75
76
  get<T>(file: string): T | undefined;
77
+ has?(file: string): boolean;
76
78
  };
77
79
  };
78
80
 
@@ -181,11 +183,18 @@ const parseEnvBool = (rawValue: string): boolean | undefined => {
181
183
  return undefined;
182
184
  };
183
185
 
184
- const resolveTraceStartupOverrides = (core: CoreApi): TraceConfigOverrides | undefined => {
186
+ const resolveTraceStartupOverrides = async (
187
+ core: CoreApi
188
+ ): Promise<TraceConfigOverrides | undefined> => {
185
189
  const traceConfigFile = core.StartupConfigFile?.Trace;
186
190
  if (typeof traceConfigFile !== 'string' || traceConfigFile.trim() === '') return undefined;
187
191
 
188
- const overrides = core.StartupConfigFileRegistry?.get<unknown>(traceConfigFile);
192
+ const registry = core.StartupConfigFileRegistry;
193
+ if (registry?.has?.(traceConfigFile) !== true && typeof registry?.preload === 'function') {
194
+ await registry.preload([traceConfigFile]);
195
+ }
196
+
197
+ const overrides = registry?.get<unknown>(traceConfigFile);
189
198
  return isObjectValue(overrides) ? (overrides as TraceConfigOverrides) : undefined;
190
199
  };
191
200
 
@@ -296,7 +305,7 @@ const assertTraceStorageReady = async (
296
305
 
297
306
  const core = (await importCore()) as CoreApi;
298
307
  const Env = core.Env;
299
- const startupOverrides = resolveTraceStartupOverrides(core);
308
+ const startupOverrides = await resolveTraceStartupOverrides(core);
300
309
 
301
310
  if (!traceAlreadyInitialized && Env) {
302
311
  const enabled = startupOverrides?.enabled === true || Env.getBool('TRACE_ENABLED', false);
@@ -1,11 +1,84 @@
1
+ import { TraceContext } from '../context';
1
2
  import type { ITraceConfig, ITraceEntry, ITraceStorage } from '../types';
3
+ import { EntryType } from '../types';
2
4
  import { TraceEntryFilter } from '../utils/entryFilter';
5
+ import { RequestFilter } from '../utils/requestFilter';
6
+
7
+ const MAX_IGNORED_BATCHES = 512;
8
+
9
+ const isObjectValue = (value: unknown): value is Record<string, unknown> => {
10
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
11
+ };
12
+
13
+ const getEntryUri = (entry: ITraceEntry): string | undefined => {
14
+ if (entry.type !== EntryType.REQUEST) return undefined;
15
+
16
+ const content = isObjectValue(entry.content) ? entry.content : undefined;
17
+ const uri = content?.['uri'];
18
+ return typeof uri === 'string' && uri.trim() !== '' ? uri : undefined;
19
+ };
20
+
21
+ const createIgnoredBatchTracker = () => {
22
+ const ignoredBatchIds = new Set<string>();
23
+ const ignoredBatchOrder: string[] = [];
24
+
25
+ const remember = (batchId: string): void => {
26
+ if (ignoredBatchIds.has(batchId)) return;
27
+
28
+ ignoredBatchIds.add(batchId);
29
+ ignoredBatchOrder.push(batchId);
30
+
31
+ if (ignoredBatchOrder.length <= MAX_IGNORED_BATCHES) return;
32
+
33
+ const evictedBatchId = ignoredBatchOrder.shift();
34
+ if (evictedBatchId !== undefined) {
35
+ ignoredBatchIds.delete(evictedBatchId);
36
+ }
37
+ };
38
+
39
+ const has = (batchId: string): boolean => {
40
+ return ignoredBatchIds.has(batchId);
41
+ };
42
+
43
+ return Object.freeze({ has, remember });
44
+ };
45
+
46
+ const shouldDropForIgnoredRequest = (
47
+ entry: ITraceEntry,
48
+ config: ITraceConfig,
49
+ tracker: ReturnType<typeof createIgnoredBatchTracker>
50
+ ): boolean => {
51
+ if (tracker.has(entry.batchId)) {
52
+ return true;
53
+ }
54
+
55
+ const currentPath = TraceContext.getRequestPath();
56
+ if (
57
+ typeof currentPath === 'string' &&
58
+ currentPath !== '' &&
59
+ RequestFilter.matchesIgnoredPath(currentPath, config)
60
+ ) {
61
+ tracker.remember(entry.batchId);
62
+ return true;
63
+ }
64
+
65
+ const uri = getEntryUri(entry);
66
+ if (typeof uri === 'string' && RequestFilter.matchesIgnoredPath(uri, config)) {
67
+ tracker.remember(entry.batchId);
68
+ return true;
69
+ }
70
+
71
+ return false;
72
+ };
3
73
 
4
74
  export const TraceEntryFiltering = Object.freeze({
5
75
  wrapStorage(storage: ITraceStorage, config: ITraceConfig): ITraceStorage {
76
+ const ignoredBatchTracker = createIgnoredBatchTracker();
77
+
6
78
  return Object.freeze({
7
79
  ...storage,
8
80
  async writeEntry(entry: ITraceEntry) {
81
+ if (shouldDropForIgnoredRequest(entry, config, ignoredBatchTracker)) return;
9
82
  if (!TraceEntryFilter.shouldCapture(entry, config)) return;
10
83
  await storage.writeEntry(entry);
11
84
  },