mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-06 09:47:54 +00:00
instrument k8s-prometheus-adapter to expose prometheus metrics
fixes #218
This commit is contained in:
parent
28a807aa9f
commit
cbba53e16b
11 changed files with 308 additions and 14 deletions
|
|
@ -2,10 +2,11 @@ package config
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/directxman12/k8s-prometheus-adapter/pkg/metrics"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// FromFile loads the configuration from a particular file.
|
||||
|
|
@ -28,5 +29,7 @@ func FromYAML(contents []byte) (*MetricsDiscoveryConfig, error) {
|
|||
if err := yaml.UnmarshalStrict(contents, &cfg); err != nil {
|
||||
return nil, fmt.Errorf("unable to parse metrics discovery config: %v", err)
|
||||
}
|
||||
metrics.Rules.WithLabelValues("normal").Set(float64(len(cfg.Rules)))
|
||||
metrics.Rules.WithLabelValues("external").Set(float64(len(cfg.ExternalRules)))
|
||||
return &cfg, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,12 +19,13 @@ package provider
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/directxman12/k8s-prometheus-adapter/pkg/errors"
|
||||
"github.com/directxman12/k8s-prometheus-adapter/pkg/metrics"
|
||||
"time"
|
||||
|
||||
"github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider"
|
||||
"github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider/helpers"
|
||||
pmodel "github.com/prometheus/common/model"
|
||||
apierr "k8s.io/apimachinery/pkg/api/errors"
|
||||
apimeta "k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
|
@ -64,6 +65,7 @@ func NewPrometheusProvider(mapper apimeta.RESTMapper, kubeClient dynamic.Interfa
|
|||
namers: namers,
|
||||
|
||||
SeriesRegistry: &basicSeriesRegistry{
|
||||
name: "custom",
|
||||
mapper: mapper,
|
||||
},
|
||||
}
|
||||
|
|
@ -97,7 +99,7 @@ func (p *prometheusProvider) metricFor(value pmodel.SampleValue, name types.Name
|
|||
func (p *prometheusProvider) metricsFor(valueSet pmodel.Vector, info provider.CustomMetricInfo, namespace string, names []string) (*custom_metrics.MetricValueList, error) {
|
||||
values, found := p.MatchValuesToNames(info, valueSet)
|
||||
if !found {
|
||||
return nil, provider.NewMetricNotFoundError(info.GroupResource, info.Metric)
|
||||
return nil, errors.NewMetricNotFoundError(info.GroupResource, info.Metric)
|
||||
}
|
||||
res := []custom_metrics.MetricValue{}
|
||||
|
||||
|
|
@ -121,7 +123,7 @@ func (p *prometheusProvider) metricsFor(valueSet pmodel.Vector, info provider.Cu
|
|||
func (p *prometheusProvider) buildQuery(info provider.CustomMetricInfo, namespace string, names ...string) (pmodel.Vector, error) {
|
||||
query, found := p.QueryForMetric(info, namespace, names...)
|
||||
if !found {
|
||||
return nil, provider.NewMetricNotFoundError(info.GroupResource, info.Metric)
|
||||
return nil, errors.NewMetricNotFoundError(info.GroupResource, info.Metric)
|
||||
}
|
||||
|
||||
// TODO: use an actual context
|
||||
|
|
@ -129,12 +131,12 @@ func (p *prometheusProvider) buildQuery(info provider.CustomMetricInfo, namespac
|
|||
if err != nil {
|
||||
klog.Errorf("unable to fetch metrics from prometheus: %v", err)
|
||||
// don't leak implementation details to the user
|
||||
return nil, apierr.NewInternalError(fmt.Errorf("unable to fetch metrics"))
|
||||
return nil, errors.NewInternalError(fmt.Errorf("unable to fetch metrics"))
|
||||
}
|
||||
|
||||
if queryResults.Type != pmodel.ValVector {
|
||||
klog.Errorf("unexpected results from prometheus: expected %s, got %s on results %v", pmodel.ValVector, queryResults.Type, queryResults)
|
||||
return nil, apierr.NewInternalError(fmt.Errorf("unable to fetch metrics"))
|
||||
return nil, errors.NewInternalError(fmt.Errorf("unable to fetch metrics"))
|
||||
}
|
||||
|
||||
return *queryResults.Vector, nil
|
||||
|
|
@ -154,7 +156,7 @@ func (p *prometheusProvider) GetMetricByName(name types.NamespacedName, info pro
|
|||
|
||||
namedValues, found := p.MatchValuesToNames(info, queryResults)
|
||||
if !found {
|
||||
return nil, provider.NewMetricNotFoundError(info.GroupResource, info.Metric)
|
||||
return nil, errors.NewMetricNotFoundError(info.GroupResource, info.Metric)
|
||||
}
|
||||
|
||||
if len(namedValues) > 1 {
|
||||
|
|
@ -177,7 +179,7 @@ func (p *prometheusProvider) GetMetricBySelector(namespace string, selector labe
|
|||
if err != nil {
|
||||
klog.Errorf("unable to list matching resource names: %v", err)
|
||||
// don't leak implementation details to the user
|
||||
return nil, apierr.NewInternalError(fmt.Errorf("unable to list matching resources"))
|
||||
return nil, errors.NewInternalError(fmt.Errorf("unable to list matching resources"))
|
||||
}
|
||||
|
||||
// construct the actual query
|
||||
|
|
@ -252,6 +254,7 @@ func (l *cachingMetricsLister) updateMetrics() error {
|
|||
// iterate through, blocking until we've got all results
|
||||
for range l.namers {
|
||||
if err := <-errs; err != nil {
|
||||
metrics.PrometheusUp.Set(0)
|
||||
return fmt.Errorf("unable to update list of all metrics: %v", err)
|
||||
}
|
||||
if ss := <-selectorSeriesChan; ss.series != nil {
|
||||
|
|
@ -259,6 +262,7 @@ func (l *cachingMetricsLister) updateMetrics() error {
|
|||
}
|
||||
}
|
||||
close(errs)
|
||||
metrics.PrometheusUp.Set(1)
|
||||
|
||||
newSeries := make([][]prom.Series, len(l.namers))
|
||||
for i, namer := range l.namers {
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ package provider
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/directxman12/k8s-prometheus-adapter/pkg/metrics"
|
||||
"sync"
|
||||
|
||||
"github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider"
|
||||
|
|
@ -66,6 +67,9 @@ type seriesInfo struct {
|
|||
|
||||
// overridableSeriesRegistry is a basic SeriesRegistry
|
||||
type basicSeriesRegistry struct {
|
||||
// registry name is used for metrics &c
|
||||
name string
|
||||
|
||||
mu sync.RWMutex
|
||||
|
||||
// info maps metric info to information about the corresponding series
|
||||
|
|
@ -122,6 +126,7 @@ func (r *basicSeriesRegistry) SetSeries(newSeriesSlices [][]prom.Series, namers
|
|||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
metrics.RegistryMetrics.WithLabelValues(r.name).Set(float64(len(newMetrics)))
|
||||
r.info = newInfo
|
||||
r.metrics = newMetrics
|
||||
|
||||
|
|
|
|||
18
pkg/errors/errors.go
Normal file
18
pkg/errors/errors.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package errors
|
||||
|
||||
import (
|
||||
"github.com/directxman12/k8s-prometheus-adapter/pkg/metrics"
|
||||
"github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider"
|
||||
apierr "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
func NewMetricNotFoundError(resource schema.GroupResource, metricName string) error {
|
||||
metrics.Errors.WithLabelValues("not_found").Inc()
|
||||
return provider.NewMetricNotFoundError(resource, metricName)
|
||||
}
|
||||
|
||||
func NewInternalError(err error) *apierr.StatusError {
|
||||
metrics.Errors.WithLabelValues("internal").Inc()
|
||||
return apierr.NewInternalError(err)
|
||||
}
|
||||
|
|
@ -14,6 +14,7 @@ limitations under the License.
|
|||
package provider
|
||||
|
||||
import (
|
||||
"github.com/directxman12/k8s-prometheus-adapter/pkg/metrics"
|
||||
"sync"
|
||||
|
||||
"github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider"
|
||||
|
|
@ -34,6 +35,9 @@ type ExternalSeriesRegistry interface {
|
|||
|
||||
// overridableSeriesRegistry is a basic SeriesRegistry
|
||||
type externalSeriesRegistry struct {
|
||||
// registry name is used for metrics &c
|
||||
name string
|
||||
|
||||
// We lock when reading/writing metrics, and metricsInfo to prevent inconsistencies.
|
||||
mu sync.RWMutex
|
||||
|
||||
|
|
@ -100,6 +104,7 @@ func (r *externalSeriesRegistry) filterAndStoreMetrics(result MetricUpdateResult
|
|||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
metrics.RegistryMetrics.WithLabelValues(r.name).Set(float64(len(apiMetricsCache)))
|
||||
r.metrics = apiMetricsCache
|
||||
r.metricsInfo = rawMetricsCache
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ package provider
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/directxman12/k8s-prometheus-adapter/pkg/errors"
|
||||
"time"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
|
|
@ -25,7 +26,6 @@ import (
|
|||
|
||||
"github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider"
|
||||
|
||||
apierr "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/metrics/pkg/apis/external_metrics"
|
||||
|
||||
|
|
@ -45,11 +45,11 @@ func (p *externalPrometheusProvider) GetExternalMetric(namespace string, metricS
|
|||
|
||||
if err != nil {
|
||||
klog.Errorf("unable to generate a query for the metric: %v", err)
|
||||
return nil, apierr.NewInternalError(fmt.Errorf("unable to fetch metrics"))
|
||||
return nil, errors.NewInternalError(fmt.Errorf("unable to fetch metrics"))
|
||||
}
|
||||
|
||||
if !found {
|
||||
return nil, provider.NewMetricNotFoundError(p.selectGroupResource(namespace), info.Metric)
|
||||
return nil, errors.NewMetricNotFoundError(p.selectGroupResource(namespace), info.Metric)
|
||||
}
|
||||
// Here is where we're making the query, need to be before here xD
|
||||
queryResults, err := p.promClient.Query(context.TODO(), pmodel.Now(), selector)
|
||||
|
|
@ -57,7 +57,7 @@ func (p *externalPrometheusProvider) GetExternalMetric(namespace string, metricS
|
|||
if err != nil {
|
||||
klog.Errorf("unable to fetch metrics from prometheus: %v", err)
|
||||
// don't leak implementation details to the user
|
||||
return nil, apierr.NewInternalError(fmt.Errorf("unable to fetch metrics"))
|
||||
return nil, errors.NewInternalError(fmt.Errorf("unable to fetch metrics"))
|
||||
}
|
||||
return p.metricConverter.Convert(info, queryResults)
|
||||
}
|
||||
|
|
|
|||
38
pkg/metrics/metrics.go
Normal file
38
pkg/metrics/metrics.go
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package metrics
|
||||
|
||||
import "github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
const MetricsNamespace = "adapter"
|
||||
|
||||
var PrometheusUp = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Namespace: MetricsNamespace,
|
||||
Name: "prometheus_up",
|
||||
Help: "1 when adapter is able to reach prometheus, 0 otherwise",
|
||||
})
|
||||
|
||||
var RegistryMetrics = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Namespace: MetricsNamespace,
|
||||
Name: "registry_metrics",
|
||||
Help: "number of metrics entries in cache registry",
|
||||
}, []string{"registry"})
|
||||
|
||||
var Errors = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: MetricsNamespace,
|
||||
Name: "errors_total",
|
||||
Help: "number of errors served",
|
||||
}, []string{"type"})
|
||||
|
||||
var Rules = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Namespace: MetricsNamespace,
|
||||
Name: "roles",
|
||||
Help: "number of configured rules",
|
||||
}, []string{"type"})
|
||||
|
||||
func init() {
|
||||
prometheus.MustRegister(
|
||||
PrometheusUp,
|
||||
RegistryMetrics,
|
||||
Errors,
|
||||
Rules,
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue