npm.io
0.4.0 • Published 1 week ago

@opentelemetry/instrumentation-console

Licence
Apache-2.0
Version
0.4.0
Deps
2
Size
33 kB
Vulns
0
Weekly
0
Stars
924

OpenTelemetry Instrumentation for Node.js Console

NPM Published Version Apache License

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-console

Supported 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: ERROR

Console 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 ConsoleLogRecordExporter or ConsoleSpanExporter together with this instrumentation in production. Use network-based exporters (e.g., OTLP) instead.

License

Apache 2.0 - See LICENSE for more information.

Keywords