github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/util/alarm.go (about) 1 // Copyright 2021 iLogtail Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package util 16 17 import ( 18 "strconv" 19 "sync" 20 "time" 21 22 "github.com/alibaba/ilogtail/pkg/protocol" 23 ) 24 25 var GlobalAlarm *Alarm 26 var mu sync.Mutex 27 28 var RegisterAlarms map[string]*Alarm 29 var regMu sync.Mutex 30 31 func RegisterAlarm(key string, alarm *Alarm) { 32 regMu.Lock() 33 defer regMu.Unlock() 34 RegisterAlarms[key] = alarm 35 } 36 37 func DeleteAlarm(key string) { 38 regMu.Lock() 39 defer regMu.Unlock() 40 delete(RegisterAlarms, key) 41 } 42 43 func RegisterAlarmsSerializeToPb(logGroup *protocol.LogGroup) { 44 regMu.Lock() 45 defer regMu.Unlock() 46 for _, alarm := range RegisterAlarms { 47 alarm.SerializeToPb(logGroup) 48 } 49 } 50 51 type AlarmItem struct { 52 Message string 53 Count int 54 } 55 56 type Alarm struct { 57 AlarmMap map[string]*AlarmItem 58 Project string 59 Logstore string 60 } 61 62 func (p *Alarm) Init(project, logstore string) { 63 mu.Lock() 64 p.AlarmMap = make(map[string]*AlarmItem) 65 p.Project = project 66 p.Logstore = logstore 67 mu.Unlock() 68 } 69 70 func (p *Alarm) Update(project, logstore string) { 71 mu.Lock() 72 defer mu.Unlock() 73 p.Project = project 74 p.Logstore = logstore 75 } 76 77 func (p *Alarm) Record(alarmType, message string) { 78 // donot record empty alarmType 79 if len(alarmType) == 0 { 80 return 81 } 82 mu.Lock() 83 alarmItem, existFlag := p.AlarmMap[alarmType] 84 if !existFlag { 85 alarmItem = &AlarmItem{} 86 p.AlarmMap[alarmType] = alarmItem 87 } 88 alarmItem.Message = message 89 alarmItem.Count++ 90 mu.Unlock() 91 } 92 93 func (p *Alarm) SerializeToPb(logGroup *protocol.LogGroup) { 94 nowTime := time.Now() 95 mu.Lock() 96 for alarmType, item := range p.AlarmMap { 97 if item.Count == 0 { 98 continue 99 } 100 log := &protocol.Log{} 101 log.Contents = append(log.Contents, &protocol.Log_Content{Key: "project_name", Value: p.Project}) 102 log.Contents = append(log.Contents, &protocol.Log_Content{Key: "category", Value: p.Logstore}) 103 log.Contents = append(log.Contents, &protocol.Log_Content{Key: "alarm_type", Value: alarmType}) 104 log.Contents = append(log.Contents, &protocol.Log_Content{Key: "alarm_count", Value: strconv.Itoa(item.Count)}) 105 log.Contents = append(log.Contents, &protocol.Log_Content{Key: "alarm_message", Value: item.Message}) 106 log.Contents = append(log.Contents, &protocol.Log_Content{Key: "ip", Value: GetIPAddress()}) 107 protocol.SetLogTime(log, uint32(nowTime.Unix())) 108 logGroup.Logs = append(logGroup.Logs, log) 109 // clear after serialize 110 item.Count = 0 111 item.Message = "" 112 } 113 mu.Unlock() 114 } 115 116 func init() { 117 GlobalAlarm = new(Alarm) 118 GlobalAlarm.Init("", "") 119 RegisterAlarms = make(map[string]*Alarm) 120 }