Skip to content

Commit

Permalink
Add Cloudwatch graph to grabana
Browse files Browse the repository at this point in the history
  • Loading branch information
Yury Nikitin committed Jan 22, 2024
1 parent c4bddda commit aef69c0
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
20 changes: 20 additions & 0 deletions graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/K-Phoen/grabana/errors"
"github.com/K-Phoen/grabana/graph/series"
"github.com/K-Phoen/grabana/links"
"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/prometheus"
Expand Down Expand Up @@ -186,6 +187,25 @@ func WithStackdriverTarget(target *stackdriver.Stackdriver) Option {
}
}

// WithCloudwatchTarget adds a cloudwatch metric to the graph.
func WithCloudwatchTarget(metric string, namespace string, options ...cloudwatch.Option) Option {
target := cloudwatch.New(metric, namespace, options...)

return func(graph *Graph) error {
graph.Builder.AddTarget(&sdk.Target{
RefID: target.Ref,
Namespace: target.Namespace,
MetricName: target.MetricName,
Statistics: target.Statistics,
Dimensions: target.Dimensions,
Region: target.Region,
LegendFormat: target.LegendFormat,
})

return nil
}
}

// DataSource sets the data source to be used by the graph.
func DataSource(source string) Option {
return func(graph *Graph) error {
Expand Down
11 changes: 11 additions & 0 deletions graph/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,14 @@ func TestInvalidSeriesOverridesAreRejected(t *testing.T) {
req.Error(err)
req.ErrorIs(err, errors.ErrInvalidArgument)
}

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

panel, err := New("", WithCloudwatchTarget(
"AWS/ELB", "HTTPCode_Backend_5XX",
))

req.NoError(err)
req.Len(panel.Builder.GraphPanel.Targets, 1)
}
65 changes: 65 additions & 0 deletions target/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cloudwatch

// Option represents an option that can be used to configure a cloudwatch metric.
type Option func(target *Cloudwatch)

// Cloudwatch represents a cloudwatch metric.
type Cloudwatch struct {
Ref string
Namespace string
MetricName string
Region string
Statistics []string
Dimensions map[string]string
Period string
LegendFormat string
}

// New creates a new cloudwatch query.
func New(metric string, namespace string, options ...Option) *Cloudwatch {
cloudwatch := &Cloudwatch{
MetricName: metric,
Namespace: namespace,
}

for _, opt := range options {
opt(cloudwatch)
}

return cloudwatch
}

// Legend sets the legend format.
func Legend(legend string) Option {
return func(cloudwatch *Cloudwatch) {
cloudwatch.LegendFormat = legend
}
}

// Region sets the region.
func Region(region string) Option {
return func(cloudwatch *Cloudwatch) {
cloudwatch.Region = region
}
}

// Statistic sets the statistic.
func Statistic(statistics []string) Option {
return func(cloudwatch *Cloudwatch) {
cloudwatch.Statistics = statistics
}
}

// Dimensions sets the dimensions.
func Dimensions(dimensions map[string]string) Option {
return func(cloudwatch *Cloudwatch) {
cloudwatch.Dimensions = dimensions
}
}

// Ref sets the reference ID for this query.
func Ref(ref string) Option {
return func(cloudwatch *Cloudwatch) {
cloudwatch.Ref = ref
}
}
63 changes: 63 additions & 0 deletions target/cloudwatch/cloudwatch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cloudwatch

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestNewCloudwatchTargetCanBeCreated(t *testing.T) {
req := require.New(t)
metricName := "Cloudwatch"
namespace := "Test"

target := New(metricName, namespace)

req.Equal(metricName, target.MetricName)
req.Equal(namespace, target.Namespace)
}

func TestLegendCanBeConfigured(t *testing.T) {
req := require.New(t)
legend := "lala"

target := New("", "", Legend(legend))

req.Equal(legend, target.LegendFormat)
}

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

target := New("", "", Ref("A"))

req.Equal("A", target.Ref)
}

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

target := New("", "", Region("C"))

req.Equal("C", target.Region)
}

func TestStatisticCanBeSet(t *testing.T) {
req := require.New(t)
statistics := []string{"A", "B", "C"}

target := New("", "", Statistic(statistics))

req.Equal(statistics, target.Statistics)
}

func TestDimensionsCanBeSet(t *testing.T) {
req := require.New(t)
dimensions := map[string]string{
"one": "1",
}

target := New("", "", Dimensions(dimensions))

req.Equal(dimensions, target.Dimensions)
}

0 comments on commit aef69c0

Please sign in to comment.