From cdcdbe3f66eb8c52954a1c4640c24992677ec7f6 Mon Sep 17 00:00:00 2001 From: Clement Erena Date: Fri, 20 Dec 2024 14:16:04 +0100 Subject: [PATCH] feat(observability-lib): add timerange to alert rule --- observability-lib/cmd/api/api.go | 2 ++ observability-lib/cmd/api/rule/delete.go | 24 ++++++++++++++++++++++++ observability-lib/cmd/api/rule/rule.go | 14 ++++++++++++++ observability-lib/grafana/alerts.go | 7 ++++++- 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 observability-lib/cmd/api/rule/delete.go create mode 100644 observability-lib/cmd/api/rule/rule.go diff --git a/observability-lib/cmd/api/api.go b/observability-lib/cmd/api/api.go index d6c407f76..ecf08cf5f 100644 --- a/observability-lib/cmd/api/api.go +++ b/observability-lib/cmd/api/api.go @@ -4,6 +4,7 @@ import ( "github.com/smartcontractkit/chainlink-common/observability-lib/cmd/api/contact_point" "github.com/smartcontractkit/chainlink-common/observability-lib/cmd/api/dashboard" "github.com/smartcontractkit/chainlink-common/observability-lib/cmd/api/notification_policy" + "github.com/smartcontractkit/chainlink-common/observability-lib/cmd/api/rule" "github.com/spf13/cobra" ) @@ -16,6 +17,7 @@ func init() { Cmd.AddCommand(contact_point.Cmd) Cmd.AddCommand(dashboard.Cmd) Cmd.AddCommand(notification_policy.Cmd) + Cmd.AddCommand(rule.Cmd) Cmd.PersistentFlags().String("grafana-url", "", "Grafana URL") errURL := Cmd.MarkPersistentFlagRequired("grafana-url") diff --git a/observability-lib/cmd/api/rule/delete.go b/observability-lib/cmd/api/rule/delete.go new file mode 100644 index 000000000..47ca40c8d --- /dev/null +++ b/observability-lib/cmd/api/rule/delete.go @@ -0,0 +1,24 @@ +package rule + +import ( + "github.com/smartcontractkit/chainlink-common/observability-lib/api" + "github.com/spf13/cobra" +) + +var deleteCmd = &cobra.Command{ + Use: "delete [name]", + Short: "Delete rule by UID", + RunE: func(cmd *cobra.Command, args []string) error { + grafanaClient := api.NewClient( + cmd.Flag("grafana-url").Value.String(), + cmd.Flag("grafana-token").Value.String(), + ) + + _, _, errDelete := grafanaClient.DeleteAlertRule(args[0]) + if errDelete != nil { + return errDelete + } + + return nil + }, +} diff --git a/observability-lib/cmd/api/rule/rule.go b/observability-lib/cmd/api/rule/rule.go new file mode 100644 index 000000000..47f0525f5 --- /dev/null +++ b/observability-lib/cmd/api/rule/rule.go @@ -0,0 +1,14 @@ +package rule + +import ( + "github.com/spf13/cobra" +) + +var Cmd = &cobra.Command{ + Use: "rule [actions]", + Short: "Perform actions on dashboard", +} + +func init() { + Cmd.AddCommand(deleteCmd) +} diff --git a/observability-lib/grafana/alerts.go b/observability-lib/grafana/alerts.go index e812f857f..e2fb8ae5f 100644 --- a/observability-lib/grafana/alerts.go +++ b/observability-lib/grafana/alerts.go @@ -12,6 +12,7 @@ type RuleQuery struct { RefID string Datasource string LegendFormat string + TimeRange int64 Instant bool } @@ -20,9 +21,13 @@ func newRuleQuery(query RuleQuery) *alerting.QueryBuilder { query.LegendFormat = "__auto" } + if query.TimeRange == 0 { + query.TimeRange = 600 + } + res := alerting.NewQueryBuilder(query.RefID). DatasourceUid(query.Datasource). - RelativeTimeRange(600, 0) // TODO + RelativeTimeRange(alerting.Duration(query.TimeRange), alerting.Duration(0)) model := prometheus.NewDataqueryBuilder(). Expr(query.Expr).