mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-06 09:47:54 +00:00
Cleaning up.
* Wiped out a ton of warnings about comments. * Created consts for `MetricType` values. * `externalInfoMap` can now track multiple series with the same name/namespace and different labels. * `namespace` parameter of external metrics queries is now respected (albeit very rudimentarily) * Metric converter values for external metrics are now converted the same way as for custom metrics (probably still some opportunity for consolidation). * Lots of TODOs actually done. * Deleted a lot of commented out code.
This commit is contained in:
parent
9641e70005
commit
a94494337e
18 changed files with 155 additions and 188 deletions
|
|
@ -5,24 +5,37 @@ import (
|
|||
"k8s.io/apimachinery/pkg/labels"
|
||||
)
|
||||
|
||||
//ExportedMetric is a description of an available metric.
|
||||
type ExportedMetric struct {
|
||||
MetricName string
|
||||
Labels labels.Set
|
||||
Namespace string
|
||||
}
|
||||
|
||||
//ExternalInfoMap is a data object that accepts and organizes information
|
||||
//about available metrics.
|
||||
type ExternalInfoMap interface {
|
||||
//Begins tracking a metric, returning it to the caller.
|
||||
TrackMetric(metricName string, generatedBy MetricNamer) ExternalMetricData
|
||||
//Exports a collection of all of the metrics currently being tracked.
|
||||
ExportMetrics() []ExportedMetric
|
||||
//Finds a tracked metric with the given metric name, if it exists.
|
||||
FindMetric(metricName string) (data ExternalMetricData, found bool)
|
||||
}
|
||||
|
||||
//ExternalMetricData is a data object that accepts and organizes information
|
||||
//about the various series/namespaces that a metric is associated with.
|
||||
type ExternalMetricData interface {
|
||||
//MetricName returns the name of the metric represented by this object.
|
||||
MetricName() string
|
||||
//WithSeries associates the provided labels with this metric.
|
||||
WithSeries(labels labels.Set)
|
||||
//WithNamespacedSeries associates the provided labels with this metric, but within a particular namespace.
|
||||
WithNamespacedSeries(namespace string, labels labels.Set)
|
||||
//Exports a collection of all the metrics currently being tracked.
|
||||
ExportMetrics() []ExportedMetric
|
||||
GenerateQuery(selector labels.Selector) (prom.Selector, error)
|
||||
//Generates a query to select the series/values for the metric this object represents.
|
||||
GenerateQuery(namespace string, selector labels.Selector) (prom.Selector, error)
|
||||
}
|
||||
|
||||
type externalInfoMap struct {
|
||||
|
|
@ -31,18 +44,20 @@ type externalInfoMap struct {
|
|||
|
||||
type externalMetricData struct {
|
||||
metricName string
|
||||
namespacedData map[string]labels.Set
|
||||
namespacedData map[string][]labels.Set
|
||||
generatedBy MetricNamer
|
||||
}
|
||||
|
||||
//NewExternalMetricData creates an ExternalMetricData for the provided metric name and namer.
|
||||
func NewExternalMetricData(metricName string, generatedBy MetricNamer) ExternalMetricData {
|
||||
return &externalMetricData{
|
||||
metricName: metricName,
|
||||
generatedBy: generatedBy,
|
||||
namespacedData: map[string]labels.Set{},
|
||||
namespacedData: map[string][]labels.Set{},
|
||||
}
|
||||
}
|
||||
|
||||
//NewExternalInfoMap creates an empty ExternalInfoMap for storing external metric information.
|
||||
func NewExternalInfoMap() ExternalInfoMap {
|
||||
return &externalInfoMap{
|
||||
metrics: map[string]ExternalMetricData{},
|
||||
|
|
@ -78,18 +93,20 @@ func (d *externalMetricData) MetricName() string {
|
|||
return d.metricName
|
||||
}
|
||||
|
||||
func (d *externalMetricData) GenerateQuery(selector labels.Selector) (prom.Selector, error) {
|
||||
return d.generatedBy.QueryForExternalSeries(d.metricName, selector)
|
||||
func (d *externalMetricData) GenerateQuery(namespace string, selector labels.Selector) (prom.Selector, error) {
|
||||
return d.generatedBy.QueryForExternalSeries(namespace, d.metricName, selector)
|
||||
}
|
||||
|
||||
func (d *externalMetricData) ExportMetrics() []ExportedMetric {
|
||||
results := make([]ExportedMetric, 0)
|
||||
for namespace, labels := range d.namespacedData {
|
||||
results = append(results, ExportedMetric{
|
||||
Labels: labels,
|
||||
MetricName: d.metricName,
|
||||
Namespace: namespace,
|
||||
})
|
||||
for namespace, labelSets := range d.namespacedData {
|
||||
for _, labelSet := range labelSets {
|
||||
results = append(results, ExportedMetric{
|
||||
Labels: labelSet,
|
||||
MetricName: d.metricName,
|
||||
Namespace: namespace,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return results
|
||||
|
|
@ -99,10 +116,13 @@ func (d *externalMetricData) WithSeries(labels labels.Set) {
|
|||
d.WithNamespacedSeries("", labels)
|
||||
}
|
||||
|
||||
func (d *externalMetricData) WithNamespacedSeries(namespace string, labels labels.Set) {
|
||||
func (d *externalMetricData) WithNamespacedSeries(namespace string, seriesLabels labels.Set) {
|
||||
data, found := d.namespacedData[namespace]
|
||||
if !found {
|
||||
data = labels
|
||||
d.namespacedData[namespace] = data
|
||||
data = []labels.Set{}
|
||||
}
|
||||
|
||||
data = append(data, seriesLabels)
|
||||
d.namespacedData[namespace] = data
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue