Skip to content

Commit 69e11a2

Browse files
authored
feat: require passing registries for metrics setup (#247)
## What does this PR do? remove global registry usage and require explicitly passing system and process registry also fix compile errors for other oses ## Why is it important? move away from global registries similar to what has been recently done in beats ## Checklist <!-- Mandatory Add a checklist of things that are required to be reviewed in order to have the PR approved List here all the items you have verified BEFORE sending this PR. Please DO NOT remove any item, striking through those that do not apply. (Just in case, strikethrough uses two tildes. ~~Scratch this.~~) --> - [ ] My code follows the style guidelines of this project - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] I have added an entry in `CHANGELOG.md` ## Author's Checklist <!-- Recommended Add a checklist of things that are required to be reviewed in order to have the PR approved --> - [ ] ## Related issues <!-- Recommended Link related issues below. Insert the issue link or reference after the word "Closes" if merging this should automatically close it. - Closes #123 - Relates #123 - Requires #123 - Superseds #123 --> -
1 parent 970079b commit 69e11a2

File tree

4 files changed

+17
-46
lines changed

4 files changed

+17
-46
lines changed

report/metrics_common.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@ import (
2727
)
2828

2929
var (
30-
ephemeralID uuid.UUID
31-
processMetrics *monitoring.Registry
32-
startTime time.Time
30+
ephemeralID uuid.UUID
31+
startTime time.Time
3332
)
3433

3534
func init() {
3635
startTime = time.Now()
37-
processMetrics = monitoring.Default.NewRegistry("beat")
3836

3937
var err error
4038
ephemeralID, err = uuid.NewV4()

report/metrics_report_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ import (
3030
)
3131

3232
func TestSystemMetricsReport(t *testing.T) {
33-
err := SetupMetrics(logptest.NewTestingLogger(t, ""), "TestSys", "test")
33+
systemMetrics := monitoring.NewRegistry()
34+
processMetrics := monitoring.NewRegistry()
35+
err := SetupMetrics(logptest.NewTestingLogger(t, ""), "TestSys", "test", systemMetrics, processMetrics)
3436
require.NoError(t, err)
3537

3638
var gotCPU, gotMem, gotInfo atomic.Bool

report/setup.go

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,17 @@ import (
3131
)
3232

3333
var (
34-
systemMetrics *monitoring.Registry
35-
3634
processStats *process.Stats
3735
)
3836

39-
func init() {
40-
systemMetrics = monitoring.Default.NewRegistry("system")
41-
}
42-
43-
type option struct {
44-
systemMetrics *monitoring.Registry
45-
processMetrics *monitoring.Registry
46-
}
47-
48-
type OptionFunc func(o *option)
49-
50-
func WithProcessRegistry(r *monitoring.Registry) OptionFunc {
51-
return func(o *option) {
52-
o.processMetrics = r
53-
}
54-
}
55-
56-
func WithSystemRegistry(r *monitoring.Registry) OptionFunc {
57-
return func(o *option) {
58-
o.systemMetrics = r
59-
}
60-
}
61-
6237
// monitoringCgroupsHierarchyOverride is an undocumented environment variable which
6338
// overrides the cgroups path under /sys/fs/cgroup, which should be set to "/" when running
6439
// Elastic Agent under Docker.
6540
const monitoringCgroupsHierarchyOverride = "LIBBEAT_MONITORING_CGROUPS_HIERARCHY_OVERRIDE"
6641

6742
// SetupMetrics creates a basic suite of metrics handlers for monitoring, including build info and system resources
68-
func SetupMetrics(logger *logp.Logger, name, version string, opts ...OptionFunc) error {
69-
opt := &option{
70-
systemMetrics: systemMetrics,
71-
processMetrics: processMetrics,
72-
}
73-
for _, o := range opts {
74-
o(opt)
75-
}
76-
monitoring.NewFunc(opt.systemMetrics, "cpu", ReportSystemCPUUsage, monitoring.Report)
43+
func SetupMetrics(logger *logp.Logger, name, version string, systemMetrics *monitoring.Registry, processMetrics *monitoring.Registry) error {
44+
monitoring.NewFunc(systemMetrics, "cpu", ReportSystemCPUUsage, monitoring.Report)
7745

7846
name = processName(name)
7947
processStats = &process.Stats{
@@ -89,12 +57,12 @@ func SetupMetrics(logger *logp.Logger, name, version string, opts ...OptionFunc)
8957
return fmt.Errorf("failed to init process stats for agent: %w", err)
9058
}
9159

92-
monitoring.NewFunc(opt.processMetrics, "memstats", MemStatsReporter(logger, processStats), monitoring.Report)
93-
monitoring.NewFunc(opt.processMetrics, "cpu", InstanceCPUReporter(logger, processStats), monitoring.Report)
94-
monitoring.NewFunc(opt.processMetrics, "runtime", ReportRuntime, monitoring.Report)
95-
monitoring.NewFunc(opt.processMetrics, "info", infoReporter(name, version), monitoring.Report)
60+
monitoring.NewFunc(processMetrics, "memstats", MemStatsReporter(logger, processStats), monitoring.Report)
61+
monitoring.NewFunc(processMetrics, "cpu", InstanceCPUReporter(logger, processStats), monitoring.Report)
62+
monitoring.NewFunc(processMetrics, "runtime", ReportRuntime, monitoring.Report)
63+
monitoring.NewFunc(processMetrics, "info", infoReporter(name, version), monitoring.Report)
9664

97-
setupPlatformSpecificMetrics(logger, processStats, opt.systemMetrics, opt.processMetrics)
65+
setupPlatformSpecificMetrics(logger, processStats, systemMetrics, processMetrics)
9866

9967
return nil
10068
}

report/setup_other.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323

2424
package report
2525

26-
import "github.com/elastic/elastic-agent-libs/logp"
26+
import (
27+
"github.com/elastic/elastic-agent-libs/logp"
28+
"github.com/elastic/elastic-agent-libs/monitoring"
29+
)
2730

28-
func SetupMetrics(logger *logp.Logger, name, version string) error {
31+
func SetupMetrics(logger *logp.Logger, name, version string, systemMetrics *monitoring.Registry, processMetrics *monitoring.Registry) error {
2932
logp.Warn("Metrics not implemented for this OS.")
3033
return nil
3134
}

0 commit comments

Comments
 (0)