這篇文章將為大家詳細講解有關SkyWalking實現(xiàn)告警功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)公司主營陜州網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,手機APP定制開發(fā),陜州h5小程序制作搭建,陜州網(wǎng)站營銷推廣歡迎陜州等地區(qū)企業(yè)咨詢
SkyWalking 告警功能是在6.x版本新增的,其核心由一組規(guī)則驅(qū)動,這些規(guī)則定義在config/alarm-settings.yml
文件中。 告警規(guī)則的定義分為兩部分:
SkyWalking 的發(fā)行版都會默認提供config/alarm-settings.yml
文件,里面預先定義了一些常用的告警規(guī)則。如下:
這些預定義的告警規(guī)則,打開config/alarm-settings.yml
文件即可看到。其具體內(nèi)容如下:
rules:
# Rule unique name, must be ended with `_rule`.
service_resp_time_rule:
metrics-name: service_resp_time
op: ">"
threshold: 1000
period: 10
count: 3
silence-period: 5
message: Response time of service {name} is more than 1000ms in 3 minutes of last 10 minutes.
service_sla_rule:
# Metrics value need to be long, double or int
metrics-name: service_sla
op: "<"
threshold: 8000
# The length of time to evaluate the metrics
period: 10
# How many times after the metrics match the condition, will trigger alarm
count: 2
# How many times of checks, the alarm keeps silence after alarm triggered, default as same as period.
silence-period: 3
message: Successful rate of service {name} is lower than 80% in 2 minutes of last 10 minutes
service_p90_sla_rule:
# Metrics value need to be long, double or int
metrics-name: service_p90
op: ">"
threshold: 1000
period: 10
count: 3
silence-period: 5
message: 90% response time of service {name} is more than 1000ms in 3 minutes of last 10 minutes
service_instance_resp_time_rule:
metrics-name: service_instance_resp_time
op: ">"
threshold: 1000
period: 10
count: 2
silence-period: 5
message: Response time of service instance {name} is more than 1000ms in 2 minutes of last 10 minutes
除此之外,官方還提供了一個config/alarm-settings-sample.yml
文件,該文件是一個告警規(guī)則的示例文件,里面展示了目前支持的所有告警規(guī)則配置項:
# Sample alarm rules.
rules:
# Rule unique name, must be ended with `_rule`.
endpoint_percent_rule:
# Metrics value need to be long, double or int
metrics-name: endpoint_percent
threshold: 75
op: <
# The length of time to evaluate the metrics
period: 10
# How many times after the metrics match the condition, will trigger alarm
count: 3
# How many times of checks, the alarm keeps silence after alarm triggered, default as same as period.
silence-period: 10
message: Successful rate of endpoint {name} is lower than 75%
service_percent_rule:
metrics-name: service_percent
# [Optional] Default, match all services in this metrics
include-names:
- service_a
- service_b
exclude-names:
- service_c
threshold: 85
op: <
period: 10
count: 4
告警規(guī)則配置項的說明:
_rule
結尾,前綴可自定義long
、double
和int
類型。詳見Official OAL script>
、<
、=
Webhook可以簡單理解為是一種Web層面的回調(diào)機制,通常由一些事件觸發(fā),與代碼中的事件回調(diào)類似,只不過是Web層面的。由于是Web層面的,所以當事件發(fā)生時,回調(diào)的不再是代碼中的方法或函數(shù),而是服務接口。例如,在告警這個場景,告警就是一個事件。當該事件發(fā)生時,SkyWalking就會主動去調(diào)用一個配置好的接口,該接口就是所謂的Webhook。
SkyWalking的告警消息會通過 HTTP 請求進行發(fā)送,請求方法為 POST
,Content-Type
為 application/json
,其JSON 數(shù)據(jù)實基于List<org.apache.skywalking.oap.server.core.alarm.AlarmMessage
進行序列化的。JSON數(shù)據(jù)示例:
[{
"scopeId": 1,
"scope": "SERVICE",
"name": "serviceA",
"id0": 12,
"id1": 0,
"ruleName": "service_resp_time_rule",
"alarmMessage": "alarmMessage xxxx",
"startTime": 1560524171000
}, {
"scopeId": 1,
"scope": "SERVICE",
"name": "serviceB",
"id0": 23,
"id1": 0,
"ruleName": "service_resp_time_rule",
"alarmMessage": "alarmMessage yyy",
"startTime": 1560524171000
}]
字段說明:
org.apache.skywalking.oap.server.core.source.DefaultScopeDefine
根據(jù)以上兩個小節(jié)的介紹,可以得知:SkyWalking是不支持直接向郵箱、短信等服務發(fā)送告警信息的,SkyWalking只會在發(fā)生告警時將告警信息發(fā)送至配置好的Webhook接口。
但我們總不能人工盯著該接口的日志信息來得知服務是否發(fā)生了告警,因此我們需要在該接口里實現(xiàn)發(fā)送郵件或短信等功能,從而達到個性化的告警通知。
接下來開始動手實踐,這里基于Spring Boot進行實現(xiàn)。首先是添加依賴:
org.springframework.boot
spring-boot-starter-mail
配置郵箱服務:
server:
port: 9134
#郵箱配置
spring:
mail:
host: smtp.163.com
#發(fā)送者郵箱賬號
username: 你的郵箱@163.com
#發(fā)送者密鑰
password: 你的郵箱服務密鑰
default-encoding: utf-8
port: 465 #端口號465或587
protocol: smtp
properties:
mail:
debug:
false
smtp:
socketFactory:
class: javax.net.ssl.SSLSocketFactory
根據(jù)SkyWalking發(fā)送的JSON數(shù)據(jù)定義一個DTO,用于接口接收數(shù)據(jù):
@Data
public class SwAlarmDTO {
private Integer scopeId;
private String scope;
private String name;
private Integer id0;
private Integer id1;
private String ruleName;
private String alarmMessage;
private Long startTime;
}
接著定義一個接口,實現(xiàn)接收SkyWalking的告警通知,并將數(shù)據(jù)發(fā)送至郵箱:
package com.example.alarmdemo.controller;
import com.example.alarmdemo.dto.SwAlarmDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/alarm")
public class SwAlarmController {
private final JavaMailSender sender;
@Value("${spring.mail.username}")
private String from;
/**
* 接收skywalking服務的告警通知并發(fā)送至郵箱
*/
@PostMapping("/receive")
public void receive(@RequestBody List alarmList) {
SimpleMailMessage message = new SimpleMailMessage();
// 發(fā)送者郵箱
message.setFrom(from);
// 接收者郵箱
message.setTo(from);
// 主題
message.setSubject("告警郵件");
String content = getContent(alarmList);
// 郵件內(nèi)容
message.setText(content);
sender.send(message);
log.info("告警郵件已發(fā)送...");
}
private String getContent(List alarmList) {
StringBuilder sb = new StringBuilder();
for (SwAlarmDTO dto : alarmList) {
sb.append("scopeId: ").append(dto.getScopeId())
.append("\nscope: ").append(dto.getScope())
.append("\n目標 Scope 的實體名稱: ").append(dto.getName())
.append("\nScope 實體的 ID: ").append(dto.getId0())
.append("\nid1: ").append(dto.getId1())
.append("\n告警規(guī)則名稱: ").append(dto.getRuleName())
.append("\n告警消息內(nèi)容: ").append(dto.getAlarmMessage())
.append("\n告警時間: ").append(dto.getStartTime())
.append("\n\n---------------\n\n");
}
return sb.toString();
}
}
最后將該接口配置到SkyWalking中,Webhook的配置位于config/alarm-settings.yml
文件的末尾,格式為http://{ip}:{port}/{uri}
。如下示例:
[root@localhost skywalking]# vim config/alarm-settings.yml
webhooks:
- http://127.0.0.1:9134/alarm/receive
完成告警接口的開發(fā)及配置后,我們來進行一個簡單的測試。這里有一條調(diào)用鏈路如下:
我在/producer
接口中增加了一行會導致異常的代碼,故意使該接口不可用:
@GetMapping
public String producer() {
log.info("received a request");
int i = 1 / 0;
return "this message from producer";
}
接下來編寫一段測試代碼,讓其服務成功率滿足在過去2分鐘內(nèi)低于80%這條默認的告警規(guī)則:
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
for (int i = 0; i < 100; i++) {
String result = restTemplate.getForObject("http://127.0.0.1:8936/consumer", String.class);
log.info(result);
}
}
執(zhí)行完測試代碼,等待約兩分鐘后,告警接口的控制臺輸出了一段日志信息:
此時,郵箱正常收到了告警郵件:
以上便是SkyWalking實現(xiàn)告警功能的介紹,雖然從篇幅上看很復雜,但是示例代碼非常詳細且容易理解,如果想了解更多相關內(nèi)容,請關注創(chuàng)新互聯(lián)行業(yè)資訊。