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

Enabled proccessmetrics for MacOS #18723

Merged
merged 7 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
16 changes: 16 additions & 0 deletions .chloggen/procesmetricsmac.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: Process Metrics for Mac

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Enabeling process metrics to be collcted for Mac in the hostmetrics receiver
8naama marked this conversation as resolved.
Show resolved Hide resolved

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

# (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:
4 changes: 2 additions & 2 deletions receiver/hostmetricsreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ The available scrapers are:
| [memory] | All | Memory utilization metrics |
| [network] | All | Network interface I/O metrics & TCP connection metrics |
| [paging] | All | Paging/Swap space utilization and I/O metrics |
| [processes] | Linux | Process count metrics |
| [process] | Linux & Windows | Per process CPU, Memory, and Disk I/O metrics |
| [processes] | Linux, Mac | Process count metrics |
| [process] | Linux, Windows, Mac | Per process CPU, Memory, and Disk I/O metrics |

[cpu]: ./internal/scraper/cpuscraper/documentation.md
[disk]: ./internal/scraper/diskscraper/documentation.md
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func (f *Factory) CreateMetricsScraper(
settings receiver.CreateSettings,
cfg internal.Config,
) (scraperhelper.Scraper, error) {
if runtime.GOOS != "linux" && runtime.GOOS != "windows" {
return nil, errors.New("process scraper only available on Linux or Windows")
if runtime.GOOS != "linux" && runtime.GOOS != "windows" && runtime.GOOS != "darwin" {
return nil, errors.New("process scraper only available on Linux, Windows, or MacOS")
}

s, err := newProcessScraper(settings, cfg.(*Config))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package processscraper // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper"

import (
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -130,7 +131,7 @@ func getProcessHandlesInternal() (processHandles, error) {

func parentPid(handle processHandle, pid int32) (int32, error) {
// special case for pid 0
8naama marked this conversation as resolved.
Show resolved Hide resolved
if pid == 0 {
if pid == 0 || (pid == 1 && runtime.GOOS == "darwin") {
return 0, nil
}
parent, err := handle.Parent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"fmt"
"runtime"
"time"

"github.com/shirou/gopsutil/v3/process"
Expand Down Expand Up @@ -273,7 +274,7 @@ func (s *scraper) scrapeAndAppendMemoryUtilizationMetric(now pcommon.Timestamp,
}

func (s *scraper) scrapeAndAppendDiskMetrics(now pcommon.Timestamp, handle processHandle) error {
if !(s.config.Metrics.ProcessDiskIo.Enabled || s.config.Metrics.ProcessDiskOperations.Enabled) {
if !(s.config.Metrics.ProcessDiskIo.Enabled || s.config.Metrics.ProcessDiskOperations.Enabled) || runtime.GOOS == "darwin" {
return nil
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build darwin
// +build darwin

package processscraper // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper"

import (
"regexp"

"github.com/shirou/gopsutil/v3/cpu"
"go.opentelemetry.io/collector/pdata/pcommon"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper/ucal"
)

func (s *scraper) recordCPUTimeMetric(now pcommon.Timestamp, cpuTime *cpu.TimesStat) {
s.mb.RecordProcessCPUTimeDataPoint(now, cpuTime.User, metadata.AttributeStateUser)
s.mb.RecordProcessCPUTimeDataPoint(now, cpuTime.System, metadata.AttributeStateSystem)
s.mb.RecordProcessCPUTimeDataPoint(now, cpuTime.Iowait, metadata.AttributeStateWait)
}

func (s *scraper) recordCPUUtilization(now pcommon.Timestamp, cpuUtilization ucal.CPUUtilization) {
s.mb.RecordProcessCPUUtilizationDataPoint(now, cpuUtilization.User, metadata.AttributeStateUser)
s.mb.RecordProcessCPUUtilizationDataPoint(now, cpuUtilization.System, metadata.AttributeStateSystem)
s.mb.RecordProcessCPUUtilizationDataPoint(now, cpuUtilization.Iowait, metadata.AttributeStateWait)
}

func getProcessExecutable(proc processHandle) (*executableMetadata, error) {
name, err := proc.Name()
if err != nil {
return nil, err
}

cmdline, err := proc.Cmdline()
if err != nil {
return nil, err
}
regex := regexp.MustCompile(`^\S+`)
exe := regex.FindString(cmdline)

executable := &executableMetadata{name: name, path: exe}
return executable, nil
}

func getProcessCommand(proc processHandle) (*commandMetadata, error) {
cmdline, err := proc.Cmdline()
if err != nil {
return nil, err
}

command := &commandMetadata{command: cmdline, commandLine: cmdline}
return command, nil

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !linux && !windows
// +build !linux,!windows
//go:build !linux && !windows && !darwin
// +build !linux,!windows,!darwin

package processscraper // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper"

Expand Down