forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
94 lines (79 loc) · 3.27 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2020, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kubeletstatsreceiver
import (
"errors"
"fmt"
"time"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/config/confignet"
"k8s.io/client-go/kubernetes"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kubeletstatsreceiver/kubelet"
)
var _ configmodels.Receiver = (*Config)(nil)
type Config struct {
configmodels.ReceiverSettings `mapstructure:",squash"`
kubelet.ClientConfig `mapstructure:",squash"`
confignet.TCPAddr `mapstructure:",squash"`
CollectionInterval time.Duration `mapstructure:"collection_interval"`
// ExtraMetadataLabels contains list of extra metadata that should be taken from /pods endpoint
// and put as extra labels on metrics resource.
// No additional metadata is fetched by default, so there are no extra calls to /pods endpoint.
// Supported values include container.id and k8s.volume.type.
ExtraMetadataLabels []kubelet.MetadataLabel `mapstructure:"extra_metadata_labels"`
// MetricGroupsToCollect provides a list of metrics groups to collect metrics from.
// "container", "pod", "node" and "volume" are the only valid groups.
MetricGroupsToCollect []kubelet.MetricGroup `mapstructure:"metric_groups"`
// Configuration of the Kubernetes API client.
K8sAPIConfig *k8sconfig.APIConfig `mapstructure:"k8s_api_config"`
}
// getReceiverOptions returns receiverOptions is the config is valid,
// otherwise it will return an error.
func (cfg *Config) getReceiverOptions() (*receiverOptions, error) {
err := kubelet.ValidateMetadataLabelsConfig(cfg.ExtraMetadataLabels)
if err != nil {
return nil, err
}
mgs, err := getMapFromSlice(cfg.MetricGroupsToCollect)
if err != nil {
return nil, err
}
var k8sAPIClient kubernetes.Interface
if cfg.K8sAPIConfig != nil {
k8sAPIClient, err = k8sconfig.MakeClient(*cfg.K8sAPIConfig)
if err != nil {
return nil, fmt.Errorf("failed to create K8s API client: %w", err)
}
}
return &receiverOptions{
name: cfg.Name(),
collectionInterval: cfg.CollectionInterval,
extraMetadataLabels: cfg.ExtraMetadataLabels,
metricGroupsToCollect: mgs,
k8sAPIClient: k8sAPIClient,
}, nil
}
// getMapFromSlice returns a set of kubelet.MetricGroup values from
// the provided list. Returns an err if invalid entries are encountered.
func getMapFromSlice(collect []kubelet.MetricGroup) (map[kubelet.MetricGroup]bool, error) {
out := make(map[kubelet.MetricGroup]bool, len(collect))
for _, c := range collect {
if !kubelet.ValidMetricGroups[c] {
return nil, errors.New("invalid entry in metric_groups")
}
out[c] = true
}
return out, nil
}