Skip to content

Commit

Permalink
Add alarm tests in the e2e (apache#5961)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrproliu authored Dec 8, 2020
1 parent fbf49b2 commit 4aa9f30
Show file tree
Hide file tree
Showing 22 changed files with 822 additions and 10 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,32 @@ jobs:
with:
name: logs
path: logs

FeatureGroup05:
name: Alarm
runs-on: ubuntu-latest
timeout-minutes: 90
steps:
- uses: actions/checkout@v2
with:
submodules: true
- name: Cache local Maven repository
uses: actions/cache@v2
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Compile and Build
run: make docker
- name: Copy dist package
run: cp -R dist test/e2e/
- name: Meter receiver
run: ./mvnw --batch-mode -f test/e2e/pom.xml -am -DfailIfNoTests=false verify -Dit.test=org.apache.skywalking.e2e.alarm.AlarmE2E
- name: Report Coverage
run: bash -x tools/coverage/report.sh
- uses: actions/upload-artifact@v1
if: failure()
with:
name: logs
path: logs
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Release Notes.
8.4.0
------------------
#### Project
* Incompatible with previous releases when use H2/MySQL/TiDB storage options, due to support multiple alarm rules triggered for one entity.
* Chore: adapt `create_source_release.sh` to make it runnable on Linux.

#### Java Agent
Expand All @@ -15,6 +16,7 @@ Release Notes.
* Make meter receiver support MAL.
* Support influxDB connection response format option. Fix some error when use JSON as influxDB response format.
* Support Kafka MirrorMaker 2.0 to replicate topics between Kafka clusters.
* Add the rule name field to alarm record storage entity as a part of ID, to support multiple alarm rules triggered for one entity. The scope id has been removed from the ID.
* Fix MAL concurrent execution issues

#### UI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package org.apache.skywalking.oap.server.core.alarm;

import java.util.HashMap;
import java.util.Map;
import lombok.Getter;
import lombok.Setter;
import org.apache.skywalking.oap.server.core.Const;
Expand All @@ -31,6 +29,9 @@
import org.apache.skywalking.oap.server.core.storage.StorageBuilder;
import org.apache.skywalking.oap.server.core.storage.annotation.Column;

import java.util.HashMap;
import java.util.Map;

import static org.apache.skywalking.oap.server.core.source.DefaultScopeDefine.ALARM;

@Getter
Expand All @@ -46,10 +47,11 @@ public class AlarmRecord extends Record {
public static final String ID1 = "id1";
public static final String START_TIME = "start_time";
public static final String ALARM_MESSAGE = "alarm_message";
public static final String RULE_NAME = "rule_name";

@Override
public String id() {
return getTimeBucket() + Const.ID_CONNECTOR + scope + Const.ID_CONNECTOR + id0 + Const.ID_CONNECTOR + id1;
return getTimeBucket() + Const.ID_CONNECTOR + ruleName + Const.ID_CONNECTOR + id0 + Const.ID_CONNECTOR + id1;
}

@Column(columnName = SCOPE)
Expand All @@ -64,6 +66,8 @@ public String id() {
private long startTime;
@Column(columnName = ALARM_MESSAGE, matchQuery = true)
private String alarmMessage;
@Column(columnName = RULE_NAME)
private String ruleName;

public static class Builder implements StorageBuilder<AlarmRecord> {

Expand All @@ -77,6 +81,7 @@ public Map<String, Object> data2Map(AlarmRecord storageData) {
map.put(ALARM_MESSAGE, storageData.getAlarmMessage());
map.put(START_TIME, storageData.getStartTime());
map.put(TIME_BUCKET, storageData.getTimeBucket());
map.put(RULE_NAME, storageData.getRuleName());
return map;
}

Expand All @@ -90,6 +95,7 @@ public AlarmRecord map2Data(Map<String, Object> dbMap) {
record.setAlarmMessage((String) dbMap.get(ALARM_MESSAGE));
record.setStartTime(((Number) dbMap.get(START_TIME)).longValue());
record.setTimeBucket(((Number) dbMap.get(TIME_BUCKET)).longValue());
record.setRuleName((String) dbMap.get(RULE_NAME));
return record;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

package org.apache.skywalking.oap.server.core.alarm;

import java.util.List;
import org.apache.skywalking.oap.server.core.analysis.TimeBucket;
import org.apache.skywalking.oap.server.core.analysis.worker.RecordStreamProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
* Save the alarm info into storage for UI query.
*/
Expand All @@ -46,6 +47,7 @@ public void doAlarm(List<AlarmMessage> alarmMessage) {
record.setAlarmMessage(message.getAlarmMessage());
record.setStartTime(message.getStartTime());
record.setTimeBucket(TimeBucket.getRecordTimeBucket(message.getStartTime()));
record.setRuleName(message.getRuleName());

RecordStreamProcessor.getInstance().in(record);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@
package org.apache.skywalking.e2e;

import com.google.common.io.Resources;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.skywalking.e2e.alarm.AlarmQuery;
import org.apache.skywalking.e2e.alarm.GetAlarm;
import org.apache.skywalking.e2e.alarm.GetAlarmData;
import org.apache.skywalking.e2e.browser.BrowserErrorLog;
import org.apache.skywalking.e2e.browser.BrowserErrorLogQuery;
import org.apache.skywalking.e2e.browser.BrowserErrorLogsData;
Expand Down Expand Up @@ -60,6 +57,13 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@SuppressWarnings("UnstableApiUsage")
@Slf4j
public class SimpleQueryClient {
Expand Down Expand Up @@ -358,4 +362,29 @@ public List<ReadMetrics> readLabeledMetrics(final ReadMetricsQuery query) throws

return Objects.requireNonNull(responseEntity.getBody()).getData().getReadLabeledMetricsValues();
}

public GetAlarm readAlarms(final AlarmQuery query) throws Exception {
final URL queryFileUrl = Resources.getResource("read-alarms.gql");
final String queryString = Resources.readLines(queryFileUrl, StandardCharsets.UTF_8)
.stream()
.filter(it -> !it.startsWith("#"))
.collect(Collectors.joining())
.replace("{step}", query.step())
.replace("{start}", query.start())
.replace("{end}", query.end())
.replace("{pageSize}", "20")
.replace("{needTotal}", "true");
LOGGER.info("Query: {}", queryString);
final ResponseEntity<GQLResponse<GetAlarmData>> responseEntity = restTemplate.exchange(
new RequestEntity<>(queryString, HttpMethod.POST, URI.create(endpointUrl)),
new ParameterizedTypeReference<GQLResponse<GetAlarmData>>() {
}
);

if (responseEntity.getStatusCode() != HttpStatus.OK) {
throw new RuntimeException("Response status != 200, actual: " + responseEntity.getStatusCode());
}

return Objects.requireNonNull(responseEntity.getBody()).getData().getGetAlarm();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.skywalking.e2e.alarm;

import lombok.Data;
import org.apache.skywalking.e2e.verification.AbstractMatcher;

@Data
public class AlarmMatcher extends AbstractMatcher<AlarmMatcher> {
private String startTime;
private String scope;
private String id;
private String message;

@Override
public void verify(AlarmMatcher alarmMatcher) {
doVerify(this.startTime, alarmMatcher.startTime);
doVerify(this.scope, alarmMatcher.scope);
doVerify(this.id, alarmMatcher.id);
doVerify(this.message, alarmMatcher.message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.skywalking.e2e.alarm;

import org.apache.skywalking.e2e.AbstractQuery;

public class AlarmQuery extends AbstractQuery<AlarmQuery> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.skywalking.e2e.alarm;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

@Data
@Slf4j
public class AlarmsMatcher {
private GetAlarm alarm;

public void verify(final GetAlarm alarm) {
LOGGER.info("alarms:{} matchers:{}", alarm, this.alarm);
Assert.assertEquals(this.alarm.getTotal(), alarm.getTotal());

assertThat(this.alarm.getMsgs()).hasSameSizeAs(alarm.getMsgs());

for (int i = 0; i < this.alarm.getMsgs().size(); i++) {
boolean matched = false;
for (AlarmMatcher alarmMatcher : alarm.getMsgs()) {
try {
this.alarm.getMsgs().get(i).verify(alarmMatcher);
matched = true;
} catch (Throwable ignored) {
}
}
if (!matched) {
fail("\nExpected: %s\nActual: %s", this.alarm, alarm);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.skywalking.e2e.alarm;

import lombok.Data;

import java.util.List;

@Data
public class GetAlarm {
private int total;
private List<AlarmMatcher> msgs;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.skywalking.e2e.alarm;

import lombok.Data;

@Data
public class GetAlarmData {
private GetAlarm getAlarm;
}
Loading

0 comments on commit 4aa9f30

Please sign in to comment.