mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-06 17:57:51 +00:00
Notable next steps include: * Intelligently select the aggregation method (rate/avg_over_time/etc). * Intelligenty select the aggregation window. * Figure out how to handle "namespace", if at all. * Fix the crazy type conversion in `sample_converter.go`.
53 lines
1.7 KiB
Go
53 lines
1.7 KiB
Go
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
|
|
}
|