Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] trans and use assert #1841

Merged
merged 4 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri
long responseTime = System.currentTimeMillis() - startTime;
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// 发送统计命令
// Send a command to collect statistics
Map<String, String> resultMap = new HashMap<>(128);
parseCmdResponse(resultMap, in, out, STATS);
parseCmdResponse(resultMap, in, out, STATS_SETTINGS);
parseSizesOutput(resultMap, in, out);

resultMap.put(CollectorConstants.RESPONSE_TIME, Long.toString(responseTime));

// 关闭输出流和Socket连接
// Close the output stream and socket connection
in.close();
out.close();
socket.close();
Expand Down Expand Up @@ -130,7 +130,7 @@ private static void parseCmdResponse(Map<String, String> statsMap,
out.println(cmd);
String line;
while ((line = in.readLine()) != null && !line.equals(STATS_END_RSP)) {
// 解析每一行,将键值对存入HashMap
// Parse each line and store the key-value pairs in a HashMap
String[] parts = line.split(" ");
if (parts.length == 3) {
statsMap.put(parts[1], parts[2]);
Expand All @@ -145,7 +145,7 @@ private static void parseSizesOutput(Map<String, String> statsMap,
String line;
while ((line = in.readLine()) != null && !line.equals(STATS_END_RSP)) {
String[] parts = line.split("\\s+");
// 提取 slab size slab count,并放入HashMap
// Extract slab size and slab count, then add them to the HashMap
if (parts.length >= 3 && "STAT".equals(parts[0])) {
statsMap.put("item_size", parts[1]);
statsMap.put("item_count", parts[2]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ private void fillBuilder(Metrics metrics, CollectRep.ValueRow.Builder valueRowBu
* Check that the mongodb connection information in metrics is complete
*/
private void preCheck(Metrics metrics) {
if (metrics == null || metrics.getMongodb() == null) {
throw new IllegalArgumentException("Mongodb collect must has mongodb params");
}
Assert.isTrue(metrics != null && metrics.getMongodb() != null, "Mongodb collect must has mongodb params");
MongodbProtocol mongodbProtocol = metrics.getMongodb();
Assert.hasText(mongodbProtocol.getCommand(), "Mongodb Protocol command is required.");
Assert.hasText(mongodbProtocol.getHost(), "Mongodb Protocol host is required.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ public String supportProtocol() {
* @param metrics metrics config
*/
private void preCheck(Metrics metrics) {
if (metrics == null || metrics.getRocketmq() == null) {
throw new IllegalArgumentException("Mongodb collect must has rocketmq params");
}
Assert.isTrue(metrics != null && metrics.getRocketmq() != null, "Mongodb collect must has rocketmq params");
RocketmqProtocol rocketmq = metrics.getRocketmq();
Assert.hasText(rocketmq.getNamesrvHost(), "Rocketmq Protocol namesrvHost is required.");
Assert.hasText(rocketmq.getNamesrvPort(), "Rocketmq Protocol namesrvPort is required.");
Expand Down Expand Up @@ -339,7 +337,8 @@ private void collectTopicData(DefaultMQAdminExt mqAdminExt, RocketmqCollectData
Map<String, List<RocketmqCollectData.TopicQueueInfo>> topicQueueInfoTable = new HashMap<>(32);
List<RocketmqCollectData.TopicQueueInfo> topicQueueInfoList = new ArrayList<>();

// todo 查询topic的queue信息需要for循环调用 mqAdminExt.examineTopicStats(), topic数量很大的情况, 调用次数也会很多
// When querying queue information for a topic, you need to use a for-loop to call mqAdminExt.examineTopicStats().
// If the number of topics is large, the number of calls will also be high
topicQueueInfoTable.put(topic, topicQueueInfoList);
topicInfoList.add(topicQueueInfoTable);
rocketmqCollectData.setTopicInfoList(topicInfoList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri
HttpUriRequest request = createHttpRequest(nebulaGraph.getHost(), nebulaGraph.getPort(),
nebulaGraph.getUrl(), nebulaGraph.getTimeout());
try {
// 发起http请求,获取响应数据
// Send an HTTP request to obtain response data
response = CommonHttpClient.getHttpClient().execute(request, httpContext);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != SUCCESS_CODE) {
Expand All @@ -110,7 +110,7 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri
resp = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
responseTime = System.currentTimeMillis() - startTime;
resultMap.put(CollectorConstants.RESPONSE_TIME, Long.toString(responseTime));
// 根据API进行不同解析
// Parse the response differently depending on the API
if (GRAPH_API.equals(nebulaGraph.getUrl())) {
parseStatsResponse(resp, nebulaGraph.getTimePeriod(), resultMap);
} else if (STORAGE_API.equals(nebulaGraph.getUrl())) {
Expand Down Expand Up @@ -177,13 +177,13 @@ private HttpUriRequest createHttpRequest(String host, String port, String url, S
}

/**
* 解析Stats响应通过时间间隔进行筛选
* Parse Stats response and filter by time period
*
* @param responseBody 响应体
* @param timePeriod 时间间隔
* @param responseBody response body
* @param timePeriod time period
*/
private void parseStatsResponse(String responseBody, String timePeriod, HashMap<String, String> resultMap) {
// 设置正则匹配
// Set up regular expression matching
String timeRegex = String.format(REGEX, timePeriod);
Pattern pattern = Pattern.compile(timeRegex);
String[] strArray = responseBody.split(STR_SPLIT);
Expand All @@ -198,9 +198,9 @@ private void parseStatsResponse(String responseBody, String timePeriod, HashMap<


/**
* 解析Storage响应通过时间间隔进行筛选
* Parse the Storage response and filter by time period
*
* @param responseBody 响应体
* @param responseBody response body
*/
private void parseStorageResponse(String responseBody, HashMap<String, String> resultMap) {
String[] strArray = responseBody.split(STR_SPLIT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private void parseNginxStatusResponse(CollectRep.MetricsData.Builder builder, St
}

/**
* 解析ngx_http_reqstat_module模块暴露信息
* Analyze the information exposed by the ngx_http_reqstat_module module
*
* @param builder builder
* @param resp resp
Expand All @@ -228,17 +228,17 @@ private void parseNginxStatusResponse(CollectRep.MetricsData.Builder builder, St
private void parseReqStatusResponse(CollectRep.MetricsData.Builder builder, String resp, Metrics metrics,
Long responseTime) {
//example
//zone_name key max_active max_bw traffic requests active bandwidth
//imgstore_appid 43 27 6M 63G 374063 0 0
//imgstore_appid 53 329 87M 2058G 7870529 50 25M
//server_addr 10.128.1.17 2 8968 24M 1849 0 0
//server_addr 127.0.0.1 1 6M 5G 912 1 0
//server_addr 180.96.x.1 3358 934M 27550G 141277391 891 356M
//server_addr 180.96.x.2 78 45M 220G 400704 0 0
//server_addr 180.96.x.3 242 58M 646G 2990547 42 7M
//server_name d.123.sogou.com 478 115M 2850G 30218726 115 39M
//server_name dl.pinyin.sogou.com 913 312M 8930G 35345453 225 97M
//server_name download.ie.sogou.com 964 275M 7462G 7979817 297 135M
//zone_name key max_active max_bw traffic requests active bandwidth
//imgstore_appid 43 27 6M 63G 374063 0 0
//imgstore_appid 53 329 87M 2058G 7870529 50 25M
//server_addr 10.128.1.17 2 8968 24M 1849 0 0
//server_addr 127.0.0.1 1 6M 5G 912 1 0
//server_addr 180.96.x.1 3358 934M 27550G 141277391 891 356M
//server_addr 180.96.x.2 78 45M 220G 400704 0 0
//server_addr 180.96.x.3 242 58M 646G 2990547 42 7M
//server_name d.123.sogou.com 478 115M 2850G 30218726 115 39M
//server_name dl.pinyin.sogou.com 913 312M 8930G 35345453 225 97M
//server_name download.ie.sogou.com 964 275M 7462G 7979817 297 135M
List<ReqStatusResponse> reqStatusResponses = regexReqStatusMatch(resp);
List<String> aliasFields = metrics.getAliasFields();

Expand Down Expand Up @@ -273,7 +273,7 @@ private Object reflect(ReqStatusResponse reqStatusResponse, String methodName) t

private Map<String, Object> regexNginxStatusMatch(String resp, Integer aliasFieldsSize) {
Map<String, Object> metricsMap = new HashMap<>(aliasFieldsSize);
// 正则提取监控信息
// Extract monitoring information using regular expressions
Pattern pattern = Pattern.compile(REGEX_SERVER);
Matcher matcher = pattern.matcher(resp);
while (matcher.find()) {
Expand Down
Loading