Skip to main content
Logs support in the OpenTelemetry JavaScript SDK is currently experimental and subject to change. To check the status for each language, refer to the OpenTelemetry status page.
This example shows how to configure the OpenTelemetry JavaScript SDK to capture Bunyan logging calls and send logs from a JavaScript application to Honeycomb or an OpenTelemetry Collector.
1

Acquire Dependencies

Install the OpenTelemetry Node.js SDK, OTLP exporter, and Bunyan logging framework packages:
npm install --save @opentelemetry/sdk-node \
    @opentelemetry/exporter-logs-otlp-http \
    @opentelemetry/instrumentation-bunyan \
    bunyan
2

Configure the Logger Provider

During application setup, create and configure the logger provider. This is typically done in a separate file that you require before starting your application.For example, add the following 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();
// Shut down the SDK gracefully before exiting to export all pending logs
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 it in your app
const logger = bunyan.createLogger({name: 'myapp', level: 'info'});
logger.info({'app.message':'Something interesting happened'});
3

Run Your Application

Set environment variables to configure the exporter and define your service name, then run your application.To send logs directly to Honeycomb:
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
To send logs to an OpenTelemetry Collector instead:
OTEL_SERVICE_NAME="my-service" \
  OTEL_EXPORTER_OTLP_ENDPOINT="my-collector:4317" \
  OTEL_EXPORTER_OTLP_INSECURE=true \
  node -r ./telemetry.js app.js

Supported Logging Frameworks

The OpenTelemetry JavaScript SDK can automatically create logs from these supported logging frameworks:
  • Bunyan
  • Winston