@agoric/telemetry 0.6.3-other-dev-8f8782b.0 → 0.6.3-other-dev-fbe72e7.0.fbe72e7
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/CHANGELOG.md +0 -9
- package/package.json +36 -27
- package/scripts/ingest.sh +2 -2
- package/src/context-aware-slog-file.js +42 -0
- package/src/context-aware-slog.js +387 -0
- package/src/flight-recorder.js +173 -90
- package/src/frcat-entrypoint.js +15 -11
- package/src/index.js +29 -30
- package/src/ingest-slog-entrypoint.js +46 -15
- package/src/make-slog-sender.js +126 -109
- package/src/otel-and-flight-recorder.js +4 -1
- package/src/otel-context-aware-slog.js +131 -0
- package/src/otel-metrics.js +229 -0
- package/src/otel-trace.js +11 -1
- package/src/prometheus.js +18 -0
- package/src/serialize-slog-obj.js +32 -4
- package/src/slog-file.js +1 -1
- package/src/slog-sender-pipe-entrypoint.js +76 -74
- package/src/slog-sender-pipe.js +87 -112
- package/src/slog-to-otel.js +26 -11
- package/test/flight-recorder.test.js +114 -0
- package/test/prepare-test-env-ava.js +0 -2
- package/{jsconfig.json → tsconfig.json} +1 -0
- package/test/test-flight-recorder.js +0 -53
- /package/test/{test-import.js → import.test.js} +0 -0
package/src/make-slog-sender.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import tmp from 'tmp';
|
|
3
|
-
import { PromiseAllOrErrors } from '@agoric/internal';
|
|
3
|
+
import { PromiseAllOrErrors, unprefixedProperties } from '@agoric/internal';
|
|
4
4
|
import { serializeSlogObj } from './serialize-slog-obj.js';
|
|
5
5
|
|
|
6
|
+
export const DEFAULT_SLOGSENDER_AGENT = 'self';
|
|
6
7
|
export const DEFAULT_SLOGSENDER_MODULE =
|
|
7
8
|
'@agoric/telemetry/src/flight-recorder.js';
|
|
8
9
|
export const SLOGFILE_SENDER_MODULE = '@agoric/telemetry/src/slog-file.js';
|
|
10
|
+
export const PROMETHEUS_SENDER_MODULE = '@agoric/telemetry/src/prometheus.js';
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
/** @typedef {import('./index.js').SlogSender} SlogSender */
|
|
12
|
+
/** @import {SlogSender} from './index.js' */
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* @template T
|
|
@@ -19,102 +19,119 @@ export const DEFAULT_SLOGSENDER_AGENT = 'self';
|
|
|
19
19
|
const filterTruthy = arr => /** @type {any[]} */ (arr.filter(Boolean));
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
22
|
+
* Create an aggregate slog sender that fans out inbound slog entries to modules
|
|
23
|
+
* as indicated by variables in the supplied `env` option. The SLOGSENDER value
|
|
24
|
+
* (or a default DEFAULT_SLOGSENDER_MODULE defined above) is split on commas
|
|
25
|
+
* into a list of module identifiers and adjusted by automatic insertions (a
|
|
26
|
+
* non-empty SLOGFILE value inserts DEFAULT_SLOGSENDER_AGENT defined above), and
|
|
27
|
+
* then each identifier is dynamically `import`ed for its own `makeSlogSender`
|
|
28
|
+
* export, which is invoked with a non-empty `stateDir` option and a modified
|
|
29
|
+
* `env` in which SLOGSENDER_AGENT_* variables have overridden their unprefixed
|
|
30
|
+
* equivalents to produce a subordinate slog sender.
|
|
31
|
+
* Subordinate slog senders remain isolated from each other, and any errors from
|
|
32
|
+
* them are caught and held until the next `forceFlush()` without disrupting
|
|
33
|
+
* any remaining slog entry fanout.
|
|
34
|
+
* If SLOGSENDER_AGENT is 'process', 'slog-sender-pipe.js' is used to load the
|
|
35
|
+
* subordinates in a child process rather than the main process.
|
|
36
|
+
* When there are no subordinates, the return value will be `undefined` rather
|
|
37
|
+
* than a slog sender function.
|
|
38
|
+
*
|
|
39
|
+
* @type {import('./index.js').MakeSlogSender}
|
|
23
40
|
*/
|
|
24
41
|
export const makeSlogSender = async (opts = {}) => {
|
|
25
42
|
const { env = {}, stateDir: stateDirOption, ...otherOpts } = opts;
|
|
26
43
|
const {
|
|
27
44
|
SLOGSENDER = DEFAULT_SLOGSENDER_MODULE,
|
|
28
45
|
SLOGSENDER_AGENT = DEFAULT_SLOGSENDER_AGENT,
|
|
46
|
+
// While cosmic-swingset/kernel code includes its own Prometheus metrics
|
|
47
|
+
// export, that trumps a slog sender module doing so.
|
|
48
|
+
// This extraction can be removed when that changes, but in the meantime,
|
|
49
|
+
// opt-in is only by SLOGSENDER_AGENT_OTEL_EXPORTER_PROMETHEUS_PORT.
|
|
50
|
+
OTEL_EXPORTER_PROMETHEUS_PORT: _prometheusExportPort,
|
|
29
51
|
...otherEnv
|
|
30
52
|
} = env;
|
|
31
53
|
|
|
32
54
|
const agentEnv = {
|
|
33
55
|
...otherEnv,
|
|
34
|
-
...
|
|
35
|
-
Object.entries(otherEnv)
|
|
36
|
-
.filter(([k]) => k.match(/^(?:SLOGSENDER_AGENT_)+/)) // narrow to SLOGSENDER_AGENT_ prefixes.
|
|
37
|
-
.map(([k, v]) => [k.replace(/^(?:SLOGSENDER_AGENT_)+/, ''), v]), // Rewrite SLOGSENDER_AGENT_ to un-prefixed version.
|
|
38
|
-
),
|
|
56
|
+
...unprefixedProperties(otherEnv, 'SLOGSENDER_AGENT_'),
|
|
39
57
|
};
|
|
40
58
|
|
|
41
|
-
const slogSenderModules =
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
59
|
+
const slogSenderModules = new Set();
|
|
60
|
+
if (agentEnv.OTEL_EXPORTER_PROMETHEUS_PORT) {
|
|
61
|
+
slogSenderModules.add(PROMETHEUS_SENDER_MODULE);
|
|
62
|
+
}
|
|
63
|
+
if (agentEnv.SLOGFILE) {
|
|
64
|
+
slogSenderModules.add(SLOGFILE_SENDER_MODULE);
|
|
65
|
+
}
|
|
66
|
+
for (const moduleIdentifier of filterTruthy(SLOGSENDER.split(','))) {
|
|
67
|
+
if (moduleIdentifier.startsWith('-')) {
|
|
68
|
+
// Opt out of an automatically-included sender.
|
|
69
|
+
slogSenderModules.delete(moduleIdentifier.slice(1));
|
|
70
|
+
} else if (moduleIdentifier.startsWith('.')) {
|
|
71
|
+
// Resolve relative to the current working directory.
|
|
72
|
+
slogSenderModules.add(path.resolve(moduleIdentifier));
|
|
73
|
+
} else {
|
|
74
|
+
slogSenderModules.add(moduleIdentifier);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (!slogSenderModules.size) {
|
|
56
79
|
return undefined;
|
|
57
80
|
}
|
|
58
81
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
case 'worker':
|
|
79
|
-
default:
|
|
80
|
-
console.warn(`Unknown SLOGSENDER_AGENT=${SLOGSENDER_AGENT}`);
|
|
82
|
+
if (SLOGSENDER_AGENT === 'process') {
|
|
83
|
+
console.warn('Loading slog sender in subprocess');
|
|
84
|
+
return import('./slog-sender-pipe.js').then(async module =>
|
|
85
|
+
module.makeSlogSender({
|
|
86
|
+
env: {
|
|
87
|
+
...agentEnv,
|
|
88
|
+
SLOGSENDER,
|
|
89
|
+
SLOGSENDER_AGENT: 'self',
|
|
90
|
+
},
|
|
91
|
+
stateDir: stateDirOption,
|
|
92
|
+
...otherOpts,
|
|
93
|
+
}),
|
|
94
|
+
);
|
|
95
|
+
} else if (SLOGSENDER_AGENT && SLOGSENDER_AGENT !== 'self') {
|
|
96
|
+
console.warn(
|
|
97
|
+
`Unknown SLOGSENDER_AGENT=${SLOGSENDER_AGENT}; defaulting to 'self'`,
|
|
98
|
+
);
|
|
81
99
|
}
|
|
82
100
|
|
|
83
101
|
if (SLOGSENDER) {
|
|
84
102
|
console.warn('Loading slog sender modules:', ...slogSenderModules);
|
|
85
103
|
}
|
|
86
104
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return /** @type {const} */ ([maker, moduleIdentifier]);
|
|
105
|
-
},
|
|
106
|
-
)
|
|
107
|
-
.catch(err => {
|
|
105
|
+
/** @type {Map<import('./index.js').MakeSlogSender, string>} */
|
|
106
|
+
const makerMap = new Map();
|
|
107
|
+
await Promise.all(
|
|
108
|
+
[...slogSenderModules].map(async moduleIdentifier => {
|
|
109
|
+
await null;
|
|
110
|
+
try {
|
|
111
|
+
const module = await import(moduleIdentifier);
|
|
112
|
+
const { makeSlogSender: maker } = module;
|
|
113
|
+
if (typeof maker !== 'function') {
|
|
114
|
+
throw Error(`No 'makeSlogSender' function exported by module`);
|
|
115
|
+
} else if (maker === makeSlogSender) {
|
|
116
|
+
throw Error(`Cannot recursively load 'makeSlogSender' aggregator`);
|
|
117
|
+
}
|
|
118
|
+
const isReplacing = makerMap.get(maker);
|
|
119
|
+
if (isReplacing) {
|
|
108
120
|
console.warn(
|
|
109
|
-
`
|
|
110
|
-
err,
|
|
121
|
+
`The slog sender from ${moduleIdentifier} matches the one from ${isReplacing}.`,
|
|
111
122
|
);
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
123
|
+
}
|
|
124
|
+
makerMap.set(maker, moduleIdentifier);
|
|
125
|
+
} catch (err) {
|
|
126
|
+
console.warn(
|
|
127
|
+
`Failed to load slog sender from ${moduleIdentifier}.`,
|
|
128
|
+
err,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
}),
|
|
132
|
+
);
|
|
116
133
|
|
|
117
|
-
if (!
|
|
134
|
+
if (!makerMap.size) {
|
|
118
135
|
return undefined;
|
|
119
136
|
}
|
|
120
137
|
|
|
@@ -122,11 +139,11 @@ export const makeSlogSender = async (opts = {}) => {
|
|
|
122
139
|
|
|
123
140
|
if (stateDir === undefined) {
|
|
124
141
|
stateDir = tmp.dirSync().name;
|
|
125
|
-
console.warn(`Using ${stateDir} for stateDir`);
|
|
142
|
+
console.warn(`Using ${stateDir} for slog sender stateDir`);
|
|
126
143
|
}
|
|
127
144
|
|
|
128
145
|
const senders = await Promise.all(
|
|
129
|
-
|
|
146
|
+
[...makerMap.entries()].map(async ([maker, moduleIdentifier]) =>
|
|
130
147
|
maker({
|
|
131
148
|
...otherOpts,
|
|
132
149
|
stateDir,
|
|
@@ -137,37 +154,37 @@ export const makeSlogSender = async (opts = {}) => {
|
|
|
137
154
|
|
|
138
155
|
if (!senders.length) {
|
|
139
156
|
return undefined;
|
|
140
|
-
} else {
|
|
141
|
-
// Optimize creating a JSON serialization only if needed
|
|
142
|
-
// by any of the sender modules
|
|
143
|
-
const hasSenderUsingJsonObj = senders.some(
|
|
144
|
-
({ usesJsonObject = true }) => usesJsonObject,
|
|
145
|
-
);
|
|
146
|
-
const getJsonObj = hasSenderUsingJsonObj
|
|
147
|
-
? serializeSlogObj
|
|
148
|
-
: () => undefined;
|
|
149
|
-
const sendErrors = [];
|
|
150
|
-
/** @type {SlogSender} */
|
|
151
|
-
const slogSender = (slogObj, jsonObj = getJsonObj(slogObj)) => {
|
|
152
|
-
for (const sender of senders) {
|
|
153
|
-
try {
|
|
154
|
-
sender(slogObj, jsonObj);
|
|
155
|
-
} catch (err) {
|
|
156
|
-
sendErrors.push(err);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
return Object.assign(slogSender, {
|
|
161
|
-
forceFlush: async () =>
|
|
162
|
-
PromiseAllOrErrors([
|
|
163
|
-
...senders.map(sender => sender.forceFlush?.()),
|
|
164
|
-
...sendErrors.splice(0).map(err => Promise.reject(err)),
|
|
165
|
-
]).then(() => {}),
|
|
166
|
-
shutdown: async () =>
|
|
167
|
-
PromiseAllOrErrors(senders.map(sender => sender.shutdown?.())).then(
|
|
168
|
-
() => {},
|
|
169
|
-
),
|
|
170
|
-
usesJsonObject: hasSenderUsingJsonObj,
|
|
171
|
-
});
|
|
172
157
|
}
|
|
158
|
+
|
|
159
|
+
// Optimize creating a JSON serialization only if needed
|
|
160
|
+
// by at least one of the senders.
|
|
161
|
+
const hasSenderUsingJsonObj = senders.some(
|
|
162
|
+
({ usesJsonObject = true }) => usesJsonObject,
|
|
163
|
+
);
|
|
164
|
+
const getJsonObj = hasSenderUsingJsonObj ? serializeSlogObj : () => undefined;
|
|
165
|
+
|
|
166
|
+
const sendErrors = [];
|
|
167
|
+
|
|
168
|
+
/** @type {SlogSender} */
|
|
169
|
+
const slogSender = (slogObj, jsonObj = getJsonObj(slogObj)) => {
|
|
170
|
+
for (const sender of senders) {
|
|
171
|
+
try {
|
|
172
|
+
sender(slogObj, jsonObj);
|
|
173
|
+
} catch (err) {
|
|
174
|
+
sendErrors.push(err);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
return Object.assign(slogSender, {
|
|
179
|
+
forceFlush: async () => {
|
|
180
|
+
await PromiseAllOrErrors([
|
|
181
|
+
...senders.map(sender => sender.forceFlush?.()),
|
|
182
|
+
...sendErrors.splice(0).map(err => Promise.reject(err)),
|
|
183
|
+
]);
|
|
184
|
+
},
|
|
185
|
+
shutdown: async () => {
|
|
186
|
+
await PromiseAllOrErrors(senders.map(sender => sender.shutdown?.()));
|
|
187
|
+
},
|
|
188
|
+
usesJsonObject: hasSenderUsingJsonObj,
|
|
189
|
+
});
|
|
173
190
|
};
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import { NonNullish } from '@agoric/
|
|
1
|
+
import { NonNullish } from '@agoric/internal';
|
|
2
2
|
import { makeSlogSender as makeSlogSenderFromEnv } from './make-slog-sender.js';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @param {import('./index.js').MakeSlogSenderOptions} opts
|
|
6
|
+
*/
|
|
4
7
|
export const makeSlogSender = async opts => {
|
|
5
8
|
const { SLOGFILE: _1, SLOGSENDER: _2, ...otherEnv } = opts.env || {};
|
|
6
9
|
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/* eslint-env node */
|
|
2
|
+
import { logs, SeverityNumber } from '@opentelemetry/api-logs';
|
|
3
|
+
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http';
|
|
4
|
+
import { Resource } from '@opentelemetry/resources';
|
|
5
|
+
import {
|
|
6
|
+
LoggerProvider,
|
|
7
|
+
SimpleLogRecordProcessor,
|
|
8
|
+
} from '@opentelemetry/sdk-logs';
|
|
9
|
+
import { readFileSync, writeFileSync } from 'fs';
|
|
10
|
+
import { makeContextualSlogProcessor } from './context-aware-slog.js';
|
|
11
|
+
import { getResourceAttributes } from './index.js';
|
|
12
|
+
import { serializeSlogObj } from './serialize-slog-obj.js';
|
|
13
|
+
|
|
14
|
+
const DEFAULT_CONTEXT_FILE = 'slog-context.json';
|
|
15
|
+
const FILE_ENCODING = 'utf8';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {string} filePath
|
|
19
|
+
*/
|
|
20
|
+
export const getContextFilePersistenceUtils = filePath => {
|
|
21
|
+
console.warn(`Using file ${filePath} for slogger context`);
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
/**
|
|
25
|
+
* @param {import('./context-aware-slog.js').Context} context
|
|
26
|
+
*/
|
|
27
|
+
persistContext: context => {
|
|
28
|
+
try {
|
|
29
|
+
writeFileSync(filePath, serializeSlogObj(context), FILE_ENCODING);
|
|
30
|
+
} catch (err) {
|
|
31
|
+
console.error('Error writing context to file: ', err);
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @returns {import('./context-aware-slog.js').Context | null}
|
|
37
|
+
*/
|
|
38
|
+
restoreContext: () => {
|
|
39
|
+
try {
|
|
40
|
+
return JSON.parse(readFileSync(filePath, FILE_ENCODING));
|
|
41
|
+
} catch (parseErr) {
|
|
42
|
+
console.error('Error reading context from file: ', parseErr);
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param {import('./index.js').MakeSlogSenderOptions} options
|
|
51
|
+
*/
|
|
52
|
+
export const makeSlogSender = async options => {
|
|
53
|
+
const { CHAIN_ID, OTEL_EXPORTER_OTLP_ENDPOINT } = options.env || {};
|
|
54
|
+
if (!(OTEL_EXPORTER_OTLP_ENDPOINT && options.stateDir))
|
|
55
|
+
return console.error(
|
|
56
|
+
'Ignoring invocation of slogger "context-aware-slog" without the presence of "OTEL_EXPORTER_OTLP_ENDPOINT" and "stateDir"',
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const loggerProvider = new LoggerProvider({
|
|
60
|
+
resource: new Resource(getResourceAttributes(options)),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const otelLogExporter = new OTLPLogExporter({ keepAlive: true });
|
|
64
|
+
const logRecordProcessor = new SimpleLogRecordProcessor(otelLogExporter);
|
|
65
|
+
|
|
66
|
+
loggerProvider.addLogRecordProcessor(logRecordProcessor);
|
|
67
|
+
|
|
68
|
+
logs.setGlobalLoggerProvider(loggerProvider);
|
|
69
|
+
const logger = logs.getLogger('default');
|
|
70
|
+
|
|
71
|
+
const persistenceUtils = getContextFilePersistenceUtils(
|
|
72
|
+
process.env.SLOG_CONTEXT_FILE_PATH ||
|
|
73
|
+
`${options.stateDir}/${DEFAULT_CONTEXT_FILE}`,
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
const contextualSlogProcessor = makeContextualSlogProcessor(
|
|
77
|
+
{ 'chain-id': CHAIN_ID },
|
|
78
|
+
persistenceUtils,
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @param {import('./context-aware-slog.js').Slog} slog
|
|
83
|
+
*/
|
|
84
|
+
const slogSender = slog => {
|
|
85
|
+
const { time, ...logRecord } = contextualSlogProcessor(slog);
|
|
86
|
+
|
|
87
|
+
const [secondsStr, fractionStr] = String(time).split('.');
|
|
88
|
+
const seconds = parseInt(secondsStr, 10);
|
|
89
|
+
const nanoSeconds = parseInt(
|
|
90
|
+
(fractionStr || String(0)).padEnd(9, String(0)).slice(0, 9),
|
|
91
|
+
10,
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
logger.emit({
|
|
95
|
+
...JSON.parse(serializeSlogObj(logRecord)),
|
|
96
|
+
severityNumber: SeverityNumber.INFO,
|
|
97
|
+
timestamp: [seconds, nanoSeconds],
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const shutdown = async () => {
|
|
102
|
+
await Promise.resolve();
|
|
103
|
+
const errors = [];
|
|
104
|
+
|
|
105
|
+
try {
|
|
106
|
+
await logRecordProcessor.shutdown();
|
|
107
|
+
} catch (err) {
|
|
108
|
+
errors.push(err);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
await otelLogExporter.forceFlush();
|
|
113
|
+
} catch (err) {
|
|
114
|
+
errors.push(err);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
switch (errors.length) {
|
|
118
|
+
case 0:
|
|
119
|
+
return;
|
|
120
|
+
case 1:
|
|
121
|
+
throw errors[0];
|
|
122
|
+
default:
|
|
123
|
+
throw AggregateError(errors);
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
return Object.assign(slogSender, {
|
|
128
|
+
forceFlush: () => otelLogExporter.forceFlush(),
|
|
129
|
+
shutdown,
|
|
130
|
+
});
|
|
131
|
+
};
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { q, Fail } from '@endo/errors';
|
|
2
|
+
|
|
3
|
+
import * as ActionType from '@agoric/internal/src/action-types.js';
|
|
4
|
+
import { objectMapMutable } from '@agoric/internal/src/js-utils.js';
|
|
5
|
+
import {
|
|
6
|
+
HISTOGRAM_METRICS,
|
|
7
|
+
BLOCK_HISTOGRAM_METRICS,
|
|
8
|
+
KERNEL_STATS_METRICS,
|
|
9
|
+
makeQueueMetrics,
|
|
10
|
+
} from '@agoric/internal/src/metrics.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @import {MetricOptions, ObservableCounter, ObservableUpDownCounter} from '@opentelemetry/api';
|
|
14
|
+
* @import {MeterProvider} from '@opentelemetry/sdk-metrics';
|
|
15
|
+
* @import {TotalMap} from '@agoric/internal';
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const knownActionTypes = new Set(Object.values(ActionType.QueuedActionType));
|
|
19
|
+
|
|
20
|
+
/** @param {import('./index.js').MakeSlogSenderOptions & {otelMeterName: string, otelMeterProvider?: MeterProvider}} opts */
|
|
21
|
+
export const makeSlogSender = async (opts = /** @type {any} */ ({})) => {
|
|
22
|
+
const { otelMeterName, otelMeterProvider } = opts;
|
|
23
|
+
if (!otelMeterName) throw Fail`OTel meter name is required`;
|
|
24
|
+
if (!otelMeterProvider) return;
|
|
25
|
+
|
|
26
|
+
const shutdown = async () => {
|
|
27
|
+
await otelMeterProvider.shutdown();
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const otelMeter = otelMeterProvider.getMeter(otelMeterName);
|
|
31
|
+
|
|
32
|
+
const processedInboundActionCounter = otelMeter.createCounter(
|
|
33
|
+
'cosmic_swingset_inbound_actions',
|
|
34
|
+
{ description: 'Processed inbound action counts by type' },
|
|
35
|
+
);
|
|
36
|
+
const histograms = {
|
|
37
|
+
...objectMapMutable(HISTOGRAM_METRICS, (desc, name) => {
|
|
38
|
+
const { boundaries, ...options } = desc;
|
|
39
|
+
const advice = boundaries && { explicitBucketBoundaries: boundaries };
|
|
40
|
+
return otelMeter.createHistogram(name, { ...options, advice });
|
|
41
|
+
}),
|
|
42
|
+
...objectMapMutable(BLOCK_HISTOGRAM_METRICS, (desc, name) =>
|
|
43
|
+
otelMeter.createHistogram(name, desc),
|
|
44
|
+
),
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const inboundQueueMetrics = makeQueueMetrics({
|
|
48
|
+
otelMeter,
|
|
49
|
+
namePrefix: 'cosmic_swingset_inbound_queue',
|
|
50
|
+
descPrefix: 'inbound queue',
|
|
51
|
+
console,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Values for KERNEL_STATS_METRICS could be built up locally by observing slog
|
|
55
|
+
// entries, but they are all collectively reported in "kernel-stats"
|
|
56
|
+
// (@see {@link ../../cosmic-swingset/src/kernel-stats.js exportKernelStats})
|
|
57
|
+
// and for now we just reflect that, which requires implementation as async
|
|
58
|
+
// ("observable") instruments rather than synchronous ones.
|
|
59
|
+
/** @typedef {string} KernelStatsKey */
|
|
60
|
+
/** @typedef {string} KernelMetricName */
|
|
61
|
+
/** @type {TotalMap<KernelStatsKey, number>} */
|
|
62
|
+
const kernelStats = new Map();
|
|
63
|
+
/** @type {Map<KernelMetricName, ObservableCounter | ObservableUpDownCounter>} */
|
|
64
|
+
const kernelStatsCounters = new Map();
|
|
65
|
+
for (const meta of KERNEL_STATS_METRICS) {
|
|
66
|
+
const { key, name, sub, metricType, ...options } = meta;
|
|
67
|
+
kernelStats.set(key, 0);
|
|
68
|
+
if (metricType === 'gauge') {
|
|
69
|
+
kernelStats.set(`${key}Up`, 0);
|
|
70
|
+
kernelStats.set(`${key}Down`, 0);
|
|
71
|
+
kernelStats.set(`${key}Max`, 0);
|
|
72
|
+
} else if (metricType !== 'counter') {
|
|
73
|
+
Fail`Unknown metric type ${q(metricType)} for key ${q(key)} name ${q(name)}`;
|
|
74
|
+
}
|
|
75
|
+
let counter = kernelStatsCounters.get(name);
|
|
76
|
+
if (!counter) {
|
|
77
|
+
counter =
|
|
78
|
+
metricType === 'counter'
|
|
79
|
+
? otelMeter.createObservableCounter(name, options)
|
|
80
|
+
: otelMeter.createObservableUpDownCounter(name, options);
|
|
81
|
+
kernelStatsCounters.set(name, counter);
|
|
82
|
+
}
|
|
83
|
+
const attributes = sub ? { [sub.dimension]: sub.value } : {};
|
|
84
|
+
counter.addCallback(observer => {
|
|
85
|
+
observer.observe(kernelStats.get(key), attributes);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
const expectedKernelStats = new Set(kernelStats.keys());
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @typedef {object} LazyStats
|
|
92
|
+
* @property {string} namePrefix
|
|
93
|
+
* @property {MetricOptions} options
|
|
94
|
+
* @property {Set<string>} keys
|
|
95
|
+
* @property {Record<string, number>} data
|
|
96
|
+
*/
|
|
97
|
+
/** @type {(namePrefix: string, description: string) => LazyStats} */
|
|
98
|
+
const makeLazyStats = (namePrefix, description) => {
|
|
99
|
+
return { namePrefix, options: { description }, keys: new Set(), data: {} };
|
|
100
|
+
};
|
|
101
|
+
const dynamicAfterCommitStatsCounters = {
|
|
102
|
+
memoryUsage: makeLazyStats(
|
|
103
|
+
'memoryUsage_',
|
|
104
|
+
'kernel process memory statistic',
|
|
105
|
+
),
|
|
106
|
+
heapStats: makeLazyStats('heapStats_', 'v8 kernel heap statistic'),
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const slogSender = ({ type: slogType, ...slogObj }) => {
|
|
110
|
+
switch (slogType) {
|
|
111
|
+
// Consume cosmic-swingset block lifecycle slog entries.
|
|
112
|
+
case 'cosmic-swingset-init': {
|
|
113
|
+
const { inboundQueueInitialLengths: lengths } = slogObj;
|
|
114
|
+
inboundQueueMetrics.initLengths(lengths);
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
case 'cosmic-swingset-begin-block': {
|
|
118
|
+
const {
|
|
119
|
+
interBlockSeconds,
|
|
120
|
+
afterCommitHangoverSeconds,
|
|
121
|
+
blockLagSeconds,
|
|
122
|
+
} = slogObj;
|
|
123
|
+
|
|
124
|
+
Number.isFinite(interBlockSeconds) &&
|
|
125
|
+
histograms.interBlockSeconds.record(interBlockSeconds);
|
|
126
|
+
histograms.afterCommitHangoverSeconds.record(
|
|
127
|
+
afterCommitHangoverSeconds,
|
|
128
|
+
);
|
|
129
|
+
Number.isFinite(blockLagSeconds) &&
|
|
130
|
+
histograms.blockLagSeconds.record(blockLagSeconds);
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
case 'cosmic-swingset-run-finish': {
|
|
134
|
+
histograms.swingset_block_processing_seconds.record(slogObj.seconds);
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
case 'cosmic-swingset-end-block-finish': {
|
|
138
|
+
const { inboundQueueStartLengths, processedActionCounts } = slogObj;
|
|
139
|
+
inboundQueueMetrics.updateLengths(inboundQueueStartLengths);
|
|
140
|
+
for (const processedActionRecord of processedActionCounts) {
|
|
141
|
+
const { count, phase, type: actionType } = processedActionRecord;
|
|
142
|
+
if (!knownActionTypes.has(actionType)) {
|
|
143
|
+
console.warn('Unknown inbound action type', actionType);
|
|
144
|
+
}
|
|
145
|
+
processedInboundActionCounter.add(count, { actionType });
|
|
146
|
+
inboundQueueMetrics.decLength(phase);
|
|
147
|
+
}
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
case 'cosmic-swingset-commit-block-finish': {
|
|
151
|
+
const {
|
|
152
|
+
runSeconds,
|
|
153
|
+
chainTime,
|
|
154
|
+
saveTime,
|
|
155
|
+
cosmosCommitSeconds,
|
|
156
|
+
fullSaveTime,
|
|
157
|
+
} = slogObj;
|
|
158
|
+
histograms.swingsetRunSeconds.record(runSeconds);
|
|
159
|
+
histograms.swingsetChainSaveSeconds.record(chainTime);
|
|
160
|
+
histograms.swingsetCommitSeconds.record(saveTime);
|
|
161
|
+
histograms.cosmosCommitSeconds.record(cosmosCommitSeconds);
|
|
162
|
+
histograms.fullCommitSeconds.record(fullSaveTime);
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Consume Swingset kernel slog entries.
|
|
167
|
+
case 'vat-startup-finish': {
|
|
168
|
+
histograms.swingset_vat_startup.record(slogObj.seconds * 1000);
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
case 'crank-finish': {
|
|
172
|
+
const { crankType, messageType, seconds } = slogObj;
|
|
173
|
+
// TODO: Reflect crankType/messageType as proper dimensional attributes.
|
|
174
|
+
// For now, we're going for parity with direct metrics.
|
|
175
|
+
if (crankType !== 'routing' && messageType !== 'create-vat') {
|
|
176
|
+
histograms.swingset_crank_processing_time.record(seconds * 1000);
|
|
177
|
+
}
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Consume miscellaneous slog entries.
|
|
182
|
+
case 'kernel-stats': {
|
|
183
|
+
const { stats } = slogObj;
|
|
184
|
+
const notYetFoundKernelStats = new Set(expectedKernelStats);
|
|
185
|
+
for (const [key, value] of Object.entries(stats)) {
|
|
186
|
+
notYetFoundKernelStats.delete(key);
|
|
187
|
+
if (!kernelStats.has(key)) {
|
|
188
|
+
console.warn('Unexpected SwingSet kernel statistic', key);
|
|
189
|
+
}
|
|
190
|
+
kernelStats.set(key, value);
|
|
191
|
+
}
|
|
192
|
+
if (notYetFoundKernelStats.size) {
|
|
193
|
+
console.warn('Expected SwingSet kernel statistics not found', [
|
|
194
|
+
...notYetFoundKernelStats,
|
|
195
|
+
]);
|
|
196
|
+
}
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
case 'cosmic-swingset-after-commit-stats': {
|
|
200
|
+
const dynamicCounterEntries = Object.entries(
|
|
201
|
+
dynamicAfterCommitStatsCounters,
|
|
202
|
+
);
|
|
203
|
+
for (const [slogKey, meta] of dynamicCounterEntries) {
|
|
204
|
+
const { namePrefix, options, keys } = meta;
|
|
205
|
+
meta.data = slogObj[slogKey] || {};
|
|
206
|
+
const newKeys = Object.keys(meta.data).filter(key => !keys.has(key));
|
|
207
|
+
for (const key of newKeys) {
|
|
208
|
+
keys.add(key);
|
|
209
|
+
const name = `${namePrefix}${key}`;
|
|
210
|
+
const gauge = otelMeter.createObservableUpDownCounter(
|
|
211
|
+
name,
|
|
212
|
+
options,
|
|
213
|
+
);
|
|
214
|
+
gauge.addCallback(observer => {
|
|
215
|
+
observer.observe(meta.data[key]);
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
default:
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
return Object.assign(slogSender, {
|
|
226
|
+
shutdown,
|
|
227
|
+
usesJsonObject: false,
|
|
228
|
+
});
|
|
229
|
+
};
|