@@ -40,14 +40,40 @@ func init() {
4040 systemMetrics = monitoring .Default .NewRegistry ("system" )
4141}
4242
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+
4362// monitoringCgroupsHierarchyOverride is an undocumented environment variable which
4463// overrides the cgroups path under /sys/fs/cgroup, which should be set to "/" when running
4564// Elastic Agent under Docker.
4665const monitoringCgroupsHierarchyOverride = "LIBBEAT_MONITORING_CGROUPS_HIERARCHY_OVERRIDE"
4766
4867// SetupMetrics creates a basic suite of metrics handlers for monitoring, including build info and system resources
49- func SetupMetrics (logger * logp.Logger , name , version string ) error {
50- monitoring .NewFunc (systemMetrics , "cpu" , ReportSystemCPUUsage , monitoring .Report )
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 )
5177
5278 name = processName (name )
5379 processStats = & process.Stats {
@@ -63,12 +89,12 @@ func SetupMetrics(logger *logp.Logger, name, version string) error {
6389 return fmt .Errorf ("failed to init process stats for agent: %w" , err )
6490 }
6591
66- monitoring .NewFunc (processMetrics , "memstats" , MemStatsReporter (logger , processStats ), monitoring .Report )
67- monitoring .NewFunc (processMetrics , "cpu" , InstanceCPUReporter (logger , processStats ), monitoring .Report )
68- monitoring .NewFunc (processMetrics , "runtime" , ReportRuntime , monitoring .Report )
69- monitoring .NewFunc (processMetrics , "info" , infoReporter (name , version ), monitoring .Report )
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 )
7096
71- setupPlatformSpecificMetrics (logger , processStats )
97+ setupPlatformSpecificMetrics (logger , processStats , systemMetrics , processMetrics )
7298
7399 return nil
74100}
@@ -111,7 +137,7 @@ func infoReporter(serviceName, version string) func(_ monitoring.Mode, V monitor
111137 }
112138}
113139
114- func setupPlatformSpecificMetrics (logger * logp.Logger , processStats * process.Stats ) {
140+ func setupPlatformSpecificMetrics (logger * logp.Logger , processStats * process.Stats , systemMetrics , processMetrics * monitoring. Registry ) {
115141 if isLinux () {
116142 monitoring .NewFunc (processMetrics , "cgroup" , InstanceCroupsReporter (logger , monitoringCgroupsHierarchyOverride ), monitoring .Report )
117143 }
0 commit comments