@ -261,11 +261,36 @@ func (m *rulesRetrieverMock) CreateAlertingRules() {
false ,
log . NewNopLogger ( ) ,
)
rule4 := rules . NewAlertingRule (
"test_metric6" ,
expr2 ,
time . Second ,
0 ,
labels . FromStrings ( "testlabel" , "rule" ) ,
labels . Labels { } ,
labels . Labels { } ,
"" ,
true ,
log . NewNopLogger ( ) ,
)
rule5 := rules . NewAlertingRule (
"test_metric7" ,
expr2 ,
time . Second ,
0 ,
labels . FromStrings ( "templatedlabel" , "{{ $externalURL }}" ) ,
labels . Labels { } ,
labels . Labels { } ,
"" ,
true ,
log . NewNopLogger ( ) ,
)
var r [ ] * rules . AlertingRule
r = append ( r , rule1 )
r = append ( r , rule2 )
r = append ( r , rule3 )
r = append ( r , rule4 )
r = append ( r , rule5 )
m . alertingRules = r
}
@ -300,7 +325,9 @@ func (m *rulesRetrieverMock) CreateRuleGroups() {
recordingExpr , err := parser . ParseExpr ( ` vector(1) ` )
require . NoError ( m . testing , err , "unable to parse alert expression" )
recordingRule := rules . NewRecordingRule ( "recording-rule-1" , recordingExpr , labels . Labels { } )
recordingRule2 := rules . NewRecordingRule ( "recording-rule-2" , recordingExpr , labels . FromStrings ( "testlabel" , "rule" ) )
r = append ( r , recordingRule )
r = append ( r , recordingRule2 )
group := rules . NewGroup ( rules . GroupOptions {
Name : "grp" ,
@ -2151,6 +2178,28 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
Health : "ok" ,
Type : "alerting" ,
} ,
AlertingRule {
State : "inactive" ,
Name : "test_metric6" ,
Query : "up == 1" ,
Duration : 1 ,
Labels : labels . FromStrings ( "testlabel" , "rule" ) ,
Annotations : labels . Labels { } ,
Alerts : [ ] * Alert { } ,
Health : "ok" ,
Type : "alerting" ,
} ,
AlertingRule {
State : "inactive" ,
Name : "test_metric7" ,
Query : "up == 1" ,
Duration : 1 ,
Labels : labels . FromStrings ( "templatedlabel" , "{{ $externalURL }}" ) ,
Annotations : labels . Labels { } ,
Alerts : [ ] * Alert { } ,
Health : "ok" ,
Type : "alerting" ,
} ,
RecordingRule {
Name : "recording-rule-1" ,
Query : "vector(1)" ,
@ -2158,6 +2207,13 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
Health : "ok" ,
Type : "recording" ,
} ,
RecordingRule {
Name : "recording-rule-2" ,
Query : "vector(1)" ,
Labels : labels . FromStrings ( "testlabel" , "rule" ) ,
Health : "ok" ,
Type : "recording" ,
} ,
} ,
} ,
} ,
@ -2210,6 +2266,28 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
Health : "ok" ,
Type : "alerting" ,
} ,
AlertingRule {
State : "inactive" ,
Name : "test_metric6" ,
Query : "up == 1" ,
Duration : 1 ,
Labels : labels . FromStrings ( "testlabel" , "rule" ) ,
Annotations : labels . Labels { } ,
Alerts : nil ,
Health : "ok" ,
Type : "alerting" ,
} ,
AlertingRule {
State : "inactive" ,
Name : "test_metric7" ,
Query : "up == 1" ,
Duration : 1 ,
Labels : labels . FromStrings ( "templatedlabel" , "{{ $externalURL }}" ) ,
Annotations : labels . Labels { } ,
Alerts : nil ,
Health : "ok" ,
Type : "alerting" ,
} ,
RecordingRule {
Name : "recording-rule-1" ,
Query : "vector(1)" ,
@ -2217,6 +2295,13 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
Health : "ok" ,
Type : "recording" ,
} ,
RecordingRule {
Name : "recording-rule-2" ,
Query : "vector(1)" ,
Labels : labels . FromStrings ( "testlabel" , "rule" ) ,
Health : "ok" ,
Type : "recording" ,
} ,
} ,
} ,
} ,
@ -2276,6 +2361,28 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
Health : "ok" ,
Type : "alerting" ,
} ,
AlertingRule {
State : "inactive" ,
Name : "test_metric6" ,
Query : "up == 1" ,
Duration : 1 ,
Labels : labels . FromStrings ( "testlabel" , "rule" ) ,
Annotations : labels . Labels { } ,
Alerts : [ ] * Alert { } ,
Health : "ok" ,
Type : "alerting" ,
} ,
AlertingRule {
State : "inactive" ,
Name : "test_metric7" ,
Query : "up == 1" ,
Duration : 1 ,
Labels : labels . FromStrings ( "templatedlabel" , "{{ $externalURL }}" ) ,
Annotations : labels . Labels { } ,
Alerts : [ ] * Alert { } ,
Health : "ok" ,
Type : "alerting" ,
} ,
} ,
} ,
} ,
@ -2302,6 +2409,13 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
Health : "ok" ,
Type : "recording" ,
} ,
RecordingRule {
Name : "recording-rule-2" ,
Query : "vector(1)" ,
Labels : labels . FromStrings ( "testlabel" , "rule" ) ,
Health : "ok" ,
Type : "recording" ,
} ,
} ,
} ,
} ,
@ -2369,6 +2483,179 @@ func testEndpoints(t *testing.T, api *API, tr *testTargetRetriever, es storage.E
} ,
zeroFunc : rulesZeroFunc ,
} ,
{
endpoint : api . rules ,
query : url . Values {
"match[]" : [ ] string { ` { testlabel="rule"} ` } ,
} ,
response : & RuleDiscovery {
RuleGroups : [ ] * RuleGroup {
{
Name : "grp" ,
File : "/path/to/file" ,
Interval : 1 ,
Limit : 0 ,
Rules : [ ] Rule {
AlertingRule {
State : "inactive" ,
Name : "test_metric6" ,
Query : "up == 1" ,
Duration : 1 ,
Labels : labels . FromStrings ( "testlabel" , "rule" ) ,
Annotations : labels . Labels { } ,
Alerts : [ ] * Alert { } ,
Health : "ok" ,
Type : "alerting" ,
} ,
RecordingRule {
Name : "recording-rule-2" ,
Query : "vector(1)" ,
Labels : labels . FromStrings ( "testlabel" , "rule" ) ,
Health : "ok" ,
Type : "recording" ,
} ,
} ,
} ,
} ,
} ,
zeroFunc : rulesZeroFunc ,
} ,
{
endpoint : api . rules ,
query : url . Values {
"type" : [ ] string { "alert" } ,
"match[]" : [ ] string { ` { templatedlabel=" {{ $externalURL }} "} ` } ,
} ,
response : & RuleDiscovery {
RuleGroups : [ ] * RuleGroup {
{
Name : "grp" ,
File : "/path/to/file" ,
Interval : 1 ,
Limit : 0 ,
Rules : [ ] Rule {
AlertingRule {
State : "inactive" ,
Name : "test_metric7" ,
Query : "up == 1" ,
Duration : 1 ,
Labels : labels . FromStrings ( "templatedlabel" , "{{ $externalURL }}" ) ,
Annotations : labels . Labels { } ,
Alerts : [ ] * Alert { } ,
Health : "ok" ,
Type : "alerting" ,
} ,
} ,
} ,
} ,
} ,
zeroFunc : rulesZeroFunc ,
} ,
{
endpoint : api . rules ,
query : url . Values {
"match[]" : [ ] string { ` { testlabel="abc"} ` } ,
} ,
response : & RuleDiscovery {
RuleGroups : [ ] * RuleGroup { } ,
} ,
} ,
// This is testing OR condition, the api response should return rule if it matches one of the label selector
{
endpoint : api . rules ,
query : url . Values {
"match[]" : [ ] string { ` { testlabel="abc"} ` , ` { testlabel="rule"} ` } ,
} ,
response : & RuleDiscovery {
RuleGroups : [ ] * RuleGroup {
{
Name : "grp" ,
File : "/path/to/file" ,
Interval : 1 ,
Limit : 0 ,
Rules : [ ] Rule {
AlertingRule {
State : "inactive" ,
Name : "test_metric6" ,
Query : "up == 1" ,
Duration : 1 ,
Labels : labels . FromStrings ( "testlabel" , "rule" ) ,
Annotations : labels . Labels { } ,
Alerts : [ ] * Alert { } ,
Health : "ok" ,
Type : "alerting" ,
} ,
RecordingRule {
Name : "recording-rule-2" ,
Query : "vector(1)" ,
Labels : labels . FromStrings ( "testlabel" , "rule" ) ,
Health : "ok" ,
Type : "recording" ,
} ,
} ,
} ,
} ,
} ,
zeroFunc : rulesZeroFunc ,
} ,
{
endpoint : api . rules ,
query : url . Values {
"type" : [ ] string { "record" } ,
"match[]" : [ ] string { ` { testlabel="rule"} ` } ,
} ,
response : & RuleDiscovery {
RuleGroups : [ ] * RuleGroup {
{
Name : "grp" ,
File : "/path/to/file" ,
Interval : 1 ,
Limit : 0 ,
Rules : [ ] Rule {
RecordingRule {
Name : "recording-rule-2" ,
Query : "vector(1)" ,
Labels : labels . FromStrings ( "testlabel" , "rule" ) ,
Health : "ok" ,
Type : "recording" ,
} ,
} ,
} ,
} ,
} ,
zeroFunc : rulesZeroFunc ,
} ,
{
endpoint : api . rules ,
query : url . Values {
"type" : [ ] string { "alert" } ,
"match[]" : [ ] string { ` { testlabel="rule"} ` } ,
} ,
response : & RuleDiscovery {
RuleGroups : [ ] * RuleGroup {
{
Name : "grp" ,
File : "/path/to/file" ,
Interval : 1 ,
Limit : 0 ,
Rules : [ ] Rule {
AlertingRule {
State : "inactive" ,
Name : "test_metric6" ,
Query : "up == 1" ,
Duration : 1 ,
Labels : labels . FromStrings ( "testlabel" , "rule" ) ,
Annotations : labels . Labels { } ,
Alerts : [ ] * Alert { } ,
Health : "ok" ,
Type : "alerting" ,
} ,
} ,
} ,
} ,
} ,
zeroFunc : rulesZeroFunc ,
} ,
{
endpoint : api . queryExemplars ,
query : url . Values {