labels.go 3.1 KB

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