Skip to content

Commit f8e85ec

Browse files
MasamiYuiyinyijuntomsun28
authored
[feature] Support skywalking alert source (apache#3144)
Co-authored-by: yinyijun <[email protected]> Co-authored-by: yinyijun <yingey2011> Co-authored-by: tomsun28 <[email protected]>
1 parent 3468739 commit f8e85ec

File tree

12 files changed

+272
-1
lines changed

12 files changed

+272
-1
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.hertzbeat.alert.dto;
19+
20+
import lombok.AllArgsConstructor;
21+
import lombok.Builder;
22+
import lombok.Data;
23+
import lombok.NoArgsConstructor;
24+
25+
import java.util.List;
26+
27+
28+
/**
29+
* SkyWalking Alert Content Entity
30+
* @see <a href="https://skywalking.apache.org/docs/main/latest/en/setup/backend/backend-alarm/">backend-alarm</a>
31+
*/
32+
@Data
33+
@Builder
34+
@AllArgsConstructor
35+
@NoArgsConstructor
36+
public class SkyWalkingExternAlert {
37+
38+
private Long scopeId;
39+
40+
private String scope;
41+
42+
private String name;
43+
44+
private String id0;
45+
46+
private String id1;
47+
48+
private String ruleName;
49+
50+
private String alarmMessage;
51+
52+
private Long startTime;
53+
54+
private List<Tag> tags;
55+
56+
/**
57+
* SkyWalking Tag Entity
58+
*/
59+
@Data
60+
public static class Tag {
61+
62+
private String key;
63+
64+
private String value;
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.hertzbeat.alert.service.impl;
19+
20+
import com.fasterxml.jackson.core.type.TypeReference;
21+
import lombok.extern.slf4j.Slf4j;
22+
import org.apache.hertzbeat.alert.dto.SkyWalkingExternAlert;
23+
import org.apache.hertzbeat.alert.reduce.AlarmCommonReduce;
24+
import org.apache.hertzbeat.alert.service.ExternAlertService;
25+
import org.apache.hertzbeat.common.constants.CommonConstants;
26+
import org.apache.hertzbeat.common.entity.alerter.SingleAlert;
27+
import org.apache.hertzbeat.common.util.JsonUtil;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.stereotype.Service;
30+
31+
import java.time.Instant;
32+
import java.util.HashMap;
33+
import java.util.List;
34+
import java.util.Map;
35+
36+
/**
37+
* SkyWalking external alarm service impl
38+
*/
39+
@Slf4j
40+
@Service
41+
public class SkyWalkingExternAlertService implements ExternAlertService {
42+
43+
@Autowired
44+
private AlarmCommonReduce alarmCommonReduce;
45+
46+
47+
@Override
48+
public void addExternAlert(String content) {
49+
TypeReference<List<SkyWalkingExternAlert>> typeReference = new TypeReference<>() {};
50+
List<SkyWalkingExternAlert> alerts = JsonUtil.fromJson(content, typeReference);
51+
if (alerts == null || alerts.isEmpty()) {
52+
log.warn("Parse SkyWalking extern alert content failed! content: {}", content);
53+
return;
54+
}
55+
for (SkyWalkingExternAlert alert : alerts) {
56+
SingleAlert singleAlert = SingleAlert.builder()
57+
.content(alert.getAlarmMessage())
58+
.status(CommonConstants.ALERT_STATUS_FIRING)
59+
.activeAt(Instant.now().toEpochMilli())
60+
.startAt(alert.getStartTime() != null ? alert.getStartTime() : Instant.now().toEpochMilli())
61+
.labels(acquireAlertLabels(alert))
62+
.annotations(acquireAlertAnnotations(alert))
63+
.triggerTimes(1)
64+
.build();
65+
alarmCommonReduce.reduceAndSendAlarm(singleAlert);
66+
}
67+
}
68+
69+
@Override
70+
public String supportSource() {
71+
return "skywalking";
72+
}
73+
74+
private Map<String, String> acquireAlertLabels(SkyWalkingExternAlert externAlert){
75+
Map<String, String> labels = new HashMap<>(8);
76+
labels.put("__source__", "skywalking");
77+
List<SkyWalkingExternAlert.Tag> tags = externAlert.getTags();
78+
if (tags == null || tags.isEmpty()){
79+
return labels;
80+
}
81+
tags.forEach(tag -> labels.put(tag.getKey(), tag.getValue()));
82+
return labels;
83+
}
84+
85+
private Map<String, String> acquireAlertAnnotations(SkyWalkingExternAlert externAlert){
86+
Map<String, String> annotations = new HashMap<>(8);
87+
annotations.putIfAbsent("scope", externAlert.getScope());
88+
annotations.putIfAbsent("name", externAlert.getName());
89+
annotations.putIfAbsent("id0", externAlert.getId0());
90+
annotations.putIfAbsent("id1", externAlert.getId1());
91+
annotations.putIfAbsent("ruleName", externAlert.getRuleName());
92+
return annotations;
93+
}
94+
95+
}

web-app/src/app/routes/alert/alert-integration/alert-integration.component.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ export class AlertIntegrationComponent implements OnInit {
6262
id: 'alertmanager',
6363
name: this.i18nSvc.fanyi('alert.integration.source.alertmanager'),
6464
icon: 'assets/img/integration/prometheus.svg'
65+
},
66+
{
67+
id: 'skywalking',
68+
name: this.i18nSvc.fanyi('alert.integration.source.skywalking'),
69+
icon: 'assets/img/integration/skywalking.svg'
6570
}
6671
// {
6772
// id: 'tencent',
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
>Send SkyWalking Alerts to HertzBeat Alert Platform via Webhook.
2+
3+
### SkyWalking Service Configuration
4+
5+
- Edit the SkyWalking configuration file `alarm-settings.yml` to add HertzBeat as the alert receiver configuration
6+
```yaml
7+
hooks:
8+
webhook:
9+
default:
10+
is-default: true
11+
urls:
12+
- http://{hertzbeat_host}:1157/api/alerts/report/skywalking
13+
```
14+
- `http://{hertzbeat_host}:1157/api/alerts/report/skywalking` is the Webhook API endpoint provided by HertzBeat
15+
- Reload and restart the SkyWalking OAP Server
16+
17+
### Verify Configuration
18+
19+
1. Ensure the SkyWalking alarm configuration is correct and reload the configuration
20+
2. Check the status of SkyWalking alert rules
21+
3. Trigger a test alert and check in the HertzBeat alert center
22+
23+
### Common Issues
24+
25+
- Ensure the HertzBeat URL is accessible from the SkyWalking OAP server
26+
- Check the SkyWalking logs for any error messages regarding alert sending failures
27+
- Verify the correctness of the alert rule expressions
28+
29+
For more information, please refer to the [ SkyWalking Alarm Configuration Documentation](https://skywalking.apache.org/docs/main/latest/en/setup/backend/backend-alarm/)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
>将 SkyWalking 的告警通过 Webhook 方式发送到 HertzBeat 告警平台。
2+
3+
### SkyWalking 服务配置
4+
5+
- 编辑 SkyWalking 配置文件 `alarm-settings.yml`,添加 HertzBeat 作为告警接收端配置
6+
```yaml
7+
hooks:
8+
webhook:
9+
default:
10+
is-default: true
11+
urls:
12+
- http://{hertzbeat_host}:1157/api/alerts/report/skywalking
13+
```
14+
- `http://{hertzbeat_host}:1157/api/alerts/report/skywalking` 为 HertzBeat 提供的 webhook 接口地址
15+
- 重新加载启动 SkyWalking OAP Server
16+
17+
### 验证配置
18+
19+
1. 确保 SkyWalking 配置正确并重新加载配置
20+
2. 检查 SkyWalking 告警规则状态
21+
3. 触发测试告警并在 SkyWalking 告警中心查看
22+
23+
### 常见问题
24+
25+
- 确保 HertzBeat URL 可以被 SkyWalking 服务器访问
26+
- 检查 SkyWalking 日志中是否有告警发送失败的错误信息
27+
- 验证告警规则表达式的正确性
28+
29+
更多信息请参考 [SkyWalking 告警配置文档](https://skywalking.apache.org/docs/main/latest/en/setup/backend/backend-alarm/)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
>將 SkyWalking 的告警透過 Webhook 方式發送到 HertzBeat 告警平台。
2+
3+
### SkyWalking 服務配置
4+
5+
- 編輯 SkyWalking 配置文件 `alarm-settings.yml`,添加 HertzBeat 作為告警接收端配置
6+
```yaml
7+
hooks:
8+
webhook:
9+
default:
10+
is-default: true
11+
urls:
12+
- http://{hertzbeat_host}:1157/api/alerts/report/skywalking
13+
14+
```
15+
- `{hertzbeat_host}:1157` 為 HertzBeat Server 地址和端口,根據實際情況修改,需要保證網絡連通性
16+
- 重新加載啟動 SkyWalking OAP Server
17+
18+
### 驗證配置
19+
20+
1. 確保 SkyWalking 配置正確並重新加載配置
21+
```bash
22+
curl -X POST http://localhost:9090/-/reload
23+
```
24+
2. 檢查 SkyWalking 告警規則狀態
25+
```bash
26+
curl http://localhost:9090/api/v1/rules
27+
```
28+
3. 觸發測試告警並在 HertzBeat 告警中心查看
29+
30+
### 常見問題
31+
32+
- 確保 HertzBeat URL 可以被 SkyWalking 伺服器訪問
33+
- 檢查 SkyWalking 日誌中是否有告警發送失敗的錯誤信息
34+
- 驗證告警規則表達式的正確性
35+
36+
更多信息請參考 [SkyWalking 告警配置文檔](https://skywalking.apache.org/docs/main/latest/en/setup/backend/backend-alarm/)

web-app/src/assets/i18n/en-US.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
"alert.integration.source.prometheus": "Prometheus",
9898
"alert.integration.source.tencent": "Tencent Cloud",
9999
"alert.integration.source.webhook": "Default Webhook",
100+
"alert.integration.source.skywalking": "SkyWalking",
100101
"alert.integration.token.desc": "Token you generated that can be used to access the HertzBeat API.",
101102
"alert.integration.token.new": "Click to Generate Token",
102103
"alert.integration.token.notice": "Token only be displayed once. Please keep your token secure. Do not share it with others.",

web-app/src/assets/i18n/ja-JP.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
"alert.integration.source.prometheus": "Prometheus",
9898
"alert.integration.source.tencent": "Tencent Cloud",
9999
"alert.integration.source.webhook": "デフォルトWebhook",
100+
"alert.integration.source.skywalking": "SkyWalking",
100101
"alert.integration.token.desc": "HertzBeat APIにアクセスするために生成したトークン。",
101102
"alert.integration.token.new": "トークンを生成するにはクリック",
102103
"alert.integration.token.notice": "トークンは一度だけ表示されます。トークンを安全に保管し、他人と共有しないでください。",

web-app/src/assets/i18n/pt-BR.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@
366366
"alert.notice.sender.sms.tencent.templateId": "ID do Modelo do SMS Tencent",
367367
"alert.export.switch-type": "Selecione o formato do arquivo de exportação!",
368368
"alert.export.use-type": "Exportar regras no formato de arquivo {{type}}",
369+
"alert.integration.source.skywalking": "SkyWalking",
369370
"dashboard.alerts.title": "Lista de Alarmes Recentes",
370371
"dashboard.alerts.title-no": "Alarmes Pendentes Recentes",
371372
"dashboard.alerts.no": "Nenhum Alarme Pendente",
@@ -767,4 +768,4 @@
767768
"validation.standard.required": "Por favor, insira uma métrica",
768769
"expand": "Expandir",
769770
"collapse": "Recolher"
770-
}
771+
}

web-app/src/assets/i18n/zh-CN.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
"alert.integration.source.prometheus": "Prometheus",
9898
"alert.integration.source.tencent": "腾讯云监控",
9999
"alert.integration.source.webhook": "默认Webhook",
100+
"alert.integration.source.skywalking": "SkyWalking",
100101
"alert.integration.token.desc": "生成的 Token 可用于访问 HertzBeat API",
101102
"alert.integration.token.new": "点击生成 Token",
102103
"alert.integration.token.notice": "此内容只会展示一次,请妥善保管您的 Token,不要泄露给他人",

web-app/src/assets/i18n/zh-TW.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
"alert.integration.source.prometheus": "Prometheus",
9898
"alert.integration.source.tencent": "腾讯云监控",
9999
"alert.integration.source.webhook": "默认Webhook",
100+
"alert.integration.source.skywalking": "SkyWalking",
100101
"alert.integration.token.desc": "生成的 Token 可用于访问 HertzBeat API",
101102
"alert.integration.token.new": "点击生成 Token",
102103
"alert.integration.token.notice": "此内容只会展示一次,请妥善保管您的 Token,不要泄露给他人",
Lines changed: 6 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)