Documents the monitoring stack delivered by BIT-14225: structured logging, Prometheus metrics, health/metrics HTTP endpoints, and webhook alerting. All implementation files were committed in prior commits (b095214,de9fff6, etc.); this adds the ops reference doc. Paperclip-Task-Id: BIT-14225 Paperclip-Run-Id: $PAPERCLIP_RUN_ID
6.8 KiB
Observability Foundation — BIT-14225
This document describes the observability foundation (logs, metrics, alerts) for ai-news-writer as part of BIT-2708.3.
Overview
The ai-news-writer application includes a comprehensive observability foundation with:
- Metrics: In-process collection with Prometheus-compatible exposition
- Structured Logging: JSON-formatted logs with correlation IDs
- Health Checks: HTTP endpoints for monitoring and load balancers
- Alerting: Webhook-based pipeline alerts for critical failures
Components
1. Metrics System
Location: src/monitoring/metrics.js
Features:
- Counters: Increment-only metrics (articles generated, errors, etc.)
- Histograms: Latency and size distributions with percentiles
- Gauges: Point-in-time values (memory usage, queue depth, etc.)
- System Metrics: Automatic CPU, memory, and load avg tracking
- Prometheus Exposition: OpenMetrics 0.0.4 text format for scraping
Key Metrics:
articles.generated,articles.published,articles.failedpipeline.*- RSS fetch, scrape, parse, publish operationsquality.score- Article quality assessmentprocess.memory.*,process.cpu.*,process.loadavg*
2. Pipeline-Specific Metrics
Location: src/monitoring/pipeline-metrics.js
Features:
- Source-specific fetch tracking with success/failure rates
- Scrape quality and extraction strategy metrics
- Parse operation tracking by type (RSS, HTML, JSON)
- Circuit breaker state transitions
- Rate limit and backoff tracking
- Queue depth and concurrency monitoring
3. Structured Logging
Location: src/logger/structured.js
Features:
- Winston-based structured JSON logging
- Correlation ID support for request tracing
- Multiple transports: console + file (combined.log, error.log)
- Log levels: debug, info, warn, error
- Module-based context for better filtering
Log Files:
logs/combined.log- All log levelslogs/error.log- Error level only
4. Health Endpoints
Location: src/http/status-server.js
Endpoints:
GET /health- JSON health status with metrics summaryGET /metrics- Full metrics export (JSON or Prometheus format)GET /- Read-only ops landing page
Health Response Includes:
- Service version and uptime
- RSS listener status and last tick metrics
- WordPress and OpenAI configuration status
- Metrics summary (key counters and gauges)
- Feature flags (RSS count, X queries, etc.)
5. Alerting System
Location: src/monitoring/pipeline-alert.js
Features:
- Webhook-based alerting for critical pipeline failures
- Configurable via
PIPELINE_ALERT_WEBHOOK_URLenvironment variable - Never throws or sends secrets
- Tracks alert firing success/failure metrics
Alert Types:
- WordPress publish failures (both write and listen modes)
- Pipeline errors requiring operator attention
Integration Points
Application Integration
The observability components are integrated throughout the application:
-
Main Index (
src/index.js):- Article generation and publishing metrics
- Pipeline alerting on WordPress failures
- Quality scoring metrics
-
RSS Listener (
src/listener/rss-listener.js):- Tick duration and article processing metrics
- Filtering and topic detection metrics
-
RSS Crawler (
src/crawler/rss.js):- Source fetch success/failure tracking
- Circuit breaker metrics
- Domain filtering metrics
-
WordPress Publisher (
src/publisher/wordpress.js):- Publish duration and error metrics
- Transport-specific metrics (REST vs XML-RPC)
-
XML-RPC Client (
src/publisher/xmlrpc-client.js):- XML-RPC call metrics and error tracking
Configuration
Environment Variables:
# Logging
LOG_LEVEL=info # debug, info, warn, error
# Metrics
# No special configuration needed - metrics are always collected
# Alerting
PIPELINE_ALERT_WEBHOOK_URL=https://your-webhook-url.com/alerts
PIPELINE_ALERT_WEBHOOK_TIMEOUT_MS=4000
# HTTP Server
HOST=0.0.0.0
PORT=3200
HTTP_REQUEST_TIMEOUT_MS=120000
HTTP_HEADERS_TIMEOUT_MS=125000
Monitoring Stack Integration
Prometheus Scraping
Configure Prometheus to scrape the metrics endpoint:
scrape_configs:
- job_name: ai-news-writer
metrics_path: /metrics?format=prometheus
static_configs:
- targets: ['10.10.50.23:3200'] # Adjust to LXC IP
Suggested Alerts
# WordPress publish failures
- alert: AINewsWordPressPublishFailures
expr: rate(ainews_articles_failed_total[5m]) > 0
for: 5m
labels:
severity: warning
annotations:
summary: "AI News Writer WordPress publish failures detected"
# Pipeline errors
- alert: AINewsPipelineErrors
expr: rate(ainews_pipeline_publish_errors_total[5m]) > 0
for: 5m
labels:
severity: warning
annotations:
summary: "AI News Writer pipeline errors detected"
# RSS listener failures
- alert: AINewsRSSListenerFailure
expr: ainews_rss_listener_last_tick_ok == 0
for: 15m
labels:
severity: critical
annotations:
summary: "AI News Writer RSS listener failed"
Log Aggregation
Configure log shippers (Promtail, Filebeat) to collect from:
/opt/bitreport/ai-news-writer/logs/combined.log/opt/bitreport/ai-news-writer/logs/error.log
Verification
Local Testing
# Start in listen mode
npm run listen
# Check health endpoint
curl -sS "http://localhost:3200/health" | jq '.metrics_summary'
# Check metrics endpoint (JSON)
curl -sS "http://localhost:3200/metrics"
# Check metrics endpoint (Prometheus format)
curl -sS "http://localhost:3200/metrics?format=prometheus"
# Check logs
cat logs/combined.log
Deployment Verification
# Verify deployment with smoke tests
npm run verify:deploy -- "http://<lxc-ip>:3200"
# Full ops check (includes tests)
bash tools/ops-check.sh
Testing
The observability foundation includes comprehensive tests:
src/monitoring/metrics.test.js- Metrics collection and exportsrc/monitoring/pipeline-metrics.test.js- Pipeline-specific metricssrc/monitoring/pipeline-alert.test.js- Alerting systemsrc/logger/structured.test.js- Structured logging
Run tests with:
npm test
Future Enhancements
Potential improvements for future iterations:
- Distributed Tracing: Integrate with OpenTelemetry for cross-service tracing
- Advanced Alerting: More sophisticated alert conditions and routing
- Custom Dashboards: Pre-configured Grafana dashboards for common workflows
- Log Correlation: Enhanced correlation between logs and metrics
- Performance Profiling: CPU and memory profiling endpoints
References
- Runbook:
docs/runbooks/OBSERVABILITY_BIT3601.md - Architecture:
README.md§ Architecture - Deployment:
docs/runbooks/DEPLOY_LXC.md - Backup Strategy:
docs/BACKUP_STRATEGY.md