mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-07 22:25:03 +00:00
Working through some of the result conversions. I want to use this code in a harness project to exercise some of it, but there are some versioning issues around my modified code vs. the current version of the real project. The easiest way to work around them at the moment is to push this.
57 lines
1.8 KiB
Go
57 lines
1.8 KiB
Go
package provider
|
|
|
|
import (
|
|
"errors"
|
|
|
|
prom "github.com/directxman12/k8s-prometheus-adapter/pkg/client"
|
|
"github.com/prometheus/common/model"
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/metrics/pkg/apis/external_metrics"
|
|
)
|
|
|
|
type scalarConverter struct {
|
|
}
|
|
|
|
//NewScalarConverter creates a ScalarConverter capable of converting
|
|
//scalar Prometheus query results into external metric types.
|
|
func NewScalarConverter() MetricConverter {
|
|
return &scalarConverter{}
|
|
}
|
|
|
|
func (c *scalarConverter) Convert(metadata QueryMetadata, queryResult prom.QueryResult) (*external_metrics.ExternalMetricValueList, error) {
|
|
if queryResult.Type != model.ValScalar {
|
|
return nil, errors.New("scalarConverter can only convert scalar query results")
|
|
}
|
|
|
|
toConvert := queryResult.Scalar
|
|
|
|
if toConvert == nil {
|
|
return nil, errors.New("the provided input did not contain scalar query results")
|
|
}
|
|
|
|
return c.convert(metadata, toConvert)
|
|
}
|
|
|
|
func (c *scalarConverter) convert(metadata QueryMetadata, input *model.Scalar) (*external_metrics.ExternalMetricValueList, error) {
|
|
result := external_metrics.ExternalMetricValueList{
|
|
//Using prometheusProvider.metricsFor(...) as an example,
|
|
//it seems that I don't need to provide values for
|
|
//TypeMeta and ListMeta.
|
|
//TODO: Get some confirmation on this.
|
|
Items: []external_metrics.ExternalMetricValue{
|
|
external_metrics.ExternalMetricValue{
|
|
MetricName: metadata.MetricName,
|
|
Timestamp: metav1.Time{
|
|
input.Timestamp.Time(),
|
|
},
|
|
WindowSeconds: &metadata.WindowInSeconds,
|
|
//TODO: I'm not so sure about this type/conversions.
|
|
//Is there a meaningful loss of precision here?
|
|
//Does K8S only deal win integer metrics?
|
|
Value: *resource.NewQuantity(int64(input.Value), resource.DecimalSI),
|
|
},
|
|
},
|
|
}
|
|
return &result, nil
|
|
}
|