Skip to content
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

[receiver/hostmetricsreceiver] Add threads count metric #12802

Merged
merged 7 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ These are the metrics available for this scraper.
| **process.disk.io.write** | Disk bytes written. | By | Sum(Int) | <ul> </ul> |
| **process.memory.physical_usage** | The amount of physical memory in use. | By | Sum(Int) | <ul> </ul> |
| **process.memory.virtual_usage** | Virtual memory size. | By | Sum(Int) | <ul> </ul> |
| process.threads | Process threads count. | {threads} | Sum(Int) | <ul> </ul> |

**Highlighted metrics** are emitted by default. Other metrics are optional and not emitted by default.
Any metric can be enabled or disabled with the following scraper configuration:
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,12 @@ metrics:
value_type: int
aggregation: cumulative
monotonic: true

process.threads:
enabled: false
description: Process threads count.
unit: "{threads}"
sum:
value_type: int
aggregation: cumulative
monotonic: false
atoulme marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type processHandle interface {
Times() (*cpu.TimesStat, error)
MemoryInfo() (*process.MemoryInfoStat, error)
IOCounters() (*process.IOCountersStat, error)
NumThreads() (int32, error)
CreateTime() (int64, error)
Parent() (*process.Process, error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ const (
cpuMetricsLen = 1
memoryMetricsLen = 2
diskMetricsLen = 1
threadMetricsLen = 1

metricsLen = cpuMetricsLen + memoryMetricsLen + diskMetricsLen
metricsLen = cpuMetricsLen + memoryMetricsLen + diskMetricsLen + threadMetricsLen
)

// scraper for Process Metrics
Expand Down Expand Up @@ -124,6 +125,10 @@ func (s *scraper) scrape(_ context.Context) (pmetric.Metrics, error) {
errs.AddPartial(diskMetricsLen, fmt.Errorf("error reading disk usage for process %q (pid %v): %w", md.executable.name, md.pid, err))
}

if err = s.scrapeAndAppendThreadsMetrics(now, md.handle); err != nil {
errs.AddPartial(threadMetricsLen, fmt.Errorf("error reading thread info for process %q (pid %v): %w", md.executable.name, md.pid, err))
}

s.mb.EmitForResource(md.resourceOptions()...)
}

Expand Down Expand Up @@ -239,3 +244,13 @@ func (s *scraper) scrapeAndAppendDiskIOMetric(now pcommon.Timestamp, handle proc

return nil
}

func (s *scraper) scrapeAndAppendThreadsMetrics(now pcommon.Timestamp, handle processHandle) error {
threads, err := handle.NumThreads()
dmitryax marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
s.mb.RecordProcessThreadsDataPoint(now, int64(threads))

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ func (p *processHandleMock) IOCounters() (*process.IOCountersStat, error) {
return args.Get(0).(*process.IOCountersStat), args.Error(1)
}

func (p *processHandleMock) NumThreads() (int32, error) {
args := p.MethodCalled("NumThreads")
return args.Get(0).(int32), args.Error(1)
}

func (p *processHandleMock) CreateTime() (int64, error) {
args := p.MethodCalled("CreateTime")
return args.Get(0).(int64), args.Error(1)
Expand All @@ -325,6 +330,7 @@ func newDefaultHandleMock() *processHandleMock {
handleMock.On("MemoryInfo").Return(&process.MemoryInfoStat{}, nil)
handleMock.On("IOCounters").Return(&process.IOCountersStat{}, nil)
handleMock.On("Parent").Return(&process.Process{Pid: 2}, nil)
handleMock.On("NumThreads").Return(int32(0), nil)
return handleMock
}

Expand Down Expand Up @@ -468,6 +474,7 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) {
ioCountersError error
createTimeError error
parentPidError error
numThreadsError error
expectedError string
}

Expand Down Expand Up @@ -518,6 +525,11 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) {
parentPidError: errors.New("err8"),
expectedError: `error reading parent pid for process "test" (pid 1): err8`,
},
{
name: "Thread count Error",
numThreadsError: errors.New("err8"),
expectedError: `error reading thread info for process "test" (pid 1): err8`,
},
{
name: "Multiple Errors",
cmdlineError: errors.New("err2"),
Expand All @@ -526,12 +538,14 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) {
timesError: errors.New("err5"),
memoryInfoError: errors.New("err6"),
ioCountersError: errors.New("err7"),
numThreadsError: errors.New("err8"),
expectedError: `error reading command for process "test" (pid 1): err2; ` +
`error reading username for process "test" (pid 1): err3; ` +
`error reading create time for process "test" (pid 1): err4; ` +
`error reading cpu times for process "test" (pid 1): err5; ` +
`error reading memory info for process "test" (pid 1): err6; ` +
`error reading disk usage for process "test" (pid 1): err7`,
`error reading disk usage for process "test" (pid 1): err7; ` +
`error reading thread info for process "test" (pid 1): err8`,
},
}

Expand All @@ -541,7 +555,9 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) {
t.Skipf("skipping test %v on %v", test.name, runtime.GOOS)
}

scraper, err := newProcessScraper(componenttest.NewNopReceiverCreateSettings(), &Config{Metrics: metadata.DefaultMetricsSettings()})
metricsSettings := metadata.DefaultMetricsSettings()
metricsSettings.ProcessThreads.Enabled = true
scraper, err := newProcessScraper(componenttest.NewNopReceiverCreateSettings(), &Config{Metrics: metricsSettings})
require.NoError(t, err, "Failed to create process scraper: %v", err)
err = scraper.start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err, "Failed to initialize process scraper: %v", err)
Expand All @@ -562,22 +578,23 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) {
handleMock.On("IOCounters").Return(&process.IOCountersStat{}, test.ioCountersError)
handleMock.On("CreateTime").Return(int64(0), test.createTimeError)
handleMock.On("Parent").Return(&process.Process{Pid: 2}, test.parentPidError)
handleMock.On("NumThreads").Return(int32(0), test.numThreadsError)

scraper.getProcessHandles = func() (processHandles, error) {
return &processHandlesMock{handles: []*processHandleMock{handleMock}}, nil
}

md, err := scraper.scrape(context.Background())

expectedResourceMetricsLen, expectedMetricsLen := getExpectedLengthOfReturnedMetrics(test.nameError, test.exeError, test.timesError, test.memoryInfoError, test.ioCountersError)
expectedResourceMetricsLen, expectedMetricsLen := getExpectedLengthOfReturnedMetrics(test.nameError, test.exeError, test.timesError, test.memoryInfoError, test.ioCountersError, test.numThreadsError)
assert.Equal(t, expectedResourceMetricsLen, md.ResourceMetrics().Len())
assert.Equal(t, expectedMetricsLen, md.MetricCount())

assert.EqualError(t, err, test.expectedError)
isPartial := scrapererror.IsPartialScrapeError(err)
assert.True(t, isPartial)
if isPartial {
expectedFailures := getExpectedScrapeFailures(test.nameError, test.exeError, test.timesError, test.memoryInfoError, test.ioCountersError)
expectedFailures := getExpectedScrapeFailures(test.nameError, test.exeError, test.timesError, test.memoryInfoError, test.ioCountersError, test.numThreadsError)
var scraperErr scrapererror.PartialScrapeError
require.ErrorAs(t, err, &scraperErr)
assert.Equal(t, expectedFailures, scraperErr.Failed)
Expand All @@ -586,7 +603,7 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) {
}
}

func getExpectedLengthOfReturnedMetrics(nameError, exeError, timeError, memError, diskError error) (int, int) {
func getExpectedLengthOfReturnedMetrics(nameError, exeError, timeError, memError, diskError, threadError error) (int, int) {
if nameError != nil || exeError != nil {
return 0, 0
}
Expand All @@ -601,18 +618,21 @@ func getExpectedLengthOfReturnedMetrics(nameError, exeError, timeError, memError
if diskError == nil {
expectedLen += diskMetricsLen
}
if threadError == nil {
expectedLen += threadMetricsLen
}

if expectedLen == 0 {
return 0, 0
}
return 1, expectedLen
}

func getExpectedScrapeFailures(nameError, exeError, timeError, memError, diskError error) int {
func getExpectedScrapeFailures(nameError, exeError, timeError, memError, diskError, threadError error) int {
if nameError != nil || exeError != nil {
return 1
}
_, expectedMetricsLen := getExpectedLengthOfReturnedMetrics(nameError, exeError, timeError, memError, diskError)
_, expectedMetricsLen := getExpectedLengthOfReturnedMetrics(nameError, exeError, timeError, memError, diskError, threadError)
return metricsLen - expectedMetricsLen
}

Expand Down
16 changes: 16 additions & 0 deletions unreleased/add-threads-count-metric.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: hostmetricsreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add threads count metric

# One or more tracking issues related to the change
issues: [12482]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: