Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Apr 29, 2024
1 parent 6dd89bb commit 99ac427
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions exporter/collector/googlemanagedprometheus/naming.go
Expand Up @@ -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
Expand All @@ -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/<baseName>_sum/summary:counter
Expand All @@ -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
}

0 comments on commit 99ac427

Please sign in to comment.