mirror of
https://github.com/zalando-incubator/kube-metrics-adapter.git
synced 2025-02-03 10:58:20 +00:00
3f019a1ceb
This fixes an issue of setting up a ZMON collector where the incorrect key `Configuration` was used, which was not initialized in the metrics config parser. The `Config` key is the right one to use. Signed-off-by: Mikkel Oscar Lyderik Larsen <mikkel.larsen@zalando.de>
147 lines
4.3 KiB
Go
147 lines
4.3 KiB
Go
package collector
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/zalando-incubator/kube-metrics-adapter/pkg/zmon"
|
|
autoscalingv2 "k8s.io/api/autoscaling/v2beta2"
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/metrics/pkg/apis/external_metrics"
|
|
)
|
|
|
|
type zmonMock struct {
|
|
dataPoints []zmon.DataPoint
|
|
entities []zmon.Entity
|
|
}
|
|
|
|
func (m zmonMock) Query(checkID int, key string, tags map[string]string, aggregators []string, duration time.Duration) ([]zmon.DataPoint, error) {
|
|
return m.dataPoints, nil
|
|
}
|
|
|
|
func TestZMONCollectorNewCollector(t *testing.T) {
|
|
collectPlugin, _ := NewZMONCollectorPlugin(zmonMock{})
|
|
|
|
config := &MetricConfig{
|
|
MetricTypeName: MetricTypeName{
|
|
Metric: newMetricIdentifier(ZMONCheckMetric),
|
|
},
|
|
Config: map[string]string{
|
|
zmonCheckIDLabelKey: "1234",
|
|
zmonAggregatorsLabelKey: "max",
|
|
zmonTagPrefixLabelKey + "alias": "cluster_alias",
|
|
zmonDurationLabelKey: "5m",
|
|
zmonKeyLabelKey: "key",
|
|
},
|
|
}
|
|
|
|
hpa := &autoscalingv2.HorizontalPodAutoscaler{}
|
|
|
|
collector, err := collectPlugin.NewCollector(hpa, config, 1*time.Second)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, collector)
|
|
zmonCollector := collector.(*ZMONCollector)
|
|
require.Equal(t, "key", zmonCollector.key)
|
|
require.Equal(t, 1234, zmonCollector.checkID)
|
|
require.Equal(t, 1*time.Second, zmonCollector.interval)
|
|
require.Equal(t, 5*time.Minute, zmonCollector.duration)
|
|
require.Equal(t, []string{"max"}, zmonCollector.aggregators)
|
|
require.Equal(t, map[string]string{"alias": "cluster_alias"}, zmonCollector.tags)
|
|
|
|
// check that annotations overwrites labels
|
|
hpa.ObjectMeta = metav1.ObjectMeta{
|
|
Annotations: map[string]string{
|
|
zmonKeyAnnotationKey: "annotation_key",
|
|
zmonTagPrefixAnnotationKey + "alias": "cluster_alias_annotation",
|
|
},
|
|
}
|
|
collector, err = collectPlugin.NewCollector(hpa, config, 1*time.Second)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, collector)
|
|
zmonCollector = collector.(*ZMONCollector)
|
|
require.Equal(t, "annotation_key", zmonCollector.key)
|
|
require.Equal(t, map[string]string{"alias": "cluster_alias_annotation"}, zmonCollector.tags)
|
|
|
|
// should fail if the metric name isn't ZMON
|
|
config.Metric = newMetricIdentifier("non-zmon-check")
|
|
_, err = collectPlugin.NewCollector(nil, config, 1*time.Second)
|
|
require.Error(t, err)
|
|
|
|
// should fail if the check id is not specified.
|
|
delete(config.Config, zmonCheckIDLabelKey)
|
|
config.Metric.Name = ZMONCheckMetric
|
|
_, err = collectPlugin.NewCollector(nil, config, 1*time.Second)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func newMetricIdentifier(metricName string) autoscalingv2.MetricIdentifier {
|
|
selector := metav1.LabelSelector{}
|
|
return autoscalingv2.MetricIdentifier{Name: metricName, Selector: &selector}
|
|
}
|
|
|
|
func TestZMONCollectorGetMetrics(tt *testing.T) {
|
|
|
|
config := &MetricConfig{
|
|
MetricTypeName: MetricTypeName{
|
|
Metric: newMetricIdentifier(ZMONCheckMetric),
|
|
Type: "foo",
|
|
},
|
|
Config: map[string]string{
|
|
zmonCheckIDLabelKey: "1234",
|
|
zmonAggregatorsLabelKey: "max",
|
|
zmonTagPrefixLabelKey + "alias": "cluster_alias",
|
|
zmonDurationLabelKey: "5m",
|
|
zmonKeyLabelKey: "key",
|
|
},
|
|
}
|
|
|
|
for _, ti := range []struct {
|
|
msg string
|
|
dataPoints []zmon.DataPoint
|
|
collectedMetrics []CollectedMetric
|
|
}{
|
|
{
|
|
msg: "test successfully getting metrics",
|
|
dataPoints: []zmon.DataPoint{
|
|
{
|
|
Time: time.Time{},
|
|
Value: 1.0,
|
|
},
|
|
},
|
|
collectedMetrics: []CollectedMetric{
|
|
{
|
|
Type: config.Type,
|
|
External: external_metrics.ExternalMetricValue{
|
|
MetricName: config.Metric.Name,
|
|
MetricLabels: config.Metric.Selector.MatchLabels,
|
|
Timestamp: metav1.Time{Time: time.Time{}},
|
|
Value: *resource.NewMilliQuantity(int64(1.0)*1000, resource.DecimalSI),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
msg: "test not getting any metrics",
|
|
},
|
|
} {
|
|
tt.Run(ti.msg, func(t *testing.T) {
|
|
z := zmonMock{
|
|
dataPoints: ti.dataPoints,
|
|
}
|
|
|
|
zmonCollector, err := NewZMONCollector(z, config, nil, 1*time.Second)
|
|
require.NoError(t, err)
|
|
|
|
metrics, _ := zmonCollector.GetMetrics()
|
|
require.Equal(t, ti.collectedMetrics, metrics)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestZMONCollectorInterval(t *testing.T) {
|
|
collector := ZMONCollector{interval: 1 * time.Second}
|
|
require.Equal(t, 1*time.Second, collector.Interval())
|
|
}
|