Skip to content

Commit

Permalink
refactor: move regexp compilation outside function (#485)
Browse files Browse the repository at this point in the history
This improves the performances a bit.
  • Loading branch information
jooola committed Jul 16, 2024
1 parent 756f605 commit f7ac563
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions hcloud/internal/instrumentation/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ func registerOrReuse[C prometheus.Collector](registry prometheus.Registerer, col
return collector
}

var pathLabelRegexp = regexp.MustCompile("[^a-z/_]+")

func preparePathForLabel(path string) string {
path = strings.ToLower(path)

// replace all numbers and chars that are not a-z, / or _
reg := regexp.MustCompile("[^a-z/_]+")
path = reg.ReplaceAllString(path, "")
path = pathLabelRegexp.ReplaceAllString(path, "")

// replace all artifacts of number replacement (//)
path = strings.ReplaceAll(path, "//", "/")
Expand Down

0 comments on commit f7ac563

Please sign in to comment.