diff --git a/data/docs/logs-management/send-logs/nodejs-pino-logs.mdx b/data/docs/logs-management/send-logs/nodejs-pino-logs.mdx index 3842054d3..e76c62090 100644 --- a/data/docs/logs-management/send-logs/nodejs-pino-logs.mdx +++ b/data/docs/logs-management/send-logs/nodejs-pino-logs.mdx @@ -1,318 +1,180 @@ --- -date: 2024-12-07 -title: Send logs from Node.js Pino without requiring Otel collectors to SigNoz Cloud and Self-hosted +date: 2025-08-19 +title: Auto-instrumenting Node.js Applications with Pino id: nodejs-pino-logs +description: Automatically instrument your existing Node.js application with OpenTelemetry to capture both traces and logs using the Pino logging library --- -## Overview +This document shows you how to send logs from your Node.js application using the Pino logging library to SigNoz. The setup automatically correlates your logs with traces when available, providing unified observability in SigNoz. -This document explains how to configure Pino to stream Node.js application logs directly to your SigNoz dashboard. - - +## Prerequisites + +- Existing Node.js application using [Pino](https://getpino.io/) for logging -### Step 1. Install the necessary packages: +## Step 1: Install Required Packages + +Install the OpenTelemetry packages needed for auto-instrumentation: ```bash -npm i pino-opentelemetry-transport +npm i @opentelemetry/sdk-node \ + @opentelemetry/auto-instrumentations-node \ + @opentelemetry/instrumentation-pino \ + @opentelemetry/exporter-trace-otlp-http \ + @opentelemetry/exporter-logs-otlp-http ``` -### Step 2. Configure Pino Logger -Create a logger configuration file (e.g., logger.js) in the root or a suitable directory (such as utils or config) of your application. This file will define a reusable logger setup for logging important information, errors, and debug details across your application. You can then import this file wherever logging is needed to maintain consistency and reduce redundancy. +### Package Breakdown + +- **`@opentelemetry/sdk-node`** - Core OpenTelemetry SDK for Node.js runtime +- **`@opentelemetry/auto-instrumentations-node`** - Automatic instrumentation for popular libraries (HTTP, Express, etc.) +- **`@opentelemetry/instrumentation-pino`** - Specific instrumentation for Pino logging library +- **`@opentelemetry/exporter-trace-otlp-http`** - OTLP HTTP exporter for traces +- **`@opentelemetry/exporter-logs-otlp-http`** - OTLP HTTP exporter for logs + +## Step 2: Create OpenTelemetry Configuration + +Create an OpenTelemetry configuration file at your project root: + -```jsx -const pino = require('pino'); -const transport = pino.transport({ - target: 'pino-opentelemetry-transport' +```javascript:otel.js +'use strict'; +const { NodeSDK, tracing, logs } = require('@opentelemetry/sdk-node'); +const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node'); +const { PinoInstrumentation } = require('@opentelemetry/instrumentation-pino'); +const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http'); +const { OTLPLogExporter } = require('@opentelemetry/exporter-logs-otlp-http'); + +// Use batch processors for production-friendly behavior +const spanProcessor = new tracing.BatchSpanProcessor(new OTLPTraceExporter()); +const logRecordProcessor = new logs.BatchLogRecordProcessor(new OTLPLogExporter()); + +// Build and start the SDK before your app loads Pino +const sdk = new NodeSDK({ + spanProcessor, + logRecordProcessor, + instrumentations: [ + getNodeAutoInstrumentations(), // Creates spans for HTTP, Express, and more + new PinoInstrumentation({ + }), + ], }); -const logger = pino(transport); +sdk.start(); -logger.info('Hello, World! from pino'); +// Optional graceful shutdown +process.on('SIGTERM', () => { + sdk.shutdown().finally(() => process.exit(0)); +}); ``` + -```ts -import pino from 'pino'; -const transport = pino.transport({ - target: 'pino-opentelemetry-transport' +```typescript:otel.ts +import { NodeSDK, tracing, logs } from '@opentelemetry/sdk-node'; +import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'; +import { PinoInstrumentation } from '@opentelemetry/instrumentation-pino'; +import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; +import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http'; + +// Use batch processors for production-friendly behavior +const spanProcessor = new tracing.BatchSpanProcessor(new OTLPTraceExporter()); +const logRecordProcessor = new logs.BatchLogRecordProcessor(new OTLPLogExporter()); + +// Build and start the SDK before your app loads Pino +const sdk = new NodeSDK({ + spanProcessor, + logRecordProcessor, + instrumentations: [ + getNodeAutoInstrumentations(), // Creates spans for HTTP, Express, and more + new PinoInstrumentation({ + // Optional: customize injected keys + // logKeys: { traceId: 'trace_id', spanId: 'span_id', traceFlags: 'trace_flags' } + }), + ], }); -const logger = pino(transport); +sdk.start(); -logger.info('Hello, World! from pino'); +// Optional graceful shutdown +process.on('SIGTERM', () => { + sdk.shutdown().finally(() => process.exit(0)); +}); ``` + -### Step 2. Run Your Application +### Configuration Notes + +- **Batch processors** are used for production efficiency, batching spans and logs before export +- **Auto-instrumentations** automatically create spans for HTTP requests, Express routes, and other common libraries +- **Pino instrumentation** injects trace context into your logs and forwards log records to the OpenTelemetry Logs SDK +- The SDK must be started **before** your application imports Pino + +## Step 3: Configure and Run Your Application + +### Set Environment Variables -Execute your application with OpenTelemetry Environment variables: + + -```Bash -OTEL_EXPORTER_OTLP_HEADERS="signoz-ingestion-key=" OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=https://ingest..signoz.cloud:443/v1/logs OTEL_RESOURCE_ATTRIBUTES="service.name=,service.version=1.2.3" node app.js +```bash +export OTEL_SERVICE_NAME= +export OTEL_EXPORTER_OTLP_ENDPOINT="https://ingest..signoz.cloud:443" +export OTEL_EXPORTER_OTLP_HEADERS="signoz-ingestion-key=" ``` + - Set the `` to match your SigNoz Cloud [region](https://signoz.io/docs/ingestion/signoz-cloud/overview/#endpoint) - Replace `` with your SigNoz [ingestion key](https://signoz.io/docs/ingestion/signoz-cloud/keys/) - `` is name of your service -## Usage Examples -The below examples show how to log messages with different severity levels and include structured metadata that can be used for filtering and analysis in SigNoz. -### Basic Logging + + -```jsx -logger.info('Hello, World! from pino'); -``` -
- Node.js Pino Logs -
- -Severity Level: INFO - -
-
-```jsx -logger.warn('High memory usage detected'); -``` -
- Node.js Pino Logs -
- -Severity Level: WARN - -
-
-```jsx -logger.error('Failed to process request'); -``` -
- Node.js Pino Logs -
- -Severity Level: ERROR - -
-
-```jsx -logger.error({ - msg: 'Error occurred', - error: error.message, - stack: error.stack -}); +```bash +export OTEL_SERVICE_NAME= +export OTEL_EXPORTER_OTLP_ENDPOINT="http://:4318" ``` -
- Node.js Pino Logs -
- -Severity Level: ERROR - -
-
- -## Output - -After successful implementation, your logs will appear in your SigNoz Logs section where you can: - -- Monitor logs in [real-time](https://signoz.io/docs/product-features/logs-explorer/#live-view) -- [Filter logs](https://signoz.io/docs/userguide/logs_query_builder/) by level and custom attributes -- Search through log contents -- Create [alerts based on log](https://signoz.io/docs/alerts-management/log-based-alerts/) patterns -- Visualize log trends and patterns - - - -
- Node.js Pino Logs -
- -Node.js Pino Logs in SigNoz - -
-
- - -## Troubleshooting -If logs are not appearing in SigNoz: - -1. Verify your Ingestion Keys and endpoint URL -2. Ensure the transport is properly configured -3. Review SigNoz Logs Section filters -4. Check Pino log level settings - -## Additional Resources -- [Pino OpenTelemetry Transport](https://github.com/pinojs/pino-opentelemetry-transport) +- Replace `` with your OpenTelemetry collector host +- `` is name of your service
- - - - - -### Step 1. Install the necessary packages: +
-```bash -npm i pino-opentelemetry-transport -``` +### Start Your Application -### Step 2. Configure Pino Logger -Add the following code to your application: -```jsx -const pino = require('pino'); - -const transport = pino.transport({ - target: 'pino-opentelemetry-transport' -}); -const logger = pino(transport); - -logger.info('Hello, World! from pino'); +```bash +node -r ./otel.js app.js ``` + -```ts -import pino from 'pino'; - -const transport = pino.transport({ - target: 'pino-opentelemetry-transport' -}); -const logger = pino(transport); - -logger.info('Hello, World! from pino'); +```bash +node -r ./otel.ts app.ts ``` + -### Step 2. Run Your Application +The `-r` flag ensures the OpenTelemetry SDK loads and initializes before your application imports Pino. -Execute your application with OpenTelemetry Environment variables: +## Verification -```Bash -OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=http://localhost:4317/v1/logs OTEL_RESOURCE_ATTRIBUTES="service.name=,service.version=1.2.3" node app.js -``` -- Replace `` with name of your service -## Usage Examples -The below examples show how to log messages with different severity levels and include structured metadata that can be used for filtering and analysis in SigNoz. +After starting your application: -### Basic Logging +1. **Make some requests** to your application endpoints +2. **Check SigNoz** for incoming logs +3. **Verify correlation** - your logs should contain trace IDs and be correlated with spans if available -```jsx -logger.info('Hello, World! from pino'); -``` -
- Node.js Pino Logs -
- -Severity Level: INFO - -
+
+ Pino Logs with Span ID and Trace ID +
Pino Logs with Span ID and Trace ID for Correlation
-```jsx -logger.warn('High memory usage detected'); -``` - -
- Node.js Pino Logs -
- -Severity Level: WARN - -
-
- - -```jsx -logger.error('Failed to process request'); -``` -
- Node.js Pino Logs -
- -Severity Level: ERROR - -
-
- -```jsx -logger.error({ - msg: 'Error occurred', - error: error.message, - stack: error.stack -}); -``` -
- Node.js Pino Logs -
- -Severity Level: ERROR - -
-
-## Output - -After successful implementation, your logs will appear in your SigNoz Logs Section where you can: - -- Monitor logs in [real-time](https://signoz.io/docs/product-features/logs-explorer/#live-view) -- [Filter logs](https://signoz.io/docs/userguide/logs_query_builder/) by level and custom attributes -- [Search](https://signoz.io/docs/product-features/logs-explorer/#search) through log contents -- Create [alerts based on log](https://signoz.io/docs/alerts-management/log-based-alerts/) patterns -- Visualize log trends and patterns - -
- Node.js Pino Logs -
- -Node.js Pino Logs in SigNoz - -
-
- -## Troubleshooting - -If logs are not appearing in SigNoz: - -1. Verify endpoint URL -2. Ensure the transport is properly configured -3. Review SigNoz Logs Section filters -4. Check Pino log level settings - -## Additional Resources - -- [Pino OpenTelemetry Transport](https://github.com/pinojs/pino-opentelemetry-transport) - - diff --git a/public/img/docs/logs-management/send-logs/nodejs-pino-logs.webp b/public/img/docs/logs-management/send-logs/nodejs-pino-logs.webp index 499b9d890..c2f66623d 100644 Binary files a/public/img/docs/logs-management/send-logs/nodejs-pino-logs.webp and b/public/img/docs/logs-management/send-logs/nodejs-pino-logs.webp differ