欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

SkyWalking實(shí)現(xiàn)告警功能

這篇文章將為大家詳細(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 告警功能

SkyWalking 告警功能是在6.x版本新增的,其核心由一組規(guī)則驅(qū)動(dòng),這些規(guī)則定義在config/alarm-settings.yml文件中。 告警規(guī)則的定義分為兩部分:

  1. 告警規(guī)則:它們定義了應(yīng)該如何觸發(fā)度量警報(bào),應(yīng)該考慮什么條件。
  2. Webhook(網(wǎng)絡(luò)鉤子):定義當(dāng)警告觸發(fā)時(shí),哪些服務(wù)終端需要被告知

告警規(guī)則

SkyWalking 的發(fā)行版都會(huì)默認(rèn)提供config/alarm-settings.yml文件,里面預(yù)先定義了一些常用的告警規(guī)則。如下:

  1. 過去3分鐘內(nèi)服務(wù)平均響應(yīng)時(shí)間超過1秒
  2. 服務(wù)成功率在過去2分鐘內(nèi)低于80%
  3. 服務(wù)90%響應(yīng)時(shí)間在過去3分鐘內(nèi)低于1000毫秒
  4. 服務(wù)實(shí)例在過去2分鐘內(nèi)的平均響應(yīng)時(shí)間超過1秒
  5. 端點(diǎn)平均響應(yīng)時(shí)間過去2分鐘超過1秒

這些預(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 name:規(guī)則名稱,也是在告警信息中顯示的唯一名稱。必須以_rule結(jié)尾,前綴可自定義
  • Metrics name:度量名稱,取值為oal腳本中的度量名,目前只支持long、doubleint類型。詳見Official OAL script
  • Include names:該規(guī)則作用于哪些實(shí)體名稱,比如服務(wù)名,終端名(可選,默認(rèn)為全部)
  • Exclude names:該規(guī)則作不用于哪些實(shí)體名稱,比如服務(wù)名,終端名(可選,默認(rèn)為空)
  • Threshold:閾值
  • OP:操作符,目前支持 &gt;&lt;、=
  • Period:多久告警規(guī)則需要被核實(shí)一下。這是一個(gè)時(shí)間窗口,與后端部署環(huán)境時(shí)間相匹配
  • Count:在一個(gè)Period窗口中,如果values超過Threshold值(按op),達(dá)到Count值,需要發(fā)送警報(bào)
  • Silence period:在時(shí)間N中觸發(fā)報(bào)警后,在TN -> TN + period這個(gè)階段不告警。 默認(rèn)情況下,它和Period一樣,這意味著相同的告警(在同一個(gè)Metrics name擁有相同的Id)在同一個(gè)Period內(nèi)只會(huì)觸發(fā)一次
  • message:告警消息

Webhook(網(wǎng)絡(luò)鉤子)

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-Typeapplication/json,其JSON 數(shù)據(jù)實(shí)基于List&lt;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
}]

字段說明:

  • scopeId、scope:所有可用的 Scope 詳見 org.apache.skywalking.oap.server.core.source.DefaultScopeDefine
  • name:目標(biāo) Scope 的實(shí)體名稱
  • id0:Scope 實(shí)體的 ID
  • id1:保留字段,目前暫未使用
  • ruleName:告警規(guī)則名稱
  • alarmMessage:告警消息內(nèi)容
  • startTime:告警時(shí)間,格式為時(shí)間戳

郵件告警功能實(shí)踐

根據(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

測(cè)試告警功能

完成告警接口的開發(fā)及配置后,我們來進(jìn)行一個(gè)簡單的測(cè)試。這里有一條調(diào)用鏈路如下:
SkyWalking實(shí)現(xiàn)告警功能

我在/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)輸出了一段日志信息:
SkyWalking實(shí)現(xiàn)告警功能

此時(shí),郵箱正常收到了告警郵件:
SkyWalking實(shí)現(xiàn)告警功能

以上便是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)

成都做網(wǎng)站
亚洲高清精品一区二区| 成人黄色av免费在线观看| 亚洲老熟女老妇老女人| 黄片欧美精品在线观看| 久久草福利视频在线观看| 欧美亚洲精品一区二区三区| 中文字幕中文字幕久久不卡| 亚洲国产日韩伦中文字幕| 日韩 欧美 国产 亚洲 综合| 97超碰97资源在线| 在线观看免费国产k片| 男人的天堂av东京热一区| 国产一区二区精品久久久女同| 中文字幕乱码视频日本| 91久久亚洲综合精品日本| 91麻豆亚洲国产成人久久| 日韩中文字幕在线二区| 午夜视频在线观看黄片| 国产视频一区二区三区网| 日韩日美日韩av影视| 欧亚日韩精品一区二区在线| 色哟哟在线观看国产精品| 久久综合中文字幕一区| 91一区二区三区在线| 亚洲男人的av天堂生活| 日韩欧美亚洲一区二区| 国产一级av在线播放| 国产尤物直播在线观看| 美味人妻手机在线观看| 亚洲一区二区午夜福利亚洲| 国产三级视频网站在线观看| 人妻中字幕出轨中文字幕| 久久成人午夜免费电影| 亚州精品少妇久久久久久| av全欧国男人在线天堂| 国产又粗又硬又长又爽在线观看| 欧美特黄高清在线观看| 国产精品国产精品三级在线观看| 精品欧美日韩国产一区| 夜夜春久久天堂亚洲精品| 久久综合给合综合久久|