@friggframework/core 2.0.0-next.112 → 2.0.0-next.113
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/.eslintrc.json +18 -1
- package/CLAUDE.md +51 -12
- package/README.md +19 -22
- package/core/CLAUDE.md +28 -13
- package/core/Worker.js +32 -37
- package/core/create-handler.js +97 -197
- package/core/index.js +8 -1
- package/core/invocation-scope.js +225 -0
- package/core/secrets-to-env.js +7 -6
- package/database/config.js +0 -7
- package/database/encryption/README.md +5 -1
- package/database/encryption/encryption-schema-registry.js +3 -0
- package/database/encryption/logger.js +5 -2
- package/errors/fetch-error.js +48 -65
- package/handlers/app-definition-loader.js +38 -2
- package/handlers/app-handler-helpers.js +18 -4
- package/handlers/backend-utils.js +10 -13
- package/handlers/database-migration-handler.js +10 -1
- package/handlers/integration-event-dispatcher.js +2 -2
- package/handlers/routers/integration-webhook-routers.js +13 -6
- package/handlers/routers/websocket.js +27 -7
- package/handlers/workers/db-migration.js +12 -1
- package/handlers/workers/dlq-processor.js +64 -38
- package/index.js +19 -1
- package/integrations/integration-base.js +56 -38
- package/integrations/integration-router.js +21 -20
- package/integrations/use-cases/delete-integration-for-user.js +24 -13
- package/jest.config.js +2 -0
- package/logs/.eslintrc.json +26 -1
- package/logs/__fixtures__/events.js +82 -0
- package/logs/__fixtures__/matchers.js +45 -0
- package/logs/__fixtures__/secrets.js +59 -0
- package/logs/__fixtures__/vectors.js +329 -0
- package/logs/__fixtures__/with-env.js +45 -0
- package/logs/context.js +109 -0
- package/logs/contract-keys.js +67 -0
- package/logs/debug-shims.js +46 -0
- package/logs/denied-keys.js +21 -0
- package/logs/index.js +14 -3
- package/logs/jest-logger-setup.js +25 -0
- package/logs/levels.js +71 -0
- package/logs/logger-runtime.js +137 -0
- package/logs/logger.js +134 -42
- package/logs/record.js +276 -0
- package/logs/redact.js +328 -0
- package/logs/serialize.js +329 -0
- package/logs/sinks.js +93 -0
- package/logs/summarize-event.js +163 -0
- package/modules/module.js +32 -19
- package/modules/requester/oauth-2.js +28 -22
- package/modules/requester/requester.js +69 -6
- package/package.json +5 -6
- package/queues/queue-delivery.js +41 -0
- package/queues/queuer-util.js +1 -15
- package/telemetry/bind-telemetry-context.js +4 -0
- package/telemetry/instrument-handler.js +19 -2
- package/telemetry/no-op-telemetry.js +4 -0
- package/telemetry/otel-telemetry.js +25 -6
- package/telemetry/telemetry-context.js +19 -8
- package/telemetry/telemetry-runtime.js +17 -0
- package/telemetry/telemetry-service.js +1 -0
- package/types/errors/index.d.ts +24 -10
- package/types/logs/index.d.ts +61 -2
- package/user/repositories/user-repository-documentdb.js +10 -42
package/logs/levels.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
const LEVELS = Object.freeze({
|
|
2
|
+
TRACE: 10,
|
|
3
|
+
DEBUG: 20,
|
|
4
|
+
INFO: 30,
|
|
5
|
+
WARN: 40,
|
|
6
|
+
ERROR: 50,
|
|
7
|
+
FATAL: 60,
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
const LEVEL_NAMES = Object.freeze(Object.keys(LEVELS));
|
|
11
|
+
const DEFAULT_LEVEL = 'INFO';
|
|
12
|
+
const MAX_ECHO_LENGTH = 32;
|
|
13
|
+
|
|
14
|
+
function parseLevel(value) {
|
|
15
|
+
if (typeof value !== 'string') return null;
|
|
16
|
+
const name = value.trim().toUpperCase();
|
|
17
|
+
return Object.prototype.hasOwnProperty.call(LEVELS, name) ? name : null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function isSet(value) {
|
|
21
|
+
return typeof value === 'string' && value.trim() !== '';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isLocalRun(env) {
|
|
25
|
+
if (env.STAGE === 'local') return true;
|
|
26
|
+
const offline = String(env.IS_OFFLINE ?? '').trim().toLowerCase();
|
|
27
|
+
return offline === 'true' || offline === '1';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function lessVerbose(a, b) {
|
|
31
|
+
return LEVELS[a] >= LEVELS[b] ? a : b;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function resolveLevel(env = {}) {
|
|
35
|
+
const warnings = [];
|
|
36
|
+
let level;
|
|
37
|
+
|
|
38
|
+
if (isSet(env.FRIGG_LOG_LEVEL)) {
|
|
39
|
+
level = parseLevel(env.FRIGG_LOG_LEVEL);
|
|
40
|
+
if (!level) {
|
|
41
|
+
level = DEFAULT_LEVEL;
|
|
42
|
+
warnings.push({
|
|
43
|
+
eventName: 'frigg.logger.invalid_level',
|
|
44
|
+
message: 'Unknown FRIGG_LOG_LEVEL, using INFO',
|
|
45
|
+
fields: {
|
|
46
|
+
received: env.FRIGG_LOG_LEVEL.trim().slice(0, MAX_ECHO_LENGTH),
|
|
47
|
+
allowed: LEVEL_NAMES,
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
} else if (env.DEBUG_VERBOSE === '1' || isLocalRun(env)) {
|
|
52
|
+
level = 'DEBUG';
|
|
53
|
+
} else {
|
|
54
|
+
level = DEFAULT_LEVEL;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const awsLevel = parseLevel(env.AWS_LAMBDA_LOG_LEVEL);
|
|
58
|
+
if (awsLevel && awsLevel !== level) {
|
|
59
|
+
const effective = lessVerbose(level, awsLevel);
|
|
60
|
+
warnings.push({
|
|
61
|
+
eventName: 'frigg.logger.level_conflict',
|
|
62
|
+
message: 'FRIGG_LOG_LEVEL and AWS_LAMBDA_LOG_LEVEL differ, using the less verbose level',
|
|
63
|
+
fields: { friggLevel: level, awsLevel, effectiveLevel: effective },
|
|
64
|
+
});
|
|
65
|
+
level = effective;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return { level, warnings };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
module.exports = { LEVELS, LEVEL_NAMES, DEFAULT_LEVEL, parseLevel, resolveLevel };
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
const { LEVELS, parseLevel, resolveLevel } = require('./levels');
|
|
2
|
+
|
|
3
|
+
// On globalThis so jest.resetModules and nested core copies share one state.
|
|
4
|
+
const STATE_KEY = Symbol.for('@friggframework/core.logs');
|
|
5
|
+
|
|
6
|
+
function state() {
|
|
7
|
+
if (!globalThis[STATE_KEY]) {
|
|
8
|
+
globalThis[STATE_KEY] = {
|
|
9
|
+
config: null,
|
|
10
|
+
levelOverride: null,
|
|
11
|
+
sinks: null,
|
|
12
|
+
spanContextProvider: null,
|
|
13
|
+
warned: new Set(),
|
|
14
|
+
inLog: false,
|
|
15
|
+
trackViolations: false,
|
|
16
|
+
violations: [],
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
return globalThis[STATE_KEY];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function readEnv(name) {
|
|
23
|
+
const value = process.env[name];
|
|
24
|
+
return typeof value === 'string' && value !== '' ? value : undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function getConfig() {
|
|
28
|
+
const s = state();
|
|
29
|
+
if (!s.config) {
|
|
30
|
+
const resolved = s.levelOverride
|
|
31
|
+
? { level: s.levelOverride, warnings: [] }
|
|
32
|
+
: resolveLevel(process.env);
|
|
33
|
+
s.config = {
|
|
34
|
+
level: resolved.level,
|
|
35
|
+
threshold: LEVELS[resolved.level],
|
|
36
|
+
appName: readEnv('FRIGG_STACK'),
|
|
37
|
+
stage: readEnv('STAGE'),
|
|
38
|
+
pendingWarnings: resolved.warnings,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return s.config;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function takeConfigWarnings() {
|
|
45
|
+
const s = state();
|
|
46
|
+
const config = getConfig();
|
|
47
|
+
const pending = config.pendingWarnings.splice(0);
|
|
48
|
+
return pending.filter((warning) => {
|
|
49
|
+
if (s.warned.has(warning.eventName)) return false;
|
|
50
|
+
s.warned.add(warning.eventName);
|
|
51
|
+
return true;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function getSinks() {
|
|
56
|
+
const s = state();
|
|
57
|
+
if (!s.sinks) {
|
|
58
|
+
s.sinks = [require('./sinks').createStdoutSink()];
|
|
59
|
+
}
|
|
60
|
+
return s.sinks;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function setSinks(sinks) {
|
|
64
|
+
state().sinks = Array.isArray(sinks) ? [...sinks] : null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function hasFlushableSinks() {
|
|
68
|
+
return getSinks().some((sink) => typeof sink.flush === 'function');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function flushSinks({ signal } = {}) {
|
|
72
|
+
await Promise.allSettled(
|
|
73
|
+
getSinks()
|
|
74
|
+
.filter((sink) => typeof sink.flush === 'function')
|
|
75
|
+
.map(async (sink) => sink.flush({ signal }))
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function setSpanContextProvider(provider) {
|
|
80
|
+
state().spanContextProvider = typeof provider === 'function' ? provider : null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function getSpanContextProvider() {
|
|
84
|
+
return state().spanContextProvider;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function getSpanContext() {
|
|
88
|
+
const provider = state().spanContextProvider;
|
|
89
|
+
if (!provider) return null;
|
|
90
|
+
try {
|
|
91
|
+
const ctx = provider();
|
|
92
|
+
if (ctx && typeof ctx.traceId === 'string' && typeof ctx.spanId === 'string') {
|
|
93
|
+
return ctx;
|
|
94
|
+
}
|
|
95
|
+
} catch {
|
|
96
|
+
// A broken provider must not break logging.
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Off in production, so a warm container never grows this list.
|
|
102
|
+
function recordViolation(violation) {
|
|
103
|
+
const s = state();
|
|
104
|
+
if (s.trackViolations) s.violations.push(violation);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function takeViolationsForTests() {
|
|
108
|
+
return state().violations.splice(0);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function resetLoggerForTests({ level, sinks, trackViolations } = {}) {
|
|
112
|
+
const s = state();
|
|
113
|
+
s.config = null;
|
|
114
|
+
s.levelOverride = parseLevel(level);
|
|
115
|
+
setSinks(sinks);
|
|
116
|
+
s.spanContextProvider = null;
|
|
117
|
+
s.warned.clear();
|
|
118
|
+
s.violations.length = 0;
|
|
119
|
+
s.inLog = false;
|
|
120
|
+
if (typeof trackViolations === 'boolean') s.trackViolations = trackViolations;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
module.exports = {
|
|
124
|
+
state,
|
|
125
|
+
getConfig,
|
|
126
|
+
takeConfigWarnings,
|
|
127
|
+
getSinks,
|
|
128
|
+
setSinks,
|
|
129
|
+
hasFlushableSinks,
|
|
130
|
+
flushSinks,
|
|
131
|
+
setSpanContextProvider,
|
|
132
|
+
getSpanContextProvider,
|
|
133
|
+
getSpanContext,
|
|
134
|
+
recordViolation,
|
|
135
|
+
takeViolationsForTests,
|
|
136
|
+
resetLoggerForTests,
|
|
137
|
+
};
|
package/logs/logger.js
CHANGED
|
@@ -1,65 +1,157 @@
|
|
|
1
|
-
const
|
|
1
|
+
const { LEVELS, parseLevel } = require('./levels');
|
|
2
|
+
const { getLoggerScope } = require('./context');
|
|
3
|
+
const runtime = require('./logger-runtime');
|
|
4
|
+
const { buildRecord, DEFAULT_LOGGER_NAME } = require('./record');
|
|
2
5
|
|
|
3
|
-
|
|
4
|
-
/* eslint-disable no-console */
|
|
6
|
+
const LOGGER_OWN_NAME = 'frigg.logger';
|
|
5
7
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
function copyBindings(bindings, into = {}) {
|
|
9
|
+
if (!bindings || typeof bindings !== 'object') return into;
|
|
10
|
+
for (const key of Object.keys(bindings)) {
|
|
11
|
+
try {
|
|
12
|
+
into[key] = bindings[key];
|
|
13
|
+
} catch {
|
|
14
|
+
// A throwing getter drops only that binding.
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return into;
|
|
18
|
+
}
|
|
13
19
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
20
|
+
// One plain object per record. Static sources were copied at child() time.
|
|
21
|
+
function evaluateBindings(sources) {
|
|
22
|
+
const merged = {};
|
|
23
|
+
for (const source of sources) {
|
|
24
|
+
if (typeof source !== 'function') {
|
|
25
|
+
Object.assign(merged, source);
|
|
26
|
+
continue;
|
|
18
27
|
}
|
|
28
|
+
let value;
|
|
29
|
+
try {
|
|
30
|
+
value = source();
|
|
31
|
+
} catch {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
copyBindings(value, merged);
|
|
19
35
|
}
|
|
36
|
+
return merged;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function isFriggViolation(record) {
|
|
40
|
+
return (
|
|
41
|
+
record.logger.startsWith('frigg.') &&
|
|
42
|
+
LEVELS[record.level] >= LEVELS.WARN &&
|
|
43
|
+
!record.eventName
|
|
44
|
+
);
|
|
20
45
|
}
|
|
21
46
|
|
|
22
|
-
|
|
23
|
-
|
|
47
|
+
class Logger {
|
|
48
|
+
constructor(name, bindingSources = []) {
|
|
49
|
+
this.name = name;
|
|
50
|
+
this._bindingSources = bindingSources;
|
|
51
|
+
}
|
|
24
52
|
|
|
25
|
-
|
|
26
|
-
|
|
53
|
+
trace(message, fields) {
|
|
54
|
+
this._log('TRACE', message, fields);
|
|
55
|
+
}
|
|
27
56
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
57
|
+
debug(message, fields) {
|
|
58
|
+
this._log('DEBUG', message, fields);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
info(message, fields) {
|
|
62
|
+
this._log('INFO', message, fields);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
warn(message, fields) {
|
|
66
|
+
this._log('WARN', message, fields);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
error(message, fields) {
|
|
70
|
+
this._log('ERROR', message, fields);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
fatal(message, fields) {
|
|
74
|
+
this._log('FATAL', message, fields);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
child(bindings) {
|
|
78
|
+
const source = typeof bindings === 'function' ? bindings : copyBindings(bindings);
|
|
79
|
+
return new Logger(this.name, [...this._bindingSources, source]);
|
|
80
|
+
}
|
|
31
81
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
82
|
+
isLevelEnabled(level) {
|
|
83
|
+
try {
|
|
84
|
+
const name = parseLevel(level);
|
|
85
|
+
return name !== null && LEVELS[name] >= runtime.getConfig().threshold;
|
|
86
|
+
} catch {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
37
89
|
}
|
|
38
90
|
|
|
39
|
-
|
|
91
|
+
_log(level, message, fields) {
|
|
92
|
+
const state = runtime.state();
|
|
93
|
+
if (state.inLog) return;
|
|
94
|
+
try {
|
|
95
|
+
const config = runtime.getConfig();
|
|
96
|
+
emitConfigWarnings();
|
|
97
|
+
if (LEVELS[level] < config.threshold) return;
|
|
98
|
+
// Set before fields, bindings and sinks run: any of them may log.
|
|
99
|
+
state.inLog = true;
|
|
40
100
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
101
|
+
const record = buildRecord({
|
|
102
|
+
level,
|
|
103
|
+
logger: this.name,
|
|
104
|
+
message,
|
|
105
|
+
fields,
|
|
106
|
+
bindings: evaluateBindings(this._bindingSources),
|
|
107
|
+
scope: getLoggerScope(),
|
|
108
|
+
spanContext: runtime.getSpanContext(),
|
|
109
|
+
resource: { appName: config.appName, stage: config.stage },
|
|
110
|
+
});
|
|
111
|
+
if (isFriggViolation(record)) {
|
|
112
|
+
runtime.recordViolation({
|
|
113
|
+
logger: record.logger,
|
|
114
|
+
level: record.level,
|
|
115
|
+
message: record.message,
|
|
116
|
+
});
|
|
46
117
|
}
|
|
118
|
+
writeToSinks(record);
|
|
119
|
+
} catch {
|
|
120
|
+
// The logger never throws.
|
|
121
|
+
} finally {
|
|
122
|
+
state.inLog = false;
|
|
47
123
|
}
|
|
48
124
|
}
|
|
125
|
+
}
|
|
49
126
|
|
|
50
|
-
|
|
51
|
-
|
|
127
|
+
function writeToSinks(record) {
|
|
128
|
+
for (const sink of runtime.getSinks()) {
|
|
129
|
+
try {
|
|
130
|
+
sink.write(record);
|
|
131
|
+
} catch {
|
|
132
|
+
// One failing sink must not stop the others.
|
|
133
|
+
}
|
|
52
134
|
}
|
|
135
|
+
}
|
|
53
136
|
|
|
54
|
-
|
|
137
|
+
function emitConfigWarnings() {
|
|
138
|
+
const warnings = runtime.takeConfigWarnings();
|
|
139
|
+
if (!warnings.length) return;
|
|
140
|
+
const logger = getLogger(LOGGER_OWN_NAME);
|
|
141
|
+
for (const warning of warnings) {
|
|
142
|
+
logger.warn(warning.message, {
|
|
143
|
+
...warning.fields,
|
|
144
|
+
eventName: warning.eventName,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
55
148
|
|
|
56
|
-
|
|
149
|
+
const loggers = new Map();
|
|
57
150
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
151
|
+
function getLogger(name) {
|
|
152
|
+
const key = typeof name === 'string' && name ? name : DEFAULT_LOGGER_NAME;
|
|
153
|
+
if (!loggers.has(key)) loggers.set(key, new Logger(key));
|
|
154
|
+
return loggers.get(key);
|
|
63
155
|
}
|
|
64
156
|
|
|
65
|
-
module.exports = {
|
|
157
|
+
module.exports = { Logger, getLogger };
|
package/logs/record.js
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
const { LEVELS } = require('./levels');
|
|
2
|
+
const { serializeValue, serializeError, isError } = require('./serialize');
|
|
3
|
+
const { isPlainObject } = require('./context');
|
|
4
|
+
const {
|
|
5
|
+
RESOURCE_KEYS,
|
|
6
|
+
DROPPED_KEYS_FIELD,
|
|
7
|
+
OWNED_KEYS,
|
|
8
|
+
RESERVED_KEYS,
|
|
9
|
+
PAYLOAD_KEYS,
|
|
10
|
+
} = require('./contract-keys');
|
|
11
|
+
|
|
12
|
+
const MAX_RECORD_BYTES = 16 * 1024;
|
|
13
|
+
const DEFAULT_LOGGER_NAME = 'frigg.unknown';
|
|
14
|
+
|
|
15
|
+
function deepFreeze(value) {
|
|
16
|
+
if (value && typeof value === 'object' && !Object.isFrozen(value)) {
|
|
17
|
+
for (const key of Object.keys(value)) deepFreeze(value[key]);
|
|
18
|
+
Object.freeze(value);
|
|
19
|
+
}
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function readFields(source) {
|
|
24
|
+
const result = {};
|
|
25
|
+
if (!source || typeof source !== 'object') return result;
|
|
26
|
+
let keys;
|
|
27
|
+
try {
|
|
28
|
+
keys = Object.keys(source);
|
|
29
|
+
} catch {
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
for (const key of keys) {
|
|
33
|
+
try {
|
|
34
|
+
result[key] = source[key];
|
|
35
|
+
} catch {
|
|
36
|
+
result[key] = '[Getter threw]';
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Returns the message text. An Error message is serialized once; when the
|
|
43
|
+
// call site has no `error`, that result becomes the record's `error`.
|
|
44
|
+
function buildMessage(message, callSite) {
|
|
45
|
+
if (typeof message === 'string') {
|
|
46
|
+
return { text: serializeValue(message) };
|
|
47
|
+
}
|
|
48
|
+
if (message === undefined || message === null) return { text: '' };
|
|
49
|
+
if (isError(message)) {
|
|
50
|
+
const error = serializeError(message);
|
|
51
|
+
const text = typeof error?.message === 'string' ? error.message : '';
|
|
52
|
+
if (callSite.error !== undefined) return { text };
|
|
53
|
+
callSite.error = message;
|
|
54
|
+
return { text, error };
|
|
55
|
+
}
|
|
56
|
+
if (callSite.value === undefined) callSite.value = message;
|
|
57
|
+
return { text: '[non-string message]' };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const INVOCATION_KEY = 'invocation';
|
|
61
|
+
|
|
62
|
+
// A boundary adds request detail to the scope's invocation; scope keys win.
|
|
63
|
+
function extendInvocation(scoped, detail) {
|
|
64
|
+
const merged = { ...scoped };
|
|
65
|
+
for (const key of Object.keys(detail)) {
|
|
66
|
+
if (!(key in merged)) merged[key] = detail[key];
|
|
67
|
+
}
|
|
68
|
+
return merged;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function traceFields(spanContext) {
|
|
72
|
+
if (
|
|
73
|
+
!spanContext ||
|
|
74
|
+
typeof spanContext.traceId !== 'string' ||
|
|
75
|
+
typeof spanContext.spanId !== 'string'
|
|
76
|
+
) {
|
|
77
|
+
return {};
|
|
78
|
+
}
|
|
79
|
+
const flags = Number(spanContext.traceFlags) || 0;
|
|
80
|
+
return {
|
|
81
|
+
trace_id: spanContext.traceId,
|
|
82
|
+
span_id: spanContext.spanId,
|
|
83
|
+
trace_flags: (flags & 0xff).toString(16).padStart(2, '0'),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function sameValue(a, b) {
|
|
88
|
+
if (a === b) return true;
|
|
89
|
+
return (
|
|
90
|
+
typeof a === 'object' &&
|
|
91
|
+
typeof b === 'object' &&
|
|
92
|
+
JSON.stringify(a) === JSON.stringify(b)
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function byteLength(record) {
|
|
97
|
+
return Buffer.byteLength(JSON.stringify(record));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function isDroppedKey(key, dropPayloads) {
|
|
101
|
+
return (
|
|
102
|
+
OWNED_KEYS.has(key) ||
|
|
103
|
+
RESERVED_KEYS.has(key) ||
|
|
104
|
+
(dropPayloads && PAYLOAD_KEYS.has(key))
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Filters and serializes one source. `error` is serialized on its own, once
|
|
109
|
+
// (or taken from `preSerializedError`), and never walked by serializeValue.
|
|
110
|
+
function prepareSource(source, dropPayloads, preSerializedError) {
|
|
111
|
+
const accepted = {};
|
|
112
|
+
const keys = [];
|
|
113
|
+
const dropped = [];
|
|
114
|
+
let rawError;
|
|
115
|
+
for (const [key, value] of Object.entries(source)) {
|
|
116
|
+
if (value === undefined || value === null) continue;
|
|
117
|
+
if (isDroppedKey(key, dropPayloads)) {
|
|
118
|
+
dropped.push(key);
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
keys.push(key);
|
|
122
|
+
if (key === 'error') rawError = value;
|
|
123
|
+
else accepted[key] = value;
|
|
124
|
+
}
|
|
125
|
+
// One pass over the object, so key rules (denied keys, digests,
|
|
126
|
+
// headers, OAuth shapes) apply to top-level fields too.
|
|
127
|
+
const safe = serializeValue(accepted);
|
|
128
|
+
const entries = [];
|
|
129
|
+
for (const key of keys) {
|
|
130
|
+
const value =
|
|
131
|
+
key === 'error'
|
|
132
|
+
? preSerializedError ?? serializeError(rawError)
|
|
133
|
+
: safe?.[key];
|
|
134
|
+
if (value !== undefined && value !== null) entries.push([key, value]);
|
|
135
|
+
}
|
|
136
|
+
return { entries, dropped };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// The scope object is frozen and shared by every record of one scope.
|
|
140
|
+
const preparedScopes = new WeakMap();
|
|
141
|
+
|
|
142
|
+
function prepareScope(scope, dropPayloads) {
|
|
143
|
+
if (!scope || typeof scope !== 'object') return { entries: [], dropped: [] };
|
|
144
|
+
let byLevel = preparedScopes.get(scope);
|
|
145
|
+
if (!byLevel) {
|
|
146
|
+
byLevel = {};
|
|
147
|
+
preparedScopes.set(scope, byLevel);
|
|
148
|
+
}
|
|
149
|
+
const slot = dropPayloads ? 'info' : 'debug';
|
|
150
|
+
if (!byLevel[slot]) byLevel[slot] = prepareSource(scope, dropPayloads);
|
|
151
|
+
return byLevel[slot];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function buildRecord({
|
|
155
|
+
level,
|
|
156
|
+
logger,
|
|
157
|
+
message,
|
|
158
|
+
fields,
|
|
159
|
+
bindings,
|
|
160
|
+
scope,
|
|
161
|
+
spanContext,
|
|
162
|
+
resource = {},
|
|
163
|
+
now = new Date(),
|
|
164
|
+
} = {}) {
|
|
165
|
+
const dropPayloads = LEVELS[level] >= LEVELS.INFO;
|
|
166
|
+
// log.error('msg', err): own props of an Error (axios config, request) must not become fields.
|
|
167
|
+
const callSite = readFields(isError(fields) ? { error: fields } : fields);
|
|
168
|
+
const built = buildMessage(message, callSite);
|
|
169
|
+
const record = {
|
|
170
|
+
timestamp: now.toISOString(),
|
|
171
|
+
level,
|
|
172
|
+
message: built.text,
|
|
173
|
+
logger: typeof logger === 'string' && logger ? logger : DEFAULT_LOGGER_NAME,
|
|
174
|
+
};
|
|
175
|
+
for (const key of RESOURCE_KEYS) {
|
|
176
|
+
if (typeof resource[key] === 'string' && resource[key]) {
|
|
177
|
+
record[key] = resource[key];
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
Object.assign(record, traceFields(spanContext));
|
|
181
|
+
|
|
182
|
+
const dropped = new Set();
|
|
183
|
+
const callSiteKeys = [];
|
|
184
|
+
// Call-site keys merged into the scope invocation; the size cap must reach them too.
|
|
185
|
+
const invocationDetailKeys = [];
|
|
186
|
+
const place = ({ entries, dropped: droppedHere }, isCallSite) => {
|
|
187
|
+
for (const key of droppedHere) dropped.add(key);
|
|
188
|
+
for (const [key, value] of entries) {
|
|
189
|
+
if (Object.prototype.hasOwnProperty.call(record, key)) {
|
|
190
|
+
if (
|
|
191
|
+
key === INVOCATION_KEY &&
|
|
192
|
+
isPlainObject(record[key]) &&
|
|
193
|
+
isPlainObject(value)
|
|
194
|
+
) {
|
|
195
|
+
const merged = extendInvocation(record[key], value);
|
|
196
|
+
if (isCallSite) {
|
|
197
|
+
for (const detailKey of Object.keys(value)) {
|
|
198
|
+
if (!(detailKey in record[key])) invocationDetailKeys.push(detailKey);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
record[key] = merged;
|
|
202
|
+
} else if (!sameValue(record[key], value)) {
|
|
203
|
+
// A repeat of the winning value is no conflict.
|
|
204
|
+
dropped.add(key);
|
|
205
|
+
}
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
record[key] = value;
|
|
209
|
+
if (isCallSite) callSiteKeys.push(key);
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
place(prepareScope(scope, dropPayloads), false);
|
|
213
|
+
place(prepareSource(bindings || {}, dropPayloads), false);
|
|
214
|
+
place(prepareSource(callSite, dropPayloads, built.error), true);
|
|
215
|
+
|
|
216
|
+
const setDroppedKeys = () => {
|
|
217
|
+
delete record[DROPPED_KEYS_FIELD];
|
|
218
|
+
if (dropped.size) record[DROPPED_KEYS_FIELD] = [...dropped];
|
|
219
|
+
};
|
|
220
|
+
setDroppedKeys();
|
|
221
|
+
let size = byteLength(record);
|
|
222
|
+
|
|
223
|
+
if (size > MAX_RECORD_BYTES) {
|
|
224
|
+
const candidates = [
|
|
225
|
+
...callSiteKeys
|
|
226
|
+
.filter((key) => key !== 'error')
|
|
227
|
+
.map((key) => ({
|
|
228
|
+
name: key,
|
|
229
|
+
value: record[key],
|
|
230
|
+
remove: () => delete record[key],
|
|
231
|
+
})),
|
|
232
|
+
...invocationDetailKeys.map((key) => ({
|
|
233
|
+
name: `${INVOCATION_KEY}.${key}`,
|
|
234
|
+
value: record[INVOCATION_KEY][key],
|
|
235
|
+
remove: () => {
|
|
236
|
+
const rest = { ...record[INVOCATION_KEY] };
|
|
237
|
+
delete rest[key];
|
|
238
|
+
record[INVOCATION_KEY] = rest;
|
|
239
|
+
},
|
|
240
|
+
})),
|
|
241
|
+
];
|
|
242
|
+
const bySize = candidates
|
|
243
|
+
.map((candidate) => [candidate, Buffer.byteLength(JSON.stringify(candidate.value))])
|
|
244
|
+
.sort((a, b) => b[1] - a[1]);
|
|
245
|
+
for (const [candidate] of bySize) {
|
|
246
|
+
candidate.remove();
|
|
247
|
+
dropped.add(candidate.name);
|
|
248
|
+
setDroppedKeys();
|
|
249
|
+
size = byteLength(record);
|
|
250
|
+
if (size <= MAX_RECORD_BYTES) break;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
for (const part of ['stack', 'cause']) {
|
|
254
|
+
if (
|
|
255
|
+
size > MAX_RECORD_BYTES &&
|
|
256
|
+
record.error &&
|
|
257
|
+
typeof record.error === 'object' &&
|
|
258
|
+
record.error[part] !== undefined
|
|
259
|
+
) {
|
|
260
|
+
const { [part]: _removed, ...rest } = record.error;
|
|
261
|
+
record.error = rest;
|
|
262
|
+
size = byteLength(record);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return deepFreeze(record);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
module.exports = {
|
|
270
|
+
buildRecord,
|
|
271
|
+
deepFreeze,
|
|
272
|
+
MAX_RECORD_BYTES,
|
|
273
|
+
RESERVED_KEYS,
|
|
274
|
+
PAYLOAD_KEYS,
|
|
275
|
+
DEFAULT_LOGGER_NAME,
|
|
276
|
+
};
|