Skip to content
This repository was archived by the owner on Nov 7, 2022. It is now read-only.

Commit c07166e

Browse files
authored
receiver/vmmetrics: Allow to have a different process mount point. (#521)
* receiver/vmmetrics: Allow to have a different process mount point. * Update unmarshal.
1 parent e6eac5a commit c07166e

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

receiver/vmmetricsreceiver/example_config.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
receivers:
22
vmmetrics:
3-
scrape_interval: 10s
3+
scrape_interval: 10
4+
mount_point: "/proc"
5+
# process_mount_point: "/data/proc" # Should only be used for Daemon Set within a container.
46

57
zpages:
68
port: 55679

receiver/vmmetricsreceiver/metrics_receiver.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ var (
3535

3636
// Configuration defines the behavior and targets of the VM metrics scrapers.
3737
type Configuration struct {
38-
scrapeInterval time.Duration `mapstructure:"scrape_interval"`
39-
mountPoint string `mapstructure:"mount_point"`
40-
metricPrefix string `mapstructure:"metric_prefix"`
38+
scrapeInterval time.Duration `mapstructure:"scrape_interval"`
39+
mountPoint string `mapstructure:"mount_point"`
40+
processMountPoint string `mapstructure:"process_mount_point"`
41+
metricPrefix string `mapstructure:"metric_prefix"`
4142
}
4243

4344
// Receiver is the type used to handle metrics from VM metrics.
@@ -59,12 +60,12 @@ func New(v *viper.Viper, consumer consumer.MetricsConsumer) (*Receiver, error) {
5960
var cfg Configuration
6061

6162
// Unmarshal our config values (using viper's mapstructure)
62-
err := v.Unmarshal(&cfg)
63+
err := unmarshal(&cfg, v.AllSettings())
6364
if err != nil {
6465
return nil, fmt.Errorf("vmmetrics receiver failed to parse config: %s", err)
6566
}
6667

67-
vmc, err := NewVMMetricsCollector(cfg.scrapeInterval, cfg.mountPoint, cfg.metricPrefix, consumer)
68+
vmc, err := NewVMMetricsCollector(cfg.scrapeInterval, cfg.mountPoint, cfg.processMountPoint, cfg.metricPrefix, consumer)
6869
if err != nil {
6970
return nil, err
7071
}
@@ -107,3 +108,21 @@ func (vmr *Receiver) StopMetricsReception(ctx context.Context) error {
107108
})
108109
return err
109110
}
111+
112+
// TODO(songya): investigate why viper.Unmarshal didn't work, remove this method and use viper.Unmarshal instead.
113+
func unmarshal(cfg *Configuration, settings map[string]interface{}) error {
114+
if interval, ok := settings["scrape_interval"]; ok {
115+
intervalInSecs := interval.(int)
116+
cfg.scrapeInterval = time.Duration(intervalInSecs * int(time.Second))
117+
}
118+
if mountPoint, ok := settings["mount_point"]; ok {
119+
cfg.mountPoint = mountPoint.(string)
120+
}
121+
if processMountPoint, ok := settings["process_mount_point"]; ok {
122+
cfg.processMountPoint = processMountPoint.(string)
123+
}
124+
if prefix, ok := settings["metric_prefix"]; ok {
125+
cfg.metricPrefix = prefix.(string)
126+
}
127+
return nil
128+
}

receiver/vmmetricsreceiver/vm_metrics_collector.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ import (
3737
type VMMetricsCollector struct {
3838
consumer consumer.MetricsConsumer
3939

40-
startTime time.Time
41-
views []*view.View
42-
fs procfs.FS
40+
startTime time.Time
41+
views []*view.View
42+
43+
fs procfs.FS
44+
processFs procfs.FS
45+
4346
scrapeInterval time.Duration
4447
metricPrefix string
4548
done chan struct{}
@@ -51,14 +54,17 @@ const (
5154
)
5255

5356
// NewVMMetricsCollector creates a new set of VM and Process Metrics (mem, cpu).
54-
func NewVMMetricsCollector(si time.Duration, mpoint, mprefix string, consumer consumer.MetricsConsumer) (*VMMetricsCollector, error) {
55-
if mpoint == "" {
56-
mpoint = defaultMountPoint
57+
func NewVMMetricsCollector(si time.Duration, mountPoint, processMountPoint, prefix string, consumer consumer.MetricsConsumer) (*VMMetricsCollector, error) {
58+
if mountPoint == "" {
59+
mountPoint = defaultMountPoint
60+
}
61+
if processMountPoint == "" {
62+
processMountPoint = defaultMountPoint
5763
}
5864
if si <= 0 {
5965
si = defaultScrapeInterval
6066
}
61-
fs, err := procfs.NewFS(mpoint)
67+
fs, err := procfs.NewFS(mountPoint)
6268
if err != nil {
6369
return nil, fmt.Errorf("failed to create new VMMetricsCollector: %s", err)
6470
}
@@ -68,10 +74,18 @@ func NewVMMetricsCollector(si time.Duration, mpoint, mprefix string, consumer co
6874
views: vmViews,
6975
fs: fs,
7076
scrapeInterval: si,
71-
metricPrefix: mprefix,
77+
metricPrefix: prefix,
7278
done: make(chan struct{}),
7379
}
7480
view.Register(vmc.views...)
81+
82+
if processMountPoint != mountPoint {
83+
vmc.processFs, err = procfs.NewFS(processMountPoint)
84+
if err != nil {
85+
return nil, fmt.Errorf("failed to create new VMMetricsCollector: %s", err)
86+
}
87+
}
88+
7589
return vmc, nil
7690
}
7791

@@ -108,7 +122,13 @@ func (vmc *VMMetricsCollector) scrape() {
108122
mRuntimeSysMem.M(int64(ms.Sys)))
109123

110124
pid := os.Getpid()
111-
proc, err := procfs.NewProc(pid)
125+
var proc procfs.Proc
126+
var err error
127+
if vmc.processFs == "" {
128+
proc, err = vmc.fs.NewProc(pid)
129+
} else {
130+
proc, err = vmc.processFs.NewProc(pid)
131+
}
112132
if err == nil {
113133
if procStat, err := proc.NewStat(); err == nil {
114134
stats.Record(ctx, mCPUSeconds.M(int64(procStat.CPUTime())))

0 commit comments

Comments
 (0)