|
| 1 | +// Copyright Splunk, Inc. |
| 2 | +// |
| 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 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package configconverter |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "regexp" |
| 21 | + |
| 22 | + sfxpb "github.com/signalfx/com_signalfx_metrics_protobuf/model" |
| 23 | + "go.opentelemetry.io/collector/confmap" |
| 24 | + |
| 25 | + "github.com/signalfx/splunk-otel-collector/internal/configconverter/dpfilters" |
| 26 | +) |
| 27 | + |
| 28 | +// Metrics deprecated by kubeletstats receiver that need to be disabled if not explicitly included in signalfx exporter. |
| 29 | +const ( |
| 30 | + k8sNodeCPUUtilization = "k8s.node.cpu.utilization" |
| 31 | + k8sPodCPUUtilization = "k8s.pod.cpu.utilization" |
| 32 | + containerCPUUtilization = "container.cpu.utilization" |
| 33 | +) |
| 34 | + |
| 35 | +// signalfxExporterConfig is the configuration for the signalfx exporter that contains the metrics filters. |
| 36 | +type signalfxExporterConfig struct { |
| 37 | + ExcludeMetrics []dpfilters.MetricFilter `mapstructure:"exclude_metrics"` |
| 38 | + IncludeMetrics []dpfilters.MetricFilter `mapstructure:"include_metrics"` |
| 39 | +} |
| 40 | + |
| 41 | +// DisableKubeletUtilizationMetrics is a MapConverter that disables the following deprecated metrics: |
| 42 | +// - `k8s.node.cpu.utilization` |
| 43 | +// - `k8s.pod.cpu.utilization` |
| 44 | +// - `container.cpu.utilization` |
| 45 | +// The converter disables the metrics at the receiver level to avoid showing users a warning message because |
| 46 | +// they are excluded in signalfx exporter by default. |
| 47 | +// We don't disable them in case if users explicitly include them in signalfx exporter. |
| 48 | +type DisableKubeletUtilizationMetrics struct{} |
| 49 | + |
| 50 | +func (DisableKubeletUtilizationMetrics) Convert(_ context.Context, cfgMap *confmap.Conf) error { |
| 51 | + if cfgMap == nil { |
| 52 | + return fmt.Errorf("cannot DisableKubeletUtilizationMetrics on nil *confmap.Conf") |
| 53 | + } |
| 54 | + |
| 55 | + receivers, err := cfgMap.Sub("receivers") |
| 56 | + if err != nil { |
| 57 | + return nil // Ignore invalid config. Rely on the config validation to catch this. |
| 58 | + } |
| 59 | + kubeletReceiverConfigs := map[string]map[string]any{} |
| 60 | + for receiverName, receiverCfg := range receivers.ToStringMap() { |
| 61 | + if regexp.MustCompile(`kubeletstats(/\w+)?`).MatchString(receiverName) { |
| 62 | + if v, ok := receiverCfg.(map[string]any); ok { |
| 63 | + kubeletReceiverConfigs[receiverName] = v |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + exporters, err := cfgMap.Sub("exporters") |
| 69 | + if err != nil { |
| 70 | + return nil // Ignore invalid config. Rely on the config validation to catch this. |
| 71 | + } |
| 72 | + sfxExporterConfigs := map[string]map[string]any{} |
| 73 | + for exporterName, exporterCfg := range exporters.ToStringMap() { |
| 74 | + if regexp.MustCompile(`signalfx(/\w+)?`).MatchString(exporterName) { |
| 75 | + if v, ok := exporterCfg.(map[string]any); ok { |
| 76 | + sfxExporterConfigs[exporterName] = v |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // If there is no signalfx exporter or kubeletstats receiver, there is nothing to do. |
| 82 | + if len(kubeletReceiverConfigs) == 0 || len(sfxExporterConfigs) == 0 { |
| 83 | + return nil |
| 84 | + } |
| 85 | + |
| 86 | + disableMetrics := map[string]bool{ |
| 87 | + k8sNodeCPUUtilization: true, |
| 88 | + k8sPodCPUUtilization: true, |
| 89 | + containerCPUUtilization: true, |
| 90 | + } |
| 91 | + |
| 92 | + // Check if the metrics are explicitly included in signalfx exporter. |
| 93 | + // If they are not included, we will disable them in kubeletstats receiver. |
| 94 | + for _, cm := range sfxExporterConfigs { |
| 95 | + cfg := signalfxExporterConfig{} |
| 96 | + err = confmap.NewFromStringMap(cm).Unmarshal(&cfg, confmap.WithIgnoreUnused()) |
| 97 | + if err != nil { |
| 98 | + return nil // Ignore invalid config. Rely on the config validation to catch this. |
| 99 | + } |
| 100 | + if len(cfg.ExcludeMetrics) == 0 { |
| 101 | + // Apply default excluded metrics if not explicitly set. |
| 102 | + // https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/f2d8efe507083b0f38b6567f8dba3f37053bfa86/exporter/signalfxexporter/internal/translation/default_metrics.go#L133 |
| 103 | + cfg.ExcludeMetrics = []dpfilters.MetricFilter{ |
| 104 | + {MetricNames: []string{"/^(?i:(container)|(k8s\\.node)|(k8s\\.pod))\\.cpu\\.utilization$/"}}, |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + var filter *dpfilters.FilterSet |
| 109 | + filter, err = dpfilters.NewFilterSet(cfg.ExcludeMetrics, cfg.IncludeMetrics) |
| 110 | + if err != nil { |
| 111 | + return nil // Ignore invalid config. Rely on the config validation to catch this. |
| 112 | + } |
| 113 | + for metricName := range disableMetrics { |
| 114 | + if !filter.Matches(&sfxpb.DataPoint{Metric: metricName}) { |
| 115 | + disableMetrics[metricName] = false |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // Disable the metrics in kubeletstats receiver. |
| 121 | + for receiverName, cfg := range kubeletReceiverConfigs { |
| 122 | + metricsCfg := map[string]any{} |
| 123 | + if cfg["metrics"] != nil { |
| 124 | + if v, ok := cfg["metrics"].(map[string]any); ok { |
| 125 | + metricsCfg = v |
| 126 | + } |
| 127 | + } |
| 128 | + if _, ok := metricsCfg[k8sNodeCPUUtilization]; !ok && disableMetrics[k8sNodeCPUUtilization] { |
| 129 | + metricsCfg[k8sNodeCPUUtilization] = map[string]any{"enabled": false} |
| 130 | + } |
| 131 | + if _, ok := metricsCfg[k8sPodCPUUtilization]; !ok && disableMetrics[k8sPodCPUUtilization] { |
| 132 | + metricsCfg[k8sPodCPUUtilization] = map[string]any{"enabled": false} |
| 133 | + } |
| 134 | + if _, ok := metricsCfg[containerCPUUtilization]; !ok && disableMetrics[containerCPUUtilization] { |
| 135 | + metricsCfg[containerCPUUtilization] = map[string]any{"enabled": false} |
| 136 | + } |
| 137 | + metricsCfgKey := fmt.Sprintf("receivers::%s::metrics", receiverName) |
| 138 | + if len(metricsCfg) > 0 { |
| 139 | + if err = cfgMap.Merge(confmap.NewFromStringMap(map[string]any{metricsCfgKey: metricsCfg})); err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return nil |
| 146 | +} |
0 commit comments