這篇文章將為大家詳細(xì)講解有關(guān)SkyWalking實(shí)現(xiàn)告警功能,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)公司主營陜州網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,手機(jī)APP定制開發(fā),陜州h5小程序制作搭建,陜州網(wǎng)站營銷推廣歡迎陜州等地區(qū)企業(yè)咨詢
SkyWalking 告警功能是在6.x版本新增的,其核心由一組規(guī)則驅(qū)動(dòng),這些規(guī)則定義在config/alarm-settings.yml
文件中。 告警規(guī)則的定義分為兩部分:
SkyWalking 的發(fā)行版都會(huì)默認(rèn)提供config/alarm-settings.yml
文件,里面預(yù)先定義了一些常用的告警規(guī)則。如下:
這些預(yù)定義的告警規(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
除此之外,官方還提供了一個(gè)config/alarm-settings-sample.yml
文件,該文件是一個(gè)告警規(guī)則的示例文件,里面展示了目前支持的所有告警規(guī)則配置項(xiàng):
# 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ī)則配置項(xiàng)的說明:
_rule
結(jié)尾,前綴可自定義long
、double
和int
類型。詳見Official OAL script>
、<
、=
Webhook可以簡單理解為是一種Web層面的回調(diào)機(jī)制,通常由一些事件觸發(fā),與代碼中的事件回調(diào)類似,只不過是Web層面的。由于是Web層面的,所以當(dāng)事件發(fā)生時(shí),回調(diào)的不再是代碼中的方法或函數(shù),而是服務(wù)接口。例如,在告警這個(gè)場(chǎng)景,告警就是一個(gè)事件。當(dāng)該事件發(fā)生時(shí),SkyWalking就會(huì)主動(dòng)去調(diào)用一個(gè)配置好的接口,該接口就是所謂的Webhook。
SkyWalking的告警消息會(huì)通過 HTTP 請(qǐng)求進(jìn)行發(fā)送,請(qǐng)求方法為 POST
,Content-Type
為 application/json
,其JSON 數(shù)據(jù)實(shí)基于List<org.apache.skywalking.oap.server.core.alarm.AlarmMessage
進(jìn)行序列化的。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ù)以上兩個(gè)小節(jié)的介紹,可以得知:SkyWalking是不支持直接向郵箱、短信等服務(wù)發(fā)送告警信息的,SkyWalking只會(huì)在發(fā)生告警時(shí)將告警信息發(fā)送至配置好的Webhook接口。
但我們總不能人工盯著該接口的日志信息來得知服務(wù)是否發(fā)生了告警,因此我們需要在該接口里實(shí)現(xiàn)發(fā)送郵件或短信等功能,從而達(dá)到個(gè)性化的告警通知。
接下來開始動(dòng)手實(shí)踐,這里基于Spring Boot進(jìn)行實(shí)現(xiàn)。首先是添加依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
配置郵箱服務(wù):
server:
port: 9134
#郵箱配置
spring:
mail:
host: smtp.163.com
#發(fā)送者郵箱賬號(hào)
username: 你的郵箱@163.com
#發(fā)送者密鑰
password: 你的郵箱服務(wù)密鑰
default-encoding: utf-8
port: 465 #端口號(hào)465或587
protocol: smtp
properties:
mail:
debug:
false
smtp:
socketFactory:
class: javax.net.ssl.SSLSocketFactory
根據(jù)SkyWalking發(fā)送的JSON數(shù)據(jù)定義一個(gè)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;
}
接著定義一個(gè)接口,實(shí)現(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服務(wù)的告警通知并發(fā)送至郵箱
*/
@PostMapping("/receive")
public void receive(@RequestBody List<SwAlarmDTO> 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<SwAlarmDTO> alarmList) {
StringBuilder sb = new StringBuilder();
for (SwAlarmDTO dto : alarmList) {
sb.append("scopeId: ").append(dto.getScopeId())
.append("\nscope: ").append(dto.getScope())
.append("\n目標(biāo) Scope 的實(shí)體名稱: ").append(dto.getName())
.append("\nScope 實(shí)體的 ID: ").append(dto.getId0())
.append("\nid1: ").append(dto.getId1())
.append("\n告警規(guī)則名稱: ").append(dto.getRuleName())
.append("\n告警消息內(nèi)容: ").append(dto.getAlarmMessage())
.append("\n告警時(shí)間: ").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ā)及配置后,我們來進(jìn)行一個(gè)簡單的測(cè)試。這里有一條調(diào)用鏈路如下:
我在/producer
接口中增加了一行會(huì)導(dǎo)致異常的代碼,故意使該接口不可用:
@GetMapping
public String producer() {
log.info("received a request");
int i = 1 / 0;
return "this message from producer";
}
接下來編寫一段測(cè)試代碼,讓其服務(wù)成功率滿足在過去2分鐘內(nèi)低于80%這條默認(rèn)的告警規(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í)行完測(cè)試代碼,等待約兩分鐘后,告警接口的控制臺(tái)輸出了一段日志信息:
此時(shí),郵箱正常收到了告警郵件:
以上便是SkyWalking實(shí)現(xiàn)告警功能的介紹,雖然從篇幅上看很復(fù)雜,但是示例代碼非常詳細(xì)且容易理解,如果想了解更多相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊。
分享文章:SkyWalking實(shí)現(xiàn)告警功能
文章地址:http://aaarwkj.com/article36/jjghsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、網(wǎng)站設(shè)計(jì)、關(guān)鍵詞優(yōu)化、電子商務(wù)、云服務(wù)器、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)