-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add alerts as event intervals for viewing
- Loading branch information
Showing
3 changed files
with
198 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,157 @@ | ||
package monitor | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math" | ||
"strings" | ||
"time" | ||
|
||
routeclient "github.com/openshift/client-go/route/clientset/versioned" | ||
"github.com/openshift/library-go/test/library/metrics" | ||
"github.com/openshift/origin/pkg/monitor/monitorapi" | ||
prometheusv1 "github.com/prometheus/client_golang/api/prometheus/v1" | ||
prometheustypes "github.com/prometheus/common/model" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
func CreateEventIntervalsForAlerts(ctx context.Context, restConfig *rest.Config, startTime time.Time) ([]monitorapi.EventInterval, error) { | ||
kubeClient, err := kubernetes.NewForConfig(restConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
routeClient, err := routeclient.NewForConfig(restConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
prometheusClient, err := metrics.NewPrometheusClient(ctx, kubeClient, routeClient) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
timeRange := prometheusv1.Range{ | ||
Start: startTime, | ||
End: time.Now(), | ||
Step: 1 * time.Second, | ||
} | ||
alerts, warningsForQuery, err := prometheusClient.QueryRange(ctx, `ALERTS{alertstate="firing"}`, timeRange) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(warningsForQuery) > 0 { | ||
fmt.Printf("#### warnings \n\t%v\n", strings.Join(warningsForQuery, "\n\t")) | ||
} | ||
|
||
firingAlerts, err := createEventIntervalsForAlerts(ctx, alerts, startTime) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
alerts, warningsForQuery, err = prometheusClient.QueryRange(ctx, `ALERTS{alertstate="pending"}`, timeRange) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(warningsForQuery) > 0 { | ||
fmt.Printf("#### warnings \n\t%v\n", strings.Join(warningsForQuery, "\n\t")) | ||
} | ||
pendingAlerts, err := createEventIntervalsForAlerts(ctx, alerts, startTime) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ret := []monitorapi.EventInterval{} | ||
ret = append(ret, firingAlerts...) | ||
ret = append(ret, pendingAlerts...) | ||
|
||
return ret, nil | ||
} | ||
|
||
func createEventIntervalsForAlerts(ctx context.Context, alerts prometheustypes.Value, startTime time.Time) ([]monitorapi.EventInterval, error) { | ||
fmt.Printf("\n\n\n#### alertType=%v\n", alerts.Type()) | ||
fmt.Printf("\n\n\n#### alerts=%v\n\n\n", alerts.String()) | ||
|
||
ret := []monitorapi.EventInterval{} | ||
|
||
switch { | ||
case alerts.Type() == prometheustypes.ValMatrix: | ||
matrixAlert := alerts.(prometheustypes.Matrix) | ||
for _, alert := range matrixAlert { | ||
alertName := alert.Metric[prometheustypes.AlertNameLabel] | ||
if alertName == "Watchdog" { | ||
continue | ||
} | ||
|
||
locator := "alert/" + alertName | ||
if node := alert.Metric["instance"]; len(node) > 0 { | ||
locator += " node/" + node | ||
} | ||
if namespace := alert.Metric["namespace"]; len(namespace) > 0 { | ||
locator += " ns/" + namespace | ||
} | ||
|
||
alertIntervalTemplate := monitorapi.EventInterval{ | ||
Condition: monitorapi.Condition{ | ||
Locator: string(locator), | ||
Message: alert.Metric.String(), | ||
}, | ||
} | ||
switch alert.Metric["severity"] { | ||
case "warning": | ||
alertIntervalTemplate.Level = monitorapi.Warning | ||
case "critical": | ||
alertIntervalTemplate.Level = monitorapi.Error | ||
case "info", "pending": | ||
alertIntervalTemplate.Level = monitorapi.Info | ||
default: | ||
alertIntervalTemplate.Level = monitorapi.Error | ||
} | ||
|
||
var alertStartTime *time.Time | ||
var lastTime *time.Time | ||
for _, currValue := range alert.Values { | ||
currTime := currValue.Timestamp.Time() | ||
if alertStartTime == nil { | ||
alertStartTime = &currTime | ||
} | ||
if lastTime == nil { | ||
lastTime = &currTime | ||
} | ||
// if it has been less than five seconds since we saw this, consider it the same interval and check | ||
// the next time. | ||
if math.Abs(currTime.Sub(*lastTime).Seconds()) < (5 * time.Second).Seconds() { | ||
lastTime = &currTime | ||
continue | ||
} | ||
|
||
// if it has been more than five seconds, consider this the start of a new occurrence and add the interval | ||
currAlertInterval := alertIntervalTemplate // shallow copy | ||
currAlertInterval.From = *alertStartTime | ||
currAlertInterval.To = *lastTime | ||
ret = append(ret, currAlertInterval) | ||
|
||
// now reset the tracking | ||
alertStartTime = &currTime | ||
lastTime = nil | ||
} | ||
|
||
currAlertInterval := alertIntervalTemplate // shallow copy | ||
currAlertInterval.From = *alertStartTime | ||
currAlertInterval.To = *lastTime | ||
ret = append(ret, currAlertInterval) | ||
} | ||
|
||
default: | ||
ret = append(ret, monitorapi.EventInterval{ | ||
Condition: monitorapi.Condition{ | ||
Level: monitorapi.Error, | ||
Locator: "alert/all", | ||
Message: fmt.Sprintf("unhandled type: %v", alerts.Type()), | ||
}, | ||
From: startTime, | ||
To: time.Now(), | ||
}) | ||
} | ||
|
||
return ret, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters