0.4.1 • Published 9 months ago
@autonomize/otlp v0.4.1
Autonomize OTLP (@autonomize/otlp)
A lightweight, easy-to-use OpenTelemetry SDK for Node.js and Nest.js applications. This SDK provides unified instrumentation for traces, metrics, and logs with automatic context propagation and built-in support for popular frameworks.
Features
Metrics Collection
- Track custom metrics with counters, histograms, and up/down counters
- Configurable metric export intervals
- Pre-configured metric attributes
Distributed Tracing
- Automatic trace context propagation
- Built-in span creation and management
- Custom attribute support
- Support for error recording
Structured Logging
- Automatic trace correlation
- Multiple severity levels (INFO, ERROR, WARN, DEBUG)
- Custom attributes support
- Error stack trace handling
Auto-Instrumentation
- HTTP client and server
- Express.js framework
- NestJS core components
- Custom request hooks
- Health check endpoint filtering
Installation
# Using npm
npm install @autonomize/otlp
# Using yarn
yarn add @autonomize/otlp
# Using pnpm
pnpm add @autonomize/otlp
Quick Start
import { Telemetry } from '@autonomize/otlp';
// Initialize telemetry
const telemetry = new Telemetry({
serviceName: 'your-service-name',
environment: 'production',
version: '1.0.0',
otlpEndpoint: 'http://localhost:4318'
});
// Start telemetry services
await telemetry.start();
// Create metrics
const requestCounter = telemetry.createCounter({
name: 'http_requests_total',
description: 'Total number of HTTP requests',
unit: 'requests'
});
const responseTime = telemetry.createHistogram({
name: 'http_response_time',
description: 'HTTP response time',
unit: 'ms'
});
// Create spans for tracing
await telemetry.createSpan('operation-name', async () => {
// Your operation code here
telemetry.addAttributes({
'custom.attribute': 'value'
});
});
// Add logs with trace context
telemetry.info('Operation started', { operationType: 'process' });
// Error handling with trace correlation
try {
// Your code
} catch (error) {
telemetry.error('Operation failed', error, {
operationType: 'process'
});
telemetry.recordError(error);
}
// Shutdown gracefully
await telemetry.shutdown();
Configuration
TelemetryConfig Options
Option | Type | Required | Default | Description |
---|---|---|---|---|
serviceName | string | Yes | - | Unique identifier for your service |
environment | string | Yes | - | Deployment environment (e.g., 'production') |
version | string | No | undefined | Service version |
otlpEndpoint | string | No | 'http://localhost:4318' | OpenTelemetry collector endpoint |
metricIntervalMs | number | No | 5000 | Metric export interval in milliseconds |
interface TelemetryConfig {
serviceName: string; // Required: Service identifier
environment: string; // Required: Deployment environment
version?: string; // Optional: Service version
otlpEndpoint?: string; // Optional: OpenTelemetry collector endpoint
metricIntervalMs?: number; // Optional: Metric export interval
}
API Reference
Metrics
Metric Type | Description | Usage Example |
---|---|---|
Counter | Only increases | telemetry.createCounter(...) |
Histogram | Distribution of values | telemetry.createHistogram(...) |
UpDownCounter | Can increase/decrease | telemetry.createUpDownCounter(...) |
// Create a counter (only increases)
const counter = telemetry.createCounter({
name: 'metric_name',
description: 'metric description',
unit: 'unit'
});
// Create a histogram (distribution of values)
const histogram = telemetry.createHistogram({
name: 'metric_name',
description: 'metric description',
unit: 'unit'
});
// Create an up/down counter (can increase/decrease)
const upDownCounter = telemetry.createUpDownCounter({
name: 'metric_name',
description: 'metric description',
unit: 'unit'
});
Tracing
// Create and manage spans
await telemetry.createSpan('operation-name', async () => {
// Operation code
}, {
'custom.attribute': 'value'
});
// Add attributes to current span
telemetry.addAttributes({
'attribute.key': 'value'
});
// Record errors in current span
telemetry.recordError(error);
Logging
Log Level | Method | Description |
---|---|---|
INFO | telemetry.info() | General information messages |
ERROR | telemetry.error() | Error messages with stack traces |
WARN | telemetry.warn() | Warning messages |
DEBUG | telemetry.debug() | Debug information |
// Log levels with optional attributes
telemetry.info('message', attributes);
telemetry.error('message', error, attributes);
telemetry.warn('message', attributes);
telemetry.debug('message', attributes);
NestJS Integration
// main.ts
import { Telemetry } from '@autonomize/otlp';
async function bootstrap() {
const telemetry = new Telemetry({
serviceName: 'nest-service',
environment: process.env.NODE_ENV
});
await telemetry.start();
const app = await NestFactory.create(AppModule);
await app.listen(3000);
// Graceful shutdown
process.on('SIGTERM', async () => {
await telemetry.shutdown();
await app.close();
});
}
Requirements
Requirement | Version |
---|---|
Node.js | >= 14.x |
OpenTelemetry Collector | Latest stable |
Dependencies
Package | Version |
---|---|
@opentelemetry/api | ^1.7.0 |
@opentelemetry/sdk-node | ^0.54.1 |
@opentelemetry/auto-instrumentations-node | ^0.52.0 |
@opentelemetry/instrumentation-nestjs-core | ^0.41.0 |
See package.json
for the complete list of dependencies.
Support
For issues and feature requests, please file an issue.
Additional Resources
License
MIT License - see LICENSE for details.