-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
314 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const parser = require('../traceql/parser') | ||
|
||
it('traceql: one selector', () => { | ||
const res = parser.ParseScript('{.testId="12345"}') | ||
expect(res.rootToken.value).toEqual('{.testId="12345"}') | ||
}) | ||
|
||
it('traceql: multiple selectors', () => { | ||
const res = parser.ParseScript('{.testId="12345" &&.spanN=9}') | ||
expect(res.rootToken.value).toEqual('{.testId="12345" &&.spanN=9}') | ||
}) | ||
|
||
it('traceql: multiple selectors OR Brackets', () => { | ||
const res = parser.ParseScript('{.testId="12345" && (.spanN=9 ||.spanN=8)}') | ||
expect(res.rootToken.value).toEqual('{.testId="12345" && (.spanN=9 ||.spanN=8)}') | ||
}) | ||
|
||
it('traceql: multiple selectors regexp', () => { | ||
const res = parser.ParseScript('{.testId="12345" &&.spanN=~"(9|8)"}') | ||
expect(res.rootToken.value).toEqual('{.testId="12345" &&.spanN=~"(9|8)"}') | ||
}) | ||
|
||
it('traceql: duration', () => { | ||
const res = parser.ParseScript('{.testId="12345" && duration>=9ms}') | ||
expect(res.rootToken.value).toEqual('{.testId="12345" && duration>=9ms}') | ||
}) | ||
|
||
it('traceql: float comparison', () => { | ||
const res = parser.ParseScript('{.testId="12345" &&.spanN>=8.9}') | ||
expect(res.rootToken.value).toEqual('{.testId="12345" &&.spanN>=8.9}') | ||
}) | ||
|
||
it('traceql: count empty result', () => { | ||
const res = parser.ParseScript('{.testId="12345" &&.spanN>=8.9} | count() > 1') | ||
expect(res.rootToken.value).toEqual('{.testId="12345" &&.spanN>=8.9} | count() > 1') | ||
}) | ||
|
||
it('traceql: max duration empty result', () => { | ||
const res = parser.ParseScript('{.testId="12345" &&.spanN>=8.9} | max(duration) > 9ms') | ||
expect(res.rootToken.value).toEqual('{.testId="12345" &&.spanN>=8.9} | max(duration) > 9ms') | ||
}) | ||
|
||
it('traceql: max duration', () => { | ||
const res = parser.ParseScript('{.testId="12345" &&.spanN>=8.9} | max(duration) > 8ms') | ||
expect(res.rootToken.value).toEqual('{.testId="12345" &&.spanN>=8.9} | max(duration) > 8ms') | ||
}) |
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
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,109 @@ | ||
const AttrConditionPlanner = require('./attr_condition') | ||
const InitIndexPlanner = require('./init') | ||
const IndexGroupByPlanner = require('./group_by') | ||
const AggregatorPlanner = require('./aggregator') | ||
const IndexLimitPlanner = require('./limit') | ||
const TracesDataPlanner = require('./traces_data') | ||
|
||
/** | ||
* @param script {Token} | ||
*/ | ||
module.exports = (script) => { | ||
return new Planner(script).plan() | ||
} | ||
|
||
class Planner { | ||
/** | ||
* | ||
* @param script {Token} | ||
*/ | ||
constructor (script) { | ||
this.script = script | ||
this.termIdx = [] | ||
this.cond = null | ||
this.aggregatedAttr = '' | ||
this.cmpVal = '' | ||
this.terms = {} | ||
this.aggFn = '' | ||
} | ||
|
||
plan () { | ||
this.check() | ||
this.analyze() | ||
let res = (new AttrConditionPlanner()) | ||
.withTerms(this.termIdx) | ||
.withConditions(this.cond) | ||
.withAggregatedAttr(this.aggregatedAttr) | ||
.withMain((new InitIndexPlanner()).build()) | ||
.build() | ||
res = (new IndexGroupByPlanner()).withMain(res).build() | ||
if (this.aggFn) { | ||
res = (new AggregatorPlanner()) | ||
.withFn(this.aggFn) | ||
.withAttr(this.aggregatedAttr) | ||
.withCompareFn(this.script.Child('cmp').value) | ||
.withCompareVal(this.script.Child('cmp_val').value) | ||
.withMain(res) | ||
.build() | ||
} | ||
res = (new IndexLimitPlanner()).withMain(res).build() | ||
res = (new TracesDataPlanner()).withMain(res).build() | ||
res = (new IndexLimitPlanner()).withMain(res).build() | ||
|
||
return res | ||
} | ||
|
||
check () { | ||
if (this.script.Children('SYNTAX').length > 1) { | ||
throw new Error('more than one selector is not supported') | ||
} | ||
} | ||
|
||
analyze () { | ||
this.terms = {} | ||
this.cond = this.analyzeCond(this.script.Child('attr_selector_exp')) | ||
this.analyzeAgg() | ||
} | ||
|
||
/** | ||
* | ||
* @param token {Token} | ||
*/ | ||
analyzeCond (token) { | ||
let res = {} | ||
const complexHead = token.tokens.find(x => x.name === 'complex_head') | ||
const simpleHead = token.tokens.find(x => x.name === 'attr_selector') | ||
if (complexHead) { | ||
res = this.analyzeCond(complexHead.tokens.find(x => x.name === 'attr_selector_exp')) | ||
} else if (simpleHead) { | ||
const term = simpleHead.value | ||
if (this.terms[term]) { | ||
res = { simpleIdx: this.terms[term] - 1, op: '', complex: [] } | ||
} else { | ||
this.termIdx.push(simpleHead) | ||
this.terms[term] = this.termIdx.length | ||
res = { simpleIdx: this.termIdx.length - 1, op: '', complex: [] } | ||
} | ||
} | ||
const tail = token.tokens.find(x => x.name === 'tail') | ||
if (tail) { | ||
res = { | ||
simpleIdx: -1, | ||
op: token.tokens.find(x => x.name === 'and_or').value, | ||
complex: [res, this.analyzeCond(tail.tokens.find(x => x.name === 'attr_selector_exp'))] | ||
} | ||
} | ||
return res | ||
} | ||
|
||
analyzeAgg () { | ||
const agg = this.script.Child('aggregator') | ||
if (!agg) { | ||
return | ||
} | ||
this.aggFn = agg.Child('fn').value | ||
const labelName = agg.Child('attr').Child('label_name') | ||
this.aggregatedAttr = labelName ? labelName.value : '' | ||
this.cmpVal = agg.Child('cmp_val').value | ||
} | ||
} |
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 |
---|---|---|
@@ -1,25 +1,35 @@ | ||
const Sql = require('@cloki/clickhouse-sql') | ||
const { format } = require('date-fns') | ||
const { standardBuilder } = require('./shared') | ||
/** | ||
* @typedef {function(Sql.Select, { | ||
* @typedef {{ | ||
* from: Date, | ||
* to: Date, | ||
* tracesAttrsTable: string, | ||
* limit: number | ||
* }): Select} ProcessFn | ||
* @type ProcessFn | ||
* limit: number, | ||
* isCluster: boolean, | ||
* tracesTable: string, | ||
* tracesDistTable: string | ||
* }} Context | ||
*/ | ||
module.exports.process = (sel, ctx) => { | ||
/** | ||
* @typedef {function(Sql.Select, Context): Select} ProcessFn | ||
*/ | ||
|
||
/** | ||
* @type {ProcessFn} | ||
*/ | ||
module.exports = standardBuilder((sel, ctx) => { | ||
return (new Sql.Select()).select(['trace_id', 'trace_id'], | ||
[new Sql.Raw('lower(hex(span_id))'), 'span_id'], | ||
[new Sql.Raw('any(duration)'), 'duration'], | ||
[new Sql.Raw('any(timestamp_ns)', 'timestamp_ns')]) | ||
[new Sql.Raw('any(timestamp_ns)'), 'timestamp_ns']) | ||
.from([ctx.tracesAttrsTable, 'traces_idx']) | ||
.where(Sql.And( | ||
Sql.Gte('date', Sql.val(format(ctx.from, 'yyyy-MM-dd'))), | ||
Sql.Lt('date', Sql.val(format(ctx.to, 'yyyy-MM-dd'))), | ||
Sql.Lte('date', Sql.val(format(ctx.to, 'yyyy-MM-dd'))), | ||
Sql.Gte('traces_idx.timestamp_ns', new Sql.Raw(ctx.from.getTime() + '000000')), | ||
Sql.Lt('traces_idx.timestamp_ns', new Sql.Raw(ctx.to.getTime() + '000000')) | ||
)).groupBy('trace_id', 'span_id') | ||
.orderBy(['timestamp_ns', 'desc']) | ||
} | ||
}) |
Oops, something went wrong.