[TOC]
リファレンスの使用に基づいてSkyWalking:
公式文書:
SkyWalkingアラーム機能
アラーム機能をSkyWalkingするバージョン6.xで新しく追加され、コアはで定義されている一連のルールによって駆動されconfig/alarm-settings.yml
たファイル。定義されたアラームルールは2つの部分に分かれています。
- アラームルール:彼らは対策がアラームをトリガーする方法を定義し、どのような条件が考慮されるべきです。
- ウェブフック(ネットワークフック):警告がトリガされたときの定義は、端末のニーズがどのサービスを告げします
アラートルール
リリースをSkyWalkingすると、デフォルトの提供するconfig/alarm-settings.yml
いくつかの一般的に使用されるプリアラームルールを定義したファイルを、。次のとおりです。
- 1秒以上3分の平均応答時間内に過去のサービス
- 最後の2分で80%未満のサービスの成功率
- サービスの90%の応答時間は、最後の3分で1000未満ミリ秒であります
- 1秒以上の最後の2分でのサービスの平均応答時間の例
- 1秒以上、過去2分で平均応答時間をエンドポイント
これらの定義済みアラームルールは、オープンconfig/alarm-settings.yml
参照するファイルを。次のように具体的な内容は以下のとおりです。
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
ファイルを、ファイルには、すべてのアラームが現在サポートされている設定項目をルールが示すサンプルファイルアラームルール、次のとおりです。
# 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
アラームルールパラメータの説明:
- ルール名:ルール名は、一意の名前は、アラーム情報に表示されます。必要があります
_rule
終了し、接頭辞をカスタマイズすることができます - メトリック名:メトリック名、メトリック値OALスクリプトの名前で、現在のみサポート
long
、double
およびint
タイプ。参照してください公式OALスクリプトを - 名前を含める:エンティティの名前は、サービス名などルール、端末名(すべてのオプション、デフォルト)を適用するにはどのようなものです
- 名前除外:エンティティ名が使用されていないルールは、そのようなサービス名、端末名などを(オプション、デフォルトは空です)
- しきい値:しきい値
- OP:オペレータは、現在サポートしています
>
、<
、=
- 期間:ルールに関する警報を検証する必要がどのくらい。これは、時間ウィンドウで、バックエンド・デプロイメント環境は、時間と一致しました
- カウント:期間]ウィンドウで、値が(OPで)しきい値を超えた場合は、カウント値を達成するために、アラートが送られる必要があります
- 沈黙期間:アラームが時間N、TNでトリガーされた後- >この段階でTN +期間はアラームではありません。デフォルトでは、期間、同期間中にこの手段同じアラーム(同じメトリック名IDが同じでは)一度だけトリガされます
- メッセージ:アラームメッセージ
ウェブフック(ネットワークフック)
ウェブフックは単に通常コード同様のイベントコールバックが、Webレベルのイベントによってトリガされるウェブ・レベルのコールバック機構として理解することができます。それはウェブ・レベルであるため、これイベントが発生したときに、コールバックメソッドがもはやコードまたは機能が、サービス・インターフェースであること。たとえば、このシナリオアラームでは、アラームはイベントです。イベントが発生すると、SkyWalkingはウェブフックと呼ばれる構成されたインタフェースを呼び出すためのイニシアチブをとるだろう。
HTTPリクエスト、リクエストメソッドを送信することによって、アラームメッセージをSkyWalking POST
、Content-Type
あるapplication/json
JSONデータに基づいて、固体をList<org.apache.skywalking.oap.server.core.alarm.AlarmMessage
シリアル化します。例JSONデータ:
[{
"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、スコープ:すべての利用可能な範囲を参照してください
org.apache.skywalking.oap.server.core.source.DefaultScopeDefine
- 名前:エンティティ名の目標範囲
- ID0: IDスコープエンティティ
- ID1:予約フィールド、まだ現在使用
- ともなうruleName:アラームルール名
- alarmMessage:アラームメッセージの内容
- startTime:形式のタイムスタンプでアラーム時刻
Eメールアラーム機能の練習
二つ以上のセクションの紹介によると、あなたは学ぶことができる:SkyWalkingは、メール、テキストメッセージや他のサービスに直接アラーム情報の送信をサポートしていない、SkyWalkingだけでアラームが発生したときにウェブフックを設定したインターフェイスにアラーム情報を送信します。
しかし、我々は手動でサービスアラームが発生するかどうかを知るためのインタフェース情報を凝視してログインすることはできません、私たちは、このようなパーソナライズされたアラーム通知を実現するために、インターフェイス上で電子メールやテキストメッセージの送信などの機能を実装する必要があります。
そして、春のブートに基づいて、ここで、ハンズオンを開始します。最初は、依存関係を追加することです:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
構成するメールサービス:
server:
port: 9134
#邮箱配置
spring:
mail:
host: smtp.163.com
#发送者邮箱账号
username: 你的邮箱@163.com
#发送者密钥
password: 你的邮箱服务密钥
default-encoding: utf-8
port: 465 #端口号465或587
protocol: smtp
properties:
mail:
debug:
false
smtp:
socketFactory:
class: javax.net.ssl.SSLSocketFactory
送信SkyWalking DTO、データを受信するためのインタフェースを定義するJSONデータ:
@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;
}
次にインターフェイス、受信したアラーム通知SkyWalkingを定義し、メールボックスにデータを送信します。
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服务的告警通知并发送至邮箱
*/
@PostMapping("/receive")
public void receive(@RequestBody List<SwAlarmDTO> alarmList) {
SimpleMailMessage message = new SimpleMailMessage();
// 发送者邮箱
message.setFrom(from);
// 接收者邮箱
message.setTo(from);
// 主题
message.setSubject("告警邮件");
String content = getContent(alarmList);
// 邮件内容
message.setText(content);
sender.send(message);
log.info("告警邮件已发送...");
}
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目标 Scope 的实体名称: ").append(dto.getName())
.append("\nScope 实体的 ID: ").append(dto.getId0())
.append("\nid1: ").append(dto.getId1())
.append("\n告警规则名称: ").append(dto.getRuleName())
.append("\n告警消息内容: ").append(dto.getAlarmMessage())
.append("\n告警时间: ").append(dto.getStartTime())
.append("\n\n---------------\n\n");
}
return sb.toString();
}
}
最後に、SkyWalkingに対するインタフェースの構成は、位置設定ウェブフック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
テストアラーム機能
アラームインターフェイスの開発と設定が完了した後、我々は簡単なテストを実行する必要があります。下のコールのリンクがあります:
私は/producer
意図的インターフェイスに負わせ、コードインターフェイスの異常なラインにリードを広げ利用できません。
@GetMapping
public String producer() {
log.info("received a request");
int i = 1 / 0;
return "this message from producer";
}
そして、最後の2分で、このデフォルトのアラートルールの80%未満の成功率を満たすために、そのサービスをテストコードを記述します。
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);
}
}
テストコードを実行した後、いくつかのログ情報のアラームコンソールインタフェース出力後2分程度待って:
この場合、通常のメールには、警告メッセージを受信しました: