// Copyright 2013 The Prometheus Authors// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.package modelimport ()typeAlertStatusstringconst (AlertFiringAlertStatus = "firing"AlertResolvedAlertStatus = "resolved")// Alert is a generic representation of an alert in the Prometheus eco-system.typeAlertstruct {// Label value pairs for purpose of aggregation, matching, and disposition // dispatching. This must minimally include an "alertname" label. Labels LabelSet`json:"labels"`// Extra key/value information which does not define alert identity. Annotations LabelSet`json:"annotations"`// The known time range for this alert. Both ends are optional. StartsAt time.Time`json:"startsAt,omitempty"` EndsAt time.Time`json:"endsAt,omitempty"` GeneratorURL string`json:"generatorURL"`}// Name returns the name of the alert. It is equivalent to the "alertname" label.func ( *Alert) () string {returnstring(.Labels[AlertNameLabel])}// Fingerprint returns a unique hash for the alert. It is equivalent to// the fingerprint of the alert's label set.func ( *Alert) () Fingerprint {return .Labels.Fingerprint()}func ( *Alert) () string { := fmt.Sprintf("%s[%s]", .Name(), .Fingerprint().String()[:7])if .Resolved() {return + "[resolved]" }return + "[active]"}// Resolved returns true iff the activity interval ended in the past.func ( *Alert) () bool {return .ResolvedAt(time.Now())}// ResolvedAt returns true iff the activity interval ended before// the given timestamp.func ( *Alert) ( time.Time) bool {if .EndsAt.IsZero() {returnfalse }return !.EndsAt.After()}// Status returns the status of the alert.func ( *Alert) () AlertStatus {return .StatusAt(time.Now())}// StatusAt returns the status of the alert at the given timestamp.func ( *Alert) ( time.Time) AlertStatus {if .ResolvedAt() {returnAlertResolved }returnAlertFiring}// Validate checks whether the alert data is inconsistent.func ( *Alert) () error {if .StartsAt.IsZero() {returnerrors.New("start time missing") }if !.EndsAt.IsZero() && .EndsAt.Before(.StartsAt) {returnerrors.New("start time must be before end time") }if := .Labels.Validate(); != nil {returnfmt.Errorf("invalid label set: %w", ) }iflen(.Labels) == 0 {returnerrors.New("at least one label pair required") }if := .Annotations.Validate(); != nil {returnfmt.Errorf("invalid annotations: %w", ) }returnnil}// Alert is a list of alerts that can be sorted in chronological order.typeAlerts []*Alertfunc ( Alerts) () int { returnlen() }func ( Alerts) (, int) { [], [] = [], [] }func ( Alerts) (, int) bool {if [].StartsAt.Before([].StartsAt) {returntrue }if [].EndsAt.Before([].EndsAt) {returntrue }return [].Fingerprint() < [].Fingerprint()}// HasFiring returns true iff one of the alerts is not resolved.func ( Alerts) () bool {for , := range {if !.Resolved() {returntrue } }returnfalse}// HasFiringAt returns true iff one of the alerts is not resolved// at the time ts.func ( Alerts) ( time.Time) bool {for , := range {if !.ResolvedAt() {returntrue } }returnfalse}// Status returns StatusFiring iff at least one of the alerts is firing.func ( Alerts) () AlertStatus {if .HasFiring() {returnAlertFiring }returnAlertResolved}// StatusAt returns StatusFiring iff at least one of the alerts is firing// at the time ts.func ( Alerts) ( time.Time) AlertStatus {if .HasFiringAt() {returnAlertFiring }returnAlertResolved}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.