Skip to content

Commit

Permalink
#fix: error return
Browse files Browse the repository at this point in the history
  • Loading branch information
akvlad committed Oct 16, 2023
1 parent 31de334 commit c1c5034
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 42 deletions.
2 changes: 1 addition & 1 deletion lib/handlers/prom_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function handler (req, res) {
}).send(response)
} catch (err) {
asyncLogError(err, req.log)
return res.send(empty)
return res.code(500).send(JSON.stringify({ status: 'error', error: err.message }))
}
}

Expand Down
10 changes: 7 additions & 3 deletions lib/handlers/prom_query_range.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ async function handler (req, res) {
const endMs = parseInt(req.query.end) * 1000 || Date.now()
const stepMs = parseInt(req.query.step) * 1000 || 15000
const query = req.query.query
const result = await rangeQuery(query, startMs, endMs, stepMs)
console.log(JSON.stringify(result))
return res.code(200).send(result)
try {
const result = await rangeQuery(query, startMs, endMs, stepMs)
console.log(JSON.stringify(result))
return res.code(200).send(result)
} catch (err) {
return res.code(500).send(JSON.stringify({ status: 'error', error: err.message }))
}
}

module.exports = handler
28 changes: 16 additions & 12 deletions lib/handlers/prom_series.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ async function handler (req, res) {
const startMs = req.query.start ? parseInt(req.query.start) * 1000 : Date.now() - 7 * 24 * 3600 * 1000
const endMs = req.query.end ? parseInt(req.query.end) * 1000 : Date.now() - 7 * 24 * 3600 * 1000
const result = []
query = query.map(async (m) => {
const _result = await series(m, startMs, endMs)
result.push.apply(result, _result)
})
await Promise.all(query)
return res.code(200).headers({
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': CORS
}).send(JSON.stringify({
status: 'success',
data: result
}))
try {
query = query.map(async (m) => {
const _result = await series(m, startMs, endMs)
result.push.apply(result, _result)
})
await Promise.all(query)
return res.code(200).headers({
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': CORS
}).send(JSON.stringify({
status: 'success',
data: result
}))
} catch (err) {
return res.code(500).send(JSON.stringify({ status: 'error', error: err.message }))
}
}

module.exports = handler
71 changes: 48 additions & 23 deletions promql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const { rawRequest } = require('../lib/db/clickhouse')
const { DATABASE_NAME } = require('../lib/utils')
const { clusterName } = require('../common')
const _dist = clusterName ? '_dist' : ''

class PSQLError extends Error {}
module.exports.PSQLError = PSQLError

/**
*
* @param query {string}
Expand All @@ -12,35 +16,56 @@ const _dist = clusterName ? '_dist' : ''
* @param stepMs {number}
*/
module.exports.rangeQuery = async (query, startMs, endMs, stepMs) => {
const resp = await prometheus.pqlRangeQuery(query, startMs, endMs, stepMs, module.exports.getData)
return JSON.parse(resp)
try {
const resp = await prometheus.pqlRangeQuery(query, startMs, endMs, stepMs, module.exports.getData)
return JSON.parse(resp)
} catch (e) {
if (e instanceof prometheus.WasmError) {
throw new PSQLError(e.message)
}
throw e
}
}

module.exports.instantQuery = async (query, timeMs) => {
const resp = await prometheus.pqlInstantQuery(query, timeMs, module.exports.getData)
return JSON.parse(resp)
try {
const resp = await prometheus.pqlInstantQuery(query, timeMs, module.exports.getData)
return JSON.parse(resp)
} catch (e) {
if (e instanceof prometheus.WasmError) {
throw new PSQLError(e.message)
}
throw e
}
}

module.exports.series = async (query, fromMs, toMs) => {
const fromS = Math.floor(fromMs / 1000)
const toS = Math.floor(toMs / 1000)
const matchers = prometheus.pqlMatchers(query)
const conds = getMatchersIdxCond(matchers[0])
const idx = getIdxSubquery(conds, fromMs, toMs)
const withIdx = new Sql.With('idx', idx, !!clusterName)
const req = (new Sql.Select())
.with(withIdx)
.select([new Sql.Raw('any(labels)'), 'labels'])
.from(`time_series${_dist}`)
.where(Sql.And(
Sql.Gte('date', new Sql.Raw(`toDate(fromUnixTimestamp(${fromS}))`)),
Sql.Lte('date', new Sql.Raw(`toDate(fromUnixTimestamp(${toS}))`)),
new Sql.In('fingerprint', 'in', new Sql.WithReference(withIdx))))
.groupBy(new Sql.Raw('fingerprint'))
const data = await rawRequest(req.toString() + ' FORMAT JSON',
null,
DATABASE_NAME())
return data.data.data.map(l => JSON.parse(l.labels))
try {
const fromS = Math.floor(fromMs / 1000)
const toS = Math.floor(toMs / 1000)
const matchers = prometheus.pqlMatchers(query)
const conds = getMatchersIdxCond(matchers[0])
const idx = getIdxSubquery(conds, fromMs, toMs)
const withIdx = new Sql.With('idx', idx, !!clusterName)
const req = (new Sql.Select())
.with(withIdx)
.select([new Sql.Raw('any(labels)'), 'labels'])
.from(`time_series${_dist}`)
.where(Sql.And(
Sql.Gte('date', new Sql.Raw(`toDate(fromUnixTimestamp(${fromS}))`)),
Sql.Lte('date', new Sql.Raw(`toDate(fromUnixTimestamp(${toS}))`)),
new Sql.In('fingerprint', 'in', new Sql.WithReference(withIdx))))
.groupBy(new Sql.Raw('fingerprint'))
const data = await rawRequest(req.toString() + ' FORMAT JSON',
null,
DATABASE_NAME())
return data.data.data.map(l => JSON.parse(l.labels))
} catch (e) {
if (e instanceof prometheus.WasmError) {
throw new PSQLError(e.message)
}
throw e
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion wasm_parts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ func wrapError(err error) []byte {
}

func wrapErrorStr(err error) string {
return fmt.Sprintf(`{"status":"error", "error":%s}`, strconv.Quote(err.Error()))
//return fmt.Sprintf(`{"status":"error", "error":%s}`, strconv.Quote(err.Error()))
return err.Error()
}

func pql(c *ctx, query func() (promql.Query, error)) uint32 {
Expand Down
7 changes: 5 additions & 2 deletions wasm_parts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const WASM_URL = join(__dirname, 'main.wasm.gz')
const fs = require('fs')
const { gunzipSync } = require('zlib')

class WasmError extends Error {}
module.exports.WasmError = WasmError

let counter = 0
var go
var wasm
Expand Down Expand Up @@ -66,7 +69,7 @@ module.exports.pqlMatchers = (query) => {
ctx.write(query)
const res1 = _wasm.exports.pqlSeries(id)
if (res1 !== 0) {
throw new Error('pql failed: ', ctx.read())
throw new WasmError(ctx.read())
}
/** @type {[[[string]]]} */
const matchersObj = JSON.parse(ctx.read())
Expand All @@ -93,7 +96,7 @@ const pql = async (query, wasmCall, getData) => {
ctx.write(query)
const res1 = wasmCall(ctx)
if (res1 !== 0) {
throw new Error('pql failed: ', ctx.read())
throw new WasmError(ctx.read())
}

const matchersObj = JSON.parse(ctx.read())
Expand Down
Binary file modified wasm_parts/main.wasm.gz
Binary file not shown.

0 comments on commit c1c5034

Please sign in to comment.