forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
8b10209
commit fb231ba
Showing
30 changed files
with
1,302 additions
and
17 deletions.
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
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
15 changes: 15 additions & 0 deletions
15
receiver/hostmetricsreceiver/internal/scraper/infoscraper/config.go
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 |
---|---|---|
@@ -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 | ||
} |
9 changes: 9 additions & 0 deletions
9
receiver/hostmetricsreceiver/internal/scraper/infoscraper/doc.go
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 |
---|---|---|
@@ -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" |
29 changes: 29 additions & 0 deletions
29
receiver/hostmetricsreceiver/internal/scraper/infoscraper/documentation.md
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 |
---|---|---|
@@ -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 | |
49 changes: 49 additions & 0 deletions
49
receiver/hostmetricsreceiver/internal/scraper/infoscraper/factory.go
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 |
---|---|---|
@@ -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), | ||
) | ||
} |
64 changes: 64 additions & 0 deletions
64
receiver/hostmetricsreceiver/internal/scraper/infoscraper/factory_test.go
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 |
---|---|---|
@@ -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) | ||
}) | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
receiver/hostmetricsreceiver/internal/scraper/infoscraper/info_scraper.go
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 |
---|---|---|
@@ -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") | ||
} |
Oops, something went wrong.