This example uses the OpenTelemetry JavaScript SDK to capture Bunyan logging calls to create log entries, and send them to Honeycomb or an OpenTelemetry Collector.
First, install the OpenTelemetry NodeSDK, OTLP exporter, and bunyan logging framework packages:
npm install --save @opentelemetry/sdk-node \
@opentelemetry/exporter-logs-otlp-http \
@opentelemetry/instrumentation-bunyan \
bunyan
Next, create and configure the logger provider during application setup.
This is typically done in a separate file and required before starting your application.
For example, the following is added to a file called telemetry.js
:
const { NodeSDK, logs } = require('@opentelemetry/sdk-node');
const { OTLPLogExporter } = require('@opentelemetry/exporter-logs-otlp-http');
const { BunyanInstrumentation } = require('@opentelemetry/instrumentation-bunyan');
// Configure the OpenTelemetry SDK with OTLP exporter and Bunyan logging instrumentation
const sdk = new NodeSDK({
logRecordProcessor: new logs.SimpleLogRecordProcessor(new OTLPLogExporter()),
instrumentations: [
new BunyanInstrumentation(),
]
});
// Start the SDK
sdk.start();
// Make sure the sdk is shutdown properly before exiting so pending logs are sent before stopping
process.on('SIGTERM', () => {
sdk
.shutdown()
.finally(() => process.exit(0));
});
Then, in your application, create and use bunyan loggers to capture interesting things:
const bunyan = require('bunyan');
// Create a logger and use in your app
const logger = bunyan.createLogger({name: 'myapp', level: 'info'});
logger.info({'app.message':'Something interesting happened'});
Finally, to send logs to Honeycomb, run your application with some environment variables to set the service name and configure the exporter:
OTEL_SERVICE_NAME="my-service" \
OTEL_EXPORTER_OTLP_ENDPOINT="https://api.honeycomb.io" \
OTEL_EXPORTER_OTLP_HEADERS="x-honeycomb-team=<your-api-key>" \
node -r ./telemetry.js app.js
Alternatively, to send logs to an OpenTelemetry collector, use these environment variables:
OTEL_SERVICE_NAME="my-service" \
OTEL_EXPORTER_OTLP_ENDPOINT="my-collector:4317" \
OTEL_EXPORTER_OTLP_INSECURE=true \
node -r ./telemetry.js app.js
The OpenTelemetry JS SDK supports the following logging frameworks and can create logs automatically.