labels.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package metrics provides utilities for metrics used by controllers.
  14. package metrics
  15. import (
  16. "maps"
  17. "regexp"
  18. "github.com/prometheus/client_golang/prometheus"
  19. )
  20. var (
  21. // NonConditionMetricLabelNames are the label names used for non-conditional metrics.
  22. NonConditionMetricLabelNames = make([]string, 0)
  23. // ConditionMetricLabelNames are the label names used for conditional metrics.
  24. ConditionMetricLabelNames = make([]string, 0)
  25. // NonConditionMetricLabels holds the actual label values for non-conditional metrics.
  26. NonConditionMetricLabels = make(map[string]string)
  27. // ConditionMetricLabels holds the actual label values for conditional metrics.
  28. ConditionMetricLabels = make(map[string]string)
  29. )
  30. var nonAlphanumericRegex *regexp.Regexp
  31. func init() {
  32. nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z0-9 ]+`)
  33. }
  34. // SetUpLabelNames initializes both non-conditional and conditional metric labels and label names.
  35. func SetUpLabelNames(addKubeStandardLabels bool) {
  36. NonConditionMetricLabelNames = []string{"name", "namespace"}
  37. ConditionMetricLabelNames = []string{"name", "namespace", "condition", "status"}
  38. // Figure out what the labels for the metrics are
  39. if addKubeStandardLabels {
  40. NonConditionMetricLabelNames = append(
  41. NonConditionMetricLabelNames,
  42. "app_kubernetes_io_name", "app_kubernetes_io_instance",
  43. "app_kubernetes_io_version", "app_kubernetes_io_component",
  44. "app_kubernetes_io_part_of", "app_kubernetes_io_managed_by",
  45. )
  46. ConditionMetricLabelNames = append(
  47. ConditionMetricLabelNames,
  48. "app_kubernetes_io_name", "app_kubernetes_io_instance",
  49. "app_kubernetes_io_version", "app_kubernetes_io_component",
  50. "app_kubernetes_io_part_of", "app_kubernetes_io_managed_by",
  51. )
  52. }
  53. // Set default values for each label
  54. for _, k := range NonConditionMetricLabelNames {
  55. NonConditionMetricLabels[k] = ""
  56. }
  57. for _, k := range ConditionMetricLabelNames {
  58. ConditionMetricLabels[k] = ""
  59. }
  60. }
  61. // RefineLabels refines the given Prometheus Labels with values from a map `newLabels`
  62. // Only overwrite a value if the corresponding key is present in the
  63. // Prometheus' Labels already to avoid adding label names which are
  64. // not defined in a metric's description. Note that non-alphanumeric
  65. // characters from keys of `newLabels` are replaced by an underscore
  66. // because Prometheus does not accept non-alphanumeric, non-underscore
  67. // characters in label names.
  68. func RefineLabels(promLabels prometheus.Labels, newLabels map[string]string) prometheus.Labels {
  69. var refinement = prometheus.Labels{}
  70. maps.Copy(refinement, promLabels)
  71. for k, v := range newLabels {
  72. cleanKey := nonAlphanumericRegex.ReplaceAllString(k, "_")
  73. if _, ok := refinement[cleanKey]; ok {
  74. refinement[cleanKey] = v
  75. }
  76. }
  77. return refinement
  78. }
  79. // RefineNonConditionMetricLabels refines the non-conditional metric labels with the given labels.
  80. func RefineNonConditionMetricLabels(labels map[string]string) prometheus.Labels {
  81. return RefineLabels(NonConditionMetricLabels, labels)
  82. }
  83. // RefineConditionMetricLabels refines the conditional metric labels with the given labels.
  84. func RefineConditionMetricLabels(labels map[string]string) prometheus.Labels {
  85. return RefineLabels(ConditionMetricLabels, labels)
  86. }