Instrumentation Files Guide
Overview
Section titled “Overview”This guide explains the observability instrumentation setup following SigNoz’s TypeScript documentation.
🎯 Primary File
Section titled “🎯 Primary File”instrumentation.cjs (Single Implementation)
Section titled “instrumentation.cjs (Single Implementation)”Purpose: JavaScript implementation following SigNoz’s exact documentation pattern
When to use: All environments (development, production, Docker)
How to use:
# For development (assuming you have a CJS-based entrypoint)node --require ./instrumentation.cjs your-app.js
# For production (in package.json scripts)"start": "NODE_OPTIONS='--import ./instrumentation.cjs' astro dev"Features:
- ✅ Follows SigNoz documentation exactly
- ✅ Production-ready and development-friendly
- ✅ Environment variable based configuration
- ✅ Automatic fallback to console export for development
- ✅ Proper resource configuration and graceful shutdown
🔧 Supporting Files
Section titled “🔧 Supporting Files”3. src/lib/observability/ Directory
Section titled “3. src/lib/observability/ Directory”Purpose: Additional observability utilities and helpers
Files:
Section titled “Files:”astro-integration.ts: Astro middleware for request tracingapi-instrumentation.ts: Wrapper functions for API routesnode-metrics.ts: Custom business metricssrc/utils/logger.ts: Main structured logger with trace correlation
🚀 Usage Patterns
Section titled “🚀 Usage Patterns”For Production (Recommended)
Section titled “For Production (Recommended)”Use the JavaScript implementation with environment variables:
# Set SigNoz configurationexport OTEL_EXPORTER_OTLP_ENDPOINT="https://ingest.us.signoz.cloud:443"export OTEL_EXPORTER_OTLP_HEADERS="signoz-ingestion-key=your-key"export OTEL_SERVICE_NAME="erp-unlocked-backend"
# Run with tracing (as configured in package.json)npm startFor Development
Section titled “For Development”Use console export for local testing:
export OTEL_CONSOLE_EXPORT=trueexport ENABLE_OBSERVABILITY=true
# Run normally - traces will appear in consolenpm run devFor No-Code Instrumentation
Section titled “For No-Code Instrumentation”Follow SigNoz’s no-code approach:
export OTEL_TRACES_EXPORTER="otlp"export OTEL_EXPORTER_OTLP_ENDPOINT="https://ingest.us.signoz.cloud:443"export OTEL_NODE_RESOURCE_DETECTORS="env,host,os"export OTEL_SERVICE_NAME="erp-unlocked-backend"export OTEL_EXPORTER_OTLP_HEADERS="signoz-ingestion-key=your-key"export NODE_OPTIONS="--require @opentelemetry/auto-instrumentations-node/register"
npm start🐛 Troubleshooting
Section titled “🐛 Troubleshooting”TypeScript Compilation
Section titled “TypeScript Compilation”For production deployment:
- Compile TypeScript:
npx tsc - The compiled
instrumentation.jswill be used in production - TypeScript provides better development experience and type safety
No Traces Appearing
Section titled “No Traces Appearing”- Check environment variables are set correctly
- Verify SigNoz endpoint and ingestion key
- Enable console export for debugging:
OTEL_CONSOLE_EXPORT=true
Performance Impact
Section titled “Performance Impact”- The instrumentation has minimal performance impact
- Metrics collection is optional and can be disabled
- Console export should only be used in development
📋 Migration Notes
Section titled “📋 Migration Notes”From Legacy Implementation
Section titled “From Legacy Implementation”- REMOVED (was redundant)src/lib/observability/node-tracer.ts- REMOVED (replaced bysrc/lib/observability/node-logger.tssrc/utils/logger.ts)- REMOVED (redundant with CJS version)tracing.js- Single
instrumentation.cjsfile following SigNoz documentation - Environment variables updated to SigNoz standards
Environment Variable Changes
Section titled “Environment Variable Changes”SIGNOZ_OTLP_ENDPOINT→OTEL_EXPORTER_OTLP_ENDPOINTSIGNOZ_API_KEY→ Include inOTEL_EXPORTER_OTLP_HEADERS- Added
OTEL_TRACES_EXPORTER=otlp - Added
OTEL_NODE_RESOURCE_DETECTORS=env,host,os
📝 Logging Integration
Section titled “📝 Logging Integration”Node.js Logging (src/utils/logger.ts)
Section titled “Node.js Logging (src/utils/logger.ts)”Purpose: SigNoz-compatible logging using Pino following SigNoz Pino documentation.
Key Features:
- Pino Transport: Uses
pino-opentelemetry-transportfor SigNoz integration - Development Mode: Pretty printing for local development
- Production Mode: JSON structured logs sent to SigNoz
- Trace Correlation: Automatic correlation with OpenTelemetry traces
- Environment Detection: Fallback for browser environments
Usage:
import { logger } from '@/utils/logger';
// Structured logging with metadatalogger.info('Order processed', { orderId: '123', amount: 99.99, customerId: 'cust-456',});
// Error logging with full contextlogger.error('Payment failed', error, { orderId: '456', paymentMethod: 'credit_card',});Environment Variables:
ENABLE_SIGNOZ_LOGS: Enable SigNoz transport (default: true in production)LOG_LEVEL: Set log level (debug, info, warn, error)OTEL_SERVICE_NAME: Service name for log correlationOTEL_EXPORTER_OTLP_LOGS_ENDPOINT: SigNoz logs endpoint
Python Logging (pdf_processor_service/app/utils/logging.py)
Section titled “Python Logging (pdf_processor_service/app/utils/logging.py)”Purpose: SigNoz-compatible structured logging with OpenTelemetry correlation.
Key Features:
- Structured JSON: SigNoz-compatible JSON formatting
- OpenTelemetry Correlation: Automatic trace and span ID correlation
- Service Context: Automatic service metadata injection
- Flexible Configuration: Environment-based configuration
Usage:
from app.utils.logging import logger, structured_logger
# Basic logging with contextlogger.info("PDF processing started", extra={ "document_id": "doc-123", "file_size": 2048576})
# Structured loggingstructured_logger.info( "pdf_extraction_completed", document_id="doc-123", processing_time=5.2, orders_extracted=15)✅ Current Status
Section titled “✅ Current Status”- Single Implementation: ✅
instrumentation.cjsonly - Production & Development Ready: ✅ Works in all environments
- SigNoz Aligned: ✅ Follows official JavaScript documentation
- TypeScript Support: ✅ Astro/Vite handles module loading
- Backwards Compatible: ✅ Legacy environment variables still work
- Logging Integration: ✅ SigNoz-compatible logging for Node.js (Pino) and Python