labels.go 3.1 KB

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