mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-07 22:25:03 +00:00
Fixed: * `basicMetricLister` wasn't applying the appropriate start time because I had forgotten to set the `lookback`. There are still a number of issues: * The `externalPrometheusProvider` is not hooked up to the web application yet, so it doesn't serve requests. * The namespace and label approach used in `external_info_map.go` is horrifically incorrect. It doesn't appropriately store multiple series with the same name but different labels. * The configuration is still not updated to appropriately handle external metrics, it's sort of half-piggy-backing on the pre-existing work.
52 lines
1.6 KiB
Go
52 lines
1.6 KiB
Go
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(queryResult prom.QueryResult) (*external_metrics.ExternalMetricValueList, error)
|
|
}
|
|
|
|
type metricConverter struct {
|
|
scalarConverter MetricConverter
|
|
vectorConverter MetricConverter
|
|
matrixConverter MetricConverter
|
|
}
|
|
|
|
func DefaultMetricConverter() MetricConverter {
|
|
sampleConverter := NewSampleConverter()
|
|
return NewMetricConverter(NewScalarConverter(), NewVectorConverter(&sampleConverter), NewMatrixConverter())
|
|
}
|
|
|
|
//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(queryResult prom.QueryResult) (*external_metrics.ExternalMetricValueList, error) {
|
|
if queryResult.Type == model.ValScalar {
|
|
return c.scalarConverter.Convert(queryResult)
|
|
}
|
|
|
|
if queryResult.Type == model.ValVector {
|
|
return c.vectorConverter.Convert(queryResult)
|
|
}
|
|
|
|
if queryResult.Type == model.ValMatrix {
|
|
return c.matrixConverter.Convert(queryResult)
|
|
}
|
|
|
|
return nil, errors.New("encountered an unexpected query result type")
|
|
}
|