Skip to content

Commit 063a428

Browse files
kruskallkhushijain21VihasMakwana
authored
feat: deprecate SetupMetrics and migrate to SetupMetricsOptions (#258)
## What does this PR do? instead of breasking the method signature move to arg options ## Why is it important? to account for consumers and older branches ## 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 --> - --------- Co-authored-by: Khushi Jain <[email protected]> Co-authored-by: Vihas Makwana <[email protected]>
1 parent b387940 commit 063a428

File tree

7 files changed

+83
-19
lines changed

7 files changed

+83
-19
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/elastic/go-sysinfo v1.14.1
1111
github.com/elastic/go-windows v1.0.1
1212
github.com/elastic/gosigar v0.14.2
13+
github.com/gofrs/uuid/v5 v5.2.0
1314
github.com/magefile/mage v1.15.0
1415
github.com/shirou/gopsutil/v4 v4.24.7
1516
github.com/stretchr/testify v1.9.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
4646
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
4747
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
4848
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
49+
github.com/gofrs/uuid/v5 v5.2.0 h1:qw1GMx6/y8vhVsx626ImfKMuS5CvJmhIKKtuyvfajMM=
50+
github.com/gofrs/uuid/v5 v5.2.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
4951
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
5052
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
5153
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=

report/metrics_common.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,25 @@ package report
1919

2020
import (
2121
"time"
22+
23+
"github.com/gofrs/uuid/v5"
2224
)
2325

2426
var (
25-
startTime time.Time
27+
ephemeralID uuid.UUID
28+
startTime time.Time
2629
)
2730

2831
func init() {
2932
startTime = time.Now()
33+
34+
ephemeralID, _ = uuid.NewV4()
35+
}
36+
37+
// EphemeralID returns generated EphemeralID
38+
//
39+
// Deprecated: generate your own EphemeralID
40+
func EphemeralID() uuid.UUID {
41+
return ephemeralID
42+
3043
}

report/metrics_report_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ import (
3232
func TestSystemMetricsReport(t *testing.T) {
3333
systemMetrics := monitoring.NewRegistry()
3434
processMetrics := monitoring.NewRegistry()
35-
err := SetupMetrics(logptest.NewTestingLogger(t, ""), "TestSys", "test", "", systemMetrics, processMetrics)
35+
err := SetupMetricsOptions(MetricOptions{
36+
Name: t.Name(),
37+
Version: "test",
38+
EphemeralID: "",
39+
SystemMetrics: systemMetrics,
40+
ProcessMetrics: processMetrics,
41+
Logger: logptest.NewTestingLogger(t, ""),
42+
})
3643
require.NoError(t, err)
3744

3845
var gotCPU, gotMem, gotInfo atomic.Bool

report/setup.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,44 @@ var (
4040
const monitoringCgroupsHierarchyOverride = "LIBBEAT_MONITORING_CGROUPS_HIERARCHY_OVERRIDE"
4141

4242
// SetupMetrics creates a basic suite of metrics handlers for monitoring, including build info and system resources
43-
func SetupMetrics(logger *logp.Logger, name, version, ephemeralID string, systemMetrics *monitoring.Registry, processMetrics *monitoring.Registry) error {
44-
monitoring.NewFunc(systemMetrics, "cpu", ReportSystemCPUUsage, monitoring.Report)
43+
//
44+
// Deprecated: use SetupMetricsOptions
45+
func SetupMetrics(logger *logp.Logger, name, version string) error {
46+
return SetupMetricsOptions(MetricOptions{
47+
Name: name,
48+
Version: version,
49+
EphemeralID: ephemeralID.String(),
50+
Logger: logger,
51+
SystemMetrics: monitoring.Default.GetOrCreateRegistry("system"),
52+
ProcessMetrics: monitoring.Default.GetOrCreateRegistry("beat"),
53+
})
54+
}
55+
56+
// SetupMetricsOptions performs creation of metrics handlers using specified options.
57+
func SetupMetricsOptions(opts MetricOptions) error {
58+
monitoring.NewFunc(opts.SystemMetrics, "cpu", ReportSystemCPUUsage, monitoring.Report)
4559

46-
name = processName(name)
60+
opts.Name = processName(opts.Name)
4761
processStats = &process.Stats{
48-
Procs: []string{name},
62+
Procs: []string{opts.Name},
4963
EnvWhitelist: nil,
5064
CPUTicks: true,
5165
CacheCmdLine: true,
5266
IncludeTop: process.IncludeTopConfig{},
53-
Logger: logger,
67+
Logger: opts.Logger,
5468
}
5569

5670
err := processStats.Init()
5771
if err != nil {
5872
return fmt.Errorf("failed to init process stats for agent: %w", err)
5973
}
6074

61-
monitoring.NewFunc(processMetrics, "memstats", MemStatsReporter(logger, processStats), monitoring.Report)
62-
monitoring.NewFunc(processMetrics, "cpu", InstanceCPUReporter(logger, processStats), monitoring.Report)
63-
monitoring.NewFunc(processMetrics, "runtime", ReportRuntime, monitoring.Report)
64-
monitoring.NewFunc(processMetrics, "info", infoReporter(name, version, ephemeralID), monitoring.Report)
75+
monitoring.NewFunc(opts.ProcessMetrics, "memstats", MemStatsReporter(opts.Logger, processStats), monitoring.Report)
76+
monitoring.NewFunc(opts.ProcessMetrics, "cpu", InstanceCPUReporter(opts.Logger, processStats), monitoring.Report)
77+
monitoring.NewFunc(opts.ProcessMetrics, "runtime", ReportRuntime, monitoring.Report)
78+
monitoring.NewFunc(opts.ProcessMetrics, "info", infoReporter(opts.Name, opts.Version, opts.EphemeralID), monitoring.Report)
6579

66-
setupPlatformSpecificMetrics(logger, processStats, systemMetrics, processMetrics)
80+
setupPlatformSpecificMetrics(opts.Logger, processStats, opts.SystemMetrics, opts.ProcessMetrics)
6781

6882
return nil
6983
}

report/setup_common.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package report
19+
20+
import (
21+
"github.com/elastic/elastic-agent-libs/logp"
22+
"github.com/elastic/elastic-agent-libs/monitoring"
23+
)
24+
25+
type MetricOptions struct {
26+
Name string
27+
Version string
28+
EphemeralID string
29+
Logger *logp.Logger
30+
SystemMetrics *monitoring.Registry
31+
ProcessMetrics *monitoring.Registry
32+
}

report/setup_other.go

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

2424
package report
2525

26-
import (
27-
"github.com/elastic/elastic-agent-libs/logp"
28-
"github.com/elastic/elastic-agent-libs/monitoring"
29-
)
30-
31-
func SetupMetrics(logger *logp.Logger, name, version, ephemeralID string, systemMetrics *monitoring.Registry, processMetrics *monitoring.Registry) error {
32-
logger.Warn("Metrics not implemented for this OS.")
26+
func SetupMetricsOptions(opts MetricOptions) error {
27+
opts.Logger.Warn("Metrics not implemented for this OS.")
3328
return nil
3429
}

0 commit comments

Comments
 (0)