Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding cloudwatch target #227

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions decoder/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestUnmarshalYAML(t *testing.T) {
logsPanel(),
statPanel(),
gaugePanel(),
graphPanelWithCloudwatchTarget(),
}

for _, testCase := range testCases {
Expand Down Expand Up @@ -569,6 +570,34 @@ rows:
}
}

func graphPanelWithCloudwatchTarget() testCase {
yaml := `title: Awesome dashboard

rows:
- name: Test row
panels:
- graph:
title: Dummy
datasource: cloudwatch-test
targets:
- cloudwatch:
queryparams:
dimensions:
QueueName: "test-queue"
namespace: "AWS/SQS"
metricname: "NumberofMessagesReceived"
statistics:
- "Sum"
period: "30"
region: "us-east-1"
`

return testCase{
name: "single row with single graph panel and cloudwatch target",
yaml: yaml,
expectedGrafanaJSON: "graph_panel_cloudwatch_target.json"}
}

func singleStatPanel() testCase {
yaml := `title: Awesome dashboard

Expand Down
3 changes: 3 additions & 0 deletions decoder/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ func (gaugePanel DashboardGauge) target(t Target) (gauge.Option, error) {

return gauge.WithStackdriverTarget(stackdriverTarget), nil
}
if t.Cloudwatch != nil {
return gauge.WithCloudwatchTarget(t.Cloudwatch.QueryParams, t.Cloudwatch.toOptions()...), nil
}

return nil, ErrTargetNotConfigured
}
24 changes: 24 additions & 0 deletions decoder/gauge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"

"github.com/K-Phoen/grabana/gauge"
"github.com/K-Phoen/grabana/target/cloudwatch"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -45,3 +47,25 @@ func TestGaugeValidValueTypes(t *testing.T) {
})
}
}

func TestGaugeCloudwatchTarget(t *testing.T) {

panel := DashboardGauge{
Title: "cloudwatch target test",
Targets: []Target{
{
Cloudwatch: &CloudwatchTarget{
QueryParams: cloudwatch.QueryParams{
Dimensions: map[string]string{
"Name": "test",
},
},
},
},
},
}

option, err := panel.target(panel.Targets[0])
assert.NoError(t, err)
assert.NotNil(t, option)
}
3 changes: 3 additions & 0 deletions decoder/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ func (graphPanel *DashboardGraph) target(t Target) (graph.Option, error) {

return graph.WithStackdriverTarget(stackdriverTarget), nil
}
if t.Cloudwatch != nil {
return graph.WithCloudwatchTarget(t.Cloudwatch.QueryParams, t.Cloudwatch.toOptions()...), nil
}

return nil, ErrTargetNotConfigured
}
Expand Down
3 changes: 3 additions & 0 deletions decoder/heatmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ func (heatmapPanel DashboardHeatmap) target(t Target) (heatmap.Option, error) {

return heatmap.WithStackdriverTarget(stackdriverTarget), nil
}
if t.Cloudwatch != nil {
return heatmap.WithCloudwatchTarget(t.Cloudwatch.QueryParams, t.Cloudwatch.toOptions()...), nil
}

return nil, ErrTargetNotConfigured
}
23 changes: 23 additions & 0 deletions decoder/heatmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (

"github.com/K-Phoen/grabana/heatmap/axis"
"github.com/K-Phoen/grabana/row"
"github.com/K-Phoen/grabana/target/cloudwatch"
"github.com/K-Phoen/sdk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -123,3 +125,24 @@ func TestHeatmapCanNotBeDecodedIfTargetIsInvalid(t *testing.T) {
req.Error(err)
req.Equal(ErrTargetNotConfigured, err)
}

func TestHeatmapCloudwatchTarget(t *testing.T) {
panel := DashboardHeatmap{
Title: "heatmap target test",
Targets: []Target{
{
Cloudwatch: &CloudwatchTarget{
QueryParams: cloudwatch.QueryParams{
Dimensions: map[string]string{
"Name": "test",
},
},
},
},
},
}

option, err := panel.target(panel.Targets[0])
assert.NoError(t, err)
assert.NotNil(t, option)
}
3 changes: 3 additions & 0 deletions decoder/singlestat.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ func (singleStatPanel DashboardSingleStat) target(t Target) (singlestat.Option,

return singlestat.WithStackdriverTarget(stackdriverTarget), nil
}
if t.Cloudwatch != nil {
return singlestat.WithCloudwatchTarget(t.Cloudwatch.QueryParams, t.Cloudwatch.toOptions()...), nil
}

return nil, ErrTargetNotConfigured
}
23 changes: 23 additions & 0 deletions decoder/singlestat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"

"github.com/K-Phoen/grabana/singlestat"
"github.com/K-Phoen/grabana/target/cloudwatch"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -74,3 +76,24 @@ func TestSparkLineModes(t *testing.T) {
})
}
}

func TestSinglestatCloudwatchTarget(t *testing.T) {
panel := DashboardSingleStat{
Title: "Singlestat target test",
Targets: []Target{
{
Cloudwatch: &CloudwatchTarget{
QueryParams: cloudwatch.QueryParams{
Dimensions: map[string]string{
"Name": "test",
},
},
},
},
},
}

option, err := panel.target(panel.Targets[0])
assert.NoError(t, err)
assert.NotNil(t, option)
}
3 changes: 3 additions & 0 deletions decoder/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ func (statPanel DashboardStat) target(t Target) (stat.Option, error) {

return stat.WithStackdriverTarget(stackdriverTarget), nil
}
if t.Cloudwatch != nil {
return stat.WithCloudwatchTarget(t.Cloudwatch.QueryParams, t.Cloudwatch.toOptions()...), nil
}

return nil, ErrTargetNotConfigured
}
23 changes: 23 additions & 0 deletions decoder/stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"

"github.com/K-Phoen/grabana/stat"
"github.com/K-Phoen/grabana/target/cloudwatch"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -77,3 +79,24 @@ func TestStatValidColorMode(t *testing.T) {
})
}
}

func TestStatCloudwatchTarget(t *testing.T) {
panel := DashboardStat{
Title: "Stat target test",
Targets: []Target{
{
Cloudwatch: &CloudwatchTarget{
QueryParams: cloudwatch.QueryParams{
Dimensions: map[string]string{
"Name": "test",
},
},
},
},
},
}

option, err := panel.target(panel.Targets[0])
assert.NoError(t, err)
assert.NotNil(t, option)
}
4 changes: 3 additions & 1 deletion decoder/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func (tablePanel *DashboardTable) target(t Target) (table.Option, error) {
if t.InfluxDB != nil {
return table.WithInfluxDBTarget(t.InfluxDB.Query, t.InfluxDB.toOptions()...), nil
}

if t.Cloudwatch != nil {
return table.WithCloudwatchTarget(t.Cloudwatch.QueryParams, t.Cloudwatch.toOptions()...), nil
}
return nil, ErrTargetNotConfigured
}
29 changes: 29 additions & 0 deletions decoder/table_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package decoder

import (
"testing"

"github.com/K-Phoen/grabana/target/cloudwatch"
"github.com/stretchr/testify/assert"
)

func TestTableCloudwatchTarget(t *testing.T) {
panel := DashboardTable{
Title: "Table target test",
Targets: []Target{
{
Cloudwatch: &CloudwatchTarget{
QueryParams: cloudwatch.QueryParams{
Dimensions: map[string]string{
"Name": "test",
},
},
},
},
},
}

option, err := panel.target(panel.Targets[0])
assert.NoError(t, err)
assert.NotNil(t, option)
}
20 changes: 20 additions & 0 deletions decoder/target.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/target/cloudwatch"
"github.com/K-Phoen/grabana/target/graphite"
"github.com/K-Phoen/grabana/target/influxdb"
"github.com/K-Phoen/grabana/target/loki"
Expand All @@ -22,6 +23,7 @@ type Target struct {
InfluxDB *InfluxDBTarget `yaml:"influxdb,omitempty"`
Stackdriver *StackdriverTarget `yaml:",omitempty"`
Loki *LokiTarget `yaml:",omitempty"`
Cloudwatch *CloudwatchTarget `yaml:",omitempty"`
}

type PrometheusTarget struct {
Expand Down Expand Up @@ -325,3 +327,21 @@ func (t StackdriverAlignment) toOption() (stackdriver.Option, error) {
return nil, ErrInvalidStackdriverAlignment
}
}

type CloudwatchTarget struct {
QueryParams cloudwatch.QueryParams `yaml:",omitempty"`
Ref string `yaml:",omitempty"`
Hidden bool `yaml:",omitempty"`
}

func (t CloudwatchTarget) toOptions() []cloudwatch.Option {
opts := []cloudwatch.Option{
cloudwatch.Ref(t.Ref),
}

if t.Hidden {
opts = append(opts, cloudwatch.Hide())
}

return opts
}
24 changes: 24 additions & 0 deletions decoder/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package decoder
import (
"testing"

"github.com/K-Phoen/grabana/target/cloudwatch"
"github.com/K-Phoen/grabana/target/influxdb"

"github.com/K-Phoen/grabana/target/graphite"
Expand Down Expand Up @@ -341,3 +342,26 @@ func TestGraphiteHiddenTarget(t *testing.T) {

req.True(target.Builder.Hide)
}

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

query := cloudwatch.QueryParams{}
opts := CloudwatchTarget{
QueryParams: query,
}.toOptions()
target := cloudwatch.New(query, opts...)

req.False(target.Builder.Hide)
}

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

query := cloudwatch.QueryParams{}

opts := CloudwatchTarget{Hidden: true}.toOptions()
target := cloudwatch.New(query, opts...)

req.True(target.Builder.Hide)
}
Loading