strconv.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2013 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package strutil
  14. import (
  15. "fmt"
  16. "net/url"
  17. "regexp"
  18. )
  19. var (
  20. invalidLabelCharRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
  21. )
  22. // TableLinkForExpression creates an escaped relative link to the table view of
  23. // the provided expression.
  24. func TableLinkForExpression(expr string) string {
  25. escapedExpression := url.QueryEscape(expr)
  26. return fmt.Sprintf("/graph?g0.expr=%s&g0.tab=1", escapedExpression)
  27. }
  28. // GraphLinkForExpression creates an escaped relative link to the graph view of
  29. // the provided expression.
  30. func GraphLinkForExpression(expr string) string {
  31. escapedExpression := url.QueryEscape(expr)
  32. return fmt.Sprintf("/graph?g0.expr=%s&g0.tab=0", escapedExpression)
  33. }
  34. // SanitizeLabelName replaces anything that doesn't match
  35. // client_label.LabelNameRE with an underscore.
  36. func SanitizeLabelName(name string) string {
  37. return invalidLabelCharRE.ReplaceAllString(name, "_")
  38. }