Skip to content

Commit

Permalink
Merge pull request #229 from ronanbarrett/add-support-for-project-on-…
Browse files Browse the repository at this point in the history
…alerts-grab

Allow ProjectName to be configurable on Stackdriver alerts
  • Loading branch information
K-Phoen authored Aug 14, 2023
2 parents e25e564 + d07fe1f commit e246fdd
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 4 deletions.
7 changes: 7 additions & 0 deletions alert/queries/stackdriver/stackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions alert/queries/stackdriver/stackdriver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions decoder/alert_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions decoder/alert_targets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
github.com/blang/semver v3.5.1+incompatible
github.com/prometheus/common v0.39.0
github.com/rhysd/go-github-selfupdate v1.2.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +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 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=
Expand Down
1 change: 1 addition & 0 deletions vendor/github.com/K-Phoen/sdk/alert.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# github.com/K-Phoen/sdk v0.12.2
# 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
Expand Down

0 comments on commit e246fdd

Please sign in to comment.