Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

88 rindas
2.5 KiB

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