labels.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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. "regexp"
  17. "github.com/prometheus/client_golang/prometheus"
  18. )
  19. var (
  20. // NonConditionMetricLabelNames are the label names used for non-conditional metrics.
  21. NonConditionMetricLabelNames = make([]string, 0)
  22. // ConditionMetricLabelNames are the label names used for conditional metrics.
  23. ConditionMetricLabelNames = make([]string, 0)
  24. // NonConditionMetricLabels holds the actual label values for non-conditional metrics.
  25. NonConditionMetricLabels = make(map[string]string)
  26. // ConditionMetricLabels holds the actual label values for conditional metrics.
  27. ConditionMetricLabels = make(map[string]string)
  28. )
  29. var nonAlphanumericRegex *regexp.Regexp
  30. func init() {
  31. nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z0-9 ]+`)
  32. }
  33. // SetUpLabelNames initializes both non-conditional and conditional metric labels and label names.
  34. func SetUpLabelNames(addKubeStandardLabels bool) {
  35. NonConditionMetricLabelNames = []string{"name", "namespace"}
  36. ConditionMetricLabelNames = []string{"name", "namespace", "condition", "status"}
  37. // Figure out what the labels for the metrics are
  38. if addKubeStandardLabels {
  39. NonConditionMetricLabelNames = append(
  40. NonConditionMetricLabelNames,
  41. "app_kubernetes_io_name", "app_kubernetes_io_instance",
  42. "app_kubernetes_io_version", "app_kubernetes_io_component",
  43. "app_kubernetes_io_part_of", "app_kubernetes_io_managed_by",
  44. )
  45. ConditionMetricLabelNames = append(
  46. ConditionMetricLabelNames,
  47. "app_kubernetes_io_name", "app_kubernetes_io_instance",
  48. "app_kubernetes_io_version", "app_kubernetes_io_component",
  49. "app_kubernetes_io_part_of", "app_kubernetes_io_managed_by",
  50. )
  51. }
  52. // Set default values for each label
  53. for _, k := range NonConditionMetricLabelNames {
  54. NonConditionMetricLabels[k] = ""
  55. }
  56. for _, k := range ConditionMetricLabelNames {
  57. ConditionMetricLabels[k] = ""
  58. }
  59. }
  60. // RefineLabels refines the given Prometheus Labels with values from a map `newLabels`
  61. // Only overwrite a value if the corresponding key is present in the
  62. // Prometheus' Labels already to avoid adding label names which are
  63. // not defined in a metric's description. Note that non-alphanumeric
  64. // characters from keys of `newLabels` are replaced by an underscore
  65. // because Prometheus does not accept non-alphanumeric, non-underscore
  66. // characters in label names.
  67. func RefineLabels(promLabels prometheus.Labels, newLabels map[string]string) prometheus.Labels {
  68. var refinement = prometheus.Labels{}
  69. for k, v := range promLabels {
  70. refinement[k] = v
  71. }
  72. for k, v := range newLabels {
  73. cleanKey := nonAlphanumericRegex.ReplaceAllString(k, "_")
  74. if _, ok := refinement[cleanKey]; ok {
  75. refinement[cleanKey] = v
  76. }
  77. }
  78. return refinement
  79. }
  80. // RefineNonConditionMetricLabels refines the non-conditional metric labels with the given labels.
  81. func RefineNonConditionMetricLabels(labels map[string]string) prometheus.Labels {
  82. return RefineLabels(NonConditionMetricLabels, labels)
  83. }
  84. // RefineConditionMetricLabels refines the conditional metric labels with the given labels.
  85. func RefineConditionMetricLabels(labels map[string]string) prometheus.Labels {
  86. return RefineLabels(ConditionMetricLabels, labels)
  87. }