forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.ingest.ts
More file actions
24 lines (20 loc) · 770 Bytes
/
Copy pathanalytics.ingest.ts
File metadata and controls
24 lines (20 loc) · 770 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Kafka consumer for metrics ingestion
import { Kafka, logLevel } from 'kafkajs';
import { PlatformMetrics } from './analytics.metrics';
const kafka = new Kafka({
clientId: 'analytics-engine',
brokers: [process.env.KAFKA_BROKER || 'localhost:9092'],
logLevel: logLevel.ERROR,
});
const consumer = kafka.consumer({ groupId: 'analytics-group' });
export async function startMetricsIngestion(onMetric: (metric: PlatformMetrics) => void) {
await consumer.connect();
await consumer.subscribe({ topic: 'platform-metrics', fromBeginning: false });
await consumer.run({
eachMessage: async ({ message }) => {
if (!message.value) return;
const metric: PlatformMetrics = JSON.parse(message.value.toString());
onMetric(metric);
},
});
}