feat: implement configurable label filtering with tests
This commit is contained in:
parent
e0a0eb256f
commit
7a18c00146
|
|
@ -415,6 +415,14 @@ func installMetrics(config v1alpha1.MetricsConfig, reg *prometheus.Registry, log
|
||||||
return metrics
|
return metrics
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func configuredLabelsOnly(allLabels prometheus.Labels, configuredLabels []string) prometheus.Labels {
|
||||||
|
labels := make(prometheus.Labels, len(configuredLabels))
|
||||||
|
for _, label := range configuredLabels {
|
||||||
|
labels[label] = allLabels[label]
|
||||||
|
}
|
||||||
|
return labels
|
||||||
|
}
|
||||||
|
|
||||||
func (e *exporter) ListenAndServe(ctx context.Context) error {
|
func (e *exporter) ListenAndServe(ctx context.Context) error {
|
||||||
e.logger.Info("starting metrics server", "addr", e.srv.Addr)
|
e.logger.Info("starting metrics server", "addr", e.srv.Addr)
|
||||||
go func() {
|
go func() {
|
||||||
|
|
@ -432,10 +440,7 @@ func (e *exporter) setGauge(name string, allLabels prometheus.Labels, val float6
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
labels := make(prometheus.Labels, len(m.config.Labels))
|
labels := configuredLabelsOnly(allLabels, m.config.Labels)
|
||||||
for _, label := range m.config.Labels {
|
|
||||||
labels[label] = allLabels[label]
|
|
||||||
}
|
|
||||||
m.gauge.With(labels).Set(val)
|
m.gauge.With(labels).Set(val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -444,10 +449,7 @@ func (e *exporter) incCounter(name string, allLabels prometheus.Labels) {
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
labels := make(prometheus.Labels, len(m.config.Labels))
|
labels := configuredLabelsOnly(allLabels, m.config.Labels)
|
||||||
for _, label := range m.config.Labels {
|
|
||||||
labels[label] = allLabels[label]
|
|
||||||
}
|
|
||||||
m.counter.With(labels).Inc()
|
m.counter.With(labels).Inc()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -456,10 +458,7 @@ func (e *exporter) observeHistogram(name string, allLabels prometheus.Labels, va
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
labels := make(prometheus.Labels, len(m.config.Labels))
|
labels := configuredLabelsOnly(allLabels, m.config.Labels)
|
||||||
for _, label := range m.config.Labels {
|
|
||||||
labels[label] = allLabels[label]
|
|
||||||
}
|
|
||||||
m.histogram.With(labels).Observe(val)
|
m.histogram.With(labels).Observe(val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -265,6 +265,26 @@ func TestExporterConfigDefaults(t *testing.T) {
|
||||||
assert.Equal(t, want, config)
|
assert.Equal(t, want, config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfiguredLabelsOnly(t *testing.T) {
|
||||||
|
allLabels := prometheus.Labels{
|
||||||
|
labelKeyRepository: "test-repo",
|
||||||
|
labelKeyOrganization: "test-org",
|
||||||
|
labelKeyJobName: "test-job",
|
||||||
|
}
|
||||||
|
|
||||||
|
configuredLabels := []string{labelKeyRepository, labelKeyJobName}
|
||||||
|
|
||||||
|
got := configuredLabelsOnly(allLabels, configuredLabels)
|
||||||
|
|
||||||
|
want := prometheus.Labels{
|
||||||
|
labelKeyRepository: "test-repo",
|
||||||
|
labelKeyJobName: "test-job",
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, want, got)
|
||||||
|
assert.NotContains(t, got, labelKeyOrganization)
|
||||||
|
}
|
||||||
|
|
||||||
func TestCompletedJobAllLabels(t *testing.T) {
|
func TestCompletedJobAllLabels(t *testing.T) {
|
||||||
config := ExporterConfig{
|
config := ExporterConfig{
|
||||||
ScaleSetName: "test-scale-set",
|
ScaleSetName: "test-scale-set",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue