Skip to content

Commit

Permalink
Namespace external metrics (#259)
Browse files Browse the repository at this point in the history
Signed-off-by: Mikkel Oscar Lyderik Larsen <[email protected]>
  • Loading branch information
mikkeloscar authored Feb 19, 2021
1 parent 942e753 commit b7aa886
Show file tree
Hide file tree
Showing 13 changed files with 317 additions and 68 deletions.
9 changes: 6 additions & 3 deletions pkg/collector/aws_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewAWSCollectorPlugin(sessions map[string]*session.Session) *AWSCollectorPl

// NewCollector initializes a new skipper collector from the specified HPA.
func (c *AWSCollectorPlugin) NewCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) {
return NewAWSSQSCollector(c.sessions, config, interval)
return NewAWSSQSCollector(c.sessions, hpa, config, interval)
}

type AWSSQSCollector struct {
Expand All @@ -42,11 +42,12 @@ type AWSSQSCollector struct {
region string
queueURL string
queueName string
namespace string
metric autoscalingv2.MetricIdentifier
metricType autoscalingv2.MetricSourceType
}

func NewAWSSQSCollector(sessions map[string]*session.Session, config *MetricConfig, interval time.Duration) (*AWSSQSCollector, error) {
func NewAWSSQSCollector(sessions map[string]*session.Session, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (*AWSSQSCollector, error) {
if config.Metric.Selector == nil {
return nil, fmt.Errorf("selector for queue is not specified")
}
Expand Down Expand Up @@ -80,6 +81,7 @@ func NewAWSSQSCollector(sessions map[string]*session.Session, config *MetricConf
interval: interval,
queueURL: aws.StringValue(resp.QueueUrl),
queueName: name,
namespace: hpa.Namespace,
metric: config.Metric,
metricType: config.Type,
}, nil
Expand All @@ -103,7 +105,8 @@ func (c *AWSSQSCollector) GetMetrics() ([]CollectedMetric, error) {
}

metricValue := CollectedMetric{
Type: c.metricType,
Namespace: c.namespace,
Type: c.metricType,
External: external_metrics.ExternalMetricValue{
MetricName: c.metric.Name,
MetricLabels: c.metric.Selector.MatchLabels,
Expand Down
7 changes: 4 additions & 3 deletions pkg/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,10 @@ type MetricTypeName struct {
}

type CollectedMetric struct {
Type autoscalingv2.MetricSourceType
Custom custom_metrics.MetricValue
External external_metrics.ExternalMetricValue
Type autoscalingv2.MetricSourceType
Namespace string
Custom custom_metrics.MetricValue
External external_metrics.ExternalMetricValue
}

type Collector interface {
Expand Down
10 changes: 7 additions & 3 deletions pkg/collector/http_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ func NewHTTPCollectorPlugin() (*HTTPCollectorPlugin, error) {
return &HTTPCollectorPlugin{}, nil
}

func (p *HTTPCollectorPlugin) NewCollector(_ *v2beta2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) {
collector := &HTTPCollector{}
func (p *HTTPCollectorPlugin) NewCollector(hpa *v2beta2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) {
collector := &HTTPCollector{
namespace: hpa.Namespace,
}
var (
value string
ok bool
Expand Down Expand Up @@ -74,6 +76,7 @@ func (p *HTTPCollectorPlugin) NewCollector(_ *v2beta2.HorizontalPodAutoscaler, c
type HTTPCollector struct {
endpoint *url.URL
interval time.Duration
namespace string
metricType v2beta2.MetricSourceType
metricsGetter *httpmetrics.JSONPathMetricsGetter
metric v2beta2.MetricIdentifier
Expand All @@ -86,7 +89,8 @@ func (c *HTTPCollector) GetMetrics() ([]CollectedMetric, error) {
}

value := CollectedMetric{
Type: c.metricType,
Namespace: c.namespace,
Type: c.metricType,
External: external_metrics.ExternalMetricValue{
MetricName: c.metric.Name,
MetricLabels: c.metric.Selector.MatchLabels,
Expand Down
9 changes: 8 additions & 1 deletion pkg/collector/http_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"github.com/stretchr/testify/require"
"k8s.io/api/autoscaling/v2beta2"
autoscalingv2 "k8s.io/api/autoscaling/v2beta2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -60,7 +62,12 @@ func TestHTTPCollector(t *testing.T) {
plugin, err := NewHTTPCollectorPlugin()
require.NoError(t, err)
testConfig := makeTestHTTPCollectorConfig(testServer, tc.aggregator)
collector, err := plugin.NewCollector(nil, testConfig, testInterval)
hpa := &autoscalingv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
},
}
collector, err := plugin.NewCollector(hpa, testConfig, testInterval)
require.NoError(t, err)
metrics, err := collector.GetMetrics()
require.NoError(t, err)
Expand Down
9 changes: 6 additions & 3 deletions pkg/collector/influxdb_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewInfluxDBCollectorPlugin(client kubernetes.Interface, address, token, org
}

func (p *InfluxDBCollectorPlugin) NewCollector(hpa *v2beta2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) {
return NewInfluxDBCollector(p.address, p.token, p.org, config, interval)
return NewInfluxDBCollector(hpa, p.address, p.token, p.org, config, interval)
}

type InfluxDBCollector struct {
Expand All @@ -53,13 +53,15 @@ type InfluxDBCollector struct {
metric autoscalingv2.MetricIdentifier
metricType autoscalingv2.MetricSourceType
query string
namespace string
}

func NewInfluxDBCollector(address string, token string, org string, config *MetricConfig, interval time.Duration) (*InfluxDBCollector, error) {
func NewInfluxDBCollector(hpa *v2beta2.HorizontalPodAutoscaler, address string, token string, org string, config *MetricConfig, interval time.Duration) (*InfluxDBCollector, error) {
collector := &InfluxDBCollector{
interval: interval,
metric: config.Metric,
metricType: config.Type,
namespace: hpa.Namespace,
}
switch configType := config.Type; configType {
case autoscalingv2.ObjectMetricSourceType:
Expand Down Expand Up @@ -135,7 +137,8 @@ func (c *InfluxDBCollector) GetMetrics() ([]CollectedMetric, error) {
return nil, err
}
cm := CollectedMetric{
Type: c.metricType,
Namespace: c.namespace,
Type: c.metricType,
External: external_metrics.ExternalMetricValue{
MetricName: c.metric.Name,
MetricLabels: c.metric.Selector.MatchLabels,
Expand Down
13 changes: 10 additions & 3 deletions pkg/collector/influxdb_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ import (
"time"

"k8s.io/api/autoscaling/v2beta2"
autoscalingv2 "k8s.io/api/autoscaling/v2beta2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestInfluxDBCollector_New(t *testing.T) {
hpa := &autoscalingv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
},
}
t.Run("simple", func(t *testing.T) {
m := &MetricConfig{
MetricTypeName: MetricTypeName{
Expand All @@ -32,7 +39,7 @@ func TestInfluxDBCollector_New(t *testing.T) {
"query-name": "range2m",
},
}
c, err := NewInfluxDBCollector("http://localhost:9999", "secret", "deadbeef", m, time.Second)
c, err := NewInfluxDBCollector(hpa, "http://localhost:9999", "secret", "deadbeef", m, time.Second)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -73,7 +80,7 @@ func TestInfluxDBCollector_New(t *testing.T) {
"query-name": "range3m",
},
}
c, err := NewInfluxDBCollector("http://localhost:8888", "secret", "deadbeef", m, time.Second)
c, err := NewInfluxDBCollector(hpa, "http://localhost:8888", "secret", "deadbeef", m, time.Second)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -143,7 +150,7 @@ func TestInfluxDBCollector_New(t *testing.T) {
CollectorType: "influxdb",
Config: tc.config,
}
_, err := NewInfluxDBCollector("http://localhost:9999", "secret", "deadbeef", m, time.Second)
_, err := NewInfluxDBCollector(hpa, "http://localhost:9999", "secret", "deadbeef", m, time.Second)
if err == nil {
t.Fatal("expected error got none")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/collector/pod_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ func (c *PodCollector) getPodMetric(pod corev1.Pod, ch chan CollectedMetric, err
}

ch <- CollectedMetric{
Type: c.metricType,
Namespace: c.namespace,
Type: c.metricType,
Custom: custom_metrics.MetricValue{
DescribedObject: custom_metrics.ObjectReference{
APIVersion: "v1",
Expand Down
6 changes: 4 additions & 2 deletions pkg/collector/prometheus_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ func (c *PrometheusCollector) GetMetrics() ([]CollectedMetric, error) {
switch c.metricType {
case autoscalingv2.ObjectMetricSourceType:
metricValue = CollectedMetric{
Type: c.metricType,
Namespace: c.hpa.Namespace,
Type: c.metricType,
Custom: custom_metrics.MetricValue{
DescribedObject: c.objectReference,
Metric: custom_metrics.MetricIdentifier{Name: c.metric.Name, Selector: c.metric.Selector},
Expand All @@ -184,7 +185,8 @@ func (c *PrometheusCollector) GetMetrics() ([]CollectedMetric, error) {
}
case autoscalingv2.ExternalMetricSourceType:
metricValue = CollectedMetric{
Type: c.metricType,
Namespace: c.hpa.Namespace,
Type: c.metricType,
External: external_metrics.ExternalMetricValue{
MetricName: c.metric.Name,
MetricLabels: c.metric.Selector.MatchLabels,
Expand Down
9 changes: 6 additions & 3 deletions pkg/collector/zmon_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewZMONCollectorPlugin(zmon zmon.ZMON) (*ZMONCollectorPlugin, error) {

// NewCollector initializes a new ZMON collector from the specified HPA.
func (c *ZMONCollectorPlugin) NewCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) {
return NewZMONCollector(c.zmon, config, interval)
return NewZMONCollector(c.zmon, hpa, config, interval)
}

// ZMONCollector defines a collector that is able to collect metrics from ZMON.
Expand All @@ -57,10 +57,11 @@ type ZMONCollector struct {
aggregators []string
metric autoscalingv2.MetricIdentifier
metricType autoscalingv2.MetricSourceType
namespace string
}

// NewZMONCollector initializes a new ZMONCollector.
func NewZMONCollector(zmon zmon.ZMON, config *MetricConfig, interval time.Duration) (*ZMONCollector, error) {
func NewZMONCollector(zmon zmon.ZMON, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (*ZMONCollector, error) {
if config.Metric.Selector == nil {
return nil, fmt.Errorf("selector for zmon-check is not specified")
}
Expand Down Expand Up @@ -117,6 +118,7 @@ func NewZMONCollector(zmon zmon.ZMON, config *MetricConfig, interval time.Durati
aggregators: aggregators,
metric: config.Metric,
metricType: config.Type,
namespace: hpa.Namespace,
}, nil
}

Expand All @@ -136,7 +138,8 @@ func (c *ZMONCollector) GetMetrics() ([]CollectedMetric, error) {
point := dataPoints[len(dataPoints)-1]

metricValue := CollectedMetric{
Type: c.metricType,
Namespace: c.namespace,
Type: c.metricType,
External: external_metrics.ExternalMetricValue{
MetricName: c.metric.Name,
MetricLabels: c.metric.Selector.MatchLabels,
Expand Down
11 changes: 9 additions & 2 deletions pkg/collector/zmon_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ func TestZMONCollectorGetMetrics(tt *testing.T) {
},
collectedMetrics: []CollectedMetric{
{
Type: config.Type,
Namespace: "default",
Type: config.Type,
External: external_metrics.ExternalMetricValue{
MetricName: config.Metric.Name,
MetricLabels: config.Metric.Selector.MatchLabels,
Expand All @@ -115,7 +116,13 @@ func TestZMONCollectorGetMetrics(tt *testing.T) {
dataPoints: ti.dataPoints,
}

zmonCollector, err := NewZMONCollector(z, config, 1*time.Second)
hpa := &autoscalingv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
},
}

zmonCollector, err := NewZMONCollector(z, hpa, config, 1*time.Second)
require.NoError(t, err)

metrics, _ := zmonCollector.GetMetrics()
Expand Down
4 changes: 3 additions & 1 deletion pkg/provider/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func (p *HPAProvider) updateHPAs() error {
newHPAs := 0

for _, hpa := range hpas.Items {
hpa := *hpa.DeepCopy()
resourceRef := resourceReference{
Name: hpa.Name,
Namespace: hpa.Namespace,
Expand Down Expand Up @@ -246,7 +247,8 @@ func (p *HPAProvider) collectMetrics(ctx context.Context) {
value.Custom.DescribedObject.Name,
)
case autoscalingv2.ExternalMetricSourceType:
p.logger.Infof("Collected new external metric '%s' (%s) [%s]",
p.logger.Infof("Collected new external metric '%s/%s' (%s) [%s]",
value.Namespace,
value.External.MetricName,
value.External.Value.String(),
labels.Set(value.External.MetricLabels).String(),
Expand Down
Loading

0 comments on commit b7aa886

Please sign in to comment.