instrument k8s-prometheus-adapter to expose prometheus metrics

fixes #218
This commit is contained in:
pdbogen 2019-07-19 16:12:29 -07:00
parent 28a807aa9f
commit cbba53e16b
11 changed files with 308 additions and 14 deletions

View file

@ -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

View file

@ -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)
}