mirror of
https://github.com/kubernetes-sigs/prometheus-adapter.git
synced 2026-04-06 17:57:51 +00:00
dependency-injected non-global metrics object
This commit is contained in:
parent
b394496f5c
commit
bda754ad2d
7 changed files with 162 additions and 78 deletions
|
|
@ -46,6 +46,8 @@ type externalSeriesRegistry struct {
|
|||
metrics []provider.ExternalMetricInfo
|
||||
// metricsInfo is a lookup from a metric to SeriesConverter for the sake of generating queries
|
||||
metricsInfo map[string]seriesInfo
|
||||
|
||||
serviceMetrics *metrics.ServiceMetrics
|
||||
}
|
||||
|
||||
type seriesInfo struct {
|
||||
|
|
@ -57,10 +59,11 @@ type seriesInfo struct {
|
|||
}
|
||||
|
||||
// NewExternalSeriesRegistry creates an ExternalSeriesRegistry driven by the data from the provided MetricLister.
|
||||
func NewExternalSeriesRegistry(lister MetricListerWithNotification) ExternalSeriesRegistry {
|
||||
func NewExternalSeriesRegistry(lister MetricListerWithNotification, serviceMetrics *metrics.ServiceMetrics) ExternalSeriesRegistry {
|
||||
var registry = externalSeriesRegistry{
|
||||
metrics: make([]provider.ExternalMetricInfo, 0),
|
||||
metricsInfo: map[string]seriesInfo{},
|
||||
metrics: make([]provider.ExternalMetricInfo, 0),
|
||||
metricsInfo: map[string]seriesInfo{},
|
||||
serviceMetrics: serviceMetrics,
|
||||
}
|
||||
|
||||
lister.AddNotificationReceiver(registry.filterAndStoreMetrics)
|
||||
|
|
@ -105,7 +108,9 @@ func (r *externalSeriesRegistry) filterAndStoreMetrics(result MetricUpdateResult
|
|||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
metrics.RegistryMetrics.WithLabelValues(r.name).Set(float64(len(apiMetricsCache)))
|
||||
if r.serviceMetrics != nil {
|
||||
r.serviceMetrics.RegistryMetrics.WithLabelValues(r.name).Set(float64(len(apiMetricsCache)))
|
||||
}
|
||||
r.metrics = apiMetricsCache
|
||||
r.metricsInfo = rawMetricsCache
|
||||
|
||||
|
|
|
|||
|
|
@ -40,6 +40,8 @@ type externalPrometheusProvider struct {
|
|||
metricConverter MetricConverter
|
||||
|
||||
seriesRegistry ExternalSeriesRegistry
|
||||
|
||||
serviceMetrics *metrics.ServiceMetrics
|
||||
}
|
||||
|
||||
func (p *externalPrometheusProvider) GetExternalMetric(namespace string, metricSelector labels.Selector, info provider.ExternalMetricInfo) (*external_metrics.ExternalMetricValueList, error) {
|
||||
|
|
@ -47,12 +49,16 @@ func (p *externalPrometheusProvider) GetExternalMetric(namespace string, metricS
|
|||
|
||||
if err != nil {
|
||||
klog.Errorf("unable to generate a query for the metric: %v", err)
|
||||
metrics.Errors.WithLabelValues("internal").Inc()
|
||||
if p.serviceMetrics != nil {
|
||||
p.serviceMetrics.Errors.WithLabelValues("internal").Inc()
|
||||
}
|
||||
return nil, apierr.NewInternalError(fmt.Errorf("unable to fetch metrics"))
|
||||
}
|
||||
|
||||
if !found {
|
||||
metrics.Errors.WithLabelValues("not_found").Inc()
|
||||
if p.serviceMetrics != nil {
|
||||
p.serviceMetrics.Errors.WithLabelValues("not_found").Inc()
|
||||
}
|
||||
return nil, provider.NewMetricNotFoundError(p.selectGroupResource(namespace), info.Metric)
|
||||
}
|
||||
// Here is where we're making the query, need to be before here xD
|
||||
|
|
@ -61,7 +67,9 @@ 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
|
||||
metrics.Errors.WithLabelValues("internal").Inc()
|
||||
if p.serviceMetrics != nil {
|
||||
p.serviceMetrics.Errors.WithLabelValues("internal").Inc()
|
||||
}
|
||||
return nil, apierr.NewInternalError(fmt.Errorf("unable to fetch metrics"))
|
||||
}
|
||||
return p.metricConverter.Convert(info, queryResults)
|
||||
|
|
@ -83,14 +91,15 @@ func (p *externalPrometheusProvider) selectGroupResource(namespace string) schem
|
|||
}
|
||||
|
||||
// NewExternalPrometheusProvider creates an ExternalMetricsProvider capable of responding to Kubernetes requests for external metric data
|
||||
func NewExternalPrometheusProvider(promClient prom.Client, namers []naming.MetricNamer, updateInterval time.Duration) (provider.ExternalMetricsProvider, Runnable) {
|
||||
func NewExternalPrometheusProvider(promClient prom.Client, namers []naming.MetricNamer, updateInterval time.Duration, serviceMetrics *metrics.ServiceMetrics) (provider.ExternalMetricsProvider, Runnable) {
|
||||
metricConverter := NewMetricConverter()
|
||||
basicLister := NewBasicMetricLister(promClient, namers, updateInterval)
|
||||
periodicLister, _ := NewPeriodicMetricLister(basicLister, updateInterval)
|
||||
seriesRegistry := NewExternalSeriesRegistry(periodicLister)
|
||||
seriesRegistry := NewExternalSeriesRegistry(periodicLister, serviceMetrics)
|
||||
return &externalPrometheusProvider{
|
||||
promClient: promClient,
|
||||
seriesRegistry: seriesRegistry,
|
||||
metricConverter: metricConverter,
|
||||
serviceMetrics: serviceMetrics,
|
||||
}, periodicLister
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue