Skip to content

Commit

Permalink
Add PanelName to Alert structure
Browse files Browse the repository at this point in the history
Added PanelName as a field to the Alert structure. Updated related code to make use of this new field in alert creation and handling. The new field provides more specific information about the panel associated with the alert.
  • Loading branch information
lueurxax committed Apr 30, 2024
1 parent 44c4aad commit 966d976
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 6 deletions.
4 changes: 3 additions & 1 deletion alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ type Alert struct {
Datasource string
DashboardUID string
PanelID string
PanelName string
}

// New creates a new alert.
func New(name string, options ...Option) *Alert {
func New(name, panelName string, options ...Option) *Alert {
nope := false

alert := &Alert{
Expand Down Expand Up @@ -87,6 +88,7 @@ func New(name string, options ...Option) *Alert {
Labels: map[string]string{},
},
},
PanelName: panelName,

Check failure on line 91 in alert/alert.go

View workflow job for this annotation

GitHub Actions / Tests (1.20)

unknown field PanelName in struct literal of type sdk.Alert

Check failure on line 91 in alert/alert.go

View workflow job for this annotation

GitHub Actions / Tests (1.20)

unknown field PanelName in struct literal of type sdk.Alert) (typecheck)

Check failure on line 91 in alert/alert.go

View workflow job for this annotation

GitHub Actions / Tests (1.20)

unknown field PanelName in struct literal of type sdk.Alert) (typecheck)

Check failure on line 91 in alert/alert.go

View workflow job for this annotation

GitHub Actions / Tests (1.20)

unknown field PanelName in struct literal of type sdk.Alert

Check failure on line 91 in alert/alert.go

View workflow job for this annotation

GitHub Actions / Tests (1.21)

unknown field PanelName in struct literal of type sdk.Alert) (typecheck)

Check failure on line 91 in alert/alert.go

View workflow job for this annotation

GitHub Actions / Tests (1.21)

unknown field PanelName in struct literal of type sdk.Alert) (typecheck)

Check failure on line 91 in alert/alert.go

View workflow job for this annotation

GitHub Actions / Tests (1.21)

unknown field PanelName in struct literal of type sdk.Alert

Check failure on line 91 in alert/alert.go

View workflow job for this annotation

GitHub Actions / Tests (1.21)

unknown field PanelName in struct literal of type sdk.Alert
},
}

Expand Down
1 change: 1 addition & 0 deletions cmd/builder-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func main() {
timeseries.Legend(timeseries.Last, timeseries.AsTable),
timeseries.Alert(
"Too many heap allocations",
alert.Summary("Too many heap allocations"),
alert.Description("Yup, too much of {{ app }}"),
alert.Runbook("https://google.com"),
alert.Tags(map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (client *Client) UpsertDashboard(ctx context.Context, folder *Folder, build
alert := *alerts[i]

alert.HookDashboardUID(dashboardFromGrafana.UID)
alert.HookPanelID(panelIDByTitle(dashboardFromGrafana, alert.Builder.Name))
alert.HookPanelID(panelIDByTitle(dashboardFromGrafana, alert.Builder.PanelName))

if err := client.AddAlert(ctx, folder.Title, alert, datasourcesMap); err != nil {
return nil, fmt.Errorf("could not add new alerts for dashboard: %w", err)
Expand Down
6 changes: 5 additions & 1 deletion decoder/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package decoder
import (
"fmt"

"github.com/K-Phoen/grabana/alert"
"github.com/K-Phoen/grabana/axis"
"github.com/K-Phoen/grabana/graph"
"github.com/K-Phoen/grabana/graph/series"
Expand Down Expand Up @@ -82,7 +83,10 @@ func (graphPanel DashboardGraph) toOption() (row.Option, error) {
return nil, err
}

opts = append(opts, graph.Alert(graphPanel.Alert.Summary, alertOpts...))
opts = append(opts, graph.Alert(
graphPanel.Alert.Summary,
append(alertOpts, alert.Summary(graphPanel.Alert.Summary))...),
)
}
if graphPanel.Visualization != nil {
opts = append(opts, graphPanel.Visualization.toOptions()...)
Expand Down
6 changes: 5 additions & 1 deletion decoder/timeseries.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package decoder
import (
"fmt"

"github.com/K-Phoen/grabana/alert"
"github.com/K-Phoen/grabana/row"
"github.com/K-Phoen/grabana/timeseries"
"github.com/K-Phoen/grabana/timeseries/axis"
Expand Down Expand Up @@ -80,7 +81,10 @@ func (timeseriesPanel DashboardTimeSeries) toOption() (row.Option, error) {
return nil, err
}

opts = append(opts, timeseries.Alert(timeseriesPanel.Alert.Summary, alertOpts...))
opts = append(opts, timeseries.Alert(
timeseriesPanel.Alert.Summary,
append(alertOpts, alert.Summary(timeseriesPanel.Alert.Summary))...,
))
}
if timeseriesPanel.Visualization != nil {
vizOpts, err := timeseriesPanel.Visualization.toOptions()
Expand Down
2 changes: 1 addition & 1 deletion graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func XAxis(opts ...axis.Option) Option {
// Alert creates an alert for this graph.
func Alert(name string, opts ...alert.Option) Option {
return func(graph *Graph) error {
al := alert.New(graph.Builder.Title, append(opts, alert.Summary(name))...)
al := alert.New(name, graph.Builder.Title, opts...)
al.Builder.Name = graph.Builder.Title
graph.Alerts = append(graph.Alerts, al)

Expand Down
2 changes: 1 addition & 1 deletion timeseries/timeseries.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func Transparent() Option {
// Alert creates an alert for this graph.
func Alert(name string, opts ...alert.Option) Option {
return func(timeseries *TimeSeries) error {
al := alert.New(timeseries.Builder.Title, append(opts, alert.Summary(name))...)
al := alert.New(name, timeseries.Builder.Title, opts...)
al.Builder.Name = timeseries.Builder.Title
timeseries.Alerts = append(timeseries.Alerts, al)

Expand Down

0 comments on commit 966d976

Please sign in to comment.