From 966d976e7ce5744d5869c1e4429a4cdcaae956e7 Mon Sep 17 00:00:00 2001 From: lueurxax Date: Tue, 30 Apr 2024 17:32:40 +0200 Subject: [PATCH] Add PanelName to Alert structure 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. --- alert/alert.go | 4 +++- cmd/builder-example/main.go | 1 + dashboards.go | 2 +- decoder/graph.go | 6 +++++- decoder/timeseries.go | 6 +++++- graph/graph.go | 2 +- timeseries/timeseries.go | 2 +- 7 files changed, 17 insertions(+), 6 deletions(-) diff --git a/alert/alert.go b/alert/alert.go index 4f6e09f6..ef2d090d 100644 --- a/alert/alert.go +++ b/alert/alert.go @@ -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{ @@ -87,6 +88,7 @@ func New(name string, options ...Option) *Alert { Labels: map[string]string{}, }, }, + PanelName: panelName, }, } diff --git a/cmd/builder-example/main.go b/cmd/builder-example/main.go index 1bf6c02b..94d5196b 100644 --- a/cmd/builder-example/main.go +++ b/cmd/builder-example/main.go @@ -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{ diff --git a/dashboards.go b/dashboards.go index 3af5cb19..78659373 100644 --- a/dashboards.go +++ b/dashboards.go @@ -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) diff --git a/decoder/graph.go b/decoder/graph.go index 4edc1d0b..a5d2b0e4 100644 --- a/decoder/graph.go +++ b/decoder/graph.go @@ -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" @@ -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()...) diff --git a/decoder/timeseries.go b/decoder/timeseries.go index 29615d16..6dd94cb0 100644 --- a/decoder/timeseries.go +++ b/decoder/timeseries.go @@ -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" @@ -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() diff --git a/graph/graph.go b/graph/graph.go index 7f0b80f7..eafc0468 100644 --- a/graph/graph.go +++ b/graph/graph.go @@ -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) diff --git a/timeseries/timeseries.go b/timeseries/timeseries.go index b6d9a9e1..150b6b1c 100644 --- a/timeseries/timeseries.go +++ b/timeseries/timeseries.go @@ -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)