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

Sanitize metric type prefixes #319

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 36 additions & 1 deletion stackdriver_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"fmt"
"net/http"
"os"
"slices"
"strings"

"github.com/PuerkitoBio/rehttp"
Expand Down Expand Up @@ -303,7 +304,8 @@ func main() {

level.Info(logger).Log("msg", "Using Google Cloud Project IDs", "projectIDs", fmt.Sprintf("%v", projectIDs))

metricsTypePrefixes := strings.Split(*monitoringMetricsTypePrefixes, ",")
inputPrefixes := strings.Split(*monitoringMetricsTypePrefixes, ",")
metricsTypePrefixes := parseMetricTypePrefixes(inputPrefixes)
metricExtraFilters := parseMetricExtraFilters()

if *metricsPath == *stackdriverMetricsPath {
Expand Down Expand Up @@ -353,6 +355,39 @@ func main() {
}
}

func parseMetricTypePrefixes(inputPrefixes []string) (metricTypePrefixes []string) {
// only keep unique prefixes
uniqueKeys := make(map[string]bool)
uniquePrefixes := []string{}
for _, prefix := range inputPrefixes {
if _, ok := uniqueKeys[prefix]; !ok {
uniqueKeys[prefix] = true
uniquePrefixes = append(uniquePrefixes, prefix)
}
}

// drop prefixes that start with another existing prefix to avoid error:
// "collected metric xxx was collected before with the same name and label values"
slices.Sort(uniquePrefixes)
Comment on lines +359 to +371
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified with slices.Compact().

Suggested change
// only keep unique prefixes
uniqueKeys := make(map[string]bool)
uniquePrefixes := []string{}
for _, prefix := range inputPrefixes {
if _, ok := uniqueKeys[prefix]; !ok {
uniqueKeys[prefix] = true
uniquePrefixes = append(uniquePrefixes, prefix)
}
}
// drop prefixes that start with another existing prefix to avoid error:
// "collected metric xxx was collected before with the same name and label values"
slices.Sort(uniquePrefixes)
// Drop duplicate prefixes and that start with another existing prefix to avoid error:
// "collected metric xxx was collected before with the same name and label values"
slices.Sort(inputPrefixes)
uniquePrefixes := slices.Compact(inputPrefixes)

for i, prefix := range uniquePrefixes {
if i == 0 {
metricTypePrefixes = []string{prefix}
} else {
previousIndex := len(metricTypePrefixes) - 1

// current prefix starts with previous one
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// current prefix starts with previous one
// Drop current prefix starts with previous one.

if strings.HasPrefix(prefix, metricTypePrefixes[previousIndex]) {
// drop current prefix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// drop current prefix

continue
}

metricTypePrefixes = append(metricTypePrefixes, prefix)
}
}

return metricTypePrefixes
}

func parseMetricExtraFilters() []collectors.MetricFilter {
var extraFilters []collectors.MetricFilter
for _, ef := range *monitoringMetricsExtraFilter {
Expand Down
37 changes: 37 additions & 0 deletions stackdriver_exporter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2024 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import "testing"
import "reflect"

func TestParseMetricTypePrefixes(t *testing.T) {
inputPrefixes := []string{
"redis.googleapis.com/stats/memory/usage",
"loadbalancing.googleapis.com/https/request_count",
"loadbalancing.googleapis.com",
"redis.googleapis.com/stats/memory/usage_ratio",
"redis.googleapis.com/stats/memory/usage_ratio",
}
expectedOutputPrefixes := []string{
"loadbalancing.googleapis.com",
"redis.googleapis.com/stats/memory/usage",
}

outputPrefixes := parseMetricTypePrefixes(inputPrefixes)

if !reflect.DeepEqual(outputPrefixes, expectedOutputPrefixes) {
t.Errorf("Metric type prefix sanitization did not produce expected output. Expected:\n%s\nGot:\n%s", expectedOutputPrefixes, outputPrefixes)
}
}