From c39caef756699a8fa5c4c86c7509ae464d127243 Mon Sep 17 00:00:00 2001 From: Kurt Kellner Date: Mon, 16 Nov 2020 20:09:12 -0700 Subject: [PATCH] Convert Tags to Labels --- collectors/counter_events_collector.go | 27 ++++++++++++--------- collectors/counter_events_collector_test.go | 26 +++++++++++++++++--- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/collectors/counter_events_collector.go b/collectors/counter_events_collector.go index 10a3c3c7..f2f71533 100644 --- a/collectors/counter_events_collector.go +++ b/collectors/counter_events_collector.go @@ -40,20 +40,27 @@ func NewCounterEventsCollector( func (c CounterEventsCollector) Collect(ch chan<- prometheus.Metric) { for _, counterEvent := range c.metricsStore.GetCounterEvents() { metricName := utils.NormalizeName(counterEvent.Origin) + "_" + utils.NormalizeName(counterEvent.Name) + "_total" + + constLabels := []string{"origin", "bosh_deployment", "bosh_job_name", "bosh_job_id", "bosh_job_ip"} + labelValues := []string{counterEvent.Origin, counterEvent.Deployment, counterEvent.Job, counterEvent.Index, counterEvent.IP} + + for k, v := range counterEvent.Tags { + if (len(k) > 0) && (len(v) > 0) { + constLabels = append(constLabels, k) + labelValues = append(labelValues, v) + } + } + tcm, err := prometheus.NewConstMetric( prometheus.NewDesc( prometheus.BuildFQName(c.namespace, counter_events_subsystem, metricName), fmt.Sprintf("Cloud Foundry Firehose '%s' total counter event from '%s'.", utils.NormalizeNameDesc(counterEvent.Name), utils.NormalizeOriginDesc(counterEvent.Origin)), - []string{"origin", "bosh_deployment", "bosh_job_name", "bosh_job_id", "bosh_job_ip"}, + constLabels, prometheus.Labels{"environment": c.environment}, ), prometheus.CounterValue, float64(counterEvent.Total), - counterEvent.Origin, - counterEvent.Deployment, - counterEvent.Job, - counterEvent.Index, - counterEvent.IP, + labelValues..., ) if err != nil { log.Errorf("Counter Event `%s` from `%s` discarded: %s", counterEvent.Name, counterEvent.Origin, err) @@ -66,16 +73,12 @@ func (c CounterEventsCollector) Collect(ch chan<- prometheus.Metric) { prometheus.NewDesc( prometheus.BuildFQName(c.namespace, counter_events_subsystem, metricName), fmt.Sprintf("Cloud Foundry Firehose '%s' delta counter event from '%s'.", utils.NormalizeNameDesc(counterEvent.Name), utils.NormalizeOriginDesc(counterEvent.Origin)), - []string{"origin", "bosh_deployment", "bosh_job_name", "bosh_job_id", "bosh_job_ip"}, + constLabels, prometheus.Labels{"environment": c.environment}, ), prometheus.GaugeValue, float64(counterEvent.Delta), - counterEvent.Origin, - counterEvent.Deployment, - counterEvent.Job, - counterEvent.Index, - counterEvent.IP, + labelValues..., ) if err != nil { log.Errorf("Counter Event `%s` from `%s` discarded: %s", counterEvent.Name, counterEvent.Origin, err) diff --git a/collectors/counter_events_collector_test.go b/collectors/counter_events_collector_test.go index 0e05bd9b..f30dc111 100644 --- a/collectors/counter_events_collector_test.go +++ b/collectors/counter_events_collector_test.go @@ -98,6 +98,14 @@ var _ = Describe("CounterEventsCollector", func() { deltaCounterEvent1 prometheus.Metric totalCounterEvent2 prometheus.Metric deltaCounterEvent2 prometheus.Metric + + tag1Name = "tag1" + tag1NameNormalized = "tag1" + tag1Value = "fakeTag1" + + tag2Name = "tag2" + tag2NameNormalized = "tag2" + tag2Value = "fakeTag2" ) BeforeEach(func() { @@ -115,6 +123,9 @@ var _ = Describe("CounterEventsCollector", func() { Delta: proto.Uint64(counterEvent1Delta), Total: proto.Uint64(counterEvent1Total), }, + Tags: map[string]string{ + tag1Name: tag1Value, + }, }, ) @@ -132,6 +143,9 @@ var _ = Describe("CounterEventsCollector", func() { Delta: proto.Uint64(counterEvent2Delta), Total: proto.Uint64(counterEvent2Total), }, + Tags: map[string]string{ + tag2Name: tag2Value, + }, }, ) @@ -141,7 +155,7 @@ var _ = Describe("CounterEventsCollector", func() { prometheus.NewDesc( prometheus.BuildFQName(namespace, "counter_event", counterEvent1OriginNameNormalized+"_"+counterEvent1NameNormalized+"_total"), fmt.Sprintf("Cloud Foundry Firehose '%s' total counter event from '%s'.", counterEvent1DescNormalized, counterEvent1OriginDescNormalized), - []string{"origin", "bosh_deployment", "bosh_job_name", "bosh_job_id", "bosh_job_ip"}, + []string{"origin", "bosh_deployment", "bosh_job_name", "bosh_job_id", "bosh_job_ip", tag1NameNormalized}, prometheus.Labels{"environment": environment}, ), prometheus.CounterValue, @@ -151,13 +165,14 @@ var _ = Describe("CounterEventsCollector", func() { boshJob, boshIndex, boshIP, + tag1Value, ) deltaCounterEvent1 = prometheus.MustNewConstMetric( prometheus.NewDesc( prometheus.BuildFQName(namespace, "counter_event", counterEvent1OriginNameNormalized+"_"+counterEvent1NameNormalized+"_delta"), fmt.Sprintf("Cloud Foundry Firehose '%s' delta counter event from '%s'.", counterEvent1DescNormalized, counterEvent1OriginDescNormalized), - []string{"origin", "bosh_deployment", "bosh_job_name", "bosh_job_id", "bosh_job_ip"}, + []string{"origin", "bosh_deployment", "bosh_job_name", "bosh_job_id", "bosh_job_ip", tag1NameNormalized}, prometheus.Labels{"environment": environment}, ), prometheus.GaugeValue, @@ -167,13 +182,14 @@ var _ = Describe("CounterEventsCollector", func() { boshJob, boshIndex, boshIP, + tag1Value, ) totalCounterEvent2 = prometheus.MustNewConstMetric( prometheus.NewDesc( prometheus.BuildFQName(namespace, "counter_event", counterEvent2OriginNameNormalized+"_"+counterEvent2NameNormalized+"_total"), fmt.Sprintf("Cloud Foundry Firehose '%s' total counter event from '%s'.", counterEvent2DescNormalized, counterEvent2OriginDescNormalized), - []string{"origin", "bosh_deployment", "bosh_job_name", "bosh_job_id", "bosh_job_ip"}, + []string{"origin", "bosh_deployment", "bosh_job_name", "bosh_job_id", "bosh_job_ip", tag2NameNormalized}, prometheus.Labels{"environment": environment}, ), prometheus.CounterValue, @@ -183,13 +199,14 @@ var _ = Describe("CounterEventsCollector", func() { boshJob, boshIndex, boshIP, + tag2Value, ) deltaCounterEvent2 = prometheus.MustNewConstMetric( prometheus.NewDesc( prometheus.BuildFQName(namespace, "counter_event", counterEvent2OriginNameNormalized+"_"+counterEvent2NameNormalized+"_delta"), fmt.Sprintf("Cloud Foundry Firehose '%s' delta counter event from '%s'.", counterEvent2DescNormalized, counterEvent2OriginDescNormalized), - []string{"origin", "bosh_deployment", "bosh_job_name", "bosh_job_id", "bosh_job_ip"}, + []string{"origin", "bosh_deployment", "bosh_job_name", "bosh_job_id", "bosh_job_ip", tag2NameNormalized}, prometheus.Labels{"environment": environment}, ), prometheus.GaugeValue, @@ -199,6 +216,7 @@ var _ = Describe("CounterEventsCollector", func() { boshJob, boshIndex, boshIP, + tag2Value, ) })