-
Notifications
You must be signed in to change notification settings - Fork 362
feat: push metrics #5025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+188
−68
Closed
feat: push metrics #5025
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ff195e0
feat: push metrics
istae 964b174
fix: lint
istae 1544c3e
fix: include only
istae 87e9872
fix: bee mode
istae bda61ec
feat: added bee mode also cpu and mem usage metrics
martinconic ded4c32
fix: lint issue
martinconic c9a55f9
fix: go mod tidy check
martinconic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Copyright 2025 The Swarm Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package registry | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/ethersphere/bee/v2" | ||
"github.com/ethersphere/bee/v2/pkg/log" | ||
"github.com/ethersphere/bee/v2/pkg/metrics" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/collectors" | ||
"github.com/prometheus/client_golang/prometheus/push" | ||
"github.com/shirou/gopsutil/cpu" | ||
"github.com/shirou/gopsutil/mem" | ||
) | ||
|
||
type Registry struct { | ||
register *prometheus.Registry | ||
pushRegister *prometheus.Registry | ||
cpuGauge prometheus.Gauge | ||
memGauge prometheus.Gauge | ||
} | ||
|
||
func NewRegistry(push bool, mode string) *Registry { | ||
r := &Registry{ | ||
register: prometheus.NewRegistry(), | ||
pushRegister: prometheus.NewRegistry(), | ||
} | ||
|
||
c := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{ | ||
Namespace: metrics.Namespace, | ||
}) | ||
|
||
g := collectors.NewGoCollector() | ||
|
||
// Create CPU and memory gauge metrics | ||
cpuGauge := prometheus.NewGauge(prometheus.GaugeOpts{ | ||
Namespace: metrics.Namespace, | ||
Name: "system_cpu_usage_percent", | ||
Help: "System CPU usage percentage", | ||
}) | ||
|
||
memGauge := prometheus.NewGauge(prometheus.GaugeOpts{ | ||
Namespace: metrics.Namespace, | ||
Name: "system_memory_usage_percent", | ||
Help: "System memory usage percentage", | ||
}) | ||
|
||
v := prometheus.NewGauge(prometheus.GaugeOpts{ | ||
Namespace: metrics.Namespace, | ||
Name: "info", | ||
Help: "Bee information.", | ||
ConstLabels: prometheus.Labels{ | ||
"version": bee.Version, | ||
"mode": mode, | ||
}, | ||
}) | ||
|
||
r.MustRegister(c, g, v, cpuGauge, memGauge) | ||
|
||
if push { | ||
r.MustPushRegister(c, g, v, cpuGauge, memGauge) | ||
} | ||
|
||
// Store references to gauges for updating in PushWorker | ||
r.cpuGauge = cpuGauge | ||
r.memGauge = memGauge | ||
|
||
return r | ||
} | ||
|
||
func (r *Registry) MetricsRegistry() *prometheus.Registry { | ||
return r.register | ||
} | ||
|
||
func (r *Registry) PushWorker(ctx context.Context, path string, job string, logger log.Logger) func() error { | ||
metricsPusher := push.New(path, job).Collector(r.pushRegister) | ||
|
||
ctx, cancel := context.WithCancel(ctx) | ||
|
||
go func() { | ||
ticker := time.NewTicker(time.Minute) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-ticker.C: | ||
// Collect CPU metrics | ||
percentages, err := cpu.Percent(0, false) | ||
if err == nil && len(percentages) > 0 { | ||
r.cpuGauge.Set(percentages[0]) | ||
} else if err != nil { | ||
logger.Debug("failed to collect CPU metrics", "error", err) | ||
} | ||
|
||
// Collect memory metrics | ||
vm, err := mem.VirtualMemory() | ||
if err == nil { | ||
r.memGauge.Set(vm.UsedPercent) | ||
} else { | ||
logger.Debug("failed to collect memory metrics", "error", err) | ||
} | ||
|
||
// Push metrics | ||
if err := metricsPusher.Push(); err != nil { | ||
logger.Debug("metrics push failed", "error", err) | ||
} | ||
} | ||
} | ||
}() | ||
|
||
return func() error { | ||
cancel() | ||
return metricsPusher.Push() | ||
} | ||
} | ||
|
||
func (r *Registry) MustRegister(cs ...prometheus.Collector) { | ||
r.register.MustRegister(cs...) | ||
} | ||
|
||
func (r *Registry) MustPushRegister(cs ...prometheus.Collector) { | ||
r.pushRegister.MustRegister(cs...) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think when the metrics are gathered for pushing, the mem and cpu metrics are already collected, I dont think you need to add them manually like this