Skip to content

Commit

Permalink
release @elastic/[email protected] (#358)
Browse files Browse the repository at this point in the history
This fixes the CLI script so starting via npx works.
This also adds the '-l, --log-level LEVEL' CLI option.

Also add opts.logLevel to MockOtlpServer to simplify making it quiet for testing.
  • Loading branch information
trentm authored Sep 6, 2024
1 parent f603473 commit e8928b1
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 14 deletions.
6 changes: 6 additions & 0 deletions packages/mockotlpserver/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @elastic/mockotlpserver Changelog

## v0.5.0

- fix: Add shebang line to the CLI script so `npx @elastic/mockotlpserver` works.
- feat: Add `--log-level, -l LEVEL` option. E.g. `mockotlpserver -l warn` makes startup silent.
Also add a `logLevel` option to the `MockOtlpServer` class for module usage.

## v0.4.1

(First version published to npm.)
Expand Down
25 changes: 21 additions & 4 deletions packages/mockotlpserver/lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env node

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. See the NOTICE file distributed with
Expand Down Expand Up @@ -93,13 +95,18 @@ const OPTIONS = [
type: 'bool',
help: 'Print this help and exit.',
},
{
names: ['log-level', 'l'],
type: 'string',
help: `Set the log level to one of "trace", "debug", "info", "warn", "error", "fatal".`,
default: 'info',
},
{
names: ['o'],
type: 'arrayOfPrinters',
help: `Formats for printing OTLP data. One or more of "${PRINTER_NAMES.join(
help: `Output formats for printing OTLP data. Comma-separated, one or more of "${PRINTER_NAMES.join(
'", "'
)}".`,
default: ['inspect', 'summary'],
)}". Default: "inspect,summary"`,
},
{
names: ['hostname'],
Expand All @@ -124,16 +131,24 @@ async function main() {
log.error({err}, `${CMD}: command-line options error`);
process.exit(1);
}
log.level(opts.log_level);
if (opts.help) {
var help = parser.help({includeDefault: true}).trimRight();
console.log(
'Usage:\n' +
' node lib/mockotlpserver.js [OPTIONS]\n' +
' npx @elastic/mockotlpserver [OPTIONS]\n' +
' mockotlpserver [OPTIONS] # if installed globally\n' +
'Options:\n' +
help
);
process.exit(0);
}
if (!opts.o) {
// The way dashdash `--help` output prints the default of an array is
// misleading, so we'll apply the default here and manually document
// the default in the "help:" string above.
opts.o = ['inspect', 'summary'];
}

/** @type {Array<'http'|'grpc'|'ui'>} */
const services = ['http', 'grpc'];
Expand Down Expand Up @@ -220,6 +235,8 @@ async function main() {
}
});
printers.forEach((p) => p.subscribe());

log.trace({cliOpts: opts}, 'started');
}

main();
7 changes: 7 additions & 0 deletions packages/mockotlpserver/lib/mockotlpserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class MockOtlpServer {
/**
* @param {object} [opts]
* @param {import('./luggite').Logger} [opts.log]
* @param {string} [opts.logLevel] Optionally change the log level. This
* accepts any of the log level names supported by luggite. Typically
* one would use opts.log *or* opts.logLevel. The latter enables
* tweaking the log level without having to pass in a custom logger.
* @param {Array<'http'|'grpc'|'ui'>} [opts.services] Zero or more of 'http', 'grpc',
* and 'ui'. If not provided, then defaults to starting all services.
* @param {string} [opts.httpHostname] Default 'localhost'.
Expand All @@ -76,6 +80,9 @@ class MockOtlpServer {
constructor(opts) {
opts = opts ?? {};
this._log = opts.log ?? luggite.createLogger({name: 'mockotlpserver'});
if (opts.logLevel != null) {
this._log.level(opts.logLevel);
}
this._services = opts.services ?? ['http', 'grpc', 'ui'];
this._httpHostname = opts.httpHostname ?? DEFAULT_HOSTNAME;
this._httpPort = opts.httpPort ?? DEFAULT_HTTP_PORT;
Expand Down
4 changes: 2 additions & 2 deletions packages/mockotlpserver/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/mockotlpserver/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@elastic/mockotlpserver",
"version": "0.4.1",
"version": "0.5.0",
"type": "commonjs",
"description": "A mock OTLP server, useful for dev and testing",
"publishConfig": {
Expand Down
8 changes: 1 addition & 7 deletions packages/opentelemetry-node/test/testutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ const {
normalizeMetrics,
} = require('@elastic/mockotlpserver');

const luggite = require('../lib/luggite');

/**
* Filter out instr-dns and instr-net spans for testing.
* Eventually it would be preferable to have each test run with instr-dns
Expand Down Expand Up @@ -534,11 +532,7 @@ function runTestFixtures(suite, testFixtures) {

const collector = new TestCollector();
const otlpServer = new MockOtlpServer({
// Pass in a logger solely to reduce the level to warn.
log: luggite.createLogger({
name: 'mockotlpserver',
level: 'warn',
}),
logLevel: 'warn',
services: ['http'],
httpHostname: '127.0.0.1', // avoid default 'localhost' because possible IPv6
httpPort: 0,
Expand Down

0 comments on commit e8928b1

Please sign in to comment.