Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
c20c787b
Unverified
Commit
c20c787b
authored
Nov 19, 2018
by
David
Committed by
GitHub
Nov 19, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #14104 from grafana/davkal/prometheus-rule-expansion
Prometheus: fix rules expansion
parents
0d30f3ba
e3e8be16
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
6 deletions
+32
-6
public/app/plugins/datasource/prometheus/datasource.ts
+3
-5
public/app/plugins/datasource/prometheus/language_utils.ts
+6
-0
public/app/plugins/datasource/prometheus/specs/language_utils.test.ts
+23
-1
No files found.
public/app/plugins/datasource/prometheus/datasource.ts
View file @
c20c787b
...
...
@@ -10,6 +10,7 @@ import { BackendSrv } from 'app/core/services/backend_srv';
import
addLabelToQuery
from
'./add_label_to_query'
;
import
{
getQueryHints
}
from
'./query_hints'
;
import
{
expandRecordingRules
}
from
'./language_utils'
;
export
function
alignRange
(
start
,
end
,
step
)
{
const
alignedEnd
=
Math
.
ceil
(
end
/
step
)
*
step
;
...
...
@@ -468,11 +469,8 @@ export class PrometheusDatasource {
return `sum(${query.trim()}) by ($1)`;
}
case '
EXPAND_RULES
': {
const mapping = action.mapping;
if (mapping) {
const ruleNames = Object.keys(mapping);
const rulesRegex = new RegExp(`(
\\
s|^)(${ruleNames.join('
|
')})(
\\
s|$|
\\
()`, '
ig
');
return query.replace(rulesRegex, (match, pre, name, post) => mapping[name]);
if (action.mapping) {
return expandRecordingRules(query, action.mapping);
}
}
default:
...
...
public/app/plugins/datasource/prometheus/language_utils.ts
View file @
c20c787b
...
...
@@ -83,3 +83,9 @@ export function parseSelector(query: string, cursorOffset = 1): { labelKeys: any
return
{
labelKeys
,
selector
:
selectorString
};
}
export
function
expandRecordingRules
(
query
:
string
,
mapping
:
{
[
name
:
string
]:
string
}):
string
{
const
ruleNames
=
Object
.
keys
(
mapping
);
const
rulesRegex
=
new
RegExp
(
`(\\s|^)(
${
ruleNames
.
join
(
'|'
)}
)(\\s|$|\\(|\\[|\\{)`
,
'ig'
);
return
query
.
replace
(
rulesRegex
,
(
match
,
pre
,
name
,
post
)
=>
`
${
pre
}${
mapping
[
name
]}${
post
}
`
);
}
public/app/plugins/datasource/prometheus/specs/language_utils.test.ts
View file @
c20c787b
import
{
parseSelector
}
from
'../language_utils'
;
import
{
expandRecordingRules
,
parseSelector
}
from
'../language_utils'
;
describe
(
'parseSelector()'
,
()
=>
{
let
parsed
;
...
...
@@ -62,3 +62,25 @@ describe('parseSelector()', () => {
expect
(
parsed
.
selector
).
toBe
(
'{__name__="bar:metric:1m"}'
);
});
});
describe
(
'expandRecordingRules()'
,
()
=>
{
it
(
'returns query w/o recording rules as is'
,
()
=>
{
expect
(
expandRecordingRules
(
'metric'
,
{})).
toBe
(
'metric'
);
expect
(
expandRecordingRules
(
'metric + metric'
,
{})).
toBe
(
'metric + metric'
);
expect
(
expandRecordingRules
(
'metric{}'
,
{})).
toBe
(
'metric{}'
);
});
it
(
'does not modify recording rules name in label values'
,
()
=>
{
expect
(
expandRecordingRules
(
'{__name__="metric"} + bar'
,
{
metric
:
'foo'
,
bar
:
'super'
})).
toBe
(
'{__name__="metric"} + super'
);
});
it
(
'returns query with expanded recording rules'
,
()
=>
{
expect
(
expandRecordingRules
(
'metric'
,
{
metric
:
'foo'
})).
toBe
(
'foo'
);
expect
(
expandRecordingRules
(
'metric + metric'
,
{
metric
:
'foo'
})).
toBe
(
'foo + foo'
);
expect
(
expandRecordingRules
(
'metric{}'
,
{
metric
:
'foo'
})).
toBe
(
'foo{}'
);
expect
(
expandRecordingRules
(
'metric[]'
,
{
metric
:
'foo'
})).
toBe
(
'foo[]'
);
expect
(
expandRecordingRules
(
'metric + foo'
,
{
metric
:
'foo'
,
foo
:
'bar'
})).
toBe
(
'foo + bar'
);
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment