84 lines
2.2 KiB
Markdown
84 lines
2.2 KiB
Markdown
|
### function 报警接收处理函数
|
|||
|
|
|||
|
```go
|
|||
|
func (api *API) postAlertsHandler(params alert_ops.PostAlertsParams) middleware.Responder {
|
|||
|
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
|
|||
|
|
|||
|
### 报警指纹(唯一id)生成
|
|||
|
|
|||
|
```bash
|
|||
|
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)
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
|
|||
|
|
|||
|
### 接收报警
|
|||
|
|
|||
|
```go
|
|||
|
func (o *PostAlerts) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
|||
|
route, rCtx, _ := o.Context.RouteInfo(r)
|
|||
|
if rCtx != nil {
|
|||
|
*r = *rCtx
|
|||
|
}
|
|||
|
var Params = NewPostAlertsParams()
|
|||
|
if err := o.Context.BindValidRequest(r, route, &Params); err != nil { // bind params
|
|||
|
o.Context.Respond(rw, r, route.Produces, route, err)
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
res := o.Handler.Handle(Params) // actually handle the request
|
|||
|
o.Context.Respond(rw, r, route.Produces, route, res)
|
|||
|
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
### NewAPI时注册
|
|||
|
|
|||
|
```go'
|
|||
|
func NewAPI(
|
|||
|
alerts provider.Alerts,
|
|||
|
gf groupsFn,
|
|||
|
sf getAlertStatusFn,
|
|||
|
silences *silence.Silences,
|
|||
|
peer cluster.ClusterPeer,
|
|||
|
l log.Logger,
|
|||
|
r prometheus.Registerer,
|
|||
|
) (*API, error) {
|
|||
|
openAPI.AlertGetAlertsHandler = alert_ops.GetAlertsHandlerFunc(api.getAlertsHandler)
|
|||
|
openAPI.AlertPostAlertsHandler = alert_ops.PostAlertsHandlerFunc(api.postAlertsHandler)
|
|||
|
openAPI.AlertgroupGetAlertGroupsHandler = alertgroup_ops.GetAlertGroupsHandlerFunc(api.getAlertGroupsHandler)
|
|||
|
openAPI.GeneralGetStatusHandler = general_ops.GetStatusHandlerFunc(api.getStatusHandler)
|
|||
|
openAPI.ReceiverGetReceiversHandler = receiver_ops.GetReceiversHandlerFunc(api.getReceiversHandler)
|
|||
|
openAPI.SilenceDeleteSilenceHandler = silence_ops.DeleteSilenceHandlerFunc(api.deleteSilenceHandler)
|
|||
|
openAPI.SilenceGetSilenceHandler = silence_ops.GetSilenceHandlerFunc(api.getSilenceHandler)
|
|||
|
openAPI.SilenceGetSilencesHandler = silence_ops.GetSilencesHandlerFunc(api.getSilencesHandler)
|
|||
|
openAPI.SilencePostSilencesHandler = silence_ops.PostSilencesHandlerFunc(api.postSilencesHandler)
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
|
|||
|
|