@nokkel/monitoring
v1.0.1
Published
A monitoring library for integrating monitoring and observability platforms and generating logs for monitoring
Readme
Nokkel Monitoring Library
A monitoring library for integrating monitoring and observability platforms and generating logs for monitoring.
How to use the library
To install this library follow the steps below:
npm add @nokkel/monitoringOnce installed you can use the library to do the following:
logger.ts
import { createLogger } from '@nokkel/monitoring';
export const logger = createLogger({
name: process.env.HOSTNAME,
logLevel: process.env.LOG_LEVEL,
// Can optionally configure the logger for the browser
// isBrowser: true,
});Enjoy!
Grafana Stack (Loki, Prometheus, Tempo)
Loki logging
logger.loki.ts
import { createLokiLogger } from '@nokkel/monitoring/grafana';
export const logger = createLokiLogger({
name: process.env.SERVICE_NAME || 'my-service',
logLevel: (process.env.LOG_LEVEL as any) || 'info',
loki: {
host: process.env.LOKI_URL || 'http://localhost:3100',
labels: { service: 'my-service', env: process.env.NODE_ENV || 'dev' },
},
});
logger.info({ msg: 'hello loki' });Alternatively, compose your own pino with a Loki destination:
import { createLogger } from '@nokkel/monitoring';
import { createLokiDestination } from '@nokkel/monitoring/grafana';
const destination = createLokiDestination({ host: 'http://localhost:3100' });
export const logger = createLogger({ name: 'svc', logLevel: 'info', destination });Prometheus metrics
Expose a /metrics endpoint Prometheus/Grafana Agent can scrape.
import { initMetrics, startMetricsServer, promClient } from '@nokkel/monitoring/metrics';
// set default labels and collect Node process metrics
initMetrics({ defaultLabels: { service: 'my-service' } });
// define custom metrics
export const requests = new promClient.Counter({ name: 'http_requests_total', help: 'Total HTTP requests' });
// start a metrics server on :9464/metrics
startMetricsServer({ port: 9464 });OpenTelemetry tracing (Tempo)
Send traces via OTLP HTTP to Grafana Agent or Tempo.
import { initTracing } from '@nokkel/monitoring/tracing';
await initTracing({
serviceName: 'my-service',
serviceVersion: '1.0.0',
environment: process.env.NODE_ENV || 'dev',
otlpUrl: process.env.OTLP_TRACES_URL || 'http://localhost:4318/v1/traces',
});
// Your app code; auto-instrumentations cover http/express/etc.Notes:
- Loki logging uses
pino-loki. Ensure Grafana Loki accepts pushes atLOKI_URL. - Metrics use
prom-clientand are scrape-based (no Pushgateway assumed). - Traces use OpenTelemetry OTLP/HTTP; point
OTLP_TRACES_URLto Grafana Agent/Tempo.
