Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only drop _total for untyped sums #680

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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