@opentelemetry/instrumentation-console
v0.2.0
Published
OpenTelemetry instrumentation for Node.js `console`
Readme
OpenTelemetry Instrumentation for Node.js Console
This module provides automatic instrumentation for the Node.js console module, generating OpenTelemetry LogRecords for console method calls (console.log, console.error, console.warn, etc.).
Installation
npm install @opentelemetry/instrumentation-consoleSupported Versions
- Node.js
^18.19.0 || >=20.6.0
Usage
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { ConsoleInstrumentation } = require('@opentelemetry/instrumentation-console');
const sdk = new NodeSDK({
instrumentations: [new ConsoleInstrumentation()],
});
sdk.start();
// Now console calls will generate LogRecords
console.log('Hello, world!'); // severity: INFO
console.warn('Watch out!'); // severity: WARN
console.error('Something bad'); // severity: ERRORConsole Methods Instrumented
| Console Method | Severity Number | Severity Text |
| ---------------- | --------------- | ------------- |
| console.trace | TRACE | trace |
| console.debug | DEBUG | debug |
| console.log | INFO | info |
| console.info | INFO | info |
| console.warn | WARN | warn |
| console.error | ERROR | error |
| console.dir | INFO | info |
Configuration
| Option | Type | Default | Description |
| ----------------------- | --------------------- | ------- | ------------------------------------------------------------------ |
| logSeverity | SeverityNumber | — | Minimum severity level; only logs at or above this level are sent |
Trace Context Correlation
When a console method is called within an active span context, the emitted
LogRecord is associated with that context. The OpenTelemetry Logs SDK uses this
to populate the LogRecord's spanContext (trace ID, span ID, and trace flags),
so exported log records can be correlated with their originating trace.
Important: Infinite Loop Prevention
This instrumentation requires @opentelemetry/api >= 1.9.1. Starting from that version, the DiagConsoleLogger saves references to the original console methods at module load time, so internal OTel diagnostic logging bypasses the instrumentation and avoids infinite loops.
Additionally, this instrumentation includes a re-entrancy guard to prevent loops from exporters that write to the console (e.g., ConsoleLogRecordExporter).
Note: Avoid using
ConsoleLogRecordExporterorConsoleSpanExportertogether with this instrumentation in production. Use network-based exporters (e.g., OTLP) instead.
Useful links
- For more information on OpenTelemetry, visit: https://opentelemetry.io/
- For more about OpenTelemetry JavaScript: https://github.com/open-telemetry/opentelemetry-js
- For help or feedback on this project, join us in GitHub Discussions
License
Apache 2.0 - See LICENSE for more information.
