mirror of https://github.com/grafana/loki
Introduce `overrides-exporter` module to Loki (#4520)
* Introduce overrides-exporter module from cortex into loki; add some test configs for local dev * Fix panic by adding server dependency to overrides-exporter module * Add tests for limits exporter * Remove dummy configs for testing * Add more limits to export * Add more limits; move mockTenantLimits to exporter_test.go * Introduce overrides-exporter module from cortex into loki; add some test configs for local dev * Fix panic by adding server dependency to overrides-exporter module * Add tests for limits exporter * Remove dummy configs for testing * Add more limits to export * Add more limits; move mockTenantLimits to exporter_test.go * Use reflection; add more limits to export * Fix import to satisfy linter * refactors TenantLimits for clarity & efficiency. Also fixes tenantLimits nil check * Fix breaking test with nil check in runtime_config Co-authored-by: Owen Diehl <ow.diehl@gmail.com>pull/4531/head
parent
e3726a19ca
commit
ca67292b54
@ -0,0 +1,60 @@ |
||||
package validation |
||||
|
||||
import ( |
||||
"reflect" |
||||
"time" |
||||
|
||||
"github.com/prometheus/client_golang/prometheus" |
||||
"github.com/prometheus/common/model" |
||||
|
||||
"github.com/grafana/loki/pkg/util/flagext" |
||||
) |
||||
|
||||
type OverridesExporter struct { |
||||
tenantLimits TenantLimits |
||||
description *prometheus.Desc |
||||
} |
||||
|
||||
// TODO(jordanrushing): break out overrides from defaults?
|
||||
func NewOverridesExporter(tenantLimits TenantLimits) *OverridesExporter { |
||||
return &OverridesExporter{ |
||||
tenantLimits: tenantLimits, |
||||
description: prometheus.NewDesc( |
||||
"loki_overrides", |
||||
"Resource limit overrides applied to tenants", |
||||
[]string{"limit_name", "user"}, |
||||
nil, |
||||
), |
||||
} |
||||
} |
||||
|
||||
func (oe *OverridesExporter) Describe(ch chan<- *prometheus.Desc) { |
||||
ch <- oe.description |
||||
} |
||||
|
||||
func (oe *OverridesExporter) Collect(ch chan<- prometheus.Metric) { |
||||
var metricValue float64 |
||||
var metricLabelValue string |
||||
var rv reflect.Value |
||||
|
||||
for tenant, limits := range oe.tenantLimits.AllByUserID() { |
||||
rv = reflect.ValueOf(limits).Elem() |
||||
for i := 0; i < rv.NumField(); i++ { |
||||
switch rv.Field(i).Interface().(type) { |
||||
case int, time.Duration: |
||||
metricValue = float64(rv.Field(i).Int()) |
||||
case model.Duration: |
||||
metricValue = float64(rv.Field(i).Interface().(model.Duration)) |
||||
case flagext.ByteSize: |
||||
metricValue = float64(rv.Field(i).Uint()) |
||||
case float64: |
||||
metricValue = rv.Field(i).Float() |
||||
default: |
||||
continue |
||||
} |
||||
metricLabelValue = rv.Type().Field(i).Tag.Get("yaml") |
||||
|
||||
ch <- prometheus.MustNewConstMetric(oe.description, prometheus.GaugeValue, metricValue, metricLabelValue, tenant) |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,41 @@ |
||||
package validation |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/prometheus/client_golang/prometheus/testutil" |
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
type mockTenantLimits struct { |
||||
limits map[string]*Limits |
||||
} |
||||
|
||||
func newMockTenantLimits(limits map[string]*Limits) *mockTenantLimits { |
||||
return &mockTenantLimits{ |
||||
limits: limits, |
||||
} |
||||
} |
||||
|
||||
func (l *mockTenantLimits) TenantLimits(userID string) *Limits { |
||||
return l.limits[userID] |
||||
} |
||||
|
||||
func (l *mockTenantLimits) AllByUserID() map[string]*Limits { return l.limits } |
||||
|
||||
func TestOverridesExporter_noConfig(t *testing.T) { |
||||
exporter := NewOverridesExporter(newMockTenantLimits(nil)) |
||||
count := testutil.CollectAndCount(exporter, "loki_overrides") |
||||
assert.Equal(t, 0, count) |
||||
} |
||||
|
||||
func TestOverridesExporter_withConfig(t *testing.T) { |
||||
tenantLimits := map[string]*Limits{ |
||||
"tenant-a": { |
||||
MaxQueriersPerTenant: 5, |
||||
}, |
||||
} |
||||
exporter := NewOverridesExporter(newMockTenantLimits(tenantLimits)) |
||||
count := testutil.CollectAndCount(exporter, "loki_overrides") |
||||
assert.Greater(t, count, 0) |
||||
} |
||||
Loading…
Reference in new issue