Skip to content

Commit

Permalink
feat(observability-lib): add timerange to alert rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Atrax1 committed Dec 20, 2024
1 parent bbe318c commit cdcdbe3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
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
},
}
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

0 comments on commit cdcdbe3

Please sign in to comment.