Skip to content

Commit

Permalink
Merge branch 'main' into CRE-39
Browse files Browse the repository at this point in the history
  • Loading branch information
justinkaseman authored Jan 13, 2025
2 parents f062a08 + 373e889 commit 17438c0
Show file tree
Hide file tree
Showing 16 changed files with 308 additions and 54 deletions.
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
22 changes: 14 additions & 8 deletions observability-lib/grafana/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ type Builder struct {
}

type BuilderOptions struct {
Name string
Tags []string
Refresh string
TimeFrom string
TimeTo string
TimeZone string
AlertsTags map[string]string
Name string
Tags []string
Refresh string
TimeFrom string
TimeTo string
TimeZone string
GraphTooltip dashboard.DashboardCursorSync
AlertsTags map[string]string
}

func NewBuilder(options *BuilderOptions) *Builder {
Expand All @@ -46,7 +47,9 @@ func NewBuilder(options *BuilderOptions) *Builder {
if options.TimeZone == "" {
options.TimeZone = common.TimeZoneBrowser
}
builder.dashboardBuilder.Timezone(options.TimeZone)
builder.dashboardBuilder.
Timezone(options.TimeZone).
Tooltip(options.GraphTooltip)
}

if options.AlertsTags != nil {
Expand Down Expand Up @@ -93,6 +96,9 @@ func (b *Builder) AddPanel(panel ...*Panel) {
} else if item.heatmapBuilder != nil {
item.heatmapBuilder.Id(panelID)
b.dashboardBuilder.WithPanel(item.heatmapBuilder)
} else if item.textPanelBuilder != nil {
item.textPanelBuilder.Id(panelID)
b.dashboardBuilder.WithPanel(item.textPanelBuilder)
}
if item.alertBuilders != nil && len(item.alertBuilders) > 0 {
b.AddAlert(item.alertBuilders...)
Expand Down
2 changes: 1 addition & 1 deletion observability-lib/grafana/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func TestBuilder_AddPanel(t *testing.T) {

panel := grafana.NewStatPanel(&grafana.StatPanelOptions{
PanelOptions: &grafana.PanelOptions{
Title: "Panel Title",
Title: grafana.Pointer("Panel Title"),
},
})

Expand Down
4 changes: 2 additions & 2 deletions observability-lib/grafana/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func TestGenerateJSON(t *testing.T) {
builder.AddPanel(grafana.NewTimeSeriesPanel(&grafana.TimeSeriesPanelOptions{
PanelOptions: &grafana.PanelOptions{
Datasource: "datasource-name",
Title: "ETH Balance",
Title: grafana.Pointer("ETH Balance"),
Span: 12,
Height: 6,
Decimals: 2,
Decimals: grafana.Pointer(2.0),
Query: []grafana.Query{
{
Expr: `eth_balance`,
Expand Down
Loading

0 comments on commit 17438c0

Please sign in to comment.