Skip to content

Commit 325be36

Browse files
author
Nick Wei
committed
removed assumption that source_id would always be set in metric tags
[#162748537](https://www.pivotaltracker.com/story/show/162748537)
1 parent 6eca86e commit 325be36

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

containermetrics/stats_reporter.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -151,24 +151,25 @@ func (reporter *StatsReporter) calculateAndSendMetrics(
151151
}
152152

153153
applicationId := metricsConfig.Guid
154-
if len(metricsConfig.Tags) > 0 {
155-
applicationId = metricsConfig.Tags["source_id"]
154+
if sourceID, ok := metricsConfig.Tags["source_id"]; ok {
155+
applicationId = sourceID
156156
} else {
157157
metricsConfig.Tags["source_id"] = applicationId
158158
}
159159

160-
if metricsConfig.Tags["instance_id"] == "" {
161-
metricsConfig.Tags["instance_id"] = strconv.Itoa(metricsConfig.Index)
162-
}
163-
164-
_, err := strconv.Atoi(metricsConfig.Tags["instance_id"])
165-
if err != nil {
166-
logger.Error("failed-to-retrieve-instance-id", err, lager.Data{
167-
"metrics_guid": applicationId,
168-
"metrics_index": metricsConfig.Index,
169-
"tags": metricsConfig.Tags,
170-
})
171-
metricsConfig.Tags["instance_id"] = strconv.Itoa(metricsConfig.Index)
160+
index := strconv.Itoa(metricsConfig.Index)
161+
if _, ok := metricsConfig.Tags["instance_id"]; !ok {
162+
metricsConfig.Tags["instance_id"] = index
163+
} else {
164+
_, err := strconv.Atoi(metricsConfig.Tags["instance_id"]) // Atoi will error on empty string
165+
if err != nil {
166+
logger.Error("failed-to-retrieve-instance-id", err, lager.Data{
167+
"metrics_guid": applicationId,
168+
"metrics_index": metricsConfig.Index,
169+
"tags": metricsConfig.Tags,
170+
})
171+
metricsConfig.Tags["instance_id"] = index
172+
}
172173
}
173174

174175
if applicationId != "" {

containermetrics/stats_reporter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,11 +627,11 @@ var _ = Describe("StatsReporter", func() {
627627
})
628628
})
629629

630-
Context("when there is no source_id tag set in metrics config for a container", func() {
630+
Context("when there is no source_id tag set in metrics config for a container and the metricGuid is not set", func() {
631631
BeforeEach(func() {
632632
metricsMap := map[string]executor.Metrics{
633633
"container-0": {
634-
executor.MetricsConfig{Tags: map[string]string{"some-key": "some-value"}, Guid: "some-metric-guid"},
634+
executor.MetricsConfig{Tags: map[string]string{"some-key": "some-value"}},
635635
executor.ContainerMetrics{},
636636
},
637637
}

0 commit comments

Comments
 (0)