Skip to content

Commit

Permalink
Merge branch 'master' into 0727-yuluo/add-test-03
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin979 authored Jul 28, 2024
2 parents 7228067 + 22459c5 commit 87cc826
Show file tree
Hide file tree
Showing 33 changed files with 161 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ private Alert buildAlertData(AlertReport alertReport){
StringBuilder sb = new StringBuilder();
if (alertReport.getContent() == null || alertReport.getContent().length() <= 0){
StringBuilder finalSb = sb;
annotations.forEach((k, v) -> {
finalSb.append(k).append(":").append(v).append("\n");
});
annotations.forEach((k, v) -> finalSb.append(k).append(":").append(v).append("\n"));
} else {
sb = new StringBuilder(alertReport.getContent());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import org.apache.hertzbeat.collector.collect.AbstractCollect;
import org.apache.hertzbeat.collector.collect.common.http.CommonHttpClient;
import org.apache.hertzbeat.collector.collect.http.promethus.AbstractPrometheusParse;
import org.apache.hertzbeat.collector.collect.http.promethus.PrometheusParseCreater;
import org.apache.hertzbeat.collector.collect.http.promethus.PrometheusParseCreator;
import org.apache.hertzbeat.collector.collect.http.promethus.exporter.ExporterParser;
import org.apache.hertzbeat.collector.collect.http.promethus.exporter.MetricFamily;
import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
Expand Down Expand Up @@ -375,7 +375,7 @@ private void parseResponseByJsonPath(String resp, List<String> aliasFields, Http

private void parseResponseByPromQl(String resp, List<String> aliasFields, HttpProtocol http,
CollectRep.MetricsData.Builder builder) {
AbstractPrometheusParse prometheusParser = PrometheusParseCreater.getPrometheusParse();
AbstractPrometheusParse prometheusParser = PrometheusParseCreator.getPrometheusParse();
prometheusParser.handle(resp, aliasFields, http, builder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
*/
@Slf4j
@Component
public class PrometheusParseCreater implements InitializingBean {
private static AbstractPrometheusParse PROMETHEUSPARSE = new PrometheusVectorParser();
public class PrometheusParseCreator implements InitializingBean {
private static final AbstractPrometheusParse PROMETHEUS_PARSE = new PrometheusVectorParser();

private static void create() {
PROMETHEUSPARSE.setInstance(new PrometheusMatrixParser().setInstance(new PrometheusLastParser()));
PROMETHEUS_PARSE.setInstance(new PrometheusMatrixParser().setInstance(new PrometheusLastParser()));
}

public static AbstractPrometheusParse getPrometheusParse(){
return PROMETHEUSPARSE;
return PROMETHEUS_PARSE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
@Slf4j
public class PushCollectImpl extends AbstractCollect {

private static Map<Long, Long> timeMap = new ConcurrentHashMap<>();
private static final Map<Long, Long> timeMap = new ConcurrentHashMap<>();

// ms
private static final Integer timeout = 3000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,7 @@ private Map<String, String> parseInfo(String info, Metrics metrics) {
.map(this::removeCr)
.map(r -> r.split(SignConstants.DOUBLE_MARK))
.filter(t -> t.length > 1)
.forEach(it -> {
result.put(it[0], it[1]);
});
.forEach(it -> result.put(it[0], it[1]));
// fix https://github.com/apache/hertzbeat/pull/913
if (result.size() < fieldTotalSize) {
for (Metrics.Field field : metrics.getFields()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.collector.collect.AbstractCollect;
Expand Down Expand Up @@ -202,11 +203,7 @@ private void parseResponseDataByNetcat(String result, List<String> aliasFields,
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
for (String field : aliasFields) {
String fieldValue = mapValue.get(field);
if (fieldValue == null) {
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
} else {
valueRowBuilder.addColumns(fieldValue);
}
valueRowBuilder.addColumns(Objects.requireNonNullElse(fieldValue, CommonConstants.NULL_VALUE));
}
builder.addValues(valueRowBuilder.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -173,11 +174,7 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri
} else {
String oid = oidsMap.get(alias);
String value = oidsValueMap.get(oid);
if (value != null) {
valueRowBuilder.addColumns(value);
} else {
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
}
valueRowBuilder.addColumns(Objects.requireNonNullElse(value, CommonConstants.NULL_VALUE));
}
}
builder.addValues(valueRowBuilder.build());
Expand Down Expand Up @@ -227,11 +224,7 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri
}
}
}
if (value != null) {
valueRowBuilder.addColumns(value);
} else {
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
}
valueRowBuilder.addColumns(Objects.requireNonNullElse(value, CommonConstants.NULL_VALUE));
}
}
builder.addValues(valueRowBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -206,11 +207,7 @@ private void parseResponseDataByNetcat(String result, List<String> aliasFields,
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
for (String field : aliasFields) {
String fieldValue = mapValue.get(field);
if (fieldValue == null) {
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
} else {
valueRowBuilder.addColumns(fieldValue);
}
valueRowBuilder.addColumns(Objects.requireNonNullElse(fieldValue, CommonConstants.NULL_VALUE));
}
builder.addValues(valueRowBuilder.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,10 @@ void setUp() {

@Test
void testPreCheck() {
assertDoesNotThrow(() -> {
icmpCollect.preCheck(metrics);
});
assertThrows(IllegalArgumentException.class, () -> {
icmpCollect.preCheck(null);
});
assertDoesNotThrow(() -> icmpCollect.preCheck(metrics));
assertThrows(IllegalArgumentException.class, () -> icmpCollect.preCheck(null));
metrics.setIcmp(null);
assertThrows(IllegalArgumentException.class, () -> {
icmpCollect.preCheck(null);
});
assertThrows(IllegalArgumentException.class, () -> icmpCollect.preCheck(null));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,10 @@ void setUp() {

@Test
void preCheck() {
assertDoesNotThrow(() -> {
imapCollect.preCheck(metrics);
});
assertThrows(NullPointerException.class, () -> {
imapCollect.preCheck(null);
});
assertDoesNotThrow(() -> imapCollect.preCheck(metrics));
assertThrows(NullPointerException.class, () -> imapCollect.preCheck(null));
metrics.setImap(null);
assertThrows(NullPointerException.class, () -> {
imapCollect.preCheck(null);
});
assertThrows(NullPointerException.class, () -> imapCollect.preCheck(null));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,10 @@ void setUp() {

@Test
void testPreCheck() {
assertDoesNotThrow(() -> {
memcachedCollect.preCheck(metrics);
});
assertThrows(IllegalArgumentException.class, () -> {
memcachedCollect.preCheck(null);
});
assertDoesNotThrow(() -> memcachedCollect.preCheck(metrics));
assertThrows(IllegalArgumentException.class, () -> memcachedCollect.preCheck(null));
metrics.setIcmp(null);
assertThrows(IllegalArgumentException.class, () -> {
memcachedCollect.preCheck(null);
});
assertThrows(IllegalArgumentException.class, () -> memcachedCollect.preCheck(null));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void testNginxCollectFail() throws IOException {
.build();

try (MockedStatic<CommonHttpClient> mockStatic = Mockito.mockStatic(CommonHttpClient.class)) {
mockStatic.when(() -> CommonHttpClient.getHttpClient()).thenReturn(client);
mockStatic.when(CommonHttpClient::getHttpClient).thenReturn(client);
Mockito.when(client.execute(Mockito.any(HttpUriRequest.class), Mockito.any(HttpContext.class)))
.thenReturn(mockHttpResponse);

Expand Down Expand Up @@ -111,7 +111,7 @@ public void testNginxStatusCollect() throws IOException {
.build();

try (MockedStatic<CommonHttpClient> mockedStatic = Mockito.mockStatic(CommonHttpClient.class)) {
mockedStatic.when(() -> CommonHttpClient.getHttpClient()).thenReturn(client);
mockedStatic.when(CommonHttpClient::getHttpClient).thenReturn(client);

Mockito.when(client.execute(Mockito.any(HttpUriRequest.class), Mockito.any(HttpContext.class)))
.thenReturn(mockHttpResponse);
Expand Down Expand Up @@ -176,7 +176,7 @@ public void testNginxReqStatusCollect() throws IOException {
.build();

try (MockedStatic<CommonHttpClient> mockedStatic = Mockito.mockStatic(CommonHttpClient.class)) {
mockedStatic.when(() -> CommonHttpClient.getHttpClient()).thenReturn(client);
mockedStatic.when(CommonHttpClient::getHttpClient).thenReturn(client);

Mockito.when(client.execute(Mockito.any(HttpUriRequest.class), Mockito.any(HttpContext.class)))
.thenReturn(mockHttpResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ void testCollect() {
packet.setReceiveTimeStamp(new TimeStamp(2000));
packet.setTransmitTime(new TimeStamp(1000));
TimeInfo timeInfo = new TimeInfo(packet, 1000, false);
;

MockedConstruction<NTPUDPClient> mocked =
Mockito.mockConstruction(NTPUDPClient.class, (client, context) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,8 @@ void testCollectWithSocketException() {
.build();

MockedConstruction<DatagramSocket> socketMockedConstruction =
Mockito.mockConstruction(DatagramSocket.class, (socket, context) -> {
Mockito.doThrow(new SocketTimeoutException("test exception"))
.when(socket).send(Mockito.any(DatagramPacket.class));
});
Mockito.mockConstruction(DatagramSocket.class, (socket, context) -> Mockito.doThrow(new SocketTimeoutException("test exception"))
.when(socket).send(Mockito.any(DatagramPacket.class)));


List<String> aliasField = new ArrayList<>();
Expand All @@ -123,10 +121,8 @@ void testCollectWithPortUnreachableException() {
.build();

MockedConstruction<DatagramSocket> socketMockedConstruction =
Mockito.mockConstruction(DatagramSocket.class, (socket, context) -> {
Mockito.doThrow(new PortUnreachableException("test exception"))
.when(socket).send(Mockito.any(DatagramPacket.class));
});
Mockito.mockConstruction(DatagramSocket.class, (socket, context) -> Mockito.doThrow(new PortUnreachableException("test exception"))
.when(socket).send(Mockito.any(DatagramPacket.class)));


List<String> aliasField = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

/**
Expand All @@ -34,7 +35,7 @@ public class SpringContextHolder implements ApplicationContextAware {
private static ConfigurableApplicationContext configurableApplicationContext;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
set(applicationContext);
if (applicationContext instanceof ConfigurableApplicationContext context) {
configurableApplicationContext = context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ void testParseTokenTimeInvalidInput() {
assertNull(result);

// invalid format (non-numeric)
Exception exception = assertThrows(DateTimeParseException.class, () -> {
TimePeriodUtil.parseTokenTime("abc");
});
Exception exception = assertThrows(DateTimeParseException.class, () -> TimePeriodUtil.parseTokenTime("abc"));
assertNotNull(exception.getMessage());
}

Expand Down
36 changes: 36 additions & 0 deletions home/blog/2024-07-28-new-committer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
title: Welcome to HertzBeat Community Committer!
author: linDong
author_title: linDong
author_url: https://github.com/Yanshuming1
author_image_url: https://avatars.githubusercontent.com/u/30208283?v=4
tags: [opensource, practice]
keywords: [open source monitoring system, alerting system]
---

![hertzBeathertzBeat](/img/blog/new-committer.png)

> It's an honor for me to become a Committer of Apache Hertzbeat
## Personal introduction

I graduated in 2023 and am currently working as a Java developer in an Internet company.

## First met Apache Hertzbeat

After graduation and working for half a year, I had the idea of getting involved in open source. However, when searching for open source projects on GitHub, I found that many communities were not very active, and the official documentation was not complete, making it quite difficult to start with the projects. Therefore, I put the matter of open source on hold for the time being.

One day, I saw a post by Senior Xiaobao on a certain platform and was immediately intrigued. I then privately messaged Xiaobao to consult about open source matters. Senior Xiaobao was extremely enthusiastic and meticulous in explaining many aspects of open source to me and recommended the Hertzbeat project.

## My first PR

I downloaded the source code of Hertzbeat and compiled and tested it according to the documentation. Then, when clicking on the functions on the page, I discovered a bug in the front-end pop-up window. So, I submitted my first issue and successfully claimed this issue, thus starting my first PR.

## Keep active in community

After submitting the first PR, I continuously followed the issues in the Hertzbeat community and attempted to solve the existing problems. I have successively completed tasks such as specifying @people on WeChat, integrating AI, adding a PrestoDB monitor, modifying bugs, and contributing to the official website documentation.

## Reward
After several months of contributing to open source, I have reaped a lot. I have learned the business logic in the code, code norms, some technical frameworks that I have never used before, and some algorithms. It can be said that I have benefited greatly.

## Conclusion
Becoming a Committer of Apache Hertzbeat is a very meaningful thing for me. In the future, I will continue to contribute to the Apache Hertzbeat community. I also hope that Apache Hertzbeat can successfully graduate from the incubator and that the community will become better and better.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
title: 热烈欢迎 HertzBeat 小伙伴新晋社区 Committer!
author: linDong
author_title: linDong
author_url: https://github.com/Yanshuming1
author_image_url: https://avatars.githubusercontent.com/u/131688897?v=4
tags: [opensource, practice]
keywords: [open source monitoring system, alerting system]
---

![hertzBeat](/img/blog/new-committer.png)

> 非常荣幸能成为Apache Hertzbeat的Committer
## 个人介绍

本人是2023年毕业,目前在一家互联网公司担任java开发工程师。

## 初识 Apache Hertzbeat
毕业之后,工作了半年,萌生出接触开源的想法。然而,在 GitHub 上搜索开源项目时,发现诸多社区活跃度欠佳,官方文档也不完善,致使项目上手难度颇大,因此开源之事暂且搁置。
某天,在某平台看到小宝大佬的一个帖子,我瞬间兴趣盎然,随即私聊小宝,咨询开源相关事宜。小宝大佬极其热情且细致地为我讲解了不少开源方面的情况,并向我推荐了 Hertzbeat 这个项目。

## 第一个PR

我down了 Hertzbeat 的源码,并按照文档进行编译和测试,然后在页面点击功能的时候发现了一个前端弹窗的bug,所以我提交了自己的第一个issue,并且我成功认领了此issue
于是开启了我的第一个PR。

## 持续贡献

在提交第一个 PR 之后,我持续关注 Hertzbeat 社区的 issue ,且尝试去解决当中存在的问题。相继完成了微信指定艾特人、接入 AI 、添加 PrestoDB 监控器、Bug 修改以及官网文档贡献等工作。

## 收获

经过几个月对开源的贡献,我收获满满,学习到了代码中的业务逻辑、代码规范,一些此前未曾使用过的技术框架,还有一些算法,可谓是获益良多。

## 结语

成为Apache Hertzbeat的Committer于我而言是很有意义对事情,今后我也会持续对Apache Hertzbeat社区贡献下去,也希望Apache Hertzbeat能顺利从孵化器毕业,希望社区越来越好。
Loading

0 comments on commit 87cc826

Please sign in to comment.