Files
FarmaFinder/apps/backend/redis-client.js
T
Antoni Nuñez Romeu 295e5cd8fb feat: add end-to-end observability (metrics, health, mobile RUM, dashboards, alerts)
- Backend: OTel metrics via OTLP -> Alloy -> Prometheus (OTEL_METRICS_EXPORTER=otlp)
- New business metrics (src/metrics.js): searches, CIMA latency/errors, cache
  hits/misses, logins, rate-limits, pharmacy writes/links, push sent/failed,
  DB + Redis timings/errors, HTTP req count/duration, heartbeat
- Backend health endpoints /healthz and /readyz
- Mobile (Expo): Grafana Faro RUM via @grafana/faro-react-native
- redis/postgres exporters in docker-compose + Prometheus scrape jobs
- Grafana dashboards (backend, datastores, mobile RUM, overview)
- Prometheus alert rules (farmafinder_*) -> existing Alertmanager (Telegram)
- Design/spec saved to docs/superpowers/specs/
2026-07-16 10:13:04 +02:00

49 lines
1.2 KiB
JavaScript

import { createClient } from 'redis';
import * as appMetrics from './src/metrics.js';
// Create Redis client
const redisClient = createClient({
socket: {
host: process.env.REDIS_HOST || 'localhost',
port: process.env.REDIS_PORT || 6379
},
password: process.env.REDIS_PASSWORD || undefined
});
// Error handler
redisClient.on('error', (err) => {
appMetrics.redisErrorsTotal.add(1);
console.error('Redis Client Error:', err);
});
// Connection handler
redisClient.on('connect', () => {
console.log('✅ Connected to Redis');
});
// Instrument get/setEx with command duration (used by the CIMA cache path).
const origGet = redisClient.get.bind(redisClient);
redisClient.get = async (...args) => {
const start = performance.now();
try {
return await origGet(...args);
} finally {
appMetrics.redisCmdDuration.record(performance.now() - start);
}
};
const origSetEx = redisClient.setEx.bind(redisClient);
redisClient.setEx = async (...args) => {
const start = performance.now();
try {
return await origSetEx(...args);
} finally {
appMetrics.redisCmdDuration.record(performance.now() - start);
}
};
// Connect to Redis
await redisClient.connect();
export default redisClient;