Skip to content

Commit

Permalink
Merge https://github.com/apache/hertzbeat into 0716-yuluo/add-unit-test
Browse files Browse the repository at this point in the history
  • Loading branch information
yuluo-yx committed Jul 17, 2024
2 parents 279146e + 7e5283e commit e8cccdd
Show file tree
Hide file tree
Showing 8 changed files with 385 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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.hertzbeat.common.queue.impl;

import java.util.Map;

import org.apache.hertzbeat.common.entity.alerter.Alert;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* Test case for {@link InMemoryCommonDataQueue}
*/
class InMemoryCommonDataQueueTest {

private InMemoryCommonDataQueue queue;

@BeforeEach
void setUp() {
queue = new InMemoryCommonDataQueue();
}

@Test
void testAlertsData() throws InterruptedException {

var alert = new Alert();

queue.sendAlertsData(alert);
assertEquals(1, queue.getQueueSizeMetricsInfo().get("alertDataQueue"));

var polledAlert = queue.pollAlertsData();
assertEquals(0, queue.getQueueSizeMetricsInfo().get("alertDataQueue"));

assertNotNull(polledAlert);
assertEquals(alert, polledAlert);
}

@Test
void testMetricsData() throws InterruptedException {

var metricsData = CollectRep.MetricsData.newBuilder().build();

queue.sendMetricsData(metricsData);

CollectRep.MetricsData polledMetricsData = queue.pollMetricsDataToAlerter();

assertNotNull(polledMetricsData);
assertEquals(metricsData, polledMetricsData);

polledMetricsData = queue.pollMetricsDataToPersistentStorage();

assertNotNull(polledMetricsData);
assertEquals(metricsData, polledMetricsData);

polledMetricsData = queue.pollMetricsDataToRealTimeStorage();

assertNotNull(polledMetricsData);
assertEquals(metricsData, polledMetricsData);
}

@Test
void testGetQueueSizeMetricsInfo() {

Map<String, Integer> metricsInfo = queue.getQueueSizeMetricsInfo();

assertEquals(0, metricsInfo.get("alertDataQueue"));
assertEquals(0, metricsInfo.get("metricsDataToAlertQueue"));
assertEquals(0, metricsInfo.get("metricsDataToPersistentStorageQueue"));
assertEquals(0, metricsInfo.get("metricsDataToMemoryStorageQueue"));

queue.sendAlertsData(new Alert());
queue.sendMetricsData(CollectRep.MetricsData.newBuilder().build());

metricsInfo = queue.getQueueSizeMetricsInfo();

assertEquals(1, metricsInfo.get("alertDataQueue"));
assertEquals(1, metricsInfo.get("metricsDataToAlertQueue"));
assertEquals(1, metricsInfo.get("metricsDataToPersistentStorageQueue"));
assertEquals(1, metricsInfo.get("metricsDataToMemoryStorageQueue"));
}

@Test
void testDestroy() {

queue.sendAlertsData(new Alert());
queue.sendMetricsData(CollectRep.MetricsData.newBuilder().build());

queue.destroy();

Map<String, Integer> metricsInfo = queue.getQueueSizeMetricsInfo();

assertEquals(0, metricsInfo.get("alertDataQueue"));
assertEquals(0, metricsInfo.get("metricsDataToAlertQueue"));
assertEquals(0, metricsInfo.get("metricsDataToPersistentStorageQueue"));
assertEquals(0, metricsInfo.get("metricsDataToMemoryStorageQueue"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,39 @@
* limitations under the License.
*/

package org.apache.hertzbeat.common.queue;
package org.apache.hertzbeat.common.queue.impl;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;


/**
* Test case for {@link CommonDataQueue}
* Test case for {@link KafkaCommonDataQueue}
*/
class CommonDataQueueTest {

@BeforeEach
void setUp() {
}
class KafkaCommonDataQueueTest {

@AfterEach
void tearDown() {
}
@BeforeEach
void setUp() {
}

@Test
void addAlertData() {
}
@Test
void testSendAlertsData() {
}

@Test
void pollAlertData() {
}
@Test
void testPollAlertsData() {
}

@Test
void pollAlertMetricsData() {
}
@Test
void testSendMetricsData() {
}

@Test
void pollPersistentStorageMetricsData() {
}
@Test
void testPollMetricsDataToAlerter() {
}

@Test
void pollRealTimeStorageMetricsData() {
}
@Test
void testDestroy() {
}

@Test
void sendMetricsData() {
}
}
}
34 changes: 34 additions & 0 deletions home/blog/2024-07-15-new-committer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: Welcome to HertzBeat Community Committer!
author: LiuTianyou
author_title: LiuTianyou
author_url: https://github.com/LiuTianyou
author_image_url: https://avatars.githubusercontent.com/u/30208283?v=4
tags: [opensource, practice]
keywords: [open source monitoring system, alerting system]
---


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

Hello everyone, I am very honored to receive an invitation from the community to be nominated as a Committer for Apache HertzBeat. Let me introduce myself briefly. I have been working as a backend developer since 2019, mainly using Java. Currently, I am working at a network security company, focusing on the backend development of network security-related products.

### Encounter

In my work, several physical servers are deployed, running various databases and middleware. Although we have deployed the Prometheus + Grafana monitoring combination, most services and servers require additional installation of exporters. As a result, this monitoring system does not cover the entire project. Sometimes, we only realize a service is down when it is too late. One day in April, I came across an article introducing HertzBeat. I was immediately attracted by its unique features, such as no need for agents and fully visualized configuration, along with support for one-click deployment via Docker. I quickly deployed HertzBeat and put it into use.

### Familiarization

Due to the fully visualized operation, I quickly incorporated the servers, databases, and middleware used in the project into HertzBeat's management. Afterwards, the community was preparing to release the first Apache version and needed to supplement a large amount of documentation. I attempted to submit some documents to familiarize myself with the community's process for submitting code and documentation, and at the same time, I got familiar with the relevant parts of the code while supplementing the documents.

### Trying to Add Something

The first major change I made was to enable HertzBeat to support querying metrics from NebulaGraph using NGQL statements. Additionally, I added a monitoring template for NebulaGraph clusters based on this protocol. The idea initially came from my own needs. When I submitted this idea to the community, I quickly received a response and affirmation from the community, which greatly increased my confidence in continuously participating in this project.

### Joining

Due to my continuous writing of documents and contributing code, I started to become familiar with this community and this project, and began to make some of my own suggestions. Many of these suggestions were adopted by the community and released in new versions. After the first Apache version was released, I received an invitation from @tomsun28 and @TJxiaobao to be nominated as a Committer.

### Conclusion

I am very honored to be able to participate in this project and be recognized by the community. I would like to thank @tomsun28, @TJxiaobao, @zqr10159, @tuohai666, @yuluo-yx, @crossoverJie, @zhangshenghang, and @pwallk for reviewing my code, providing guidance, and helping me. Finally, I wish Apache HertzBeat to grow strong and have more and more contributors participating.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
title: 热烈欢迎 HertzBeat 小伙伴新晋社区 Committer!
author: LiuTianyou
author_title: LiuTianyou
author_url: https://github.com/LiuTianyou
author_image_url: https://avatars.githubusercontent.com/u/30208283?v=4
tags: [opensource, practice]
keywords: [open source monitoring system, alerting system]
---


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

大家好,非常荣幸可以收到社区邀请被提名为 Apache HertzBeat 的 Committer。我先做一个简单的自我介绍,从2019年工作开始,一直从事后端开发工作,主要使用Java语言,目前在一家网络安全公司从事网络安全相关产品后端开发工作。


### 遇见

我工作的项目里部署着数台物理服务器,其中运行着各种数据库,中间件,尽管部署了 Prometheus + grafana 的监控组合,但由于大多数的服务和服务器都需要额外安装 exporter,这套监控系统并没有覆盖到项目的全部,有时候服务宕机了,被用到了才发现。四月份的某一天我刷到了一篇公众号文章介绍了 HertzBeat,我马上被不需要 agent,全程可视化配置的特新吸引加上支持docker一键部署,我迅速的部署起来了 HertzBeat 并投入了使用。

### 熟悉

由于完全可视化操作,很快就将项目中用到的服务器,数据库,中间件纳入了 HertzBeat 的管理。之后,恰好社区为发布第一个Apache版本做准备,需要补充大量的文档。我尝试提交一些文档,来熟悉社区的提交代码和文档的流程,也顺便在补充的文档的过程的熟悉相关部分的代码。

### 尝试加一点东西

我做的第一个大的改动是让 HertzBeat 支持使用NGQL语句从NebulaGraph中查询指标,并且这个基于协议增加了NebulaGraph集群的监控模板。最开始有这个想法来源于自己的需求,当我把这个想法提交给社区时,很快就得到了社区的回应,并得到了肯定,这也让对持续参与这个项目信心倍增。

### 加入

由于我不停的写文档,贡献代码,我开始熟悉这个社区,熟悉这个项目,开始尝试提出一些自己的建议。很多建议都被社区采纳,并且在新版本中发布,在第一个Apache版本发布后,我收到了@tomsun28@TJxiaobao的邀请被提名为Committer。

### 结语

非常荣幸可以参与到这个项目中并被社区认可,感谢帮我review代码,给我指导和帮助的 @tomsun28@TJxiaobao@zqr10159@tuohai666@yuluo-yx@crossoverJie@zhangshenghang@pwallk。最后祝 Apache HertzBeat 茁壮成长,有越来越多的贡献者参与其中。
2 changes: 1 addition & 1 deletion manager/src/main/resources/define/app-mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ metrics:
- field: connections
type: 0
i18n:
zh-CN: 当前连接数
zh-CN: 连接的总次数
en-US: Connections
- field: max_used_connections
type: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

package org.apache.hertzbeat.manager.controller;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.mockito.Mockito.doNothing;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.common.util.JsonUtil;
import org.apache.hertzbeat.manager.service.impl.MonitorServiceImpl;
Expand All @@ -29,11 +33,13 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.multipart.MultipartFile;

/**
* Test case for {@link MonitorsController}
Expand Down Expand Up @@ -115,4 +121,41 @@ void enableManageMonitors() throws Exception {
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE))
.andReturn();
}

@Test
void export() throws Exception {
List<Long> ids = Arrays.asList(6565463543L, 6565463544L);
String type = "JSON";

this.mockMvc.perform(MockMvcRequestBuilders.get("/api/monitors/export")
.param("ids", String.join(",", ids.stream().map(String::valueOf).collect(Collectors.toList())))
.param("type", type))
.andExpect(status().isOk())
.andReturn();
}

@Test
void export2() throws Exception {
// Mock the behavior of monitorService.importConfig
doNothing().when(monitorService).importConfig((MultipartFile) Mockito.any());

// Perform the request and verify the response
this.mockMvc.perform(MockMvcRequestBuilders.post("/api/monitors/import")
.contentType(MediaType.MULTIPART_FORM_DATA)
.param("file", "testFileContent"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value("0"))
.andExpect(jsonPath("$.msg").value("Import success"));
}

@Test
void duplicateMonitors() throws Exception {
// Mock the behavior of monitorService.copyMonitors
doNothing().when(monitorService).copyMonitors(List.of(6565463543L));

// Perform the POST request and verify the response
this.mockMvc.perform(MockMvcRequestBuilders.post("/api/monitors/copy")
.param("ids", "6565463543"))
.andExpect(status().isOk());
}
}
Loading

0 comments on commit e8cccdd

Please sign in to comment.