Skip to content

Commit

Permalink
Merge pull request #420 from metrico/filter_nan
Browse files Browse the repository at this point in the history
write nan values as NaN on ingestion
  • Loading branch information
akvlad authored Jan 3, 2024
2 parents e0d6b4c + e10df0b commit 7089fc3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lib/db/clickhouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ const { parseLabels, hashLabels, isCustomSamplesOrderingRule, isOmitTablesCreati

const { Worker, isMainThread } = require('worker_threads')

const jsonSerializer = (k, val) => typeof val === 'bigint' ? val.toString() : val
const jsonSerializer = (k, val) => typeof val === 'bigint'
? val.toString()
: typeof val === 'number' && isNaN(val)
? 'NaN'
: val

const createCsvArrayWriter = require('csv-writer').createArrayCsvStringifier

Expand Down
12 changes: 12 additions & 0 deletions lib/handlers/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ module.exports.checkCustomPlugins = async (options) => {
}
}

/**
*
* @param value {number}
* @returns {[number,boolean]} new value and if I should ingest it
*/
module.exports.checkNanValue = (value) => {
if (typeof value === 'number' && isNaN(value)) {
return [NaN, true]
}
return [value, true]
}

const newResponse = () => {
let head = null
let onWriteHead = new EventEmitter()
Expand Down
6 changes: 4 additions & 2 deletions lib/handlers/influx_write.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const DATABASE = require('../db/clickhouse')
const { bulk_labels, bulk, labels } = DATABASE.cache
const { fingerPrint } = require('../utils')
const { readonly } = require('../../common')
const { checkNanValue } = require('./common')

async function handler (req, res) {
if (!req.body && !req.body.metrics) {
Expand Down Expand Up @@ -108,13 +109,14 @@ async function handler (req, res) {
asyncLogError('no bulkable data', req.log)
return res.code(204).send('')
}
if (typeof value === 'number' && isNaN(value)) {
const [_value, ingest] = checkNanValue(value)
if (!ingest) {
return
}
const values = [
finger,
BigInt(pad('0000000000000000000', timestamp, true)),
value || 0,
_value || 0,
key || ''
]
bulk.add([values])
Expand Down
6 changes: 4 additions & 2 deletions lib/handlers/prom_push.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const DATABASE = require('../db/clickhouse')
const { bulk_labels, bulk, labels } = DATABASE.cache
const { fingerPrint } = require('../utils')
const { readonly } = require('../../common')
const { checkNanValue } = require('./common')

async function handler (req, res) {
const self = this
Expand Down Expand Up @@ -59,14 +60,15 @@ async function handler (req, res) {
asyncLogError({ entry }, req.log)
return
}
if (isNaN(entry.value)) {
const [value, ingest] = checkNanValue(entry.value)
if (!ingest) {
return
}
const ts = BigInt(entry.timestamp)
const values = [
finger,
ts,
entry.value,
value,
JSONLabels.__name__ || 'undefined'
]
dates[
Expand Down

0 comments on commit 7089fc3

Please sign in to comment.