More implementation work in conversion.

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`.
This commit is contained in:
Tony Compton 2018-06-29 11:45:37 -04:00
parent d90012439c
commit fe0859aa5c
6 changed files with 99 additions and 30 deletions

View file

@ -4,12 +4,13 @@ import (
"fmt"
s "strings"
provider "github.com/directxman12/k8s-prometheus-adapter/pkg/custom-provider/metric-converter"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
)
type ExternalMetricQueryBuilder interface {
BuildPrometheusQuery(namespace string, metricName string, metricSelector labels.Selector) string
BuildPrometheusQuery(namespace string, metricName string, metricSelector labels.Selector, queryMetadata provider.QueryMetadata) string
}
type externalMetricQueryBuilder struct {
@ -19,13 +20,27 @@ func NewExternalMetricQueryBuilder() ExternalMetricQueryBuilder {
return &externalMetricQueryBuilder{}
}
func (p *externalMetricQueryBuilder) BuildPrometheusQuery(namespace string, metricName string, metricSelector labels.Selector) string {
namespaceSelector := p.makeLabelFilter("namespace", "=", namespace)
otherSelectors := p.convertSelectors(metricSelector)
func (p *externalMetricQueryBuilder) BuildPrometheusQuery(namespace string, metricName string, metricSelector labels.Selector, queryMetadata provider.QueryMetadata) string {
//TODO: At least for my Prometheus install, the "namespace" label doesn't seem to be
//directly applied to the time series. I'm using prometheus-operator. The grafana dashboards
//seem to query for the pods in a namespace from kube_pod_info and then apply pod-specific
//label filters. This might need some more thought. Disabling for now.
// namespaceSelector := p.makeLabelFilter("namespace", "=", namespace)
labelSelectors := p.convertSelectors(metricSelector)
joinedLabels := s.Join(labelSelectors, ", ")
finalTargets := append([]string{namespaceSelector}, otherSelectors...)
joinedLabels := s.Join(finalTargets, ", ")
return fmt.Sprintf("%s{%s}", metricName, joinedLabels)
//TODO: Both the aggregation method and window should probably be configurable.
//I don't think we can make assumptions about the nature of someone's metrics.
//I'm guessing this might be covered by the recently added advanced configuration
//code, but I haven't yet had an opportunity to dig into that and understand it.
//We'll leave this here for testing purposes for now.
//As reasonable defaults, maybe:
//rate(...) for counters
//avg_over_time(...) for gauges
//I'm guessing that SeriesRegistry might store the metric type, but I haven't looked yet.
aggregation := queryMetadata.Aggregation
window := queryMetadata.WindowInSeconds
return fmt.Sprintf("%s(%s{%s}[%ss])", aggregation, metricName, joinedLabels, window)
}
func (p *externalMetricQueryBuilder) makeLabelFilter(labelName string, operator string, targetValue string) string {
@ -72,7 +87,9 @@ func (p *externalMetricQueryBuilder) selectOperator(operator selection.Operator,
func (p *externalMetricQueryBuilder) selectRegexOperator(operator selection.Operator) string {
switch operator {
case selection.Equals:
case selection.In:
return "=~"
case selection.NotIn:
case selection.NotEquals:
return "!~"
}