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

Run ListMetrics calls with goroutines. #53

Draft
wants to merge 13 commits into
base: live
Choose a base branch
from
Draft
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
Prev Previous commit
Next Next commit
Move replacer and splitRegexp to package level (#2)
  • Loading branch information
kgeckhart authored Oct 7, 2021
commit cac7ca47a608c15e1367887ce807a89ea7ce73ad
35 changes: 18 additions & 17 deletions pkg/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ var (
})
)

var replacer = strings.NewReplacer(
" ", "_",
",", "_",
"\t", "_",
"/", "_",
"\\", "_",
".", "_",
"-", "_",
":", "_",
"=", "_",
"“", "_",
"@", "_",
"<", "_",
">", "_",
"%", "_percent",
)
var splitRegexp = regexp.MustCompile(`([a-z0-9])([A-Z])`)

type PrometheusMetric struct {
name *string
labels map[string]string
Expand Down Expand Up @@ -141,26 +159,9 @@ func promStringTag(text string, labelsSnakeCase bool) string {
}

func sanitize(text string) string {
replacer := strings.NewReplacer(
" ", "_",
",", "_",
"\t", "_",
"/", "_",
"\\", "_",
".", "_",
"-", "_",
":", "_",
"=", "_",
"“", "_",
"@", "_",
"<", "_",
">", "_",
"%", "_percent",
)
return replacer.Replace(text)
}

func splitString(text string) string {
splitRegexp := regexp.MustCompile(`([a-z0-9])([A-Z])`)
return splitRegexp.ReplaceAllString(text, `$1.$2`)
}
55 changes: 55 additions & 0 deletions pkg/prometheus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package exporter

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSplitString(t *testing.T) {
var testCases = []struct {
input string
output string
}{
{
input: "GlobalTopicCount",
output: "Global.Topic.Count",
},
{
input: "CPUUtilization",
output: "CPUUtilization",
},
{
input: "StatusCheckFailed_Instance",
output: "Status.Check.Failed_Instance",
},
}

for _, tc := range testCases {
assert.Equal(t, tc.output, splitString(tc.input))
}
}

func TestSanitize(t *testing.T) {
var testCases = []struct {
input string
output string
}{
{
input: "Global.Topic.Count",
output: "Global_Topic_Count",
},
{
input: "Status.Check.Failed_Instance",
output: "Status_Check_Failed_Instance",
},
{
input: "IHaveA%Sign",
output: "IHaveA_percentSign",
},
}

for _, tc := range testCases {
assert.Equal(t, tc.output, sanitize(tc.input))
}
}