added LOKI_FLUSH_INTERVAL_MS

This commit is contained in:
Bojan Kucera 2025-06-03 15:29:45 -04:00
parent 4ab83d1dc2
commit 62baac7640
5 changed files with 6 additions and 87 deletions

View file

@ -1,3 +1,3 @@
export * from './dateUtils';
export * from './logger';
export * from './lokiClient.ts';
export * from './calculations/index';

View file

@ -1,86 +0,0 @@
/**
* Loki client for sending logs to Grafana Loki
*/
import { LoggingConfig } from '@stock-bot/config';
export class LokiClient {
private batchQueue: any[] = [];
private flushInterval: NodeJS.Timeout;
private lokiUrl: string;
private authHeader?: string;
constructor(private config: LoggingConfig) {
const { host, port, username, password } = config.loki;
this.lokiUrl = `http://${host}:${port}/loki/api/v1/push`;
if (username && password) {
const authString = Buffer.from(`${username}:${password}`).toString('base64');
this.authHeader = `Basic ${authString}`;
}
this.flushInterval = setInterval(
() => this.flush(),
config.loki.flushIntervalMs
);
}
async log(level: string, message: string, serviceName: string, labels: Record<string, string> = {}) {
const timestamp = Date.now() * 1000000; // Loki expects nanoseconds
this.batchQueue.push({
streams: [{
stream: {
level,
service: serviceName,
...this.config.loki.labels,
...labels,
},
values: [[`${timestamp}`, message]],
}],
});
if (this.batchQueue.length >= this.config.loki.batchSize) {
await this.flush();
}
}
private async flush() {
if (this.batchQueue.length === 0) return;
try {
const headers: Record<string, string> = {
'Content-Type': 'application/json',
};
if (this.authHeader) {
headers['Authorization'] = this.authHeader;
}
const response = await fetch(this.lokiUrl, {
method: 'POST',
headers,
body: JSON.stringify({
streams: this.batchQueue.flatMap(batch => batch.streams),
}),
});
if (!response.ok) {
console.error(`Failed to send logs to Loki: ${response.status} ${response.statusText}`);
const text = await response.text();
if (text) {
console.error(text);
}
}
} catch (error) {
console.error('Error sending logs to Loki:', error);
} finally {
this.batchQueue = [];
}
}
async destroy() {
clearInterval(this.flushInterval);
return this.flush();
}
}