Skip to content

Commit cdb4495

Browse files
tbregolinstuartnelson3
authored andcommitted
Make --expired list only expired silences (prometheus#1176) (prometheus#1190)
This means there's no longer a way to list both active and expired silences at the same time. This is the desired behaviour according to consensus at prometheus#1175
1 parent 907ac51 commit cdb4495

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

cli/silence_query.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import (
1717

1818
var (
1919
queryCmd = silenceCmd.Command("query", "Query Alertmanager silences.").Default()
20-
queryExpired = queryCmd.Flag("expired", "Show expired silences as well as active").Bool()
20+
queryExpired = queryCmd.Flag("expired", "Show expired silences instead of active").Bool()
2121
silenceQuery = queryCmd.Arg("matcher-groups", "Query filter").Strings()
22-
queryWithin = queryCmd.Flag("within", "Show silences that will expire within a duration").Duration()
22+
queryWithin = queryCmd.Flag("within", "Show silences that will expire or have expired within a duration").Duration()
2323
)
2424

2525
func init() {
@@ -106,12 +106,20 @@ func query(element *kingpin.ParseElement, ctx *kingpin.ParseContext) error {
106106

107107
displaySilences := []types.Silence{}
108108
for _, silence := range fetchedSilences {
109-
// If we are only returning current silences and this one has already expired skip it
109+
// skip expired silences if --expired is not set
110110
if !*queryExpired && silence.EndsAt.Before(time.Now()) {
111111
continue
112112
}
113-
114-
if int64(*queryWithin) > 0 && silence.EndsAt.After(time.Now().UTC().Add(*queryWithin)) {
113+
// skip active silences if --expired is set
114+
if *queryExpired && silence.EndsAt.After(time.Now()) {
115+
continue
116+
}
117+
// skip active silences expiring after "--within"
118+
if !*queryExpired && int64(*queryWithin) > 0 && silence.EndsAt.After(time.Now().UTC().Add(*queryWithin)) {
119+
continue
120+
}
121+
// skip silences that expired before "--within"
122+
if *queryExpired && int64(*queryWithin) > 0 && silence.EndsAt.Before(time.Now().UTC().Add(-*queryWithin)) {
115123
continue
116124
}
117125

0 commit comments

Comments
 (0)