-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #304 from metrico/metrics_agg_improvements
Metrics agg improvements
- Loading branch information
Showing
6 changed files
with
231 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const optimizations = [require('./optimization_v3_2')] | ||
|
||
module.exports = { | ||
/** | ||
* | ||
* @param token {Token} | ||
* @param fromNS {number} | ||
* @param toNS {number} | ||
* @param stepNS {number} | ||
*/ | ||
apply: (token, fromNS, toNS, stepNS) => { | ||
const optimization = optimizations.find((opt) => opt.isApplicable(token, fromNS / 1000000)) | ||
if (optimization) { | ||
return optimization.apply(token, fromNS, toNS, stepNS) | ||
} | ||
return null | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
parser/registry/smart_optimizations/log_range_agg_reg_v3_2.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const { getDuration } = require('../common') | ||
const Sql = require('@cloki/clickhouse-sql') | ||
module.exports = { | ||
/** | ||
* | ||
* @param token {Token} | ||
* @param query {Select} | ||
* @returns {Select} | ||
*/ | ||
rate: (token, query) => { | ||
const duration = getDuration(token) | ||
return genericRate(new Sql.Raw(`toFloat64(countMerge(count)) * 1000 / ${duration}`), token, query) | ||
}, | ||
|
||
/** | ||
* | ||
* @param token {Token} | ||
* @param query {Select} | ||
* @returns {Select} | ||
*/ | ||
count_over_time: (token, query) => { | ||
return genericRate(new Sql.Raw('toFloat64(countMerge(count))'), token, query) | ||
}, | ||
|
||
/** | ||
* | ||
* @param token {Token} | ||
* @param query {Select} | ||
* @returns {Select} | ||
*/ | ||
bytes_rate: (token, query) => { | ||
const duration = getDuration(token, query) | ||
return genericRate(new Sql.Raw(`toFloat64(sum(bytes) * 1000 / ${duration})`), token, query) | ||
}, | ||
/** | ||
* | ||
* @param token {Token} | ||
* @param query {Select} | ||
* @returns {Select} | ||
*/ | ||
bytes_over_time: (token, query) => { | ||
return genericRate(new Sql.Raw('toFloat64(sum(bytes))'), token, query) | ||
} | ||
} | ||
|
||
const genericRate = (valueExpr, token, query) => { | ||
const duration = getDuration(token) | ||
query.ctx.matrix = true | ||
query.ctx.duration = duration | ||
query.limit(undefined, undefined) | ||
const tsGroupingExpr = new Sql.Raw(`intDiv(timestamp_ns, ${duration}000000) * ${duration}`) | ||
query.select([tsGroupingExpr, 'timestamp_ns'], [valueExpr, 'value']) | ||
.groupBy('fingerprint', 'timestamp_ns') | ||
.orderBy(['fingerprint', 'asc'], ['timestamp_ns', 'asc']) | ||
const step = query.ctx.step | ||
if (step <= duration) { | ||
return query | ||
} | ||
const rateC = (new Sql.Select()) | ||
.select( | ||
'labels', | ||
[new Sql.Raw(`intDiv(timestamp_ns, ${step}) * ${step}`), 'timestamp_ns'], | ||
[new Sql.Raw('argMin(rate_b.value, rate_b.timestamp_ns)'), 'value'] | ||
) | ||
.from('rate_b') | ||
.groupBy('fingerprint', 'timestamp_ns') | ||
.orderBy(['fingerprint', 'asc'], ['timestamp_ns', 'asc']) | ||
return rateC.with(new Sql.With('rate_b', query)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const { getDuration } = require('../common') | ||
const reg = require('./log_range_agg_reg_v3_2') | ||
const Sql = require('@cloki/clickhouse-sql') | ||
const { DATABASE_NAME, checkVersion } = require('../../../lib/utils') | ||
const streamSelectorReg = require('../stream_selector_operator_registry') | ||
const aggOpReg = require('../high_level_aggregation_registry') | ||
|
||
/** | ||
* | ||
* @param token {Token} | ||
* @param fromMS {number} | ||
* @returns {boolean} | ||
*/ | ||
module.exports.isApplicable = (token, fromMS) => { | ||
let logAggFn = token.Child('log_range_aggregation_fn') | ||
logAggFn = logAggFn ? logAggFn.value : null | ||
if (!logAggFn) { | ||
return false | ||
} | ||
const durationMs = getDuration(token) | ||
return checkVersion('v3_2', fromMS) && | ||
!isLogPipeline(token) && reg[logAggFn] && durationMs >= 15000 && durationMs % 15000 === 0 | ||
} | ||
|
||
function isLogPipeline (token) { | ||
let isPipeline = false | ||
for (const pipeline of token.Children('log_pipeline')) { | ||
isPipeline |= !pipeline.Child('line_filter_operator') || | ||
!(pipeline.Child('line_filter_operator').value === '|=' && | ||
['""', '``'].includes(pipeline.Child('quoted_str').value)) | ||
} | ||
return isPipeline | ||
} | ||
|
||
/** | ||
* | ||
* @param token {Token} | ||
* @param fromNS {number} | ||
* @param toNS {number} | ||
* @param stepNS {number} | ||
*/ | ||
module.exports.apply = (token, fromNS, toNS, stepNS) => { | ||
fromNS = Math.floor(fromNS / 15000000000) * 15000000000 | ||
const tsClause = toNS | ||
? Sql.between('samples.timestamp_ns', fromNS, toNS) | ||
: Sql.Gt('samples.timestamp_ns', fromNS) | ||
let q = (new Sql.Select()) | ||
.select(['samples.fingerprint', 'fingerprint']) | ||
.from([`${DATABASE_NAME()}.metrics_15s`, 'samples']) | ||
.where(tsClause) | ||
q.join(`${DATABASE_NAME()}.time_series`, 'left any', | ||
Sql.Eq('samples.fingerprint', new Sql.Raw('time_series.fingerprint'))) | ||
q.select([new Sql.Raw('any(JSONExtractKeysAndValues(time_series.labels, \'String\'))'), 'labels']) | ||
|
||
q.ctx = { | ||
step: stepNS / 1000000000 | ||
} | ||
|
||
for (const streamSelectorRule of token.Children('log_stream_selector_rule')) { | ||
q = streamSelectorReg[streamSelectorRule.Child('operator').value](streamSelectorRule, q) | ||
} | ||
|
||
const lra = token.Child('log_range_aggregation') | ||
q = reg[lra.Child('log_range_aggregation_fn').value](lra, q) | ||
|
||
const aggOp = token.Child('aggregation_operator') | ||
if (aggOp) { | ||
q = aggOpReg[aggOp.Child('aggregation_operator_fn').value](aggOp, q) | ||
} | ||
|
||
return q | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters