Skip to content

Commit bda74d0

Browse files
authored
feat(mockotlpserver): 'spacer' printer to help view the output slightly (#933)
1 parent 868115c commit bda74d0

3 files changed

Lines changed: 49 additions & 5 deletions

File tree

packages/mockotlpserver/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
- feat: Add a 'spacer' printer, and add it to the default set.
6+
This will print a blank line if it has been a "while" (currently 1s) since
7+
the last printed output. This helps in viewing the printed output.
8+
E.g. `node lib/cli.js -o summary,spacer`.
9+
510
- chore: Excluding devDeps from Docker images should make them smaller.
611

712
- feat: Adds a log.debug() for every incoming HTTP and gRPC request.

packages/mockotlpserver/lib/cli.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ const os = require('os');
1313
const dashdash = require('dashdash');
1414

1515
const luggite = require('./luggite');
16-
const {JSONPrinter, InspectPrinter, FilePrinter} = require('./printers');
16+
const {
17+
JSONPrinter,
18+
InspectPrinter,
19+
FilePrinter,
20+
SpacerPrinter,
21+
} = require('./printers');
1722
const {TraceWaterfallPrinter} = require('./waterfall');
1823
const {MetricsSummaryPrinter} = require('./metrics-summary');
1924
const {LogsSummaryPrinter} = require('./logs-summary');
@@ -40,6 +45,7 @@ const PRINTER_NAMES = [
4045
'logs-summary',
4146
'summary',
4247

48+
'spacer', // print a blank line between printed outputs separated by a time gap
4349
'trace-file', // saving into fs for UI and other processing
4450
];
4551

@@ -91,9 +97,9 @@ const OPTIONS = [
9197
{
9298
names: ['o'],
9399
type: 'arrayOfPrinters',
94-
help: `Output formats for printing OTLP data. Comma-separated, one or more of "${PRINTER_NAMES.join(
95-
'", "'
96-
)}". Default: "inspect,summary"`,
100+
help: `Output formats for printing OTLP data. Comma-separated, one or more of: ${PRINTER_NAMES.join(
101+
', '
102+
)}. Default: "inspect,summary,spacer".`,
97103
},
98104
{
99105
names: ['hostname'],
@@ -139,7 +145,7 @@ async function main() {
139145
// The way dashdash `--help` output prints the default of an array is
140146
// misleading, so we'll apply the default here and manually document
141147
// the default in the "help:" string above.
142-
opts.o = ['inspect', 'summary'];
148+
opts.o = ['inspect', 'summary', 'spacer'];
143149
}
144150

145151
/** @type {Array<'http'|'grpc'|'ui'>} */
@@ -222,6 +228,9 @@ async function main() {
222228
printers.push(new LogsSummaryPrinter(log));
223229
break;
224230

231+
case 'spacer':
232+
printers.push(new SpacerPrinter(log));
233+
break;
225234
case 'trace-file':
226235
printers.push(new FilePrinter(log));
227236
break;

packages/mockotlpserver/lib/printers.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,39 @@ class FilePrinter extends Printer {
196196
}
197197
}
198198

199+
/**
200+
* If there has been a `_timeGap` since the last printable-event, then print
201+
* a blank line in the console output to provide some visual spacing. This
202+
* makes the printed output easier to reason about.
203+
*/
204+
class SpacerPrinter extends Printer {
205+
constructor(log) {
206+
super(log);
207+
this._lastPrintTime = Date.now();
208+
this._timeGap = 1000;
209+
}
210+
_handleGap() {
211+
const now = Date.now();
212+
if (now - this._lastPrintTime > this._timeGap) {
213+
console.log(); // blank line spacing between earlier group
214+
}
215+
this._lastPrintTime = now;
216+
}
217+
printTrace(_) {
218+
this._handleGap();
219+
}
220+
printMetrics(_) {
221+
this._handleGap();
222+
}
223+
printLogs(_) {
224+
this._handleGap();
225+
}
226+
}
227+
199228
module.exports = {
200229
Printer,
201230
JSONPrinter,
202231
InspectPrinter,
203232
FilePrinter,
233+
SpacerPrinter,
204234
};

0 commit comments

Comments
 (0)