Skip to content

Commit

Permalink
Merge pull request #159 from K-Phoen/stackdriver-preprocessor
Browse files Browse the repository at this point in the history
Allow stackdriver targets to configure a preprocessor
  • Loading branch information
K-Phoen authored Jan 12, 2022
2 parents c29dee6 + 60c7d79 commit fe478f4
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 14 deletions.
42 changes: 32 additions & 10 deletions decoder/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
var ErrTargetNotConfigured = fmt.Errorf("target not configured")
var ErrInvalidStackdriverType = fmt.Errorf("invalid stackdriver target type")
var ErrInvalidStackdriverAggregation = fmt.Errorf("invalid stackdriver aggregation type")
var ErrInvalidStackdriverPreprocessor = fmt.Errorf("invalid stackdriver preprocessor")
var ErrInvalidStackdriverAlignment = fmt.Errorf("invalid stackdriver alignment method")

type Target struct {
Expand Down Expand Up @@ -118,16 +119,17 @@ func (t InfluxDBTarget) toOptions() []influxdb.Option {
}

type StackdriverTarget struct {
Project string
Type string
Metric string
Filters StackdriverFilters `yaml:",omitempty"`
Aggregation string `yaml:",omitempty"`
Alignment *StackdriverAlignment `yaml:",omitempty"`
Legend string `yaml:",omitempty"`
Ref string `yaml:",omitempty"`
Hidden bool `yaml:",omitempty"`
GroupBy []string `yaml:"group_by,omitempty"`
Project string
Type string
Metric string
Filters StackdriverFilters `yaml:",omitempty"`
Aggregation string `yaml:",omitempty"`
Alignment *StackdriverAlignment `yaml:",omitempty"`
Legend string `yaml:",omitempty"`
Preprocessor string `yaml:",omitempty"`
Ref string `yaml:",omitempty"`
Hidden bool `yaml:",omitempty"`
GroupBy []string `yaml:"group_by,omitempty"`
}

type StackdriverFilters struct {
Expand Down Expand Up @@ -192,6 +194,15 @@ func (t StackdriverTarget) toOptions() ([]stackdriver.Option, error) {
opts = append(opts, opt)
}

if t.Preprocessor != "" {
opt, err := t.preprocessor()
if err != nil {
return nil, err
}

opts = append(opts, opt)
}

if t.Alignment != nil {
opt, err := t.Alignment.toOption()
if err != nil {
Expand Down Expand Up @@ -239,6 +250,17 @@ func (t StackdriverTarget) aggregation() (stackdriver.Option, error) {
}
}

func (t StackdriverTarget) preprocessor() (stackdriver.Option, error) {
switch t.Preprocessor {
case "delta":
return stackdriver.Preprocessor(stackdriver.PreprocessDelta), nil
case "rate":
return stackdriver.Preprocessor(stackdriver.PreprocessRate), nil
default:
return nil, ErrInvalidStackdriverPreprocessor
}
}

func (filters StackdriverFilters) toOptions() []stackdriver.FilterOption {
opts := []stackdriver.FilterOption{}

Expand Down
38 changes: 38 additions & 0 deletions decoder/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,44 @@ func TestValidStackdriverAggregations(t *testing.T) {
}
}

func TestValidStackdriverPreprocessor(t *testing.T) {
testCases := []struct {
input string
expected stackdriver.Reducer
}{
{input: "delta", expected: stackdriver.PreprocessDelta},
{input: "rate", expected: stackdriver.PreprocessRate},
}

for _, testCase := range testCases {
tc := testCase

t.Run(tc.input, func(t *testing.T) {
req := require.New(t)

panel := StackdriverTarget{Preprocessor: tc.input}

opt, err := panel.preprocessor()

req.NoError(err)

target := stackdriver.Delta("test")
opt(target)

req.Equal(string(tc.expected), target.Builder.Preprocessor)
})
}
}

func TestInvalidStackdriverPreprocessor(t *testing.T) {
req := require.New(t)

_, err := StackdriverTarget{Preprocessor: "invalid"}.toTarget()

req.Error(err)
req.Equal(ErrInvalidStackdriverPreprocessor, err)
}

func TestValidStackdriverTargetTypes(t *testing.T) {
testCases := []struct {
input string
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.16

require (
github.com/K-Phoen/sdk v0.8.3
github.com/K-Phoen/sdk v0.8.4
github.com/blang/semver v3.5.1+incompatible
github.com/gosimple/slug v1.12.0 // indirect
github.com/prometheus/common v0.32.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/K-Phoen/sdk v0.8.3 h1:UZU6UfWlIBkMH8rx/qUgTxcEYLtGyh49xvjJFePrVyI=
github.com/K-Phoen/sdk v0.8.3/go.mod h1:fnbOsbRksULSfcXjOI6W1/HISz5o/u1iEhF/fLedqTg=
github.com/K-Phoen/sdk v0.8.4 h1:KAH0ipC/4iWxslFudjy84Gm8fkaVRfekZK+BPAvoF30=
github.com/K-Phoen/sdk v0.8.4/go.mod h1:fnbOsbRksULSfcXjOI6W1/HISz5o/u1iEhF/fLedqTg=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down
13 changes: 13 additions & 0 deletions target/stackdriver/stackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ type Option func(target *Stackdriver)
const AlignmentStackdriverAuto = "stackdriver-auto"
const AlignmentGrafanaAuto = "grafana-auto"

// PreprocessorMethod defines the available pre-processing options.
type PreprocessorMethod string

const PreprocessRate = "rate"
const PreprocessDelta = "delta"

// Aligner specifies the operation that will be applied to the data points in
// each alignment period in a time series.
// See https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.alertPolicies#Aligner
Expand Down Expand Up @@ -148,6 +154,13 @@ func Alignment(aligner Aligner, alignmentPeriod string) Option {
}
}

// Preprocessor defines how the time series should be pre-processed.
func Preprocessor(preprocessor PreprocessorMethod) Option {
return func(stackdriver *Stackdriver) {
stackdriver.Builder.Preprocessor = string(preprocessor)
}
}

// Filter allows to specify which time series will be in the results.
func Filter(filters ...FilterOption) Option {
return func(stackdriver *Stackdriver) {
Expand Down
14 changes: 14 additions & 0 deletions target/stackdriver/stackdriver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,17 @@ func TestTargetSupportsGroupBys(t *testing.T) {

req.ElementsMatch(target.Builder.GroupBys, []string{"field", "other"})
}

func TestPreprocessorCanBeConfigured(t *testing.T) {
req := require.New(t)
preprocessors := []stackdriver.PreprocessorMethod{
stackdriver.PreprocessDelta,
stackdriver.PreprocessRate,
}

for _, preprocessor := range preprocessors {
target := stackdriver.Delta("", stackdriver.Preprocessor(preprocessor))

req.Equal(string(preprocessor), target.Builder.Preprocessor)
}
}
1 change: 1 addition & 0 deletions vendor/github.com/K-Phoen/sdk/panel.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.8.3
# github.com/K-Phoen/sdk v0.8.4
## explicit
github.com/K-Phoen/sdk
# github.com/blang/semver v3.5.1+incompatible
Expand Down

0 comments on commit fe478f4

Please sign in to comment.