Skip to content

Commit

Permalink
Move string trim outside loop and add test for totaltotaltotal
Browse files Browse the repository at this point in the history
  • Loading branch information
damemi committed Jul 18, 2023
1 parent 7bf2730 commit 2e7f4ac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
13 changes: 7 additions & 6 deletions exporter/collector/googlemanagedprometheus/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,25 @@ func getUnknownMetricName(points pmetric.NumberDataPointSlice, suffix, secondary
func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsDigit(r) },
)

untyped := false
newSuffix := suffix
for i := 0; i < points.Len(); i++ {
point := points.At(i)
if val, ok := point.Attributes().Get(GCPOpsAgentUntypedMetricKey); ok && val.AsString() == "true" {
untyped = true
// delete the special Ops Agent untyped attribute
point.Attributes().Remove(GCPOpsAgentUntypedMetricKey)

// de-normalize "_total" suffix for counters where not present on original metric name
if nameTokens[len(nameTokens)-1] != "total" && strings.HasSuffix(compliantName, "_total") {
compliantName = strings.TrimSuffix(compliantName, "_total")
}

newSuffix = "/unknown"
if len(secondarySuffix) > 0 {
newSuffix = newSuffix + ":" + secondarySuffix
}
// even though we have the suffix, keep looping to remove the attribute from other points, if any
}
}

// de-normalize "_total" suffix for counters where not present on original metric name
if untyped && nameTokens[len(nameTokens)-1] != "total" && strings.HasSuffix(compliantName, "_total") {
compliantName = strings.TrimSuffix(compliantName, "_total")
}
return compliantName + newSuffix
}
17 changes: 17 additions & 0 deletions exporter/collector/googlemanagedprometheus/naming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,23 @@ func TestGetMetricName(t *testing.T) {
},
expected: "bar/unknown:counter",
},
{
desc: "untyped sum with multiple data points and preexisting 'total' suffix and feature gate enabled and name normalization returns unknown:counter with _total",
baseName: "bar.total.total.total.total",
metric: func(m pmetric.Metric) {
//nolint:errcheck
featuregate.GlobalRegistry().Set(gcpUntypedDoubleExportGateKey, true)
//nolint:errcheck
featuregate.GlobalRegistry().Set("pkg.translator.prometheus.NormalizeName", true)
m.SetName("bar.total.total.total.total")
m.SetEmptySum()
m.Sum().SetIsMonotonic(true)
m.Sum().DataPoints().AppendEmpty().Attributes().PutStr(GCPOpsAgentUntypedMetricKey, "true")
m.Sum().DataPoints().AppendEmpty().Attributes().PutStr(GCPOpsAgentUntypedMetricKey, "true")
},
// prometheus name normalization removes all preexisting "total"s before appending its own
expected: "bar_total/unknown:counter",
},
{
desc: "normal sum without total and feature gate enabled + name normalization adds _total",
baseName: "foo",
Expand Down

0 comments on commit 2e7f4ac

Please sign in to comment.