-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
config.go
96 lines (77 loc) · 2.79 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
95
96
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package hostmetricsreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver"
import (
"errors"
"fmt"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"go.uber.org/multierr"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
)
const (
scrapersKey = "scrapers"
)
// Config defines configuration for HostMetrics receiver.
type Config struct {
scraperhelper.ControllerConfig `mapstructure:",squash"`
Scrapers map[string]internal.Config `mapstructure:"-"`
// RootPath is the host's root directory (linux only).
RootPath string `mapstructure:"root_path"`
// Collection interval for metadata.
// Metadata of the particular entity is collected when the entity changes.
// In addition metadata of all entities is collected periodically even if no changes happen.
// Setting the duration to 0 will disable periodic collection (however will not impact
// metadata collection on changes).
MetadataCollectionInterval time.Duration `mapstructure:"metadata_collection_interval"`
}
var _ component.Config = (*Config)(nil)
var _ confmap.Unmarshaler = (*Config)(nil)
// Validate checks the receiver configuration is valid
func (cfg *Config) Validate() error {
var err error
if len(cfg.Scrapers) == 0 {
err = multierr.Append(err, errors.New("must specify at least one scraper when using hostmetrics receiver"))
}
err = multierr.Append(err, validateRootPath(cfg.RootPath))
return err
}
// Unmarshal a config.Parser into the config struct.
func (cfg *Config) Unmarshal(componentParser *confmap.Conf) error {
if componentParser == nil {
return nil
}
// load the non-dynamic config normally
err := componentParser.Unmarshal(cfg, confmap.WithIgnoreUnused())
if err != nil {
return err
}
// dynamically load the individual collector configs based on the key name
cfg.Scrapers = map[string]internal.Config{}
scrapersSection, err := componentParser.Sub(scrapersKey)
if err != nil {
return err
}
for key := range scrapersSection.ToStringMap() {
factory, ok := getScraperFactory(key)
if !ok {
return fmt.Errorf("invalid scraper key: %s", key)
}
collectorCfg := factory.CreateDefaultConfig()
collectorViperSection, err := scrapersSection.Sub(key)
if err != nil {
return err
}
err = collectorViperSection.Unmarshal(collectorCfg)
if err != nil {
return fmt.Errorf("error reading settings for scraper type %q: %w", key, err)
}
collectorCfg.SetRootPath(cfg.RootPath)
envMap := setGoPsutilEnvVars(cfg.RootPath, &osEnv{})
collectorCfg.SetEnvMap(envMap)
cfg.Scrapers[key] = collectorCfg
}
return nil
}