cloki
Version:
LogQL API with Clickhouse Backend
57 lines (52 loc) • 1.61 kB
JavaScript
const {
dropAlertViews,
incAlertMark,
createMarksTable
} = require('../../clickhouse_alerting')
const transpiler = require('../../../../parser/transpiler')
const { getClickhouseStream, preprocessStream } = require('../../clickhouse')
const AlertWatcher = require('./alertWatcher')
class CallbackLogAlertWatcher extends AlertWatcher {
_dropViews () {
return dropAlertViews(this.nsName, this.group.name, this.rule.alert)
}
async _createViews () {
return createMarksTable(this.nsName, this.group.name, this.rule.alert)
}
/**
* @return {Promise<number>}
* @private
*/
async _checkViews () {
this.lastCheck = this.lastCheck || Date.now()
const lastMark = this.lastCheck
let newMark = 0
const query = transpiler.transpile({
query: this.rule.expr,
rawRequest: true,
start: `${lastMark}000000`,
end: newMark + '000000',
limit: 1000
})
const _stream = await getClickhouseStream(query)
const stream = preprocessStream(_stream, query.stream)
let alerts = []
for await (const e of stream.toGenerator()()) {
if (!e || !e.labels) {
continue
}
newMark = Math.max(newMark, Math.floor(parseInt(e.timestamp_ns) / 1000000))
alerts.push(e)
if (alerts.length > 100) {
await this.sendTextAlerts(alerts)
alerts = []
}
}
await this.sendTextAlerts(alerts)
alerts = []
newMark = newMark || lastMark
const marks = await incAlertMark(this.nsName, this.group.name, this.rule.alert, newMark)
return marks[1]
}
}
module.exports = CallbackLogAlertWatcher