### Minimalistic Pino OpenTelemetry Transport Setup Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/transport.md This is a basic example demonstrating how to set up the pino-opentelemetry-transport with default configurations. It shows the essential steps of requiring pino, creating the transport, and initializing the logger. ```javascript const pino = require('pino') const transport = pino.transport({ target: 'pino-opentelemetry-transport' }) const logger = pino(transport) transport.on('ready', () => { logger.info('test log') }) ``` -------------------------------- ### Running the Example Service Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/examples/using-multiple-record-processors/README.md Execute the example service using Node.js. Ensure the Docker Compose file for the OTLP collector is running. ```bash node multiple-processors.js ``` -------------------------------- ### Install Pino and Transport Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/README.md Install both Pino and the OpenTelemetry transport package. ```bash npm install pino pino-opentelemetry-transport ``` -------------------------------- ### Run Minimalistic Example Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/README.md Executes the minimalistic example script to test logging with the transport. ```bash node examples/minimalistic/minimalistic.js ``` -------------------------------- ### Install pino-opentelemetry-transport Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/README.md Install the package using npm. ```bash npm i pino-opentelemetry-transport ``` -------------------------------- ### Running Multiple Record Processors Example Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md Command to run the multiple record processors example, setting necessary environment variables for endpoint and service attributes. ```bash # Run the example (set endpoints via env vars) OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=http://localhost:4318 \ OTEL_RESOURCE_ATTRIBUTES="service.name=multi-processor" \ node examples/using-multiple-record-processors/multiple-processors.js ``` -------------------------------- ### Usage Examples for LogRecordProcessorOptions Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/types.md Provides examples for configuring batch and simple processors with different exporter protocols like HTTP, gRPC, and console. ```typescript // Batch processor with HTTP exporter const batchHttp: LogRecordProcessorOptions = { recordProcessorType: 'batch', exporterOptions: { protocol: 'http' } } // Simple processor with gRPC exporter const simpleGrpc: LogRecordProcessorOptions = { recordProcessorType: 'simple', exporterOptions: { protocol: 'grpc' } } // Batch processor with console exporter const batchConsole: LogRecordProcessorOptions = { recordProcessorType: 'batch', exporterOptions: { protocol: 'console' } } ``` -------------------------------- ### Usage Example for Options Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/types.md Demonstrates how to configure the transport with logger name, service version, resource attributes, custom severity mapping, and log record processor options. ```typescript import type { Options } from 'pino-opentelemetry-transport' const config: Options = { loggerName: 'my-service', serviceVersion: '1.0.0', resourceAttributes: { 'service.name': 'my-service', 'environment': 'production' }, severityNumberMap: { 30: 9, // Custom info level mapping 50: 17 // Custom error level mapping }, logRecordProcessorOptions: { recordProcessorType: 'batch', exporterOptions: { protocol: 'http' } } } ``` -------------------------------- ### Pino OpenTelemetry Transport with Custom Configuration Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/transport.md This example shows how to configure the pino-opentelemetry-transport with custom options such as logger name, service version, resource attributes, and severity number mapping. It demonstrates a more advanced setup for specific service requirements. ```javascript const pino = require('pino') const transport = pino.transport({ target: 'pino-opentelemetry-transport', options: { loggerName: 'my-service', serviceVersion: '1.0.0', resourceAttributes: { 'service.name': 'my-service', 'environment': 'production' }, severityNumberMap: { 35: 10 // Custom log level mapping } } }) const logger = pino(transport) transport.on('ready', () => { logger.info('Service started') logger.error({ err: new Error('Test error') }, 'An error occurred') }) ``` -------------------------------- ### Minimalistic Pino OpenTelemetry Setup Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md This snippet shows the most basic setup for sending logs to an OTLP collector using default configurations. It requires specific environment variables for the endpoint and service name. ```javascript 'use strict' const path = require('path') const pino = require('pino') const transport = pino.transport({ target: path.join(__dirname, '..', '..', 'lib', 'pino-opentelemetry-transport') }) const logger = pino(transport) transport.on('ready', () => { setInterval(() => { logger.info('test log') }, 1000) }) ``` ```bash # Start OTLP collector docker run --volume=$(pwd)/otel-collector-config.yaml:/etc/otel-collector-config.yaml:rw \ --volume=/tmp/test-logs:/etc/test-logs:rw \ -p 4317:4317 -p 4318:4318 \ -d otel/opentelemetry-collector-contrib:latest \ --config=/etc/otel-collector-config.yaml # Run the example OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=http://localhost:4318 \ OTEL_RESOURCE_ATTRIBUTES="service.name=minimalistic-app" \ node examples/minimalistic/minimalistic.js # View logs tail -f /tmp/test-logs/otlp-logs.log ``` -------------------------------- ### Run TypeScript Example Service Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/examples/typescript/README.md Execute the example TypeScript service using ts-node. Ensure the OTLP collector is running. ```bash npx ts-node example.ts ``` -------------------------------- ### Protobuf Exporter Usage Example Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/types.md Example of configuring the ProtobufExporterOptions, specifying the URL for the HTTP endpoint. ```typescript const config: ProtobufExporterOptions = { protocol: 'http/protobuf', protobufExporterOptions: { url: 'http://localhost:4318' } } ``` -------------------------------- ### Minimalistic Pino Transport Example Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/README.md This example demonstrates how to set up a minimal Pino logger that uses the pino-opentelemetry-transport. Ensure an OTEL collector is running and accessible. ```javascript const pino = require('pino') const transport = pino.transport({ target: 'pino-opentelemetry-transport' }) const logger = pino(transport) transport.on('ready', () => { setInterval(() => { logger.info('test log') }, 1000) }) ``` -------------------------------- ### Run Minimalistic Example with Environment Variables Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/examples/minimalistic/README.md Execute the Node.js service with specific environment variables to configure the OpenTelemetry logs exporter. Ensure the OTLP collector is running. ```bash OTEL_EXPORTER_OTLP_LOGS_PROTOCOL='grpc' OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=http://localhost:4317 OTEL_RESOURCE_ATTRIBUTES="service.name=my-service,service.version=1.2.3" node minimalistic.js ``` -------------------------------- ### gRPC Exporter Usage Example Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/types.md Example of how to configure the GrpcExporterOptions, including custom headers for authentication and other metadata. ```typescript const config: GrpcExporterOptions = { protocol: 'grpc', grpcExporterOptions: { headers: { 'authorization': 'Bearer token123', 'x-custom-header': 'custom-value' } } } ``` -------------------------------- ### Start OTLP Collector with Docker Compose Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/examples/typescript/README.md Use this command to boot the OpenTelemetry Protocol (OTLP) collector using the provided Docker Compose file. ```bash docker compose up ``` -------------------------------- ### Run Docker Collector Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/README.md Starts the OpenTelemetry Protocol (OTLP) collector in a Docker container. ```bash npm run docker-run ``` -------------------------------- ### Usage Example for PinoOptions Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/types.md Illustrates how to provide a custom mapping for Pino log level numbers to OpenTelemetry SeverityNumber values. ```typescript const options: PinoOptions = { severityNumberMap: { 10: 1, // Map trace level to TRACE 20: 5, // Map debug level to DEBUG 30: 9, // Map info level to INFO 40: 13, // Map warn level to WARN 50: 17, // Map error level to ERROR 60: 21 // Map fatal level to FATAL } } ``` -------------------------------- ### Complete JavaScript Configuration with All Options Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/configuration.md A comprehensive JavaScript example demonstrating how to configure pino-opentelemetry-transport with various options, including logger name, service version, resource attributes, severity mapping, and multiple log record processors with different exporters. ```javascript const pino = require('pino') const transport = pino.transport({ target: 'pino-opentelemetry-transport', options: { // OpenTelemetry logger configuration loggerName: 'my-service-logger', serviceVersion: '1.0.0', // Resource attributes for all logs resourceAttributes: { 'service.name': 'my-service', 'service.namespace': 'mycompany', 'environment': 'production', 'host.name': 'server-01' }, // Custom severity mapping severityNumberMap: { 10: 1, 20: 5, 30: 9, 40: 13, 50: 17, 60: 21 }, // Log record processors and exporters logRecordProcessorOptions: [ { recordProcessorType: 'batch', exporterOptions: { protocol: 'http', httpExporterOptions: { url: 'http://otel-collector:4318' } }, processorConfig: { scheduledDelayMillis: 5000, maxExportBatchSize: 512 } }, { recordProcessorType: 'simple', exporterOptions: { protocol: 'console' } } ] } }) const logger = pino(transport) transport.on('ready', () => { logger.info('Service started') }) ``` -------------------------------- ### HTTP Exporter Usage Example Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/types.md Example of configuring the HttpExporterOptions, specifying the URL and authorization headers for the HTTP endpoint. ```typescript const config: HttpExporterOptions = { protocol: 'http', httpExporterOptions: { url: 'http://localhost:4318', headers: { 'authorization': 'Bearer token123' } } } ``` -------------------------------- ### Configure Multiple Log Record Processors Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/configuration.md This example demonstrates configuring multiple processors, including batch and simple types with different exporter protocols, for advanced log processing. ```javascript const transport = pino.transport({ target: 'pino-opentelemetry-transport', options: { loggerName: 'my-service', serviceVersion: '1.0.0', logRecordProcessorOptions: [ { recordProcessorType: 'batch', exporterOptions: { protocol: 'http' } }, { recordProcessorType: 'batch', exporterOptions: { protocol: 'grpc' } }, { recordProcessorType: 'simple', exporterOptions: { protocol: 'console' } } ] } }) ``` -------------------------------- ### Example Mapper Options for toOpenTelemetry Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/opentelemetry-mapper.md Provides an example of the mapperOptions object required by the toOpenTelemetry function. This includes specifying the message key, level labels, and an optional custom severity number map. ```javascript { messageKey: 'msg', levels: { labels: { 10: 'trace', 20: 'debug', 30: 'info', 40: 'warn', 50: 'error', 60: 'fatal' } }, severityNumberMap: { 30: 9, // Custom mapping for info level 40: 13 // Custom mapping for warn level } } ``` -------------------------------- ### Run HTTP Server with Instrumentation Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/examples/trace-context/README.md Starts the HTTP server by preloading the OpenTelemetry instrumentation code. Ensure the Docker Compose file is running to boot the OTLP collector. ```bash node -r "./trace-instrumentation.js" http-server.js ``` -------------------------------- ### OpenTelemetry Collector Configuration Example Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/README.md A sample YAML configuration for an OpenTelemetry collector, defining receivers, exporters (file and debug), and a service pipeline for logs. ```yaml receivers: otlp: protocols: grpc: endpoint: 0.0.0.0:4317 http: endpoint: 0.0.0.0:4318 exporters: file: path: ./etc/test-logs/otlp-logs.log flush_interval: 1 debug: verbosity: basic processors: batch: service: pipelines: logs: receivers: [otlp] processors: [] exporters: [debug, file] ``` -------------------------------- ### Console Exporter Usage Example Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/types.md Example of configuring the ConsoleExporterOptions for debugging purposes. This exporter outputs logs to the console. ```typescript const config: ConsoleExporterOptions = { protocol: 'console' } ``` -------------------------------- ### Log Output with Trace Context Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md Example of log output showing trace and span IDs when using the transport. ```json { "severityNumber": 9, "severityText": "info", "body": "Server running at http://127.0.0.1:8080/", "attributes": { "trace_id": "12345678901234567890123456789012", "span_id": "1234567890123456", "trace_flags": "01" } } ``` -------------------------------- ### Pino OpenTelemetry Transport with Multiple Log Processors Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/transport.md This example demonstrates configuring the transport to use multiple log record processors, allowing logs to be sent to different destinations simultaneously. It includes configurations for both a batch HTTP exporter and a console exporter. ```javascript const pino = require('pino') const transport = pino.transport({ target: 'pino-opentelemetry-transport', options: { loggerName: 'multi-processor-logger', serviceVersion: '1.0.0', logRecordProcessorOptions: [ { recordProcessorType: 'batch', exporterOptions: { protocol: 'http', httpExporterOptions: { url: 'http://localhost:4318' } } }, { recordProcessorType: 'simple', exporterOptions: { protocol: 'console' } } ] } }) const logger = pino(transport) transport.on('ready', () => { logger.info('logs are being sent to HTTP collector and console') }) ``` -------------------------------- ### Configure OpenTelemetry Transport via Environment Variables Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/configuration.md A shell script example demonstrating how to set various environment variables to configure OpenTelemetry logging, including protocol, endpoint, resource attributes, and batch processor settings. ```bash #!/bin/bash # OpenTelemetry logging configuration export OTEL_EXPORTER_OTLP_LOGS_PROTOCOL=grpc export OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=grpc://otel-collector.example.com:4317 # Resource attributes export OTEL_RESOURCE_ATTRIBUTES="service.name=my-service,service.version=1.0.0,environment=production,host.name=server-01" # Batch processor configuration export OTEL_BLRP_SCHEDULE_DELAY=5000 export OTEL_BLRP_MAX_EXPORT_BATCH_SIZE=512 # Custom headers (if needed) export OTEL_EXPORTER_OTLP_LOGS_HEADERS="Authorization=Bearer mytoken123" # Timeout configuration export OTEL_EXPORTER_OTLP_LOGS_TIMEOUT=30000 # Run application node app.js ``` -------------------------------- ### Environment Variable Precedence Example Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/configuration.md Illustrates the precedence of environment variables for OpenTelemetry exporter endpoints, showing that per-signal variables (e.g., OTEL_EXPORTER_OTLP_LOGS_ENDPOINT) override general variables (e.g., OTEL_EXPORTER_OTLP_ENDPOINT). ```bash # These environment variables are set export OTEL_EXPORTER_OTLP_ENDPOINT=http://default-collector:4317 export OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=http://logs-collector:4318 # Result: OTEL_EXPORTER_OTLP_LOGS_ENDPOINT is used (logs-collector:4318) # because per-signal variables take precedence over general variables ``` -------------------------------- ### Basic Pino Log to OpenTelemetry Conversion Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/opentelemetry-mapper.md Demonstrates the basic conversion of a Pino log record to an OpenTelemetry log record using default options. Ensure `pino-opentelemetry-transport` is installed. ```javascript const { toOpenTelemetry } = require('pino-opentelemetry-transport/lib/opentelemetry-mapper') const pino = require('pino') const mapperOptions = { messageKey: 'msg', levels: pino.levels } const pinoLog = { msg: 'Application started', level: 30, time: 1621234567890, hostname: 'app-server', pid: 9876 } const otRecord = toOpenTelemetry(pinoLog, mapperOptions) // Returns: { // timestamp: 1621234567890, // body: 'Application started', // severityNumber: 9, // severityText: 'info', // attributes: {} // } ``` -------------------------------- ### Example Pino Log Object (sourceObject) Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/opentelemetry-mapper.md Illustrates the structure of a typical Pino log object that can be passed to the toOpenTelemetry function. Includes message, level, timestamp, and custom attributes. ```javascript { msg: 'User login successful', level: 30, time: 1621234567890, hostname: 'server-01', pid: 12345, userId: 'user-123', username: 'john.doe', ipAddress: '192.168.1.100' } ``` -------------------------------- ### Memory Efficient Log Record Processor Configuration Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md Configure for memory efficiency in constrained environments by using batching with shorter delays, smaller export batch sizes, and smaller queues. This example uses HTTP protocol. ```javascript logRecordProcessorOptions: { recordProcessorType: 'batch', exporterOptions: { protocol: 'http' }, processorConfig: { scheduledDelayMillis: 2000, maxExportBatchSize: 256, // Smaller batches maxQueueSize: 512 // Smaller queue } } ``` -------------------------------- ### Handling the 'unknown' Event for Unparsable Log Lines Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/transport.md This example shows how to handle the 'unknown' event, which is emitted when the transport encounters a log line that it cannot parse. It logs the problematic line and the associated error. ```javascript transport.on('unknown', (line, error) => { console.error(`Failed to parse log line: ${line}`) console.error(`Error: ${error.message}`) }) ``` -------------------------------- ### Configure HTTP Exporter Timeout and Headers Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/configuration.md Set the request timeout to 60 seconds and custom Authorization headers for the HTTP exporter. This example also explicitly sets the protocol and endpoint. ```bash export OTEL_EXPORTER_OTLP_LOGS_PROTOCOL=http export OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=http://localhost:4318 export OTEL_EXPORTER_OTLP_LOGS_TIMEOUT=60000 export OTEL_EXPORTER_OTLP_LOGS_HEADERS="Authorization=Bearer token123" ``` -------------------------------- ### Pino OpenTelemetry Transport with Custom Severity Number Mapping Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/transport.md This example illustrates how to define custom mappings for severity numbers in the pino-opentelemetry-transport. This allows for fine-grained control over how log levels are represented in OpenTelemetry. ```javascript const pino = require('pino') const transport = pino.transport({ target: 'pino-opentelemetry-transport', options: { loggerName: 'custom-severity-logger', serviceVersion: '1.0.0', severityNumberMap: { 10: 1, // trace -> TRACE (custom mapping) 20: 5, // debug -> DEBUG (custom mapping) 30: 9, // info -> INFO (custom mapping) 40: 13, // warn -> WARN (custom mapping) 50: 17, // error -> ERROR (custom mapping) 60: 21 // fatal -> FATAL (custom mapping) } } }) const logger = pino(transport) transport.on('ready', () => { logger.info('logs with custom severity mappings') }) ``` -------------------------------- ### TypeScript Configuration for Pino OpenTelemetry Transport Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/configuration.md Example of configuring pino-opentelemetry-transport using TypeScript, defining transport options with logger name, service version, resource attributes, and batch exporter configuration. ```typescript import pino from 'pino' import type { Options } from 'pino-opentelemetry-transport' const transportOptions: Options = { loggerName: 'my-service', serviceVersion: '1.0.0', resourceAttributes: { 'service.name': 'my-service', 'environment': 'production' }, logRecordProcessorOptions: { recordProcessorType: 'batch', exporterOptions: { protocol: 'grpc' } } } const transport = pino.transport({ target: 'pino-opentelemetry-transport', options: transportOptions }) const logger = pino(transport) ``` -------------------------------- ### Example OpenTelemetry LogRecord Return Value Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/opentelemetry-mapper.md Shows the structure of the OpenTelemetry LogRecord object returned by the toOpenTelemetry function. It includes timestamp, body, severity details, and attributes derived from the source log. ```javascript { timestamp: 1621234567890, body: 'User login successful', severityNumber: 9, // OpenTelemetry INFO level severityText: 'info', attributes: { userId: 'user-123', username: 'john.doe', ipAddress: '192.168.1.100' } } ``` -------------------------------- ### Configure Pino Transport with Custom Severity Mapping Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/README.md Customize the mapping of Pino log levels to OpenTelemetry severity numbers. This example maps the 'info' level (30) to a custom severity number (10). ```javascript const transport = pino.transport({ target: 'pino-opentelemetry-transport', options: { loggerName: 'my-app', serviceVersion: '1.0.0', severityNumberMap: { 30: 10 // Map info level to INFO3 instead of INFO } } }) const logger = pino(transport) ``` -------------------------------- ### Pino Log to OpenTelemetry Conversion with Custom Severity Mapping Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/opentelemetry-mapper.md Illustrates how to override the default severity number mapping for Pino log levels. This example maps Pino's 'info' (level 30) to OpenTelemetry severity number 8 and 'error' (level 50) to 18. ```javascript const mapperOptions = { messageKey: 'msg', levels: pino.levels, severityNumberMap: { 30: 8, // Override info level to map to severity 8 50: 18 // Override error level to map to severity 18 } } const pinoLog = { msg: 'Warning about deprecated API', level: 30, time: 1621234567890, hostname: 'app-server', pid: 9876, apiVersion: 'v1' } const otRecord = toOpenTelemetry(pinoLog, mapperOptions) // Returns: { // timestamp: 1621234567890, // body: 'Warning about deprecated API', // severityNumber: 8, // Uses custom mapping // severityText: 'info', // attributes: { // apiVersion: 'v1' // } // } ``` -------------------------------- ### Configure custom log levels with severityNumberMap Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/configuration.md Map custom Pino log levels to OpenTelemetry SeverityNumber values when using custom log levels defined in Pino. This example demonstrates mapping custom levels like 'silly', 'verbose', and 'custom'. ```javascript // For custom Pino log levels const logger = pino({ customLevels: { silly: 5, verbose: 25, custom: 35 } }, transport) const transport = pino.transport({ target: 'pino-opentelemetry-transport', options: { loggerName: 'my-service', serviceVersion: '1.0.0', severityNumberMap: { 5: 2, // silly → TRACE2 25: 8, // verbose → DEBUG4 35: 10 // custom → INFO2 } } }) ``` -------------------------------- ### Grafana Loki Integration Configuration Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md Configure the transport to send logs to Grafana Loki via OTLP using HTTP protocol. This setup is suitable for Grafana deployments requiring log querying. ```javascript const transport = pino.transport({ target: 'pino-opentelemetry-transport', options: { loggerName: 'loki-logger', serviceVersion: '1.0.0', logRecordProcessorOptions: { recordProcessorType: 'batch', exporterOptions: { protocol: 'http', httpExporterOptions: { url: 'http://loki:3100/otlp/v1/logs' } } }, resourceAttributes: { 'service.name': 'my-app', 'environment': 'production' } } }) ``` -------------------------------- ### Running the Quick Test Script Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md This command shows how to run the test script with the necessary environment variables to configure the OpenTelemetry endpoint and service attributes. ```bash OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=http://localhost:4318 \ OTEL_RESOURCE_ATTRIBUTES="service.name=test-app" \ node test-script.js ``` -------------------------------- ### Initialize Pino Transport with Ready Event Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/README.md This is the recommended way to initialize the transport. It allows logging to commence only after the transport is confirmed ready, preventing potential data loss or errors. ```javascript const transport = pino.transport({ target: 'pino-opentelemetry-transport', options: { loggerName: 'app', serviceVersion: '1.0.0' } }) transport.on('ready', () => { // Now safe to log logger.info('Started') }) transport.on('unknown', (line, error) => { // Handle unparsable log lines console.error('Parse error:', error) }) ``` -------------------------------- ### Initialize Pino Transport with Await Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/README.md Use this method to initialize the transport and ensure it's ready before logging. It's an alternative to waiting for the 'ready' event. ```javascript const transport = await pino.transport({ target: 'pino-opentelemetry-transport', options: { loggerName: 'app', serviceVersion: '1.0.0' } }) ``` -------------------------------- ### Handling the 'ready' Event for Pino OpenTelemetry Transport Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/transport.md This snippet demonstrates the correct way to handle the 'ready' event emitted by the transport. It ensures that logging operations are only performed after the transport has been fully initialized. ```javascript transport.on('ready', () => { // Safe to log here logger.info('Transport is ready') }) ``` -------------------------------- ### Running with Environment Variables for OTLP Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/README.md Configure the OpenTelemetry OTLP exporter endpoint, protocol, and service attributes using environment variables before running the application. ```bash export OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=http://localhost:4318 export OTEL_EXPORTER_OTLP_LOGS_PROTOCOL=grpc export OTEL_RESOURCE_ATTRIBUTES="service.name=my-service" node app.js ``` -------------------------------- ### Quick Test Script for Pino OpenTelemetry Transport Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md This script demonstrates how to initialize the transport and log messages at different levels, including structured logs. It waits for the transport to be ready before sending logs and exits after a delay. ```javascript const pino = require('pino') const transport = pino.transport({ target: 'pino-opentelemetry-transport', options: { loggerName: 'test-app', serviceVersion: '1.0.0' } }) const logger = pino(transport) transport.on('ready', () => { console.log('✓ Transport ready') // Test logging logger.trace('TRACE level message') logger.debug('DEBUG level message') logger.info('INFO level message') logger.warn('WARN level message') logger.error('ERROR level message') logger.fatal('FATAL level message') // Test with attributes logger.info({ userId: 'user-123', action: 'login', ip: '192.168.1.100' }, 'User authentication') // Wait for batch export (default 5 seconds) setTimeout(() => { console.log('✓ Logs sent') process.exit(0) }, 6000) }) transport.on('unknown', (line, error) => { console.error('✗ Parse error:', error) }) ``` -------------------------------- ### OpenTelemetry Node.js SDK with Pino Instrumentation Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md This code initializes the OpenTelemetry Node.js SDK, including HTTP and Pino instrumentation. It ensures that trace context is captured and can be propagated to logs when the Pino transport is configured. ```javascript 'use strict' const process = require('process') const opentelemetry = require('@opentelemetry/sdk-node') const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http') const { PinoInstrumentation } = require('@opentelemetry/instrumentation-pino') const { resourceFromAttributes } = require('@opentelemetry/resources') const { ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-node') const traceExporter = new ConsoleSpanExporter() const instrumentations = [ new HttpInstrumentation(), new PinoInstrumentation() ] const sdk = new opentelemetry.NodeSDK({ resource: resourceFromAttributes({ 'service.name': 'Pino OpenTelemetry Example' }), traceExporter, instrumentations }) sdk.start() process.on('SIGTERM', () => { sdk .shutdown() .then(() => console.log('Tracing terminated')) .catch(error => console.log('Error terminating tracing', error)) .finally(() => process.exit(0)) }) ``` ```bash # Terminal 1: Start OTLP collector docker run --volume=$(pwd)/otel-collector-config.yaml:/etc/otel-collector-config.yaml:rw \ -p 4317:4317 -p 4318:4318 \ -d otel/opentelemetry-collector-contrib:latest \ --config=/etc/otel-collector-config.yaml # Terminal 2: Run the instrumented server OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=http://localhost:4318 \ OTEL_RESOURCE_ATTRIBUTES="service.name=http-server" \ node -r ./examples/trace-context/trace-instrumentation.js \ examples/trace-context/http-server.js # Terminal 3: Send requests curl http://127.0.0.1:8080/ # Check logs tail -f /tmp/test-logs/otlp-logs.log ``` -------------------------------- ### gRPC with TLS Configuration Environment Variables Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md Configures the OTLP logs exporter for gRPC protocol with TLS enabled, including endpoint, certificate paths for server and client authentication. ```bash export OTEL_EXPORTER_OTLP_LOGS_PROTOCOL=grpc export OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=grpcs://otel-collector.example.com:4317 export OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE=/path/to/ca-cert.pem export OTEL_EXPORTER_OTLP_LOGS_CLIENT_CERTIFICATE=/path/to/client-cert.pem export OTEL_EXPORTER_OTLP_LOGS_CLIENT_KEY=/path/to/client-key.pem ``` -------------------------------- ### HTTP Server with Pino and Trace Context Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md This code sets up a basic HTTP server that logs messages. It's designed to be used with OpenTelemetry instrumentation to automatically propagate trace context into the logs. ```javascript // https://nodejs.dev/en/learn/#an-example-nodejs-application const http = require('http') const pino = require('pino') const path = require('path') const transport = pino.transport({ target: path.join(__dirname, '..', '..', 'lib', 'pino-opentelemetry-transport') }) const logger = pino(transport) const hostname = '127.0.0.1' const port = 8080 const server = http.createServer((req, res) => { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain') logger.info({ msg: 'test log', foo: 'bar' }) res.end('Hello World\n') }) server.listen(port, hostname, () => { logger.info(`Server running at http://${hostname}:${port}/`) }) ``` -------------------------------- ### Configure Pino Transport with OpenTelemetry Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/README.md Set up the pino-opentelemetry-transport with essential logger details and optional resource attributes. ```javascript const transport = pino.transport({ target: 'pino-opentelemetry-transport', options: { // Required loggerName: 'my-service', serviceVersion: '1.0.0', // Optional but recommended resourceAttributes: { 'service.name': 'my-service', 'environment': 'production' }, // Optional severityNumberMap: { /* custom mappings */ }, logRecordProcessorOptions: { /* processor config */ } } }) ``` -------------------------------- ### Import Main Options Type Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/types.md Imports the main configuration type 'Options' from the 'pino-opentelemetry-transport' package. ```typescript // Import main type import type { Options } from 'pino-opentelemetry-transport' ``` -------------------------------- ### Complete OTLP Configuration Environment Variables Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md Provides a comprehensive set of environment variables for configuring the OTLP logs exporter, including endpoint, protocol, resource attributes, batch processor settings, and exporter timeouts/headers. ```bash # Collector configuration export OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=http://otel-collector.example.com:4318 export OTEL_EXPORTER_OTLP_LOGS_PROTOCOL=grpc # Resource attributes export OTEL_RESOURCE_ATTRIBUTES="service.name=my-app,service.version=1.0.0,environment=production" # Batch processor configuration export OTEL_BLRP_SCHEDULE_DELAY=5000 export OTEL_BLRP_MAX_EXPORT_BATCH_SIZE=512 export OTEL_BLRP_MAX_QUEUE_SIZE=2048 # Exporter configuration export OTEL_EXPORTER_OTLP_LOGS_TIMEOUT=30000 export OTEL_EXPORTER_OTLP_LOGS_HEADERS="Authorization=Bearer token123" ``` -------------------------------- ### Configure Batch Processor Settings via Environment Variables Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/README.md Adjust batch processor settings like schedule delay and maximum export batch size using environment variables. ```bash # Batch Processor Settings export OTEL_BLRP_SCHEDULE_DELAY=5000 export OTEL_BLRP_MAX_EXPORT_BATCH_SIZE=512 ``` -------------------------------- ### HTTP + Console Configuration Pattern Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md Configuration pattern for simultaneous export to HTTP (production) and console (development) destinations. ```javascript logRecordProcessorOptions: [ { recordProcessorType: 'batch', exporterOptions: { protocol: 'http' } // Production }, { recordProcessorType: 'simple', exporterOptions: { protocol: 'console' } // Development } ] ``` -------------------------------- ### Observe OTLP Logs Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/examples/trace-context/README.md Follows the log file where OTLP logs are stored. This command is used to observe the generated log records with trace context information. ```bash tail -f /tmp/test-logs/otlp-logs.log ``` -------------------------------- ### Run Service with Environment Variables Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/README.md Execute the Node.js service while setting specific environment variables for OpenTelemetry collector endpoint, protocol, and resource attributes. ```bash OTEL_EXPORTER_OTLP_LOGS_PROTOCOL='grpc' OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=http://localhost:4317 OTEL_RESOURCE_ATTRIBUTES="service.name=my-service,service.version=1.2.3" node index.js ``` -------------------------------- ### Configure Single Log Record Processor Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/configuration.md Use this snippet to configure a single batch processor with an HTTP/Protobuf exporter for your logs. ```javascript const transport = pino.transport({ target: 'pino-opentelemetry-transport', options: { loggerName: 'my-service', serviceVersion: '1.0.0', logRecordProcessorOptions: { recordProcessorType: 'batch', exporterOptions: { protocol: 'http' } } } }) ``` -------------------------------- ### Configure Batch Processor with Environment Variables Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/configuration.md Adjust batch processor settings using environment variables for controlling delay, export batch size, and queue size. ```bash export OTEL_BLRP_SCHEDULE_DELAY=10000 export OTEL_BLRP_MAX_EXPORT_BATCH_SIZE=1000 export OTEL_BLRP_MAX_QUEUE_SIZE=4096 ``` -------------------------------- ### Production Logging with Batch Processing Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md Configure the transport for production environments using batch processing for efficiency. It includes dynamic service versioning and resource attributes from environment variables. ```javascript const transport = pino.transport({ target: 'pino-opentelemetry-transport', options: { loggerName: 'production-service', serviceVersion: process.env.APP_VERSION || '0.0.0', resourceAttributes: { 'service.name': process.env.SERVICE_NAME || 'my-service', 'deployment.environment': process.env.NODE_ENV || 'production', 'service.instance.id': process.env.HOSTNAME }, logRecordProcessorOptions: { recordProcessorType: 'batch', exporterOptions: { protocol: process.env.OTEL_EXPORTER_OTLP_LOGS_PROTOCOL || 'http' }, processorConfig: { scheduledDelayMillis: 5000, maxExportBatchSize: 512, maxQueueSize: 2048 } } } }) const logger = pino(transport) transport.on('ready', () => { logger.info('Service initialized') }) ``` -------------------------------- ### Configure gRPC Exporter with Environment Variables Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/configuration.md Set environment variables to configure the gRPC exporter for OpenTelemetry logs. This includes protocol, endpoint, security, and custom headers. ```bash export OTEL_EXPORTER_OTLP_LOGS_PROTOCOL=grpc export OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=grpc://localhost:4317 export OTEL_EXPORTER_OTLP_LOGS_INSECURE=true export OTEL_EXPORTER_OTLP_LOGS_HEADERS="Authorization=Bearer token123" ``` -------------------------------- ### Configure resourceAttributes via Environment Variable Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/configuration.md Set resource attributes using the OTEL_RESOURCE_ATTRIBUTES environment variable. Attributes are provided as a comma-separated string of key=value pairs. ```bash export OTEL_RESOURCE_ATTRIBUTES="service.name=my-service,environment=production,service.version=1.0.0" ``` -------------------------------- ### Set OpenTelemetry Protocol via Environment Variable Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/README.md Specify the communication protocol for OpenTelemetry logs (e.g., grpc, http) using the OTEL_EXPORTER_OTLP_LOGS_PROTOCOL environment variable. ```bash # Protocol (http/protobuf, http, grpc, console) export OTEL_EXPORTER_OTLP_LOGS_PROTOCOL=grpc ``` -------------------------------- ### gRPC + HTTP Configuration Pattern Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md Configuration pattern for redundancy, exporting logs to both gRPC (primary) and HTTP (fallback) destinations. ```javascript logRecordProcessorOptions: [ { recordProcessorType: 'batch', exporterOptions: { protocol: 'grpc' } // Primary }, { recordProcessorType: 'batch', exporterOptions: { protocol: 'http' } // Fallback } ] ``` -------------------------------- ### Pino Log to OpenTelemetry Conversion with Custom Attributes Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/opentelemetry-mapper.md Shows how to include additional Pino log fields as attributes in the OpenTelemetry log record. Custom fields like 'query', 'duration', and 'rowsAffected' are mapped. ```javascript const mapperOptions = { messageKey: 'msg', levels: pino.levels } const pinoLog = { msg: 'Database query executed', level: 30, time: 1621234567890, hostname: 'app-server', pid: 9876, query: 'SELECT * FROM users', duration: 125, rowsAffected: 42 } const otRecord = toOpenTelemetry(pinoLog, mapperOptions) // Returns: { // timestamp: 1621234567890, // body: 'Database query executed', // severityNumber: 9, // severityText: 'info', // attributes: { // query: 'SELECT * FROM users', // duration: 125, // rowsAffected: 42 // } // } ``` -------------------------------- ### Pino Log to OpenTelemetry Conversion Handling Trace Context Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/opentelemetry-mapper.md Demonstrates how trace context information (trace_id, span_id, trace_flags) from Pino logs is automatically mapped to OpenTelemetry attributes. Ensure these fields are present in your Pino logs. ```javascript const mapperOptions = { messageKey: 'msg', levels: pino.levels } const pinoLog = { msg: 'Request processed', level: 30, time: 1621234567890, hostname: 'app-server', pid: 9876, trace_id: '12345678901234567890123456789012', span_id: '1234567890123456', trace_flags: '01', userId: 'user-456' } const otRecord = toOpenTelemetry(pinoLog, mapperOptions) // Returns: { // timestamp: 1621234567890, // body: 'Request processed', // severityNumber: 9, // severityText: 'info', // attributes: { // trace_id: '12345678901234567890123456789012', // span_id: '1234567890123456', // trace_flags: '01', // userId: 'user-456' // } // } ``` -------------------------------- ### Configure Pino Transport with Multiple Record Processors Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/README.md Set up the transport to use multiple record processors, such as a batch exporter for remote sending and a simple exporter for local console output. ```javascript const transport = pino.transport({ target: 'pino-opentelemetry-transport', options: { loggerName: 'my-app', serviceVersion: '1.0.0', logRecordProcessorOptions: [ { recordProcessorType: 'batch', exporterOptions: { protocol: 'http' } // Remote }, { recordProcessorType: 'simple', exporterOptions: { protocol: 'console' } // Local console } ] } }) ``` -------------------------------- ### Multiple gRPC Collectors Configuration Pattern Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md Configuration pattern for load balancing across multiple gRPC collectors by specifying different URLs. ```javascript logRecordProcessorOptions: [ { recordProcessorType: 'batch', exporterOptions: { protocol: 'grpc', grpcExporterOptions: { url: 'grpc://collector1:4317' } } }, { recordProcessorType: 'batch', exporterOptions: { protocol: 'grpc', grpcExporterOptions: { url: 'grpc://collector2:4317' } } } ] ``` -------------------------------- ### Development Logging with Console Output Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md Configure the transport for development environments to output logs directly to the console. This uses a 'simple' record processor type. ```javascript const transport = pino.transport({ target: 'pino-opentelemetry-transport', options: { loggerName: 'dev-service', serviceVersion: '0.0.0-dev', logRecordProcessorOptions: { recordProcessorType: 'simple', exporterOptions: { protocol: 'console' } } } }) const logger = pino(transport) transport.on('ready', () => { logger.info('Development mode initialized') }) ``` -------------------------------- ### Multiple Record Processors Configuration Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md Configures multiple log record processors to export logs to different destinations simultaneously using HTTP, gRPC, and console protocols. ```javascript 'use strict' const pino = require('pino') const path = require('path') const transport = pino.transport({ target: path.join(__dirname, '..', '..', 'lib', 'pino-opentelemetry-transport'), options: { logRecordProcessorOptions: [ { recordProcessorType: 'batch', exporterOptions: { protocol: 'http' } }, { recordProcessorType: 'batch', exporterOptions: { protocol: 'grpc', grpcExporterOptions: { headers: { foo: 'some custom header' } } } }, { recordProcessorType: 'simple', exporterOptions: { protocol: 'console' } } ], loggerName: 'test-logger', serviceVersion: '1.0.0' } }) const logger = pino(transport) transport.on('ready', () => { setInterval(() => { logger.info('test log') }, 1000) }) ``` -------------------------------- ### Configure Pino Transport with Custom Resource Attributes Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/README.md Define a comprehensive set of custom resource attributes for your service, including name, namespace, environment, instance ID, and other custom tags. ```javascript const transport = pino.transport({ target: 'pino-opentelemetry-transport', options: { loggerName: 'my-app', serviceVersion: '1.0.0', resourceAttributes: { 'service.name': 'my-app', 'service.namespace': 'backend', 'deployment.environment': 'production', 'service.instance.id': process.env.HOSTNAME, 'custom.attribute': 'custom-value' } } }) ``` -------------------------------- ### TypeScript Usage with Pino OpenTelemetry Transport Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/examples.md Demonstrates using pino-opentelemetry-transport with TypeScript, leveraging type generics and type imports for enhanced safety. ```typescript import pino from 'pino' import { join } from 'path' import type { Options } from '../../' const transport = pino.transport({ target: join(__dirname, '..', '..', 'lib', 'pino-opentelemetry-transport'), options: { logRecordProcessorOptions: [ { recordProcessorType: 'batch', exporterOptions: { protocol: 'http' } }, { recordProcessorType: 'batch', exporterOptions: { protocol: 'grpc', grpcExporterOptions: { headers: { foo: 'some custom header' } } } }, { recordProcessorType: 'simple', exporterOptions: { protocol: 'console' } } ], loggerName: 'test-logger', serviceVersion: '1.0.0' } }) const logger = pino(transport) transport.on('ready', () => { setInterval(() => { logger.info('test log') }, 1000) }) ``` -------------------------------- ### Set Resource Attributes via Environment Variable Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/README.md Define resource attributes for OpenTelemetry logs, such as service name and environment, using the OTEL_RESOURCE_ATTRIBUTES environment variable. ```bash # Resource Attributes export OTEL_RESOURCE_ATTRIBUTES="service.name=my-service,environment=production" ``` -------------------------------- ### Configure Batch Processor Delay and Size Source: https://github.com/pinojs/pino-opentelemetry-transport/blob/main/_autodocs/README.md Set environment variables to configure the batch processor's retry delay and maximum export batch size. This is useful for optimizing throughput and resource usage in production. ```bash export OTEL_BLRP_SCHEDULE_DELAY=5000 export OTEL_BLRP_MAX_EXPORT_BATCH_SIZE=512 ```