mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-07 10:17:51 +00:00
Some more adjustment.
* Breaking out some types to make the functionality more composable and easier to digest. (e.g. `basicMetricLister` interacts with Prometheus and `periodicMetricLister` periodically invokes `basicMetricLister`. * Pulling out some of the type embedding between `basicSeriesRegistry` and `MetricLister` to make it easier to digest. * Deleting the `/metric-converter` code because I'm pretty certain it's not going to be necessary as things transition to using the namer-based configuration. * Some light-ish refactoring in `metricNamer` to get some re-use out of query generation in preparation for using it with external metrics.
This commit is contained in:
parent
277734dcdb
commit
76217a552b
15 changed files with 490 additions and 558 deletions
|
|
@ -1,37 +0,0 @@
|
|||
package provider
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
prom "github.com/directxman12/k8s-prometheus-adapter/pkg/client"
|
||||
"github.com/prometheus/common/model"
|
||||
"k8s.io/metrics/pkg/apis/external_metrics"
|
||||
)
|
||||
|
||||
type matrixConverter struct {
|
||||
}
|
||||
|
||||
//NewMatrixConverter creates a MatrixConverter capable of converting
|
||||
//matrix Prometheus query results into external metric types.
|
||||
func NewMatrixConverter() MetricConverter {
|
||||
return &matrixConverter{}
|
||||
}
|
||||
|
||||
func (c *matrixConverter) Convert(metadata QueryMetadata, queryResult prom.QueryResult) (*external_metrics.ExternalMetricValueList, error) {
|
||||
if queryResult.Type != model.ValMatrix {
|
||||
return nil, errors.New("matrixConverter can only convert scalar query results")
|
||||
}
|
||||
|
||||
toConvert := queryResult.Matrix
|
||||
|
||||
if toConvert == nil {
|
||||
return nil, errors.New("the provided input did not contain matrix query results")
|
||||
}
|
||||
|
||||
return c.convert(toConvert)
|
||||
}
|
||||
|
||||
func (c *matrixConverter) convert(result *model.Matrix) (*external_metrics.ExternalMetricValueList, error) {
|
||||
//TODO: Implementation.
|
||||
return nil, errors.New("converting Matrix results is not yet supported")
|
||||
}
|
||||
|
|
@ -1,47 +0,0 @@
|
|||
package provider
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
prom "github.com/directxman12/k8s-prometheus-adapter/pkg/client"
|
||||
"github.com/prometheus/common/model"
|
||||
"k8s.io/metrics/pkg/apis/external_metrics"
|
||||
)
|
||||
|
||||
//MetricConverter provides a unified interface for converting the results of
|
||||
//Prometheus queries into external metric types.
|
||||
type MetricConverter interface {
|
||||
Convert(metadata QueryMetadata, queryResult prom.QueryResult) (*external_metrics.ExternalMetricValueList, error)
|
||||
}
|
||||
|
||||
type metricConverter struct {
|
||||
scalarConverter MetricConverter
|
||||
vectorConverter MetricConverter
|
||||
matrixConverter MetricConverter
|
||||
}
|
||||
|
||||
//NewMetricConverter creates a MetricCoverter, capable of converting any of the three metric types
|
||||
//returned by the Prometheus client into external metrics types.
|
||||
func NewMetricConverter(scalar MetricConverter, vector MetricConverter, matrix MetricConverter) MetricConverter {
|
||||
return &metricConverter{
|
||||
scalarConverter: scalar,
|
||||
vectorConverter: vector,
|
||||
matrixConverter: matrix,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *metricConverter) Convert(metadata QueryMetadata, queryResult prom.QueryResult) (*external_metrics.ExternalMetricValueList, error) {
|
||||
if queryResult.Type == model.ValScalar {
|
||||
return c.scalarConverter.Convert(metadata, queryResult)
|
||||
}
|
||||
|
||||
if queryResult.Type == model.ValVector {
|
||||
return c.vectorConverter.Convert(metadata, queryResult)
|
||||
}
|
||||
|
||||
if queryResult.Type == model.ValMatrix {
|
||||
return c.matrixConverter.Convert(metadata, queryResult)
|
||||
}
|
||||
|
||||
return nil, errors.New("encountered an unexpected query result type")
|
||||
}
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
package provider
|
||||
|
||||
//QueryMetadata is a data object the holds information about what inputs
|
||||
//were used to generate Prometheus query results. In most cases it's not
|
||||
//necessary, as the Prometheus result come back with enough information
|
||||
//to determine the metric name. However, for scalar results, Prometheus
|
||||
//only provides the value.
|
||||
type QueryMetadata struct {
|
||||
MetricName string
|
||||
WindowInSeconds int64
|
||||
//TODO: Type this?
|
||||
Aggregation string
|
||||
}
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
package provider
|
||||
|
||||
import (
|
||||
"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 sampleConverter struct {
|
||||
}
|
||||
|
||||
//SampleConverter is capable of translating Prometheus Sample objects
|
||||
//into ExternamMetricValue objects.
|
||||
type SampleConverter interface {
|
||||
Convert(metadata QueryMetadata, sample *model.Sample) (*external_metrics.ExternalMetricValue, error)
|
||||
}
|
||||
|
||||
//NewSampleConverter creates a SampleConverter capable of translating Prometheus Sample objects
|
||||
//into ExternamMetricValue objects.
|
||||
func NewSampleConverter() SampleConverter {
|
||||
return &sampleConverter{}
|
||||
}
|
||||
|
||||
func (c *sampleConverter) Convert(metadata QueryMetadata, sample *model.Sample) (*external_metrics.ExternalMetricValue, error) {
|
||||
labels := c.convertLabels(sample.Metric)
|
||||
|
||||
singleMetric := external_metrics.ExternalMetricValue{
|
||||
MetricName: string(sample.Metric[model.LabelName("__name__")]),
|
||||
Timestamp: metav1.Time{
|
||||
sample.Timestamp.Time(),
|
||||
},
|
||||
WindowSeconds: &metadata.WindowInSeconds,
|
||||
//TODO: I'm not so sure about this type/conversions.
|
||||
//This can't possibly be the right way to convert this.
|
||||
//Also, does K8S only deal win integer metrics?
|
||||
Value: *resource.NewQuantity(int64(float64(sample.Value)), resource.DecimalSI),
|
||||
MetricLabels: labels,
|
||||
}
|
||||
|
||||
//TODO: Actual errors?
|
||||
return &singleMetric, nil
|
||||
}
|
||||
|
||||
func (c *sampleConverter) convertLabels(inLabels model.Metric) map[string]string {
|
||||
numLabels := len(inLabels)
|
||||
outLabels := make(map[string]string, numLabels)
|
||||
for labelName, labelVal := range inLabels {
|
||||
outLabels[string(labelName)] = string(labelVal)
|
||||
}
|
||||
|
||||
return outLabels
|
||||
}
|
||||
|
|
@ -1,57 +0,0 @@
|
|||
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{
|
||||
{
|
||||
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
|
||||
}
|
||||
|
|
@ -1,58 +0,0 @@
|
|||
package provider
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
prom "github.com/directxman12/k8s-prometheus-adapter/pkg/client"
|
||||
"github.com/prometheus/common/model"
|
||||
"k8s.io/metrics/pkg/apis/external_metrics"
|
||||
)
|
||||
|
||||
type vectorConverter struct {
|
||||
SampleConverter SampleConverter
|
||||
}
|
||||
|
||||
//NewVectorConverter creates a VectorConverter capable of converting
|
||||
//vector Prometheus query results into external metric types.
|
||||
func NewVectorConverter(sampleConverter *SampleConverter) MetricConverter {
|
||||
return &vectorConverter{
|
||||
SampleConverter: *sampleConverter,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *vectorConverter) Convert(metadata QueryMetadata, queryResult prom.QueryResult) (*external_metrics.ExternalMetricValueList, error) {
|
||||
if queryResult.Type != model.ValVector {
|
||||
return nil, errors.New("vectorConverter can only convert scalar query results")
|
||||
}
|
||||
|
||||
toConvert := *queryResult.Vector
|
||||
|
||||
if toConvert == nil {
|
||||
return nil, errors.New("the provided input did not contain vector query results")
|
||||
}
|
||||
|
||||
return c.convert(metadata, toConvert)
|
||||
}
|
||||
|
||||
func (c *vectorConverter) convert(metadata QueryMetadata, result model.Vector) (*external_metrics.ExternalMetricValueList, error) {
|
||||
items := []external_metrics.ExternalMetricValue{}
|
||||
metricValueList := external_metrics.ExternalMetricValueList{
|
||||
Items: items,
|
||||
}
|
||||
|
||||
numSamples := result.Len()
|
||||
if numSamples == 0 {
|
||||
return &metricValueList, nil
|
||||
}
|
||||
|
||||
for _, val := range result {
|
||||
//TODO: Care about potential errors here.
|
||||
singleMetric, _ := c.SampleConverter.Convert(metadata, val)
|
||||
items = append(items, *singleMetric)
|
||||
}
|
||||
|
||||
metricValueList = external_metrics.ExternalMetricValueList{
|
||||
Items: items,
|
||||
}
|
||||
return &metricValueList, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue