Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(observability-lib): add timerange to alert rule #979

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions observability-lib/cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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")
Expand Down
24 changes: 24 additions & 0 deletions observability-lib/cmd/api/rule/delete.go
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/nit

some type of success log/confirmation including the resource deleted may be helpful for the user

},
}
14 changes: 14 additions & 0 deletions observability-lib/cmd/api/rule/rule.go
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 6 additions & 1 deletion observability-lib/grafana/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type RuleQuery struct {
RefID string
Datasource string
LegendFormat string
TimeRange int64
Instant bool
}

Expand All @@ -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).
Expand Down
Loading