-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
config.go
93 lines (75 loc) · 2.77 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
// 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"
)
// Config defines configuration for HostMetrics receiver.
type Config struct {
scraperhelper.ControllerConfig `mapstructure:",squash"`
Scrapers map[component.Type]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.ConfigValidator = (*Config)(nil)
_ 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 = errors.New("must specify at least one scraper when using hostmetrics receiver")
}
return multierr.Append(err, validateRootPath(cfg.RootPath))
}
// 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
if err := componentParser.Unmarshal(cfg, confmap.WithIgnoreUnused()); err != nil {
return err
}
// dynamically load the individual collector configs based on the key name
cfg.Scrapers = map[component.Type]internal.Config{}
scrapersSection, err := componentParser.Sub("scrapers")
if err != nil {
return err
}
for keyStr := range scrapersSection.ToStringMap() {
key, err := component.NewType(keyStr)
if err != nil {
return fmt.Errorf("invalid scraper key name: %s", key)
}
factory, ok := scraperFactories[key]
if !ok {
return fmt.Errorf("invalid scraper key: %s", key)
}
scraperSection, err := scrapersSection.Sub(keyStr)
if err != nil {
return err
}
scraperCfg := factory.CreateDefaultConfig()
if err = scraperSection.Unmarshal(scraperCfg); err != nil {
return fmt.Errorf("error reading settings for scraper type %q: %w", key, err)
}
scraperCfg.SetRootPath(cfg.RootPath)
cfg.Scrapers[key] = scraperCfg
}
return nil
}