metric.go 2.8 KB

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