Skip to content

Commit

Permalink
添加指标 process.all.cpu.time (#2)
Browse files Browse the repository at this point in the history
* chore(cpu & memory): cpu 和 memory 指标返回pid, pname, bcwd

* fix typo

* replace cpu time total

* 更新

* 更新单测

* 更新单测

* 更新

---------

Co-authored-by: jimchen <jimchen@easyops.cn>

添加优维定制化主机信息指标 (#3)

Co-authored-by: jimchen <jimchen@easyops.cn>

Revert "添加优维定制化主机信息指标 (#3)" (#4)

This reverts commit 3f024eb.

添加优维定制化主机信息指标 (#5)

* 添加优维定制化主机信息指标

* update info.cpuNum to info.cpu.num

---------

Co-authored-by: jimchen <jimchen@easyops.cn>

修复优维主机信息初始化 (#6)

Co-authored-by: jimchen <jimchen@easyops.cn>

fix info (#7)

Co-authored-by: jimchen <jimchen@easyops.cn>

fix bug Ref #5 (#8)

Co-authored-by: jimchen <jimchen@easyops.cn>

fix(process): 修复进程创建时间未来的问题 (#9)

Co-authored-by: jimchen <jimchen@easyops.cn>

fix(process): 修复物理内存 (#10)

Co-authored-by: jimchen <jimchen@easyops.cn>
  • Loading branch information
JimChenWYU and jimchen committed Jul 8, 2023
1 parent 8b10209 commit fb231ba
Show file tree
Hide file tree
Showing 30 changed files with 1,302 additions and 17 deletions.
2 changes: 2 additions & 0 deletions receiver/hostmetricsreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The available scrapers are:
| Scraper | Supported OSs | Description |
| ------------ | ---------------------------- | ------------------------------------------------------ |
| [cpu] | All except Mac<sup>[1]</sup> | CPU utilization metrics |
| [info] | All | easyops host info |
| [disk] | All except Mac<sup>[1]</sup> | Disk I/O metrics |
| [load] | All | CPU load metrics |
| [filesystem] | All | File System utilization metrics |
Expand All @@ -49,6 +50,7 @@ The available scrapers are:
| [process] | Linux, Windows, Mac | Per process CPU, Memory, and Disk I/O metrics |

[cpu]: ./internal/scraper/cpuscraper/documentation.md
[info]: ./internal/scraper/infoscraper/documentation.md
[disk]: ./internal/scraper/diskscraper/documentation.md
[filesystem]: ./internal/scraper/filesystemscraper/documentation.md
[load]: ./internal/scraper/loadscraper/documentation.md
Expand Down
2 changes: 2 additions & 0 deletions receiver/hostmetricsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/cpuscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/diskscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/infoscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/loadscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/memoryscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/networkscraper"
Expand All @@ -30,6 +31,7 @@ import (
var (
scraperFactories = map[string]internal.ScraperFactory{
cpuscraper.TypeStr: &cpuscraper.Factory{},
infoscraper.TypeStr: &infoscraper.Factory{},
diskscraper.TypeStr: &diskscraper.Factory{},
loadscraper.TypeStr: &loadscraper.Factory{},
filesystemscraper.TypeStr: &filesystemscraper.Factory{},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/infoscraper/internal/metadata"
)

// Config relating to Load Metric Scraper.
type Config struct {
metadata.MetricsBuilderConfig `mapstructure:",squash"`
internal.ScraperConfig
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build !windows
// +build !windows

//go:generate mdatagen metadata.yaml

package infoscraper // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/infoscraper"
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[comment]: <> (Code generated by mdatagen. DO NOT EDIT.)

# hostmetricsreceiver/info

## Default Metrics

The following metrics are emitted by default. Each of them can be disabled by applying the following configuration:

```yaml
metrics:
<metric_name>:
enabled: false
```
### info.now
unix timestamp.
| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
| ---- | ----------- | ---------- | ----------------------- | --------- |
| 1 | Sum | Int | Cumulative | false |
#### Attributes
| Name | Description | Values |
| ---- | ----------- | ------ |
| org | org | Any Str |
| hostname | hostname. | Any Str |
| cpu.num | cpu num. | Any Int |
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

import (
"context"

"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/scraperhelper"

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

// This file implements Factory for Load scraper.

const (
// TypeStr the value of "type" key in configuration.
TypeStr = "info"
)

// Factory is the Factory for scraper.
type Factory struct {
}

// CreateDefaultConfig creates the default configuration for the Scraper.
func (f *Factory) CreateDefaultConfig() internal.Config {
return &Config{
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
}
}

// CreateMetricsScraper creates a scraper based on provided config.
func (f *Factory) CreateMetricsScraper(
ctx context.Context,
settings receiver.CreateSettings,
config internal.Config,
) (scraperhelper.Scraper, error) {
cfg := config.(*Config)
s := newInfoScraper(ctx, settings, cfg)

return scraperhelper.NewScraper(
TypeStr,
s.scrape,
scraperhelper.WithStart(s.start),
scraperhelper.WithShutdown(s.shutdown),
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package infoscraper

import (
"context"
"testing"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/receiver"
)

func TestFactory_CreateDefaultConfig(t *testing.T) {
tests := []struct {
name string
f *Factory
want internal.Config
}{
{
f: &Factory{},
want: &Config{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.f.CreateDefaultConfig()
assert.IsType(t, got, tt.want)
})
}
}

func TestFactory_CreateMetricsScraper(t *testing.T) {
type args struct {
settings receiver.CreateSettings
config internal.Config
}
tests := []struct {
name string
f *Factory
args args
wantErr bool
}{
{
f: &Factory{},
args: args{
settings: receiver.CreateSettings{},
config: &Config{},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.f.CreateMetricsScraper(context.Background(), tt.args.settings, tt.args.config)
if (err != nil) != tt.wantErr {
t.Errorf("Factory.CreateMetricsScraper() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.NotNil(t, got)
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// TODO: support windows
package infoscraper // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/infoscraper"

import (
"context"
"os"
"runtime"
"time"

"github.com/shirou/gopsutil/v3/host"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/scrapererror"

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

const metricsLen = 1

// scraper for Load Metrics
type scraper struct {
settings receiver.CreateSettings
config *Config
mb *metadata.MetricsBuilder

hostname func() (name string, err error)
now func() time.Time
cpuNum func() int
org func() string
// for mocking
bootTime func() (uint64, error)
}

func newInfoScraper(_ context.Context, settings receiver.CreateSettings, cfg *Config) *scraper {
return &scraper{
settings: settings,
config: cfg,

now: time.Now,
hostname: os.Hostname,
cpuNum: runtime.NumCPU,
org: org,

bootTime: host.BootTime,
}
}

func (s *scraper) start(ctx context.Context, _ component.Host) error {
bootTime, err := s.bootTime()
if err != nil {
return err
}

s.mb = metadata.NewMetricsBuilder(s.config.MetricsBuilderConfig, s.settings, metadata.WithStartTime(pcommon.Timestamp(bootTime*1e9)))
return nil
}

// shutdown
func (s *scraper) shutdown(ctx context.Context) error {
return nil
}

// scrape
func (s *scraper) scrape(_ context.Context) (pmetric.Metrics, error) {
now := pcommon.NewTimestampFromTime(s.now())

hostname, err := s.hostname()
if err != nil {
return pmetric.NewMetrics(), scrapererror.NewPartialScrapeError(err, metricsLen)
}
s.mb.RecordInfoNowDataPoint(
now, now.AsTime().Unix(),
org(),
hostname,
int64(s.cpuNum()),
)

return s.mb.Emit(), nil
}

func org() string {
return os.Getenv("EASY_ENV_COMMON_ORG")
}
Loading

0 comments on commit fb231ba

Please sign in to comment.