From 5b7920fe32113ef3760797c0b5a3ab92f2fd4068 Mon Sep 17 00:00:00 2001 From: Santiago Date: Fri, 14 Oct 2022 18:35:17 -0300 Subject: [PATCH] Backport 56822 to v9.2.x (#57023) * use Prometheus' format for notification attempts durations (#56822) (cherry picked from commit 9996dbd577b21b373aa0adfb8eaf211b25147be1) * fix merge errors --- go.mod | 2 +- go.sum | 4 +- .../alerting/api_notification_channel_test.go | 80 ++++++++++--------- .../components/receivers/ReceiversTable.tsx | 12 ++- 4 files changed, 53 insertions(+), 45 deletions(-) diff --git a/go.mod b/go.mod index d7ac11923ac..6d8f53d5e7e 100644 --- a/go.mod +++ b/go.mod @@ -369,4 +369,4 @@ replace xorm.io/xorm => github.com/grafana/xorm v0.8.3-0.20220614223926-2fcda756 // Use our fork of the upstream alertmanagers. // This is required in order to get notification delivery errors from the receivers API. -replace github.com/prometheus/alertmanager => github.com/grafana/prometheus-alertmanager v0.24.1-0.20221006171631-7f1ce00421dc +replace github.com/prometheus/alertmanager => github.com/grafana/prometheus-alertmanager v0.24.1-0.20221012142027-823cd9150293 diff --git a/go.sum b/go.sum index e759283f1ca..21f865fde7e 100644 --- a/go.sum +++ b/go.sum @@ -1374,8 +1374,8 @@ github.com/grafana/grafana-google-sdk-go v0.0.0-20211104130251-b190293eaf58/go.m github.com/grafana/grafana-plugin-sdk-go v0.114.0/go.mod h1:D7x3ah+1d4phNXpbnOaxa/osSaZlwh9/ZUnGGzegRbk= github.com/grafana/grafana-plugin-sdk-go v0.139.0 h1:2RQKM2QpSaWTtaGN6sK+R7LO7zykOeTYF0QkAMA7JsI= github.com/grafana/grafana-plugin-sdk-go v0.139.0/go.mod h1:Y+Ps2sesZ62AyCnX+hzrYnyDQYe/ZZl+A8yKLOBm12c= -github.com/grafana/prometheus-alertmanager v0.24.1-0.20221006171631-7f1ce00421dc h1:FFso22Qq3ULTBiFRnbkqmf8UbUhnPALK5ebns1EQ278= -github.com/grafana/prometheus-alertmanager v0.24.1-0.20221006171631-7f1ce00421dc/go.mod h1:HVHqK+BVPa/tmL8EMhLCCrPt2a1GdJpEyxr5hgur2UI= +github.com/grafana/prometheus-alertmanager v0.24.1-0.20221012142027-823cd9150293 h1:dJIdfHqu+XjKz+w9zXLqXKPdp6Jjx/UPSOwdeSfWdeQ= +github.com/grafana/prometheus-alertmanager v0.24.1-0.20221012142027-823cd9150293/go.mod h1:HVHqK+BVPa/tmL8EMhLCCrPt2a1GdJpEyxr5hgur2UI= github.com/grafana/saml v0.4.9-0.20220727151557-61cd9c9353fc h1:1PY8n+rXuBNr3r1JQhoytWDCpc+pq+BibxV0SZv+Cr4= github.com/grafana/saml v0.4.9-0.20220727151557-61cd9c9353fc/go.mod h1:9Zh6dWPtB3MSzTRt8fIFH60Z351QQ+s7hCU3J/tTlA4= github.com/grafana/thema v0.0.0-20220817114012-ebeee841c104 h1:dYpwFYIChrMfpq3wDa/ZBxAbUGSW5NYmYBeSezhaoao= diff --git a/pkg/tests/api/alerting/api_notification_channel_test.go b/pkg/tests/api/alerting/api_notification_channel_test.go index 051697e26ba..a5923865eaa 100644 --- a/pkg/tests/api/alerting/api_notification_channel_test.go +++ b/pkg/tests/api/alerting/api_notification_channel_test.go @@ -828,49 +828,53 @@ func TestNotificationChannels(t *testing.T) { err := json.Unmarshal([]byte(b), &receivers) require.NoError(t, err) for _, rcv := range receivers { - var expActive bool - if _, ok := expInactiveReceivers[*rcv.Name]; !ok { - expActive = true - } - var expErr bool - if _, ok := expNotificationErrors[*rcv.Name]; ok { - expErr = true - } + t.Run("Receiver "+*rcv.Name, func(t *testing.T) { + var expActive bool + if _, ok := expInactiveReceivers[*rcv.Name]; !ok { + expActive = true + } + var expErr bool + if _, ok := expNotificationErrors[*rcv.Name]; ok { + expErr = true + } - require.NotNil(t, rcv.Name) - require.NotNil(t, rcv.Active) - require.NotEmpty(t, rcv.Integrations) - if expActive { - require.True(t, *rcv.Active) - } - - // We don't have test alerts for the default notifier, continue iterating. - if *rcv.Name == "grafana-default-email" { - continue - } - - for _, integration := range rcv.Integrations { - require.NotNil(t, integration.Name) - require.NotNil(t, integration.SendResolved) - - // If the receiver is not active, no attempts to send notifications should be registered. + require.NotNil(t, rcv.Name) + require.NotNil(t, rcv.Active) + require.NotEmpty(t, rcv.Integrations) if expActive { - require.NotZero(t, integration.LastNotifyAttempt) - require.NotEqual(t, "0s", integration.LastNotifyAttemptDuration) - } else { - require.Zero(t, integration.LastNotifyAttempt) - require.Equal(t, "0s", integration.LastNotifyAttemptDuration) + require.True(t, *rcv.Active) } - // Check whether we're expecting an error on this integration. - if expErr { - for _, integration := range rcv.Integrations { - require.Equal(t, expNotificationErrors[*rcv.Name], integration.LastNotifyAttemptError) - } - } else { - require.Equal(t, "", integration.LastNotifyAttemptError) + // We don't have test alerts for the default notifier, continue iterating. + if *rcv.Name == "grafana-default-email" { + return } - } + + for _, integration := range rcv.Integrations { + require.NotNil(t, integration.Name) + t.Run("Integration "+*integration.Name, func(t *testing.T) { + require.NotNil(t, integration.SendResolved) + + // If the receiver is not active, no attempts to send notifications should be registered. + if expActive { + // Prometheus' durations get rounded down, so we might end up with "0s" if we have values smaller than 1ms. + require.NotZero(t, integration.LastNotifyAttempt) + } else { + require.Zero(t, integration.LastNotifyAttempt) + require.Equal(t, "0s", integration.LastNotifyAttemptDuration) + } + + // Check whether we're expecting an error on this integration. + if expErr { + for _, integration := range rcv.Integrations { + require.Equal(t, expNotificationErrors[*rcv.Name], integration.LastNotifyAttemptError) + } + } else { + require.Equal(t, "", integration.LastNotifyAttemptError) + } + }) + } + }) } { diff --git a/public/app/features/alerting/unified/components/receivers/ReceiversTable.tsx b/public/app/features/alerting/unified/components/receivers/ReceiversTable.tsx index 056e3996e50..7855a715e4e 100644 --- a/public/app/features/alerting/unified/components/receivers/ReceiversTable.tsx +++ b/public/app/features/alerting/unified/components/receivers/ReceiversTable.tsx @@ -133,11 +133,10 @@ type NotifierItemTableProps = DynamicTableItemProps; interface NotifiersTableProps { notifiersState: NotifiersState; } +const isLastNotifyNullDate = (lastNotify: string) => lastNotify === '0001-01-01T00:00:00.000Z'; function LastNotify({ lastNotifyDate }: { lastNotifyDate: string }) { - const isLastNotifyNullDate = lastNotifyDate === '0001-01-01T00:00:00.000Z'; - - if (isLastNotifyNullDate) { + if (isLastNotifyNullDate(lastNotifyDate)) { return <>{'-'}; } else { return ( @@ -150,6 +149,9 @@ function LastNotify({ lastNotifyDate }: { lastNotifyDate: string }) { } } +const possibleNullDurations = ['', '0', '0ms', '0s', '0m', '0h', '0d', '0w', '0y']; +const durationIsNull = (duration: string) => possibleNullDurations.includes(duration); + function NotifiersTable({ notifiersState }: NotifiersTableProps) { function getNotifierColumns(): NotifierTableColumnProps[] { return [ @@ -176,7 +178,9 @@ function NotifiersTable({ notifiersState }: NotifiersTableProps) { { id: 'lastNotifyDuration', label: 'Last duration', - renderCell: ({ data: { lastNotifyDuration } }) => <>{lastNotifyDuration}, + renderCell: ({ data: { lastNotify, lastNotifyDuration } }) => ( + <>{isLastNotifyNullDate(lastNotify) && durationIsNull(lastNotifyDuration) ? '-' : lastNotifyDuration} + ), size: 1, }, {