-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix query generation for link to explore and add to investigations. A…
…dd step to links
- Loading branch information
1 parent
63171eb
commit 9c509bd
Showing
9 changed files
with
158 additions
and
102 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
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,62 @@ | ||
import { ALL, MetricFunction, VAR_FILTERS_EXPR } from '../../../utils/shared'; | ||
|
||
interface QueryOptions { | ||
metric: MetricFunction; | ||
extraFilters?: string; | ||
groupByKey?: string; | ||
groupByStatus?: boolean; | ||
} | ||
|
||
export function generateMetricsQuery({ metric, groupByKey, extraFilters, groupByStatus }: QueryOptions) { | ||
// Generate span set filters | ||
let filters = `${VAR_FILTERS_EXPR}`; | ||
|
||
if (metric === 'errors') { | ||
filters += ' && status=error'; | ||
} | ||
|
||
if (extraFilters) { | ||
filters += ` && ${extraFilters}`; | ||
} | ||
|
||
if (groupByKey && groupByKey !== ALL) { | ||
filters += ` && ${groupByKey} != nil`; | ||
} | ||
|
||
// Generate metrics function | ||
let metricFn = 'rate()'; | ||
switch (metric) { | ||
case 'errors': | ||
metricFn = 'rate()'; | ||
break; | ||
case 'duration': | ||
metricFn = 'quantile_over_time(duration, 0.9)'; | ||
break; | ||
} | ||
|
||
// Generate group by section | ||
let groupByAttrs = []; | ||
if (groupByKey && groupByKey !== ALL) { | ||
groupByAttrs.push(groupByKey); | ||
} | ||
|
||
if (metric !== 'duration' && groupByStatus) { | ||
groupByAttrs.push('status'); | ||
} | ||
|
||
const groupBy = groupByAttrs.length ? `by(${groupByAttrs.join(', ')})` : ''; | ||
|
||
return `{${filters}} | ${metricFn} ${groupBy}`; | ||
} | ||
|
||
export function metricByWithStatus(metric: MetricFunction, tagKey?: string) { | ||
return { | ||
refId: 'A', | ||
query: generateMetricsQuery({ metric, groupByKey: tagKey, groupByStatus: true }), | ||
queryType: 'traceql', | ||
tableType: 'spans', | ||
limit: 100, | ||
spss: 10, | ||
filters: [], | ||
}; | ||
} |
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,90 +1,90 @@ | ||
import { comparisonQuery } from "./comparisonQuery"; | ||
import { buildHistogramQuery } from "./histogram"; | ||
import { rateByWithStatus } from "./rateByWithStatus"; | ||
import { comparisonQuery } from './comparisonQuery'; | ||
import { buildHistogramQuery } from './histogram'; | ||
import { metricByWithStatus } from './generateMetricsQuery'; | ||
|
||
describe('comparisonQuery', () => { | ||
it('should return correct query for no selection', () => { | ||
const query = comparisonQuery(); | ||
expect(query).toBe("{}"); | ||
expect(query).toBe('{}'); | ||
}); | ||
it('should return correct query for a selection', () => { | ||
const query = comparisonQuery({ | ||
type: "manual", | ||
type: 'manual', | ||
raw: { | ||
x: { | ||
from: 1728987790508.9485, | ||
to: 1728988005770.9075 | ||
to: 1728988005770.9075, | ||
}, | ||
y: { | ||
from: 8.29360465116279, | ||
to: 21.85174418604651 | ||
} | ||
to: 21.85174418604651, | ||
}, | ||
}, | ||
timeRange: { | ||
from: 1728987791, | ||
to: 1728988006 | ||
to: 1728988006, | ||
}, | ||
duration: { | ||
from: "0ms", | ||
to: "2s" | ||
} | ||
from: '0ms', | ||
to: '2s', | ||
}, | ||
}); | ||
expect(query).toBe("{duration >= 0ms && duration <= 2s}, 10, 1728987791000000000, 1728988006000000000"); | ||
expect(query).toBe('{duration >= 0ms && duration <= 2s}, 10, 1728987791000000000, 1728988006000000000'); | ||
}); | ||
}); | ||
|
||
describe('buildHistogramQuery', () => { | ||
it('should return correct query', () => { | ||
const query = buildHistogramQuery(); | ||
expect(query).toEqual({ | ||
filters: [], | ||
limit: 1000, | ||
query: "{${filters}} | histogram_over_time(duration)", | ||
queryType: "traceql", | ||
refId: "A", | ||
spss: 10, | ||
tableType: "spans" | ||
filters: [], | ||
limit: 1000, | ||
query: '{${filters}} | histogram_over_time(duration)', | ||
queryType: 'traceql', | ||
refId: 'A', | ||
spss: 10, | ||
tableType: 'spans', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('rateByWithStatus', () => { | ||
describe('metricByWithStatus', () => { | ||
it('should return correct query for no tag', () => { | ||
const query = rateByWithStatus('errors'); | ||
const query = metricByWithStatus('errors'); | ||
expect(query).toEqual({ | ||
filters: [], | ||
limit: 100, | ||
query: "{${filters} && status=error} | rate() ", | ||
queryType: "traceql", | ||
refId: "A", | ||
spss: 10, | ||
tableType: "spans" | ||
filters: [], | ||
limit: 100, | ||
query: '{${filters} && status=error} | rate() by(status)', | ||
queryType: 'traceql', | ||
refId: 'A', | ||
spss: 10, | ||
tableType: 'spans', | ||
}); | ||
}); | ||
|
||
it('should return correct query for errors', () => { | ||
const query = rateByWithStatus('errors', 'service'); | ||
const query = metricByWithStatus('errors', 'service'); | ||
expect(query).toEqual({ | ||
filters: [], | ||
limit: 100, | ||
query: "{${filters} && status=error} | rate() by(service, status)", | ||
queryType: "traceql", | ||
refId: "A", | ||
spss: 10, | ||
tableType: "spans" | ||
filters: [], | ||
limit: 100, | ||
query: '{${filters} && status=error && service != nil} | rate() by(service, status)', | ||
queryType: 'traceql', | ||
refId: 'A', | ||
spss: 10, | ||
tableType: 'spans', | ||
}); | ||
}); | ||
|
||
it('should return correct query for duration', () => { | ||
const query = rateByWithStatus('duration', 'service'); | ||
const query = metricByWithStatus('duration', 'service'); | ||
expect(query).toEqual({ | ||
filters: [], | ||
limit: 100, | ||
query: "{${filters}} | quantile_over_time(duration, 0.9) by(service)", | ||
queryType: "traceql", | ||
refId: "A", | ||
spss: 10, | ||
tableType: "spans" | ||
filters: [], | ||
limit: 100, | ||
query: '{${filters} && service != nil} | quantile_over_time(duration, 0.9) by(service)', | ||
queryType: 'traceql', | ||
refId: 'A', | ||
spss: 10, | ||
tableType: 'spans', | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.