Skip to content

Commit

Permalink
pkg/external-provider: set metric name for scalar values
Browse files Browse the repository at this point in the history
  • Loading branch information
s-urbaniak committed May 17, 2019
1 parent f0ec22a commit 66e7adf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 11 additions & 9 deletions pkg/external-provider/metric_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"

prom "github.com/directxman12/k8s-prometheus-adapter/pkg/client"
"github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider"
"github.com/prometheus/common/model"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -27,7 +28,7 @@ import (
// MetricConverter provides a unified interface for converting the results of
// Prometheus queries into external metric types.
type MetricConverter interface {
Convert(queryResult prom.QueryResult) (*external_metrics.ExternalMetricValueList, error)
Convert(info provider.ExternalMetricInfo, queryResult prom.QueryResult) (*external_metrics.ExternalMetricValueList, error)
}

type metricConverter struct {
Expand All @@ -39,23 +40,23 @@ func NewMetricConverter() MetricConverter {
return &metricConverter{}
}

func (c *metricConverter) Convert(queryResult prom.QueryResult) (*external_metrics.ExternalMetricValueList, error) {
func (c *metricConverter) Convert(info provider.ExternalMetricInfo, queryResult prom.QueryResult) (*external_metrics.ExternalMetricValueList, error) {
if queryResult.Type == model.ValScalar {
return c.convertScalar(queryResult)
return c.convertScalar(info, queryResult)
}

if queryResult.Type == model.ValVector {
return c.convertVector(queryResult)
return c.convertVector(info, queryResult)
}

return nil, errors.New("encountered an unexpected query result type")
}

func (c *metricConverter) convertSample(sample *model.Sample) (*external_metrics.ExternalMetricValue, error) {
func (c *metricConverter) convertSample(info provider.ExternalMetricInfo, sample *model.Sample) (*external_metrics.ExternalMetricValue, error) {
labels := c.convertLabels(sample.Metric)

singleMetric := external_metrics.ExternalMetricValue{
MetricName: string(sample.Metric[model.LabelName("__name__")]),
MetricName: info.Metric,
Timestamp: metav1.Time{
sample.Timestamp.Time(),
},
Expand All @@ -76,7 +77,7 @@ func (c *metricConverter) convertLabels(inLabels model.Metric) map[string]string
return outLabels
}

func (c *metricConverter) convertVector(queryResult prom.QueryResult) (*external_metrics.ExternalMetricValueList, error) {
func (c *metricConverter) convertVector(info provider.ExternalMetricInfo, queryResult prom.QueryResult) (*external_metrics.ExternalMetricValueList, error) {
if queryResult.Type != model.ValVector {
return nil, errors.New("incorrect query result type")
}
Expand All @@ -98,7 +99,7 @@ func (c *metricConverter) convertVector(queryResult prom.QueryResult) (*external
}

for _, val := range toConvert {
singleMetric, err := c.convertSample(val)
singleMetric, err := c.convertSample(info, val)

if err != nil {
return nil, fmt.Errorf("unable to convert vector: %v", err)
Expand All @@ -113,7 +114,7 @@ func (c *metricConverter) convertVector(queryResult prom.QueryResult) (*external
return &metricValueList, nil
}

func (c *metricConverter) convertScalar(queryResult prom.QueryResult) (*external_metrics.ExternalMetricValueList, error) {
func (c *metricConverter) convertScalar(info provider.ExternalMetricInfo, queryResult prom.QueryResult) (*external_metrics.ExternalMetricValueList, error) {
if queryResult.Type != model.ValScalar {
return nil, errors.New("scalarConverter can only convert scalar query results")
}
Expand All @@ -127,6 +128,7 @@ func (c *metricConverter) convertScalar(queryResult prom.QueryResult) (*external
result := external_metrics.ExternalMetricValueList{
Items: []external_metrics.ExternalMetricValue{
{
MetricName: info.Metric,
Timestamp: metav1.Time{
toConvert.Timestamp.Time(),
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/external-provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (p *externalPrometheusProvider) GetExternalMetric(namespace string, metricS
// don't leak implementation details to the user
return nil, apierr.NewInternalError(fmt.Errorf("unable to fetch metrics"))
}
return p.metricConverter.Convert(queryResults)
return p.metricConverter.Convert(info, queryResults)
}

func (p *externalPrometheusProvider) ListAllExternalMetrics() []provider.ExternalMetricInfo {
Expand Down

0 comments on commit 66e7adf

Please sign in to comment.