Cloudwatch: Refactor metrics resource request (#57424)
* refactor metrics request * Update pkg/tsdb/cloudwatch/routes/dimension_keys_test.go Co-authored-by: Shirley <4163034+fridgepoet@users.noreply.github.com> * return metric struct value intead of pointer * make it possible to test hard coded metrics service * test all paths in route * fix broken test * fix one more broken test * add integration test Co-authored-by: Shirley <4163034+fridgepoet@users.noreply.github.com>
This commit is contained in:
@@ -7,9 +7,9 @@ import (
|
||||
|
||||
type ListMetricsProvider interface {
|
||||
GetDimensionKeysByDimensionFilter(*request.DimensionKeysRequest) ([]string, error)
|
||||
GetHardCodedDimensionKeysByNamespace(string) ([]string, error)
|
||||
GetDimensionKeysByNamespace(string) ([]string, error)
|
||||
GetDimensionValuesByDimensionFilter(*request.DimensionValuesRequest) ([]string, error)
|
||||
GetMetricsByNamespace(namespace string) ([]Metric, error)
|
||||
}
|
||||
|
||||
type MetricsClientProvider interface {
|
||||
|
||||
@@ -2,8 +2,6 @@ package request
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/constants"
|
||||
)
|
||||
|
||||
type DimensionKeysRequestType uint32
|
||||
@@ -22,7 +20,7 @@ type DimensionKeysRequest struct {
|
||||
}
|
||||
|
||||
func (q *DimensionKeysRequest) Type() DimensionKeysRequestType {
|
||||
if _, exist := constants.NamespaceMetricsMap[q.Namespace]; !exist {
|
||||
if isCustomNamespace(q.Namespace) {
|
||||
return CustomMetricDimensionKeysRequest
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type MetricsRequestType uint32
|
||||
|
||||
const (
|
||||
MetricsByNamespaceRequestType MetricsRequestType = iota
|
||||
AllMetricsRequestType
|
||||
CustomNamespaceRequestType
|
||||
)
|
||||
|
||||
type MetricsRequest struct {
|
||||
*ResourceRequest
|
||||
Namespace string
|
||||
}
|
||||
|
||||
func GetMetricsRequest(parameters url.Values) (*MetricsRequest, error) {
|
||||
resourceRequest, err := getResourceRequest(parameters)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &MetricsRequest{
|
||||
ResourceRequest: resourceRequest,
|
||||
Namespace: parameters.Get("namespace"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *MetricsRequest) Type() MetricsRequestType {
|
||||
if r.Namespace == "" {
|
||||
return AllMetricsRequestType
|
||||
}
|
||||
|
||||
if isCustomNamespace(r.Namespace) {
|
||||
return CustomNamespaceRequestType
|
||||
}
|
||||
|
||||
return MetricsByNamespaceRequestType
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestMetricsRequest(t *testing.T) {
|
||||
t.Run("Should parse parameters", func(t *testing.T) {
|
||||
request, err := GetMetricsRequest(map[string][]string{"region": {"us-east-1"}, "namespace": {"AWS/EC2"}})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "us-east-1", request.Region)
|
||||
assert.Equal(t, "AWS/EC2", request.Namespace)
|
||||
})
|
||||
|
||||
tests := []struct {
|
||||
reqType MetricsRequestType
|
||||
params url.Values
|
||||
}{
|
||||
{
|
||||
params: map[string][]string{"region": {"us-east-1"}, "namespace": {"AWS/EC2"}},
|
||||
reqType: MetricsByNamespaceRequestType,
|
||||
},
|
||||
{
|
||||
params: map[string][]string{"region": {"us-east-1"}},
|
||||
reqType: AllMetricsRequestType,
|
||||
},
|
||||
{
|
||||
params: map[string][]string{"region": {"us-east-1"}, "namespace": {""}},
|
||||
reqType: AllMetricsRequestType,
|
||||
},
|
||||
{
|
||||
params: map[string][]string{"region": {"us-east-1"}, "namespace": {"custom-namespace"}},
|
||||
reqType: CustomNamespaceRequestType,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run("Should resolve the correct type", func(t *testing.T) {
|
||||
request, err := GetMetricsRequest(tc.params)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tc.reqType, request.Type())
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package request
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/constants"
|
||||
)
|
||||
|
||||
func parseDimensionFilter(dimensionFilter string) ([]*Dimension, error) {
|
||||
@@ -42,3 +44,10 @@ func parseDimensionFilter(dimensionFilter string) ([]*Dimension, error) {
|
||||
|
||||
return dimensions, nil
|
||||
}
|
||||
|
||||
func isCustomNamespace(namespace string) bool {
|
||||
if _, ok := constants.NamespaceMetricsMap[namespace]; ok {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -34,3 +34,8 @@ type metricStatMeta struct {
|
||||
Period int `json:"period"`
|
||||
Label string `json:"label,omitempty"`
|
||||
}
|
||||
|
||||
type Metric struct {
|
||||
Name string `json:"name"`
|
||||
Namespace string `json:"namespace"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user