-
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
Changes from 3 commits
e669457
80d35ac
d8f3dad
8dc379b
20543c6
0f3d967
32f660f
9c65bf0
0eeda28
849f534
09d5b42
6c8e7ab
c4121d6
9b890aa
61474ab
980eddd
6c7c2bf
147020e
16bfa68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package diagnostics | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/ledgerwatch/erigon-lib/common/dbg" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
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) | ||
}) | ||
|
||
// update prometheus memory stats at least every 30 seconds | ||
shohamc1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
go func() { | ||
ticker := time.NewTicker(30 * time.Second) | ||
defer ticker.Stop() | ||
|
||
for { | ||
shohamc1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dbg.GetMemUsage() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggest to be explicit when swallowing an err:
or
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From one side: “it maybe does stop the world”, from another: do stop the world in “only 1” goroutine with predictable timer maybe better than from many goroutines. so, i would say: yes, and remove all other usages. With 1min timer. |
||
<-ticker.C | ||
} | ||
}() | ||
} | ||
|
||
func writeMem(w http.ResponseWriter) { | ||
memStats, err := dbg.GetMemUsage() | ||
|
||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
json.NewEncoder(w).Encode(memStats) | ||
shohamc1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package dbg | ||
|
||
import ( | ||
"github.com/ledgerwatch/erigon-lib/metrics" | ||
"github.com/shirou/gopsutil/v3/process" | ||
"os" | ||
) | ||
|
||
var ( | ||
memRssGauge = metrics.NewGauge(`mem_rss`) | ||
memSizeGauge = metrics.NewGauge(`mem_size`) | ||
memPssGauge = metrics.NewGauge(`mem_pss`) | ||
memSharedCleanGauge = metrics.NewGauge(`mem_shared_clean`) | ||
shohamc1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
memSharedDirtyGauge = metrics.NewGauge(`mem_shared_dirty`) | ||
memPrivateCleanGauge = metrics.NewGauge(`mem_private_clean`) | ||
memPrivateDirtyGauge = metrics.NewGauge(`mem_private_dirty`) | ||
memReferencedGauge = metrics.NewGauge(`mem_referenced`) | ||
memAnonymousGauge = metrics.NewGauge(`mem_anonymous`) | ||
memSwapGauge = metrics.NewGauge(`mem_swap`) | ||
) | ||
|
||
func GetMemUsage() (process.MemoryMapsStat, error) { | ||
pid := os.Getpid() | ||
proc, err := process.NewProcess(int32(pid)) | ||
|
||
shohamc1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return process.MemoryMapsStat{}, err | ||
} | ||
|
||
memoryMaps, err := proc.MemoryMaps(true) | ||
|
||
if err != nil { | ||
return process.MemoryMapsStat{}, err | ||
} | ||
|
||
memRssGauge.SetUint64((*memoryMaps)[0].Rss) | ||
memSizeGauge.SetUint64((*memoryMaps)[0].Size) | ||
memPssGauge.SetUint64((*memoryMaps)[0].Pss) | ||
memSharedCleanGauge.SetUint64((*memoryMaps)[0].SharedClean) | ||
memSharedDirtyGauge.SetUint64((*memoryMaps)[0].SharedDirty) | ||
memPrivateCleanGauge.SetUint64((*memoryMaps)[0].PrivateClean) | ||
memPrivateDirtyGauge.SetUint64((*memoryMaps)[0].PrivateDirty) | ||
memReferencedGauge.SetUint64((*memoryMaps)[0].Referenced) | ||
memAnonymousGauge.SetUint64((*memoryMaps)[0].Anonymous) | ||
memSwapGauge.SetUint64((*memoryMaps)[0].Swap) | ||
|
||
return (*memoryMaps)[0], nil | ||
} |
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.