-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Minor additions to self-observability metrics #1549
Merged
pjanotti
merged 1 commit into
open-telemetry:master
from
james-bebbington:add-uptime-and-rss-metrics
Aug 20, 2020
Merged
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -20,17 +20,30 @@ import ( | |
"runtime" | ||
"time" | ||
|
||
"github.com/prometheus/procfs" | ||
"github.com/shirou/gopsutil/process" | ||
"go.opencensus.io/stats" | ||
"go.opencensus.io/stats/view" | ||
) | ||
|
||
// ProcessMetricsViews is a struct that contains views related to process metrics (cpu, mem, etc) | ||
type ProcessMetricsViews struct { | ||
prevTimeUnixNano int64 | ||
ballastSizeBytes uint64 | ||
views []*view.View | ||
done chan struct{} | ||
proc *procfs.Proc | ||
proc *process.Process | ||
} | ||
|
||
var mUptime = stats.Float64( | ||
"process/uptime", | ||
"Uptime of the process", | ||
stats.UnitSeconds) | ||
var viewProcessUptime = &view.View{ | ||
Name: mUptime.Name(), | ||
Description: mUptime.Description(), | ||
Measure: mUptime, | ||
Aggregation: view.Sum(), | ||
TagKeys: nil, | ||
} | ||
|
||
var mRuntimeAllocMem = stats.Int64( | ||
|
@@ -81,23 +94,37 @@ var viewCPUSeconds = &view.View{ | |
TagKeys: nil, | ||
} | ||
|
||
var mRSSMemory = stats.Int64( | ||
"process/memory/rss", | ||
"Total physical memory (resident set size)", | ||
stats.UnitDimensionless) | ||
var viewRSSMemory = &view.View{ | ||
Name: mRSSMemory.Name(), | ||
Description: mRSSMemory.Description(), | ||
Measure: mRSSMemory, | ||
Aggregation: view.LastValue(), | ||
TagKeys: nil, | ||
} | ||
|
||
// NewProcessMetricsViews creates a new set of ProcessMetrics (mem, cpu) that can be used to measure | ||
// basic information about this process. | ||
func NewProcessMetricsViews(ballastSizeBytes uint64) *ProcessMetricsViews { | ||
func NewProcessMetricsViews(ballastSizeBytes uint64) (*ProcessMetricsViews, error) { | ||
pmv := &ProcessMetricsViews{ | ||
prevTimeUnixNano: time.Now().UnixNano(), | ||
ballastSizeBytes: ballastSizeBytes, | ||
views: []*view.View{viewAllocMem, viewTotalAllocMem, viewSysMem, viewCPUSeconds}, | ||
views: []*view.View{viewProcessUptime, viewAllocMem, viewTotalAllocMem, viewSysMem, viewCPUSeconds, viewRSSMemory}, | ||
done: make(chan struct{}), | ||
} | ||
|
||
// procfs.Proc is not available on windows and expected to fail. | ||
pid := os.Getpid() | ||
proc, err := procfs.NewProc(pid) | ||
if err == nil { | ||
pmv.proc = &proc | ||
|
||
var err error | ||
pmv.proc, err = process.NewProcess(int32(pid)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return pmv | ||
return pmv, nil | ||
} | ||
|
||
// StartCollection starts a ticker'd goroutine that will update the PMV measurements every 5 seconds | ||
|
@@ -127,15 +154,22 @@ func (pmv *ProcessMetricsViews) StopCollection() { | |
} | ||
|
||
func (pmv *ProcessMetricsViews) updateViews() { | ||
now := time.Now().UnixNano() | ||
stats.Record(context.Background(), mUptime.M(float64(now-pmv.prevTimeUnixNano)/1e9)) | ||
pmv.prevTimeUnixNano = now | ||
|
||
ms := &runtime.MemStats{} | ||
pmv.readMemStats(ms) | ||
stats.Record(context.Background(), mRuntimeAllocMem.M(int64(ms.Alloc))) | ||
stats.Record(context.Background(), mRuntimeTotalAllocMem.M(int64(ms.TotalAlloc))) | ||
stats.Record(context.Background(), mRuntimeSysMem.M(int64(ms.Sys))) | ||
|
||
if pmv.proc != nil { | ||
if procStat, err := pmv.proc.Stat(); err == nil { | ||
stats.Record(context.Background(), mCPUSeconds.M(int64(procStat.CPUTime()))) | ||
if times, err := pmv.proc.Times(); err == nil { | ||
stats.Record(context.Background(), mCPUSeconds.M(int64(times.Total()))) | ||
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. This should really be a 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. We should plan to fix this in a follow-up. |
||
} | ||
if mem, err := pmv.proc.MemoryInfo(); err == nil { | ||
stats.Record(context.Background(), mRSSMemory.M(int64(mem.RSS))) | ||
} | ||
} | ||
} | ||
|
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
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.
Why not last value? Then you wouldn't need to calculate delta (i.e. record
prevTimeUnixNano
) and just report the current value, as far as I undertand.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.
Tbh I'm not super familiar with the OpenCensus APIs, and this may not be the right way to do this. But the reason for choosing
Sum
was so this is created as aCounter
metric. If I useLastValue
, this is created as aGauge
.