總的流程圖:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL0MzYhdjN0UGO3U2MwMzY1ETOlNzM5ETMhBTYzkTNkdTM0MDO0YmN58CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.png)
alerts:
alert概念
這裡先簡單介紹下AlertManager中對告警的概念和狀态描述。告警對應一個告警事件,包括告警名稱,告警時間,告警狀态以及其他告警詳細說明(自定義),其描述結構如下:
[
{
"startsAt": "2019-06-03T03:32:20.859Z",
"endsAt": "2019-06-03T03:32:20.859Z",
"annotations":
{
"additionalProp1": "string",
"additionalProp2": "string",
"additionalProp3": "string"
},
"labels":
{
"additionalProp1": "string",
"additionalProp2": "string",
"additionalProp3": "string"
},
"generatorURL": "string"
}
]
告警唯一辨別:
唯一辨別為labels所有項組合起來計算得到的一個指紋資訊,是以可以認為labels組就是一個唯一辨別。他來進行告警合并和更新告警狀态。
參考代碼如下:
// Fingerprint returns a unique hash for the alert. It is equivalent to
// the fingerprint of the alert's label set.
func (a *Alert) Fingerprint() Fingerprint {
return a.Labels.Fingerprint()
}
// labelSetToFingerprint works exactly as LabelsToSignature but takes a LabelSet as
// parameter (rather than a label map) and returns a Fingerprint.
func labelSetToFingerprint(ls LabelSet) Fingerprint {
if len(ls) == 0 {
return Fingerprint(emptyLabelSignature)
}
labelNames := make(LabelNames, 0, len(ls))
for labelName := range ls {
labelNames = append(labelNames, labelName)
}
sort.Sort(labelNames)
sum := hashNew()
for _, labelName := range labelNames {
sum = hashAdd(sum, string(labelName))
sum = hashAddByte(sum, SeparatorByte)
sum = hashAdd(sum, string(ls[labelName]))
sum = hashAddByte(sum, SeparatorByte)
}
return Fingerprint(sum)
}
告警狀态:
firing和resolved,這個值是manager根據收到了alert結構來自行判斷并指派的,manager是怎麼判斷firing呢?
firing: endsAt為空,或者目前時間小于等于endsAt時為告警。
resolved:endsAt不為空且大于目前時間
// Resolved returns true iff the activity interval ended in the past.
func (a *Alert) Resolved() bool {
return a.ResolvedAt(time.Now())
}
// ResolvedAt returns true off the activity interval ended before
// the given timestamp.
func (a *Alert) ResolvedAt(ts time.Time) bool {
if a.EndsAt.IsZero() {
return false
}
return !a.EndsAt.After(ts)
}
router
路由可以是一個樹形結構,通過這個結構可以靈活的配置一個告警的流轉路徑。
根節點:router,根節點。父節點所有屬性可以被子節點繼承,是以根節點的屬性相當于全局預設屬性。
子節點:routers,子節點可以是0個或多個,子節點可以單獨配置屬性以覆寫父節點的屬性。
每個router可以包含以下屬性
流程控制:
- match,match_re 是比對規則,這個是每個router選擇的依據。前者是等于比對,後者是正則比對。
- continue,告警是否繼續向下路由,如果是否則終止于此節點不再向下路由。
- group規則:告警聚合規則
receiver:指定接收端,可以了解為處理方式。
group
分組操作主要是應用于将同一類告警歸集為一個告警通知中,
一個特别典型的應用場景:當某核心服務或元件故障,可能引發成百上千的同類型告警,此時這個分組聚合,就會使告警通知有效減少,使告警通知保持清晰,有效。
分組有三個參數:
group by:指定分組依據哪個label,可以是多個以逗号隔開。
group_wait:分組聚合時間視窗,當第一個新分組開始到發送告警的等待時間,系統會将這段時間的同組告警合并為一條。
group_interval :同一分組的告警發送間隔,如果分組1已經成功發送了,後來的告警也還屬于分組1,則等待這個間隔時間後再發送。
storage
不支援曆史存儲,隻存儲告警快照
(1)告警狀态快照,未恢複的告警。存儲周期可以配置,預設120小時
結構:map,key GroupKey:r.GroupName,/r.Integration,/r.Idx value:MeshEntry
具體結構如下:
type MeshEntry struct {
// The original raw notify log entry.
Entry *Entry `protobuf:"bytes,1,opt,name=entry,proto3" json:"entry,omitempty"`
// A timestamp indicating when the mesh peer should evict
// the log entry from its state.
ExpiresAt time.Time `protobuf:"bytes,2,opt,name=expires_at,json=expiresAt,proto3,stdtime" json:"expires_at"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
type Entry struct {
// The key identifying the dispatching group.
GroupKey []byte `protobuf:"bytes,1,opt,name=group_key,json=groupKey,proto3" json:"group_key,omitempty"`
// The receiver that was notified.
Receiver *Receiver `protobuf:"bytes,2,opt,name=receiver,proto3" json:"receiver,omitempty"`
// Hash over the state of the group at notification time.
// Deprecated in favor of FiringAlerts field, but kept for compatibility.
GroupHash []byte `protobuf:"bytes,3,opt,name=group_hash,json=groupHash,proto3" json:"group_hash,omitempty"`
// Whether the notification was about a resolved alert.
// Deprecated in favor of ResolvedAlerts field, but kept for compatibility.
Resolved bool `protobuf:"varint,4,opt,name=resolved,proto3" json:"resolved,omitempty"`
// Timestamp of the succeeding notification.
Timestamp time.Time `protobuf:"bytes,5,opt,name=timestamp,proto3,stdtime" json:"timestamp"`
// FiringAlerts list of hashes of firing alerts at the last notification time.
FiringAlerts []uint64 `protobuf:"varint,6,rep,packed,name=firing_alerts,json=firingAlerts,proto3" json:"firing_alerts,omitempty"`
// ResolvedAlerts list of hashes of resolved alerts at the last notification time.
ResolvedAlerts []uint64 `protobuf:"varint,7,rep,packed,name=resolved_alerts,json=resolvedAlerts,proto3" json:"resolved_alerts,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
(2)告警靜音狀态。
存儲周期由靜音規則配置。