From 99ac4276096124e857f7d70a2b1e39d947d4854b Mon Sep 17 00:00:00 2001 From: David Ashpole Date: Mon, 29 Apr 2024 20:01:52 +0000 Subject: [PATCH] address feedback --- .../googlemanagedprometheus/naming.go | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/exporter/collector/googlemanagedprometheus/naming.go b/exporter/collector/googlemanagedprometheus/naming.go index cd9f7c74..2f7f4b25 100644 --- a/exporter/collector/googlemanagedprometheus/naming.go +++ b/exporter/collector/googlemanagedprometheus/naming.go @@ -25,7 +25,6 @@ import ( // GetMetricName returns the metric name with GMP-specific suffixes. The. func (c Config) GetMetricName(baseName string, metric pmetric.Metric) (string, error) { - unknown := isUnknown(metric) // First, build a name that is compliant with prometheus conventions compliantName := prometheus.BuildCompliantName(metric, "", c.AddMetricSuffixes) // Second, ad the GMP-specific suffix @@ -35,9 +34,9 @@ func (c Config) GetMetricName(baseName string, metric pmetric.Metric) (string, e // Non-monotonic sums are converted to GCM gauges return compliantName + "/gauge", nil } - return getUnknownMetricName(metric.Sum().DataPoints(), unknown, "/counter", "counter", metric.Name(), compliantName), nil + return getUnknownMetricName(metric, metric.Sum().DataPoints(), "/counter", "counter", compliantName), nil case pmetric.MetricTypeGauge: - return getUnknownMetricName(metric.Gauge().DataPoints(), unknown, "/gauge", "", metric.Name(), compliantName), nil + return getUnknownMetricName(metric, metric.Gauge().DataPoints(), "/gauge", "", compliantName), nil case pmetric.MetricTypeSummary: // summaries are sent as the following series: // * Sum: prometheus.googleapis.com/_sum/summary:counter @@ -61,25 +60,23 @@ func (c Config) GetMetricName(baseName string, metric pmetric.Metric) (string, e // "/unknown" (eg, for Gauge) or "/unknown:{secondarySuffix}" (eg, "/unknown:counter" for Sum). // It also removes the "_total" suffix on an unknown counter, if this suffix was not present in // the original metric name before calling prometheus.BuildCompliantName(), which is hacky. -// -//nolint:revive -func getUnknownMetricName(points pmetric.NumberDataPointSlice, unknown bool, suffix, secondarySuffix, originalName, compliantName string) string { +func getUnknownMetricName(metric pmetric.Metric, points pmetric.NumberDataPointSlice, suffix, secondarySuffix, compliantName string) string { nameTokens := strings.FieldsFunc( - originalName, + metric.Name(), func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsDigit(r) }, ) newSuffix := suffix - if unknown { + if isUnknown(metric) { newSuffix = "/unknown" if len(secondarySuffix) > 0 { newSuffix = newSuffix + ":" + secondarySuffix } - } - // de-normalize "_total" suffix for counters where not present on original metric name - if unknown && nameTokens[len(nameTokens)-1] != "total" && strings.HasSuffix(compliantName, "_total") { - compliantName = strings.TrimSuffix(compliantName, "_total") + // 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") + } } return compliantName + newSuffix }