mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-06 17:57:51 +00:00
* 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.
63 lines
1.9 KiB
Go
63 lines
1.9 KiB
Go
package provider
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
|
|
prom "github.com/directxman12/k8s-prometheus-adapter/pkg/client"
|
|
"github.com/directxman12/k8s-prometheus-adapter/pkg/config"
|
|
)
|
|
|
|
//MetricNameConverter provides functions for naming custom metrics from Promethes series.
|
|
type MetricNameConverter interface {
|
|
GetMetricNameForSeries(series prom.Series) (string, error)
|
|
}
|
|
|
|
type metricNameConverter struct {
|
|
nameMatches *regexp.Regexp
|
|
nameAs string
|
|
}
|
|
|
|
//NewMetricNameConverter creates a MetricNameConverter capable of translating Prometheus series names
|
|
//into custom metric names.
|
|
func NewMetricNameConverter(mapping config.NameMapping) (MetricNameConverter, error) {
|
|
var nameMatches *regexp.Regexp
|
|
var err error
|
|
if mapping.Matches != "" {
|
|
nameMatches, err = regexp.Compile(mapping.Matches)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unable to compile series name match expression %q: %v", mapping.Matches, err)
|
|
}
|
|
} else {
|
|
// this will always succeed
|
|
nameMatches = regexp.MustCompile(".*")
|
|
}
|
|
nameAs := mapping.As
|
|
if nameAs == "" {
|
|
// check if we have an obvious default
|
|
subexpNames := nameMatches.SubexpNames()
|
|
if len(subexpNames) == 1 {
|
|
// no capture groups, use the whole thing
|
|
nameAs = "$0"
|
|
} else if len(subexpNames) == 2 {
|
|
// one capture group, use that
|
|
nameAs = "$1"
|
|
} else {
|
|
return nil, fmt.Errorf("must specify an 'as' value for name matcher %q", mapping.Matches)
|
|
}
|
|
}
|
|
|
|
return &metricNameConverter{
|
|
nameMatches: nameMatches,
|
|
nameAs: nameAs,
|
|
}, nil
|
|
}
|
|
|
|
func (c *metricNameConverter) GetMetricNameForSeries(series prom.Series) (string, error) {
|
|
matches := c.nameMatches.FindStringSubmatchIndex(series.Name)
|
|
if matches == nil {
|
|
return "", fmt.Errorf("series name %q did not match expected pattern %q", series.Name, c.nameMatches.String())
|
|
}
|
|
outNameBytes := c.nameMatches.ExpandString(nil, c.nameAs, series.Name, matches)
|
|
return string(outNameBytes), nil
|
|
}
|