Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

103 linhas
2.8 KiB

  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 model
  14. import (
  15. "fmt"
  16. "regexp"
  17. "sort"
  18. "strings"
  19. )
  20. var (
  21. // MetricNameRE is a regular expression matching valid metric
  22. // names. Note that the IsValidMetricName function performs the same
  23. // check but faster than a match with this regular expression.
  24. MetricNameRE = regexp.MustCompile(`^[a-zA-Z_:][a-zA-Z0-9_:]*$`)
  25. )
  26. // A Metric is similar to a LabelSet, but the key difference is that a Metric is
  27. // a singleton and refers to one and only one stream of samples.
  28. type Metric LabelSet
  29. // Equal compares the metrics.
  30. func (m Metric) Equal(o Metric) bool {
  31. return LabelSet(m).Equal(LabelSet(o))
  32. }
  33. // Before compares the metrics' underlying label sets.
  34. func (m Metric) Before(o Metric) bool {
  35. return LabelSet(m).Before(LabelSet(o))
  36. }
  37. // Clone returns a copy of the Metric.
  38. func (m Metric) Clone() Metric {
  39. clone := make(Metric, len(m))
  40. for k, v := range m {
  41. clone[k] = v
  42. }
  43. return clone
  44. }
  45. func (m Metric) String() string {
  46. metricName, hasName := m[MetricNameLabel]
  47. numLabels := len(m) - 1
  48. if !hasName {
  49. numLabels = len(m)
  50. }
  51. labelStrings := make([]string, 0, numLabels)
  52. for label, value := range m {
  53. if label != MetricNameLabel {
  54. labelStrings = append(labelStrings, fmt.Sprintf("%s=%q", label, value))
  55. }
  56. }
  57. switch numLabels {
  58. case 0:
  59. if hasName {
  60. return string(metricName)
  61. }
  62. return "{}"
  63. default:
  64. sort.Strings(labelStrings)
  65. return fmt.Sprintf("%s{%s}", metricName, strings.Join(labelStrings, ", "))
  66. }
  67. }
  68. // Fingerprint returns a Metric's Fingerprint.
  69. func (m Metric) Fingerprint() Fingerprint {
  70. return LabelSet(m).Fingerprint()
  71. }
  72. // FastFingerprint returns a Metric's Fingerprint calculated by a faster hashing
  73. // algorithm, which is, however, more susceptible to hash collisions.
  74. func (m Metric) FastFingerprint() Fingerprint {
  75. return LabelSet(m).FastFingerprint()
  76. }
  77. // IsValidMetricName returns true iff name matches the pattern of MetricNameRE.
  78. // This function, however, does not use MetricNameRE for the check but a much
  79. // faster hardcoded implementation.
  80. func IsValidMetricName(n LabelValue) bool {
  81. if len(n) == 0 {
  82. return false
  83. }
  84. for i, b := range n {
  85. if !((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || b == '_' || b == ':' || (b >= '0' && b <= '9' && i > 0)) {
  86. return false
  87. }
  88. }
  89. return true
  90. }