From 4daa12d8f8762db9ebb1353250ecff559cd65e47 Mon Sep 17 00:00:00 2001 From: Ronan Barrett Date: Fri, 11 Aug 2023 12:16:46 +0200 Subject: [PATCH 1/3] allow ProjectName to be configurable on Stackdriver alerts --- alert/queries/stackdriver/stackdriver.go | 7 +++++++ alert/queries/stackdriver/stackdriver_test.go | 12 ++++++++++++ go.mod | 2 +- go.sum | 2 ++ vendor/github.com/K-Phoen/sdk/alert.go | 1 + vendor/modules.txt | 2 +- 6 files changed, 24 insertions(+), 2 deletions(-) diff --git a/alert/queries/stackdriver/stackdriver.go b/alert/queries/stackdriver/stackdriver.go index 06cbc56a..79ac0567 100644 --- a/alert/queries/stackdriver/stackdriver.go +++ b/alert/queries/stackdriver/stackdriver.go @@ -146,6 +146,13 @@ func Legend(legend string) Option { } } +// Project defines the GCP project to use for this target. +func Project(project string) Option { + return func(stackdriver *Stackdriver) { + stackdriver.Builder.Model.MetricQuery.ProjectName = project + } +} + // Aggregation defines how the time series will be aggregated. func Aggregation(reducer Reducer) Option { return func(stackdriver *Stackdriver) { diff --git a/alert/queries/stackdriver/stackdriver_test.go b/alert/queries/stackdriver/stackdriver_test.go index 96774340..6e055ef0 100644 --- a/alert/queries/stackdriver/stackdriver_test.go +++ b/alert/queries/stackdriver/stackdriver_test.go @@ -7,6 +7,18 @@ import ( "github.com/stretchr/testify/require" ) +func TestProjectCanBeConfigured(t *testing.T) { + req := require.New(t) + + project := "some-gcp-project-id" + + query := Delta("A", "some metric", Project(project)) + + builder := query.Builder + + req.Equal(project, builder.Model.MetricQuery.ProjectName) +} + func TestDeltaQueriesCanBeCreated(t *testing.T) { req := require.New(t) diff --git a/go.mod b/go.mod index 5aa25a1b..cad9aa4b 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/K-Phoen/grabana go 1.19 require ( - github.com/K-Phoen/sdk v0.12.2 + github.com/K-Phoen/sdk v0.12.3-0.20230811095259-e77b6356006c github.com/blang/semver v3.5.1+incompatible github.com/prometheus/common v0.39.0 github.com/rhysd/go-github-selfupdate v1.2.3 diff --git a/go.sum b/go.sum index 15285953..688eaa51 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/K-Phoen/sdk v0.12.2 h1:0QofDlKE+lloyBOzhjEEMW21061zts/WIpfpQ5NLLAs= github.com/K-Phoen/sdk v0.12.2/go.mod h1:qmM0wO23CtoDux528MXPpYvS4XkRWkWX6rvX9Za8EVU= +github.com/K-Phoen/sdk v0.12.3-0.20230811095259-e77b6356006c h1:HhFwUniM6YC8r3jOZBOZkCNrmNTcGGP/1+lCFNigmbw= +github.com/K-Phoen/sdk v0.12.3-0.20230811095259-e77b6356006c/go.mod h1:qmM0wO23CtoDux528MXPpYvS4XkRWkWX6rvX9Za8EVU= github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ= github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= diff --git a/vendor/github.com/K-Phoen/sdk/alert.go b/vendor/github.com/K-Phoen/sdk/alert.go index 08002adb..f8996580 100644 --- a/vendor/github.com/K-Phoen/sdk/alert.go +++ b/vendor/github.com/K-Phoen/sdk/alert.go @@ -74,6 +74,7 @@ type AlertModel struct { } type StackdriverAlertQuery struct { + ProjectName string `json:"projectName,omitempty"` AlignOptions []StackdriverAlignOptions `json:"alignOptions,omitempty"` AliasBy string `json:"aliasBy,omitempty"` MetricType string `json:"metricType,omitempty"` diff --git a/vendor/modules.txt b/vendor/modules.txt index e297abf0..a9b8bef1 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,4 +1,4 @@ -# github.com/K-Phoen/sdk v0.12.2 +# github.com/K-Phoen/sdk v0.12.3-0.20230811095259-e77b6356006c ## explicit; go 1.19 github.com/K-Phoen/sdk # github.com/blang/semver v3.5.1+incompatible From 7a634515094556750c75f14d2c27b873fc56d7cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Mon, 14 Aug 2023 11:57:52 +0200 Subject: [PATCH 2/3] Bump sdk dependency to v0.12.3 --- go.mod | 2 +- go.sum | 6 ++---- vendor/modules.txt | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index cad9aa4b..aba9127f 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/K-Phoen/grabana go 1.19 require ( - github.com/K-Phoen/sdk v0.12.3-0.20230811095259-e77b6356006c + github.com/K-Phoen/sdk v0.12.3 github.com/blang/semver v3.5.1+incompatible github.com/prometheus/common v0.39.0 github.com/rhysd/go-github-selfupdate v1.2.3 diff --git a/go.sum b/go.sum index 688eaa51..c0171566 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ -github.com/K-Phoen/sdk v0.12.2 h1:0QofDlKE+lloyBOzhjEEMW21061zts/WIpfpQ5NLLAs= -github.com/K-Phoen/sdk v0.12.2/go.mod h1:qmM0wO23CtoDux528MXPpYvS4XkRWkWX6rvX9Za8EVU= -github.com/K-Phoen/sdk v0.12.3-0.20230811095259-e77b6356006c h1:HhFwUniM6YC8r3jOZBOZkCNrmNTcGGP/1+lCFNigmbw= -github.com/K-Phoen/sdk v0.12.3-0.20230811095259-e77b6356006c/go.mod h1:qmM0wO23CtoDux528MXPpYvS4XkRWkWX6rvX9Za8EVU= +github.com/K-Phoen/sdk v0.12.3 h1:ScutEQASc9VEKJCm3OjIMD82BIS9B2XtNg3gEf6Gs+M= +github.com/K-Phoen/sdk v0.12.3/go.mod h1:qmM0wO23CtoDux528MXPpYvS4XkRWkWX6rvX9Za8EVU= github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ= github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= diff --git a/vendor/modules.txt b/vendor/modules.txt index a9b8bef1..5edefc5f 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,4 +1,4 @@ -# github.com/K-Phoen/sdk v0.12.3-0.20230811095259-e77b6356006c +# github.com/K-Phoen/sdk v0.12.3 ## explicit; go 1.19 github.com/K-Phoen/sdk # github.com/blang/semver v3.5.1+incompatible From d07fe1f9ffcbe45e88e8e67f7b8ef3278fefa8c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Mon, 14 Aug 2023 12:03:26 +0200 Subject: [PATCH 3/3] Decode project name from yaml-defined stackdriver alerts --- decoder/alert_targets.go | 5 +++++ decoder/alert_targets_test.go | 2 ++ 2 files changed, 7 insertions(+) diff --git a/decoder/alert_targets.go b/decoder/alert_targets.go index 37ad3f7b..3dd0cf48 100644 --- a/decoder/alert_targets.go +++ b/decoder/alert_targets.go @@ -123,6 +123,7 @@ type AlertStackdriver struct { Ref string `yaml:",omitempty"` Lookback string `yaml:",omitempty"` + Project string `yaml:",omitempty"` Type string Metric string Filters StackdriverAlertFilters `yaml:",omitempty"` @@ -195,6 +196,10 @@ func (t AlertStackdriver) targetOptions() ([]stackdriver.Option, error) { opts = append(opts, stackdriver.GroupBys(t.GroupBy...)) } + if t.Project != "" { + opts = append(opts, stackdriver.Project(t.Project)) + } + if t.Aggregation != "" { opt, err := t.aggregation() if err != nil { diff --git a/decoder/alert_targets_test.go b/decoder/alert_targets_test.go index 72e778d8..0cb86f6b 100644 --- a/decoder/alert_targets_test.go +++ b/decoder/alert_targets_test.go @@ -252,6 +252,7 @@ func TestDecodingStackdriverTarget(t *testing.T) { target := AlertTarget{ Stackdriver: &AlertStackdriver{ Ref: "A", + Project: "some-project", Lookback: "15m", Type: "gauge", Metric: "cloudsql.googleapis.com/database/cpu/utilization", @@ -279,6 +280,7 @@ func TestDecodingStackdriverTarget(t *testing.T) { req.Equal("A", query.RefID) req.Equal("A", query.Model.RefID) req.Equal("GAUGE", stackdriverData.MetricKind) + req.Equal("some-project", stackdriverData.ProjectName) req.Equal("cloudsql.googleapis.com/database/cpu/utilization", stackdriverData.MetricType) req.ElementsMatch([]string{"resource.label.database_id"}, stackdriverData.GroupBys) req.Equal("REDUCE_MEAN", stackdriverData.CrossSeriesReducer)