Skip to content

Commit

Permalink
Merge pull request #60 from kkellner/tags-to-labels
Browse files Browse the repository at this point in the history
Convert Tags to Labels
  • Loading branch information
frodenas authored Nov 24, 2020
2 parents 1d5e13c + c39caef commit 845ed37
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
27 changes: 15 additions & 12 deletions collectors/counter_events_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
26 changes: 22 additions & 4 deletions collectors/counter_events_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -115,6 +123,9 @@ var _ = Describe("CounterEventsCollector", func() {
Delta: proto.Uint64(counterEvent1Delta),
Total: proto.Uint64(counterEvent1Total),
},
Tags: map[string]string{
tag1Name: tag1Value,
},
},
)

Expand All @@ -132,6 +143,9 @@ var _ = Describe("CounterEventsCollector", func() {
Delta: proto.Uint64(counterEvent2Delta),
Total: proto.Uint64(counterEvent2Total),
},
Tags: map[string]string{
tag2Name: tag2Value,
},
},
)

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -199,6 +216,7 @@ var _ = Describe("CounterEventsCollector", func() {
boshJob,
boshIndex,
boshIP,
tag2Value,
)
})

Expand Down

0 comments on commit 845ed37

Please sign in to comment.