-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
metrics: Add memory usage statistics to Prometheus #9465
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e669457
Add mem stats to dbg
shohamc1 80d35ac
Add mem stats to metrics server
shohamc1 d8f3dad
Add to prometheus
shohamc1 8dc379b
Handle errors, split functions
shohamc1 20543c6
Update all usages of Setup
shohamc1 0f3d967
Fix logging
shohamc1 32f660f
Handle cross-platform discrepancies, move goroutine
shohamc1 9c65bf0
Rename metrics
shohamc1 0eeda28
Remove unused changes
shohamc1 849f534
Merge branch 'devel' into shohamc1/mem-stats
shohamc1 09d5b42
Fix submodule commit
shohamc1 6c8e7ab
Add log for exiting goroutine
shohamc1 c4121d6
Handle logging using build tags, remove unused logger
shohamc1 9b890aa
Convert slice inside function
shohamc1 61474ab
Fix mac build
shohamc1 980eddd
Merge branch 'devel' into shohamc1/mem-stats
shohamc1 6c7c2bf
Merge branch 'devel' into shohamc1/mem-stats
AskAlexSharov 147020e
save
AskAlexSharov 16bfa68
Fix lint
shohamc1 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 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,28 @@ | ||
package diagnostics | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/ledgerwatch/erigon-lib/common/mem" | ||
) | ||
|
||
func SetupMemAccess(metricsMux *http.ServeMux) { | ||
metricsMux.HandleFunc("/mem", func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Access-Control-Allow-Origin", "*") | ||
w.Header().Set("Content-Type", "application/json") | ||
writeMem(w) | ||
}) | ||
} | ||
|
||
func writeMem(w http.ResponseWriter) { | ||
memStats, err := mem.ReadVirtualMemStats() | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err := json.NewEncoder(w).Encode(memStats); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
} |
This file contains 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 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,19 @@ | ||
//go:build !linux | ||
|
||
package mem | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/ledgerwatch/log/v3" | ||
"github.com/shirou/gopsutil/v3/process" | ||
) | ||
|
||
func ReadVirtualMemStats() (process.MemoryMapsStat, error) { | ||
return process.MemoryMapsStat{}, errors.New("unsupported platform") | ||
} | ||
|
||
func UpdatePrometheusVirtualMemStats(p process.MemoryMapsStat) {} | ||
|
||
func LogVirtualMemStats(ctx context.Context, logger log.Logger) {} |
This file contains 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,100 @@ | ||
//go:build linux | ||
|
||
package mem | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"reflect" | ||
"time" | ||
|
||
"github.com/ledgerwatch/log/v3" | ||
"github.com/shirou/gopsutil/v3/process" | ||
|
||
"github.com/ledgerwatch/erigon-lib/metrics" | ||
) | ||
|
||
type VirtualMemStat struct { | ||
process.MemoryMapsStat | ||
} | ||
|
||
// Fields converts VirtualMemStat to slice | ||
func (m VirtualMemStat) Fields() []interface{} { | ||
typ := reflect.TypeOf(m.MemoryMapsStat) | ||
val := reflect.ValueOf(m.MemoryMapsStat) | ||
|
||
var s []interface{} | ||
for i := 0; i < typ.NumField(); i++ { | ||
t := typ.Field(i).Name | ||
if t == "Path" { // always empty for aggregated smap statistics | ||
continue | ||
} | ||
|
||
s = append(s, t, val.Field(i).Interface()) | ||
} | ||
|
||
return s | ||
} | ||
|
||
var ( | ||
memRssGauge = metrics.NewGauge(`mem_rss`) | ||
memSizeGauge = metrics.NewGauge(`mem_size`) | ||
memPssGauge = metrics.NewGauge(`mem_pss`) | ||
memSharedCleanGauge = metrics.NewGauge(`mem_shared{type="clean"}`) | ||
memSharedDirtyGauge = metrics.NewGauge(`mem_shared{type="dirty"}`) | ||
memPrivateCleanGauge = metrics.NewGauge(`mem_private{type="clean"}`) | ||
memPrivateDirtyGauge = metrics.NewGauge(`mem_private{type="dirty"}`) | ||
memReferencedGauge = metrics.NewGauge(`mem_referenced`) | ||
memAnonymousGauge = metrics.NewGauge(`mem_anonymous`) | ||
memSwapGauge = metrics.NewGauge(`mem_swap`) | ||
) | ||
|
||
func ReadVirtualMemStats() (process.MemoryMapsStat, error) { | ||
pid := os.Getpid() | ||
proc, err := process.NewProcess(int32(pid)) | ||
if err != nil { | ||
return process.MemoryMapsStat{}, err | ||
} | ||
|
||
memoryMaps, err := proc.MemoryMaps(true) | ||
if err != nil { | ||
return process.MemoryMapsStat{}, err | ||
} | ||
|
||
return (*memoryMaps)[0], nil | ||
} | ||
|
||
func UpdatePrometheusVirtualMemStats(p process.MemoryMapsStat) { | ||
memRssGauge.SetUint64(p.Rss) | ||
memSizeGauge.SetUint64(p.Size) | ||
memPssGauge.SetUint64(p.Pss) | ||
memSharedCleanGauge.SetUint64(p.SharedClean) | ||
memSharedDirtyGauge.SetUint64(p.SharedDirty) | ||
memPrivateCleanGauge.SetUint64(p.PrivateClean) | ||
memPrivateDirtyGauge.SetUint64(p.PrivateDirty) | ||
memReferencedGauge.SetUint64(p.Referenced) | ||
memAnonymousGauge.SetUint64(p.Anonymous) | ||
memSwapGauge.SetUint64(p.Swap) | ||
} | ||
|
||
func LogVirtualMemStats(ctx context.Context, logger log.Logger) { | ||
logEvery := time.NewTicker(180 * time.Second) | ||
defer logEvery.Stop() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-logEvery.C: | ||
memStats, err := ReadVirtualMemStats() | ||
if err != nil { | ||
logger.Warn("[mem] error reading virtual memory stats", "err", err) | ||
shohamc1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
continue | ||
} | ||
|
||
v := VirtualMemStat{memStats} | ||
logger.Info("[mem] virtual memory stats", v.Fields()...) | ||
UpdatePrometheusVirtualMemStats(memStats) | ||
} | ||
} | ||
} |
This file contains 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 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 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.
a bit unclear why need separated http endpoint for memory metrics. we already have
/debug/metrics/prometheus
(enabling by flag --metrics)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 may want to use it to show it in diagnostics
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.
@dvovk i see. but how it answers the question: "why not use existing
/debug/metrics/prometheus
? (enabling by flag --metrics)"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.
Diagnostics are enabled by the metrics flag, however metrics doesn't use the prometheus protocol for gathering data and works using individual requests for specific diagnostics rather than scraping all. Hence it uses additional endpoints for the data its needs.
Having said that it should hit the same data point.