Skip to content

Commit

Permalink
fix: only drop _total for untyped sums
Browse files Browse the repository at this point in the history
  • Loading branch information
damemi committed Jul 18, 2023
1 parent b6fc3f0 commit 7bf2730
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
10 changes: 6 additions & 4 deletions exporter/collector/googlemanagedprometheus/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,23 @@ func getUnknownMetricName(points pmetric.NumberDataPointSlice, suffix, secondary
return compliantName + suffix
}

// de-normalize "_total" suffix for counters where not present on original metric name
nameTokens := strings.FieldsFunc(
originalName,
func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsDigit(r) },
)
if nameTokens[len(nameTokens)-1] != "total" && strings.HasSuffix(compliantName, "_total") {
compliantName = strings.TrimSuffix(compliantName, "_total")
}

newSuffix := suffix
for i := 0; i < points.Len(); i++ {
point := points.At(i)
if val, ok := point.Attributes().Get(GCPOpsAgentUntypedMetricKey); ok && val.AsString() == "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
Expand Down
18 changes: 16 additions & 2 deletions exporter/collector/googlemanagedprometheus/naming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestGetMetricName(t *testing.T) {
expectErr bool
}{
{
desc: "sum without total gets added",
desc: "sum without total is unaffected",
baseName: "foo",
metric: func(m pmetric.Metric) {
m.SetName("foo")
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestGetMetricName(t *testing.T) {
expected: "bar/unknown:counter",
},
{
desc: "untyped sum with feature gate enabled + name normalization returns unknown:counter",
desc: "untyped sum with feature gate enabled + name normalization returns unknown:counter without _total",
baseName: "bar",
metric: func(m pmetric.Metric) {
//nolint:errcheck
Expand All @@ -205,6 +205,20 @@ func TestGetMetricName(t *testing.T) {
},
expected: "bar/unknown:counter",
},
{
desc: "normal sum without total and feature gate enabled + name normalization adds _total",
baseName: "foo",
metric: func(m pmetric.Metric) {
//nolint:errcheck
featuregate.GlobalRegistry().Set(gcpUntypedDoubleExportGateKey, true)
//nolint:errcheck
featuregate.GlobalRegistry().Set("pkg.translator.prometheus.NormalizeName", true)
m.SetName("foo")
sum := m.SetEmptySum()
sum.SetIsMonotonic(true)
},
expected: "foo_total/counter",
},
} {
t.Run(tc.desc, func(t *testing.T) {
assert.True(t, gate.IsEnabled())
Expand Down

0 comments on commit 7bf2730

Please sign in to comment.