labels.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2018 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 prometheus
  14. import (
  15. "errors"
  16. "fmt"
  17. "strings"
  18. "unicode/utf8"
  19. "github.com/prometheus/common/model"
  20. )
  21. // Labels represents a collection of label name -> value mappings. This type is
  22. // commonly used with the With(Labels) and GetMetricWith(Labels) methods of
  23. // metric vector Collectors, e.g.:
  24. //
  25. // myVec.With(Labels{"code": "404", "method": "GET"}).Add(42)
  26. //
  27. // The other use-case is the specification of constant label pairs in Opts or to
  28. // create a Desc.
  29. type Labels map[string]string
  30. // reservedLabelPrefix is a prefix which is not legal in user-supplied
  31. // label names.
  32. const reservedLabelPrefix = "__"
  33. var errInconsistentCardinality = errors.New("inconsistent label cardinality")
  34. func makeInconsistentCardinalityError(fqName string, labels, labelValues []string) error {
  35. return fmt.Errorf(
  36. "%w: %q has %d variable labels named %q but %d values %q were provided",
  37. errInconsistentCardinality, fqName,
  38. len(labels), labels,
  39. len(labelValues), labelValues,
  40. )
  41. }
  42. func validateValuesInLabels(labels Labels, expectedNumberOfValues int) error {
  43. if len(labels) != expectedNumberOfValues {
  44. return fmt.Errorf(
  45. "%w: expected %d label values but got %d in %#v",
  46. errInconsistentCardinality, expectedNumberOfValues,
  47. len(labels), labels,
  48. )
  49. }
  50. for name, val := range labels {
  51. if !utf8.ValidString(val) {
  52. return fmt.Errorf("label %s: value %q is not valid UTF-8", name, val)
  53. }
  54. }
  55. return nil
  56. }
  57. func validateLabelValues(vals []string, expectedNumberOfValues int) error {
  58. if len(vals) != expectedNumberOfValues {
  59. return fmt.Errorf(
  60. "%w: expected %d label values but got %d in %#v",
  61. errInconsistentCardinality, expectedNumberOfValues,
  62. len(vals), vals,
  63. )
  64. }
  65. for _, val := range vals {
  66. if !utf8.ValidString(val) {
  67. return fmt.Errorf("label value %q is not valid UTF-8", val)
  68. }
  69. }
  70. return nil
  71. }
  72. func checkLabelName(l string) bool {
  73. return model.LabelName(l).IsValid() && !strings.HasPrefix(l, reservedLabelPrefix)
  74. }