@agoric/telemetry 0.6.3-upgrade-17-dev-a1453b2.0 → 0.6.3-upgrade-18-dev-d7c994b.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/CHANGELOG.md CHANGED
@@ -3,22 +3,6 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
- ### [0.6.3-u17.0](https://github.com/Agoric/agoric-sdk/compare/@agoric/telemetry@0.6.2...@agoric/telemetry@0.6.3-u17.0) (2024-09-17)
7
-
8
-
9
- ### Features
10
-
11
- * **cosmic-swingset:** add JS upgrade plan handler stub ([655133e](https://github.com/Agoric/agoric-sdk/commit/655133ed909b5d632dc033e992214a7b6a1b5ab1))
12
- * simple CircularBuffer with fs offsets ([8d9cb7a](https://github.com/Agoric/agoric-sdk/commit/8d9cb7abe96e8905f5aaa0927e02914ef09279c4))
13
- * use writeSync slogSender ([47a2add](https://github.com/Agoric/agoric-sdk/commit/47a2adda72a5377eda181a425130cdc5a7fd7ff5))
14
-
15
-
16
- ### Bug Fixes
17
-
18
- * ensure script main rejections exit with error ([abdab87](https://github.com/Agoric/agoric-sdk/commit/abdab879014a5c3124ebd0e9246995ac6b1ce6e5))
19
-
20
-
21
-
22
6
  ### [0.6.2](https://github.com/Agoric/agoric-sdk/compare/@agoric/telemetry@0.6.1...@agoric/telemetry@0.6.2) (2023-06-02)
23
7
 
24
8
  **Note:** Version bump only for package @agoric/telemetry
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agoric/telemetry",
3
- "version": "0.6.3-upgrade-17-dev-a1453b2.0+a1453b2",
3
+ "version": "0.6.3-upgrade-18-dev-d7c994b.0+d7c994b",
4
4
  "description": "Agoric's telemetry implementation",
5
5
  "type": "module",
6
6
  "repository": "https://github.com/Agoric/agoric-sdk",
@@ -22,26 +22,29 @@
22
22
  "author": "Agoric",
23
23
  "license": "Apache-2.0",
24
24
  "dependencies": {
25
- "@agoric/internal": "0.4.0-upgrade-17-dev-a1453b2.0+a1453b2",
26
- "@agoric/store": "0.9.3-upgrade-17-dev-a1453b2.0+a1453b2",
27
- "@endo/errors": "^1.2.5",
28
- "@endo/init": "^1.1.4",
29
- "@endo/marshal": "^1.5.3",
30
- "@endo/stream": "^1.2.5",
25
+ "@agoric/internal": "0.3.3-upgrade-18-dev-d7c994b.0+d7c994b",
26
+ "@agoric/store": "0.9.3-upgrade-18-dev-d7c994b.0+d7c994b",
27
+ "@endo/errors": "^1.2.7",
28
+ "@endo/init": "^1.1.6",
29
+ "@endo/marshal": "^1.6.1",
30
+ "@endo/stream": "^1.2.7",
31
31
  "@opentelemetry/api": "~1.3.0",
32
+ "@opentelemetry/api-logs": "0.53.0",
33
+ "@opentelemetry/exporter-logs-otlp-http": "0.53.0",
32
34
  "@opentelemetry/exporter-prometheus": "~0.35.0",
33
35
  "@opentelemetry/exporter-trace-otlp-http": "~0.35.0",
34
36
  "@opentelemetry/resources": "~1.9.0",
37
+ "@opentelemetry/sdk-logs": "0.53.0",
35
38
  "@opentelemetry/sdk-metrics": "~1.9.0",
36
39
  "@opentelemetry/sdk-trace-base": "~1.9.0",
37
- "@opentelemetry/semantic-conventions": "~1.9.0",
40
+ "@opentelemetry/semantic-conventions": "~1.27.0",
38
41
  "anylogger": "^0.21.0",
39
42
  "better-sqlite3": "^9.1.1",
40
43
  "tmp": "^0.2.1"
41
44
  },
42
45
  "devDependencies": {
43
- "@endo/lockdown": "^1.0.10",
44
- "@endo/ses-ava": "^1.2.5",
46
+ "@endo/lockdown": "^1.0.12",
47
+ "@endo/ses-ava": "^1.2.7",
45
48
  "ava": "^5.3.0",
46
49
  "c8": "^9.1.0",
47
50
  "tmp": "^0.2.1"
@@ -65,5 +68,5 @@
65
68
  "typeCoverage": {
66
69
  "atLeast": 87.03
67
70
  },
68
- "gitHead": "a1453b2877b017a7f5b43a92364067d001901953"
71
+ "gitHead": "d7c994b8d33c0cd22b257f3e33b579588ab6c6d8"
69
72
  }
@@ -0,0 +1,42 @@
1
+ /* eslint-env node */
2
+
3
+ import { makeFsStreamWriter } from '@agoric/internal/src/node/fs-stream.js';
4
+ import { makeContextualSlogProcessor } from './context-aware-slog.js';
5
+ import { serializeSlogObj } from './serialize-slog-obj.js';
6
+
7
+ /**
8
+ * @param {import('./index.js').MakeSlogSenderOptions} options
9
+ */
10
+ export const makeSlogSender = async options => {
11
+ const { CHAIN_ID, CONTEXTUAL_SLOGFILE } = options.env || {};
12
+ if (!CONTEXTUAL_SLOGFILE)
13
+ return console.warn(
14
+ 'Ignoring invocation of slogger "context-aware-slog-file" without the presence of "CONTEXTUAL_SLOGFILE"',
15
+ );
16
+
17
+ const stream = await makeFsStreamWriter(CONTEXTUAL_SLOGFILE);
18
+
19
+ if (!stream)
20
+ return console.error(
21
+ `Couldn't create a write stream on file "${CONTEXTUAL_SLOGFILE}"`,
22
+ );
23
+
24
+ const contextualSlogProcessor = makeContextualSlogProcessor({
25
+ 'chain-id': CHAIN_ID,
26
+ });
27
+
28
+ /**
29
+ * @param {import('./context-aware-slog.js').Slog} slog
30
+ */
31
+ const slogSender = slog => {
32
+ const contextualizedSlog = contextualSlogProcessor(slog);
33
+
34
+ // eslint-disable-next-line prefer-template
35
+ stream.write(serializeSlogObj(contextualizedSlog) + '\n').catch(() => {});
36
+ };
37
+
38
+ return Object.assign(slogSender, {
39
+ forceFlush: () => stream.flush(),
40
+ shutdown: () => stream.close(),
41
+ });
42
+ };
@@ -0,0 +1,345 @@
1
+ /* eslint-env node */
2
+
3
+ /**
4
+ * @typedef {Partial<{
5
+ * 'block.height': Slog['blockHeight'];
6
+ * 'block.time': Slog['blockTime'];
7
+ * 'crank.deliveryNum': Slog['deliveryNum'];
8
+ * 'crank.num': Slog['crankNum'];
9
+ * 'crank.type': Slog['crankType'];
10
+ * 'crank.vatID': Slog['vatID'];
11
+ * init: boolean;
12
+ * replay: boolean;
13
+ * 'run.id': string;
14
+ * 'run.num': string | null;
15
+ * 'run.trigger.blockHeight': Slog['blockHeight'];
16
+ * 'run.trigger.msgIdx': number;
17
+ * 'run.trigger.sender': Slog['sender'];
18
+ * 'run.trigger.source': Slog['source'];
19
+ * 'run.trigger.time': Slog['blockTime'];
20
+ * 'run.trigger.txHash': string;
21
+ * 'run.trigger.type': string;
22
+ * }>
23
+ * } Context
24
+ *
25
+ * @typedef {{
26
+ * 'crank.syscallNum'?: Slog['syscallNum'];
27
+ * 'process.uptime': Slog['monotime'];
28
+ * } & Context} LogAttributes
29
+ *
30
+ * @typedef {{
31
+ * blockHeight?: number;
32
+ * blockTime?: number;
33
+ * crankNum?: bigint;
34
+ * crankType?: string;
35
+ * deliveryNum?: bigint;
36
+ * inboundNum?: string;
37
+ * monotime: number;
38
+ * remainingBeans?: bigint;
39
+ * replay?: boolean;
40
+ * runNum?: number;
41
+ * sender?: string;
42
+ * source?: string;
43
+ * syscallNum?: number;
44
+ * time: number;
45
+ * type: string;
46
+ * vatID?: string;
47
+ * }} Slog
48
+ */
49
+
50
+ const SLOG_TYPES = {
51
+ CLIST: 'clist',
52
+ CONSOLE: 'console',
53
+ COSMIC_SWINGSET: {
54
+ AFTER_COMMIT_STATS: 'cosmic-swingset-after-commit-stats',
55
+ BEGIN_BLOCK: 'cosmic-swingset-begin-block',
56
+ BOOTSTRAP_BLOCK: {
57
+ FINISH: 'cosmic-swingset-bootstrap-block-finish',
58
+ START: 'cosmic-swingset-bootstrap-block-start',
59
+ },
60
+ BRIDGE_INBOUND: 'cosmic-swingset-bridge-inbound',
61
+ COMMIT: {
62
+ FINISH: 'cosmic-swingset-commit-finish',
63
+ START: 'cosmic-swingset-commit-start',
64
+ },
65
+ DELIVER_INBOUND: 'cosmic-swingset-deliver-inbound',
66
+ END_BLOCK: {
67
+ FINISH: 'cosmic-swingset-end-block-finish',
68
+ START: 'cosmic-swingset-end-block-start',
69
+ },
70
+ // eslint-disable-next-line no-restricted-syntax
71
+ RUN: {
72
+ FINISH: 'cosmic-swingset-run-finish',
73
+ START: 'cosmic-swingset-run-start',
74
+ },
75
+ },
76
+ CRANK: {
77
+ FINISH: 'crank-finish',
78
+ START: 'crank-start',
79
+ },
80
+ DELIVER: 'deliver',
81
+ DELIVER_RESULT: 'deliver-result',
82
+ KERNEL: {
83
+ INIT: {
84
+ FINISH: 'kernel-init-finish',
85
+ START: 'kernel-init-start',
86
+ },
87
+ },
88
+ REPLAY: {
89
+ FINISH: 'finish-replay',
90
+ START: 'start-replay',
91
+ },
92
+ SYSCALL: 'syscall',
93
+ SYSCALL_RESULT: 'syscall-result',
94
+ };
95
+
96
+ /**
97
+ * @template {Record<string, any>} [T={}]
98
+ * @param {T} [staticContext]
99
+ * @param {Partial<{ persistContext: (context: Context) => void; restoreContext: () => Context | null; }>} [persistenceUtils]
100
+ */
101
+ export const makeContextualSlogProcessor = (
102
+ staticContext,
103
+ persistenceUtils = {},
104
+ ) => {
105
+ /** @type Array<Context | null> */
106
+ let [
107
+ blockContext,
108
+ crankContext,
109
+ initContext,
110
+ lastPersistedTriggerContext,
111
+ replayContext,
112
+ triggerContext,
113
+ ] = [null, null, null, null, null, null];
114
+
115
+ /**
116
+ * @param {Context} context
117
+ */
118
+ const persistContext = context => {
119
+ lastPersistedTriggerContext = context;
120
+ return persistenceUtils?.persistContext?.(context);
121
+ };
122
+
123
+ const restoreContext = () => {
124
+ if (!lastPersistedTriggerContext)
125
+ lastPersistedTriggerContext =
126
+ persistenceUtils?.restoreContext?.() || null;
127
+ return lastPersistedTriggerContext;
128
+ };
129
+
130
+ /**
131
+ * @param {Slog} slog
132
+ * @returns {{ attributes: T & LogAttributes, body: Partial<Slog>; timestamp: Slog['time'] }}
133
+ */
134
+ const slogProcessor = ({ monotime, time: timestamp, ...body }) => {
135
+ const finalBody = { ...body };
136
+
137
+ /** @type {{'crank.syscallNum'?: Slog['syscallNum']}} */
138
+ const eventLogAttributes = {};
139
+
140
+ /**
141
+ * Add any before report operations here
142
+ * like setting context data
143
+ */
144
+ switch (body.type) {
145
+ case SLOG_TYPES.KERNEL.INIT.START: {
146
+ initContext = { init: true };
147
+ break;
148
+ }
149
+ case SLOG_TYPES.COSMIC_SWINGSET.BEGIN_BLOCK: {
150
+ blockContext = {
151
+ 'block.height': finalBody.blockHeight,
152
+ 'block.time': finalBody.blockTime,
153
+ };
154
+ break;
155
+ }
156
+ case SLOG_TYPES.COSMIC_SWINGSET.BOOTSTRAP_BLOCK.START: {
157
+ blockContext = {
158
+ 'block.height': finalBody.blockHeight || 0,
159
+ 'block.time': finalBody.blockTime,
160
+ };
161
+ break;
162
+ }
163
+ case SLOG_TYPES.COSMIC_SWINGSET.END_BLOCK.START:
164
+ case SLOG_TYPES.COSMIC_SWINGSET.END_BLOCK.FINISH:
165
+ case SLOG_TYPES.COSMIC_SWINGSET.BOOTSTRAP_BLOCK.FINISH:
166
+ case SLOG_TYPES.COSMIC_SWINGSET.COMMIT.START:
167
+ case SLOG_TYPES.COSMIC_SWINGSET.COMMIT.FINISH:
168
+ case SLOG_TYPES.COSMIC_SWINGSET.AFTER_COMMIT_STATS: {
169
+ assert(!!blockContext && !triggerContext);
170
+ break;
171
+ }
172
+ case SLOG_TYPES.COSMIC_SWINGSET.BRIDGE_INBOUND:
173
+ case SLOG_TYPES.COSMIC_SWINGSET.DELIVER_INBOUND: {
174
+ const [blockHeight, txHash, msgIdx] = (
175
+ finalBody.inboundNum || ''
176
+ ).split('-');
177
+ const [, triggerType] =
178
+ /cosmic-swingset-([^-]+)-inbound/.exec(body.type) || [];
179
+
180
+ triggerContext = {
181
+ 'run.num': undefined,
182
+ 'run.id': `${triggerType}-${finalBody.inboundNum}`,
183
+ 'run.trigger.type': triggerType,
184
+ 'run.trigger.source': finalBody.source,
185
+ 'run.trigger.sender': finalBody.sender,
186
+ 'run.trigger.blockHeight': Number(blockHeight),
187
+ 'run.trigger.txHash': txHash,
188
+ 'run.trigger.msgIdx': Number(msgIdx),
189
+ };
190
+ break;
191
+ }
192
+ // eslint-disable-next-line no-restricted-syntax
193
+ case SLOG_TYPES.COSMIC_SWINGSET.RUN.START: {
194
+ if (!finalBody.runNum) {
195
+ assert(!triggerContext);
196
+ triggerContext = restoreContext(); // Restore persisted context if any
197
+ } else if (!triggerContext) {
198
+ assert(!!blockContext);
199
+ // TODO: add explicit slog events of both timer poll and install bundle
200
+ // https://github.com/Agoric/agoric-sdk/issues/10332
201
+ triggerContext = {
202
+ 'run.num': undefined,
203
+ 'run.id': `unknown-${finalBody.blockHeight}-${finalBody.runNum}`,
204
+ 'run.trigger.type': 'unknown',
205
+ 'run.trigger.blockHeight': finalBody.blockHeight,
206
+ };
207
+ }
208
+
209
+ if (!triggerContext) triggerContext = {};
210
+ triggerContext['run.num'] = `${finalBody.runNum}`;
211
+
212
+ break;
213
+ }
214
+ case SLOG_TYPES.CRANK.START: {
215
+ crankContext = {
216
+ 'crank.num': finalBody.crankNum,
217
+ 'crank.type': finalBody.crankType,
218
+ };
219
+ break;
220
+ }
221
+ case SLOG_TYPES.CLIST: {
222
+ assert(!!crankContext);
223
+ crankContext['crank.vatID'] = finalBody.vatID;
224
+ break;
225
+ }
226
+ case SLOG_TYPES.REPLAY.START:
227
+ case SLOG_TYPES.REPLAY.FINISH: {
228
+ replayContext = { replay: true, 'crank.vatID': finalBody.vatID };
229
+ break;
230
+ }
231
+ case SLOG_TYPES.DELIVER: {
232
+ if (replayContext) {
233
+ assert(finalBody.replay);
234
+ replayContext = {
235
+ ...replayContext,
236
+ 'crank.vatID': finalBody.vatID,
237
+ 'crank.deliveryNum': finalBody.deliveryNum,
238
+ };
239
+ } else {
240
+ assert(!!crankContext && !finalBody.replay);
241
+ crankContext = {
242
+ ...crankContext,
243
+ 'crank.vatID': finalBody.vatID,
244
+ 'crank.deliveryNum': finalBody.deliveryNum,
245
+ };
246
+ }
247
+
248
+ delete finalBody.deliveryNum;
249
+ delete finalBody.replay;
250
+
251
+ break;
252
+ }
253
+ case SLOG_TYPES.DELIVER_RESULT: {
254
+ delete finalBody.deliveryNum;
255
+ delete finalBody.replay;
256
+
257
+ break;
258
+ }
259
+ case SLOG_TYPES.SYSCALL:
260
+ case SLOG_TYPES.SYSCALL_RESULT: {
261
+ eventLogAttributes['crank.syscallNum'] = finalBody.syscallNum;
262
+
263
+ delete finalBody.deliveryNum;
264
+ delete finalBody.replay;
265
+ delete finalBody.syscallNum;
266
+
267
+ break;
268
+ }
269
+ case SLOG_TYPES.CONSOLE: {
270
+ delete finalBody.crankNum;
271
+ delete finalBody.deliveryNum;
272
+
273
+ break;
274
+ }
275
+ default:
276
+ // All other log types are logged as is (using existing contexts) without
277
+ // any change to the slogs or any contributions to the contexts. This also
278
+ // means that any unexpected slog type will pass through. To fix that, add
279
+ // all remaining cases of expected slog types above with a simple break
280
+ // statement and log a warning here
281
+ break;
282
+ }
283
+
284
+ const logAttributes = {
285
+ ...staticContext,
286
+ 'process.uptime': monotime,
287
+ ...initContext, // Optional prelude
288
+ ...blockContext, // Block is the first level of execution nesting
289
+ ...triggerContext, // run and trigger info is nested next
290
+ ...crankContext, // Finally cranks are the last level of nesting
291
+ ...replayContext, // Replay is a substitute for crank context during vat page in
292
+ ...eventLogAttributes,
293
+ };
294
+
295
+ /**
296
+ * Add any after report operations here
297
+ * like resetting context data
298
+ */
299
+ switch (body.type) {
300
+ case SLOG_TYPES.KERNEL.INIT.FINISH: {
301
+ initContext = null;
302
+ break;
303
+ }
304
+ case SLOG_TYPES.COSMIC_SWINGSET.BOOTSTRAP_BLOCK.START: {
305
+ triggerContext = {
306
+ 'run.num': undefined,
307
+ 'run.id': `bootstrap-${finalBody.blockTime}`,
308
+ 'run.trigger.type': 'bootstrap',
309
+ 'run.trigger.time': finalBody.blockTime,
310
+ };
311
+ break;
312
+ }
313
+ case SLOG_TYPES.COSMIC_SWINGSET.AFTER_COMMIT_STATS:
314
+ case SLOG_TYPES.COSMIC_SWINGSET.BOOTSTRAP_BLOCK.FINISH: {
315
+ blockContext = null;
316
+ break;
317
+ }
318
+ // eslint-disable-next-line no-restricted-syntax
319
+ case SLOG_TYPES.COSMIC_SWINGSET.RUN.FINISH: {
320
+ assert(!!triggerContext);
321
+ persistContext(finalBody.remainingBeans ? {} : triggerContext);
322
+ triggerContext = null;
323
+ break;
324
+ }
325
+ case SLOG_TYPES.CRANK.FINISH: {
326
+ crankContext = null;
327
+ break;
328
+ }
329
+ case SLOG_TYPES.REPLAY.FINISH: {
330
+ replayContext = null;
331
+ break;
332
+ }
333
+ default:
334
+ break;
335
+ }
336
+
337
+ return {
338
+ attributes: /** @type {T & LogAttributes} */ (logAttributes),
339
+ body: finalBody,
340
+ timestamp,
341
+ };
342
+ };
343
+
344
+ return slogProcessor;
345
+ };
@@ -1,5 +1,5 @@
1
1
  // @ts-check
2
- /* global Buffer */
2
+ /* eslint-env node */
3
3
  /// <reference types="ses" />
4
4
 
5
5
  import fs from 'node:fs';
@@ -1,5 +1,5 @@
1
1
  #! /usr/bin/env node
2
- /* global process */
2
+ /* eslint-env node */
3
3
  // frcat - print out the contents of a flight recorder
4
4
  // NOTE: this only works on inactive recorder files where the writer has terminated
5
5
 
@@ -1,5 +1,5 @@
1
1
  #! /usr/bin/env node
2
- /* global setTimeout */
2
+ /* eslint-env node */
3
3
  import '@endo/init';
4
4
 
5
5
  import fs from 'fs';
@@ -11,7 +11,8 @@ import { makeSlogSender } from './make-slog-sender.js';
11
11
 
12
12
  const LINE_COUNT_TO_FLUSH = 10000;
13
13
  const ELAPSED_MS_TO_FLUSH = 3000;
14
- const MAX_LINE_COUNT_PER_PERIOD = 1000;
14
+ const MAX_LINE_COUNT_PER_PERIOD = 10000;
15
+ const MAX_BLOCKS_PER_PERIOD = 10;
15
16
  const PROCESSING_PERIOD = 1000;
16
17
 
17
18
  async function run() {
@@ -29,7 +30,7 @@ async function run() {
29
30
  return;
30
31
  }
31
32
 
32
- const [slogFile] = args;
33
+ const slogFile = args[0] === '-' ? undefined : args[0];
33
34
  const slogSender = await makeSlogSender({
34
35
  serviceName,
35
36
  stateDir: '.',
@@ -56,14 +57,21 @@ async function run() {
56
57
  const lines = readline.createInterface({ input: slogF });
57
58
  const slogFileName = slogFile || '*stdin*';
58
59
 
59
- const progressFileName = `${slogFileName}.ingest-progress`;
60
- if (!fs.existsSync(progressFileName)) {
61
- const progress = { virtualTimeOffset: 0, lastSlogTime: 0 };
62
- fs.writeFileSync(progressFileName, JSON.stringify(progress));
60
+ const progressFileName = slogFile && `${slogFileName}.ingest-progress`;
61
+ const progress = { virtualTimeOffset: 0, lastSlogTime: 0 };
62
+ if (progressFileName) {
63
+ if (!fs.existsSync(progressFileName)) {
64
+ fs.writeFileSync(progressFileName, JSON.stringify(progress));
65
+ } else {
66
+ Object.assign(
67
+ progress,
68
+ JSON.parse(fs.readFileSync(progressFileName).toString()),
69
+ );
70
+ }
63
71
  }
64
- const progress = JSON.parse(fs.readFileSync(progressFileName).toString());
65
72
 
66
73
  let linesProcessedThisPeriod = 0;
74
+ let blocksInThisPeriod = 0;
67
75
  let startOfLastPeriod = 0;
68
76
 
69
77
  let lastTime = Date.now();
@@ -75,10 +83,12 @@ async function run() {
75
83
  return;
76
84
  }
77
85
  await slogSender.forceFlush?.();
78
- fs.writeFileSync(progressFileName, JSON.stringify(progress));
86
+ if (progressFileName) {
87
+ fs.writeFileSync(progressFileName, JSON.stringify(progress));
88
+ }
79
89
  };
80
90
 
81
- console.log(`parsing`, slogFileName);
91
+ console.warn(`parsing`, slogFileName);
82
92
 
83
93
  let update = false;
84
94
  const maybeUpdateStats = async now => {
@@ -106,9 +116,14 @@ async function run() {
106
116
  continue;
107
117
  }
108
118
 
119
+ const isAfterCommit = obj.type === 'cosmic-swingset-after-commit-stats';
120
+
109
121
  // Maybe wait for the next period to process a bunch of lines.
110
122
  let maybeWait;
111
- if (linesProcessedThisPeriod >= MAX_LINE_COUNT_PER_PERIOD) {
123
+ if (
124
+ linesProcessedThisPeriod >= MAX_LINE_COUNT_PER_PERIOD ||
125
+ blocksInThisPeriod >= MAX_BLOCKS_PER_PERIOD
126
+ ) {
112
127
  const delayMS = PROCESSING_PERIOD - (now - startOfLastPeriod);
113
128
  maybeWait = new Promise(resolve => setTimeout(resolve, delayMS));
114
129
  }
@@ -118,8 +133,8 @@ async function run() {
118
133
  if (now - startOfLastPeriod >= PROCESSING_PERIOD) {
119
134
  startOfLastPeriod = now;
120
135
  linesProcessedThisPeriod = 0;
136
+ blocksInThisPeriod = 0;
121
137
  }
122
- linesProcessedThisPeriod += 1;
123
138
 
124
139
  if (progress.virtualTimeOffset) {
125
140
  const virtualTime = obj.time + progress.virtualTimeOffset;
@@ -133,10 +148,17 @@ async function run() {
133
148
  // Use the original.
134
149
  slogSender(obj);
135
150
  }
151
+
152
+ linesProcessedThisPeriod += 1;
153
+ if (isAfterCommit) {
154
+ blocksInThisPeriod += 1;
155
+ lastTime = Date.now();
156
+ await stats(true);
157
+ }
136
158
  }
137
159
 
138
160
  await stats(true);
139
- console.log(
161
+ console.warn(
140
162
  `done parsing`,
141
163
  slogFileName,
142
164
  `(${lineCount} lines, ${byteCount} bytes)`,
@@ -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 { timestamp, ...logRecord } = contextualSlogProcessor(slog);
86
+
87
+ const [secondsStr, fractionStr] = String(timestamp).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
+ };
package/src/slog-file.js CHANGED
@@ -11,7 +11,7 @@ export const makeSlogSender = async ({ env: { SLOGFILE } = {} } = {}) => {
11
11
 
12
12
  const slogSender = (slogObj, jsonObj = serializeSlogObj(slogObj)) => {
13
13
  // eslint-disable-next-line prefer-template
14
- void stream.write(jsonObj + '\n');
14
+ stream.write(jsonObj + '\n').catch(() => {});
15
15
  };
16
16
 
17
17
  return Object.assign(slogSender, {
@@ -1,4 +1,4 @@
1
- /* global process */
1
+ /* eslint-env node */
2
2
  import '@endo/init';
3
3
 
4
4
  import anylogger from 'anylogger';