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

support monitor metrics name i18n #1198

Merged
merged 9 commits into from
Aug 23, 2023
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 @@ -27,6 +27,7 @@
import org.dromara.hertzbeat.common.entity.job.protocol.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -51,6 +52,13 @@ public class Metrics {
* 公共属性-名称 eg: cpu | memory | health
*/
private String name;
/**
* metrics group name's i18n value
* 指标集合的国际化名称
* zh-CN: CPU信息
* en-US: CPU Info
*/
private Map<String, String> i18n;
/**
* 公共属性-采集监控协议 eg: sql, ssh, http, telnet, wmi, snmp, sdk
*/
Expand Down Expand Up @@ -241,6 +249,13 @@ public static class Field {
* 指标名称
*/
private String field;
/**
* metric field name's i18n value
* 指标的国际化名称
* zh-CN: CPU 版本号
* en-US: CPU Version
*/
private Map<String, String> i18n;
/**
* Indicator type 0-number: number 1-string: string
* 指标类型 0-number:数字 1-string:字符串
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -134,5 +136,14 @@ public static String removeBlankLine(String value) {
}
return value.replaceAll("(?m)^\\s*$(\\n|\\r\\n)", "");
}

public static String getLangMappingValueFromI18nMap(String lang, Map<String, String> i18nMap) {
if (i18nMap == null || i18nMap.isEmpty()) {
return null;
}
return Optional.ofNullable(i18nMap.get(lang))
.orElse(i18nMap.values().stream()
.findFirst().orElse(null));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public ResponseEntity<Message<Void>> updateAppDefineYml(@Valid @RequestBody Moni
}

@GetMapping(path = "/hierarchy")
@Operation(summary = "Query all monitored types-indicator group-indicator level, output in a hierarchical structure", description = "查询所有监控的类型-指标组-指标层级,以层级结构输出")
@Operation(summary = "Query all monitor metrics level, output in a hierarchical structure", description = "查询所有监控的类型-指标组-指标层级,以层级结构输出")
public ResponseEntity<Message<List<Hierarchy>>> queryAppsHierarchy(
@Parameter(description = "en: language type,zh: 语言类型",
example = "zh-CN")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.dromara.hertzbeat.common.entity.job.Metrics;
import org.dromara.hertzbeat.common.entity.manager.Monitor;
import org.dromara.hertzbeat.common.support.SpringContextHolder;
import org.dromara.hertzbeat.common.util.CommonUtil;
import org.dromara.hertzbeat.manager.dao.MonitorDao;
import org.dromara.hertzbeat.manager.pojo.dto.Hierarchy;
import org.dromara.hertzbeat.common.entity.manager.ParamDefine;
Expand Down Expand Up @@ -118,37 +119,41 @@ public List<String> getAppDefineMetricNames(String app) {
public Map<String, String> getI18nResources(String lang) {
Map<String, String> i18nMap = new HashMap<>(128);
for (Job job : appDefines.values()) {
// TODO needs to support the metrics name i18n
// TODO 后面需要支持指标名称国际化
Map<String, String> name = job.getName();
if (name != null && !name.isEmpty()) {
String i18nName = Optional.ofNullable(name.get(lang)).orElse(name.values().stream().findFirst().orElse(null));
if (i18nName != null) {
i18nMap.put("monitor.app." + job.getApp(), i18nName);
}
String i18nName = CommonUtil.getLangMappingValueFromI18nMap(lang, name);
if (i18nName != null) {
i18nMap.put("monitor.app." + job.getApp(), i18nName);
}
Map<String, String> help = job.getHelp();
if (help != null && !help.isEmpty()) {
String i18nHelp = Optional.ofNullable(help.get(lang)).orElse(help.values().stream().findFirst().orElse(null));
if (i18nHelp != null) {
i18nMap.put("monitor.app." + job.getApp() + ".help", i18nHelp);
}
String i18nHelp = CommonUtil.getLangMappingValueFromI18nMap(lang, help);
if (i18nHelp != null) {
i18nMap.put("monitor.app." + job.getApp() + ".help", i18nHelp);
}

Map<String, String> helpLink = job.getHelpLink();
if (helpLink != null && !helpLink.isEmpty()) {
String i18nHelp = Optional.ofNullable(helpLink.get(lang)).orElse(helpLink.values().stream().findFirst().orElse(null));
if (i18nHelp != null) {
i18nMap.put("monitor.app." + job.getApp() + ".helpLink", i18nHelp);
}
String i18nHelpLink = CommonUtil.getLangMappingValueFromI18nMap(lang, helpLink);
if (i18nHelpLink != null) {
i18nMap.put("monitor.app." + job.getApp() + ".helpLink", i18nHelpLink);
}

for (ParamDefine paramDefine : job.getParams()) {
Map<String, String> paramDefineName = paramDefine.getName();
if (paramDefineName != null && !paramDefineName.isEmpty()) {
String i18nName = Optional.ofNullable(paramDefineName.get(lang)).orElse(paramDefineName.values().stream().findFirst().orElse(null));
if (i18nName != null) {
i18nMap.put("monitor.app." + job.getApp() + ".param." + paramDefine.getField(), i18nName);
String i18nParamName = CommonUtil.getLangMappingValueFromI18nMap(lang, paramDefineName);
if (i18nParamName != null) {
i18nMap.put("monitor.app." + job.getApp() + ".param." + paramDefine.getField(), i18nParamName);
}
}
for (Metrics metrics : job.getMetrics()) {
Map<String, String> metricsI18nName = metrics.getI18n();
String i18nMetricsName = CommonUtil.getLangMappingValueFromI18nMap(lang, metricsI18nName);
if (i18nMetricsName != null) {
i18nMap.put("monitor.app." + job.getApp() + ".metrics." + metrics.getName(), i18nMetricsName);
}
for (Metrics.Field field : metrics.getFields()) {
Map<String, String> fieldI18nName = field.getI18n();
String i18nMetricName = CommonUtil.getLangMappingValueFromI18nMap(lang, fieldI18nName);
if (i18nMetricName != null) {
i18nMap.put("monitor.app." + job.getApp() + ".metrics." + metrics.getName() + ".metric." + field.getField(), i18nMetricName);
}
}
}
Expand All @@ -165,7 +170,7 @@ public List<Hierarchy> getAllAppHierarchy(String lang) {
hierarchyApp.setValue(job.getApp());
Map<String, String> nameMap = job.getName();
if (nameMap != null && !nameMap.isEmpty()) {
String i18nName = Optional.ofNullable(nameMap.get(lang)).orElse(nameMap.values().stream().findFirst().orElse(null));
String i18nName = CommonUtil.getLangMappingValueFromI18nMap(lang, nameMap);
if (i18nName != null) {
hierarchyApp.setLabel(i18nName);
}
Expand All @@ -175,13 +180,15 @@ public List<Hierarchy> getAllAppHierarchy(String lang) {
for (Metrics metrics : job.getMetrics()) {
Hierarchy hierarchyMetric = new Hierarchy();
hierarchyMetric.setValue(metrics.getName());
hierarchyMetric.setLabel(metrics.getName());
String metricsI18nName = CommonUtil.getLangMappingValueFromI18nMap(lang, metrics.getI18n());
hierarchyMetric.setLabel(metricsI18nName != null ? metricsI18nName : metrics.getName());
List<Hierarchy> hierarchyFieldList = new LinkedList<>();
if (metrics.getFields() != null) {
for (Metrics.Field field : metrics.getFields()) {
Hierarchy hierarchyField = new Hierarchy();
hierarchyField.setValue(field.getField());
hierarchyField.setLabel(field.getField());
String metricI18nName = CommonUtil.getLangMappingValueFromI18nMap(lang, field.getI18n());
hierarchyField.setLabel(metricI18nName != null ? metricI18nName : field.getField());
hierarchyField.setIsLeaf(true);
// for metric
hierarchyField.setType(field.getType());
Expand Down
36 changes: 34 additions & 2 deletions manager/src/main/resources/define/app-a_example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ metrics:
# metrics - cpu
# 监控指标组 - cpu
- name: cpu
# metrics name i18n label
# 指标组国际化展示名称
i18n:
zh-CN: CPU 信息
en-US: CPU Info
# metrics group scheduling priority(0->127)->(high->low), metrics with the same priority will be scheduled in parallel
# priority 0's metrics group is availability metrics, it will be scheduled first, only availability metrics collect success will the scheduling continue
# 指标组调度优先级(0->127)->(优先级高->低) 优先级低的指标组会等优先级高的指标组采集完成后才会被调度, 相同优先级的指标组会并行调度采集
Expand All @@ -182,19 +187,31 @@ metrics:
# collect metrics content
# 具体监控指标列表
fields:
# field-metric name, type-metric type(0-number,1-string), unit-metric unit('%','ms','MB'), instance-if is metrics group unique identifier
# field-指标名称, type-指标类型(0-number数字,1-string字符串), unit-指标单位('%','ms','MB'), instance-是否是指标集合唯一标识符字段
# field-metric name, i18n-metric name i18n label, type-metric type(0-number,1-string), unit-metric unit('%','ms','MB'), instance-if is metrics group unique identifier
# field-指标名称, i18n-指标国际化展示名称, type-指标类型(0-number数字,1-string字符串), unit-指标单位('%','ms','MB'), instance-是否是指标集合唯一标识符字段
- field: hostname
type: 1
instance: true
i18n:
zh-CN: 主机名称
en-US: Host Name
- field: usage
type: 0
unit: '%'
i18n:
zh-CN: 使用率
en-US: Usage
- field: cores
type: 0
i18n:
zh-CN: 核数
en-US: Cores
- field: waitTime
type: 0
unit: s
i18n:
zh-CN: 主机名称
en-US: Host Name
# (optional)metrics field alias name, it is used as an alias field to map and convert the collected data and metrics field
# (可选)监控指标别名, 做为中间字段与采集数据字段和指标字段映射转换
aliasFields:
Expand Down Expand Up @@ -245,19 +262,34 @@ metrics:
parseScript: '$'

- name: memory
i18n:
zh-CN: 内存信息
en-US: Memory Info
priority: 1
fields:
- field: hostname
type: 1
instance: true
i18n:
zh-CN: 主机名称
en-US: Hostname
- field: total
type: 0
unit: kb
i18n:
zh-CN: 总量
en-US: Total
- field: usage
type: 0
unit: '%'
i18n:
zh-CN: 使用率
en-US: Usage
- field: speed
type: 0
i18n:
zh-CN: 速率
en-US: Speed
protocol: http
http:
host: ^_^host^_^
Expand Down
Loading
Loading