Skip to content

Commit

Permalink
feat: logs signal support (#54)
Browse files Browse the repository at this point in the history
The Elastic SDK now adds a global Logger Provider.
mockotlpserver now has a 'logs-summary' printer to nicely show
received 'ExportLogsServiceRequest' data, and the 'json' printer
will normalize the service request data similar to as is being
done for Trace and Metrics service requests.

'examples/bunyan-logging-app.js' shows using a Bunyan logger with
'examples/start-elastic-sdk.js' which shows how to start the
Elastic distro SDK manually, including how to specify custom
instrumentations -- in this case the Bunyan instrumentation.

Refs: #41
  • Loading branch information
trentm authored Feb 7, 2024
1 parent 0c5d92d commit 8cf8831
Show file tree
Hide file tree
Showing 12 changed files with 957 additions and 74 deletions.
18 changes: 18 additions & 0 deletions examples/bunyan-logging-app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Usage:
// node -r @elastic/opentelemetry-node/start.js bunyan-logging-app.js

const path = require('path');
const otel = require('@opentelemetry/api');
const bunyan = require('bunyan');

const log = bunyan.createLogger({name: path.parse(__filename).name});

log.info({foo: 'bar'}, 'hi there');
log.warn(new Error('boom'), 'oops');
log.debug('hi at debug-level');

const tracer = otel.trace.getTracer('example');
tracer.startActiveSpan('manual-span', (span) => {
log.info('this record will have trace-correlation fields');
span.end();
});
4 changes: 3 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
"private": true,
"version": "0.1.0",
"scripts": {},
"dependencies": {
"devDependencies": {
"@elastic/opentelemetry-node": "*",
"@opentelemetry/instrumentation-bunyan": "^0.35.0",
"bunyan": "^1.8.15",
"express": "^4.18.2"
}
}
49 changes: 49 additions & 0 deletions examples/start-elastic-sdk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Setup and start the Elastic OpenTelemetry Node.js SDK distro.
*
* This is an alternative to the `node -r @elastic/opentelemetry-node/start.js`
* convenience for starting the SDK. Starting the SDK manually via a local
* file can be useful to allow configuring the SDK with code.
*
* Usage:
* node -r ./start-elastic-sdk.js SCRIPT.js
*/

const path = require('path');

const {ElasticNodeSDK} = require('@elastic/opentelemetry-node');

// TODO: remove BunyanInstrumentation manual usage when the distro includes it by default
const {
BunyanInstrumentation,
} = require('@opentelemetry/instrumentation-bunyan');

const sdk = new ElasticNodeSDK({
serviceName: path.parse(process.argv[1]).name,
instrumentations: [
// One can **override** the instrumentations provided by ElasticNodeSDK
// by specifying `instrumentations`. How to **extended** the set of
// instrumentations provided by ElasticNodeSDK is to be determined (TODO).
new BunyanInstrumentation(),
],
});

process.on('SIGTERM', async () => {
try {
await sdk.shutdown();
} catch (err) {
console.warn('warning: error shutting down OTel SDK', err);
}
process.exit();
});

process.once('beforeExit', async () => {
// Flush recent telemetry data if about the shutdown.
try {
await sdk.shutdown();
} catch (err) {
console.warn('warning: error shutting down OTel SDK', err);
}
});

sdk.start();
Loading

0 comments on commit 8cf8831

Please sign in to comment.