-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added processes scraper to scrape the no. of processes created, runni…
…ng, blocked (currently not supported on Windows)
- Loading branch information
1 parent
add8f61
commit 85f75f8
Showing
10 changed files
with
410 additions
and
5 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
22 changes: 22 additions & 0 deletions
22
receiver/hostmetricsreceiver/internal/scraper/processesscraper/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,22 @@ | ||
// 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. | ||
|
||
package processesscraper | ||
|
||
import "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal" | ||
|
||
// Config relating to Processes Metric Scraper. | ||
type Config struct { | ||
internal.ConfigSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct | ||
} |
49 changes: 49 additions & 0 deletions
49
receiver/hostmetricsreceiver/internal/scraper/processesscraper/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 | ||
// | ||
// 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. | ||
|
||
package processesscraper | ||
|
||
import ( | ||
"context" | ||
|
||
"go.uber.org/zap" | ||
|
||
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal" | ||
) | ||
|
||
// This file implements Factory for Processes scraper. | ||
|
||
const ( | ||
// The value of "type" key in configuration. | ||
TypeStr = "processes" | ||
) | ||
|
||
// 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{} | ||
} | ||
|
||
// CreateMetricsScraper creates a scraper based on provided config. | ||
func (f *Factory) CreateMetricsScraper( | ||
ctx context.Context, | ||
_ *zap.Logger, | ||
config internal.Config, | ||
) (internal.Scraper, error) { | ||
cfg := config.(*Config) | ||
return newProcessesScraper(ctx, cfg), nil | ||
} |
39 changes: 39 additions & 0 deletions
39
receiver/hostmetricsreceiver/internal/scraper/processesscraper/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,39 @@ | ||
// 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. | ||
|
||
package processesscraper | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestCreateDefaultConfig(t *testing.T) { | ||
factory := &Factory{} | ||
cfg := factory.CreateDefaultConfig() | ||
assert.IsType(t, &Config{}, cfg) | ||
} | ||
|
||
func TestCreateMetricsScraper(t *testing.T) { | ||
factory := &Factory{} | ||
cfg := &Config{} | ||
|
||
scraper, err := factory.CreateMetricsScraper(context.Background(), zap.NewNop(), cfg) | ||
|
||
assert.NoError(t, err) | ||
assert.NotNil(t, scraper) | ||
} |
51 changes: 51 additions & 0 deletions
51
receiver/hostmetricsreceiver/internal/scraper/processesscraper/processes_metadata.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,51 @@ | ||
// 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. | ||
|
||
package processesscraper | ||
|
||
import ( | ||
"go.opentelemetry.io/collector/consumer/pdata" | ||
) | ||
|
||
// descriptors | ||
|
||
var processesCreatedDescriptor = func() pdata.MetricDescriptor { | ||
descriptor := pdata.NewMetricDescriptor() | ||
descriptor.InitEmpty() | ||
descriptor.SetName("system.processes.created") | ||
descriptor.SetDescription("Total number of times a process was created.") | ||
descriptor.SetUnit("1") | ||
descriptor.SetType(pdata.MetricTypeMonotonicInt64) | ||
return descriptor | ||
}() | ||
|
||
var processesRunningDescriptor = func() pdata.MetricDescriptor { | ||
descriptor := pdata.NewMetricDescriptor() | ||
descriptor.InitEmpty() | ||
descriptor.SetName("system.processes.running") | ||
descriptor.SetDescription("Total number of running processes.") | ||
descriptor.SetUnit("1") | ||
descriptor.SetType(pdata.MetricTypeInt64) | ||
return descriptor | ||
}() | ||
|
||
var processesBlockedDescriptor = func() pdata.MetricDescriptor { | ||
descriptor := pdata.NewMetricDescriptor() | ||
descriptor.InitEmpty() | ||
descriptor.SetName("system.processes.blocked") | ||
descriptor.SetDescription("Total number of blocked processes.") | ||
descriptor.SetUnit("1") | ||
descriptor.SetType(pdata.MetricTypeInt64) | ||
return descriptor | ||
}() |
67 changes: 67 additions & 0 deletions
67
receiver/hostmetricsreceiver/internal/scraper/processesscraper/processes_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,67 @@ | ||
// 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. | ||
|
||
package processesscraper | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/shirou/gopsutil/host" | ||
"github.com/shirou/gopsutil/load" | ||
"go.opencensus.io/trace" | ||
|
||
"go.opentelemetry.io/collector/consumer/pdata" | ||
) | ||
|
||
// scraper for Processes Metrics | ||
type scraper struct { | ||
config *Config | ||
startTime pdata.TimestampUnixNano | ||
|
||
// for mocking gopsutil load.Misc | ||
misc getMiscStats | ||
} | ||
|
||
type getMiscStats func() (*load.MiscStat, error) | ||
|
||
// newProcessesScraper creates a set of Processes related metrics | ||
func newProcessesScraper(_ context.Context, cfg *Config) *scraper { | ||
return &scraper{config: cfg, misc: load.Misc} | ||
} | ||
|
||
// Initialize | ||
func (s *scraper) Initialize(_ context.Context) error { | ||
bootTime, err := host.BootTime() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s.startTime = pdata.TimestampUnixNano(bootTime) | ||
return nil | ||
} | ||
|
||
// Close | ||
func (s *scraper) Close(_ context.Context) error { | ||
return nil | ||
} | ||
|
||
// ScrapeMetrics | ||
func (s *scraper) ScrapeMetrics(ctx context.Context) (pdata.MetricSlice, error) { | ||
_, span := trace.StartSpan(ctx, "processesscraper.ScrapeMetrics") | ||
defer span.End() | ||
|
||
metrics := pdata.NewMetricSlice() | ||
appendSystemSpecificProcessesMetrics(metrics, 0, s.misc) | ||
return metrics, nil | ||
} |
25 changes: 25 additions & 0 deletions
25
receiver/hostmetricsreceiver/internal/scraper/processesscraper/processes_scraper_fallback.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,25 @@ | ||
// 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. | ||
|
||
// +build !linux,!darwin,!freebsd,!openbsd | ||
|
||
package processesscraper | ||
|
||
import ( | ||
"go.opentelemetry.io/collector/consumer/pdata" | ||
) | ||
|
||
func appendSystemSpecificProcessesMetrics(metrics pdata.MetricSlice, startIndex int, miscFunc getMiscStats) error { | ||
return nil | ||
} |
57 changes: 57 additions & 0 deletions
57
receiver/hostmetricsreceiver/internal/scraper/processesscraper/processes_scraper_others.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,57 @@ | ||
// 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. | ||
|
||
// +build linux darwin freebsd openbsd | ||
|
||
package processesscraper | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/shirou/gopsutil/load" | ||
|
||
"go.opentelemetry.io/collector/consumer/pdata" | ||
) | ||
|
||
const processesStatesLen = 2 | ||
|
||
func appendSystemSpecificProcessesMetrics(metrics pdata.MetricSlice, startIndex int, miscFunc getMiscStats) error { | ||
misc, err := miscFunc() | ||
if err != nil { | ||
return metrics, err | ||
} | ||
|
||
metrics.Resize(startIndex + 2) | ||
initializeProcessesRunningMetric(metrics.At(startIndex+0), misc) | ||
initializeProcessesBlockedMetric(metrics.At(startIndex+1), misc) | ||
return nil | ||
} | ||
|
||
func initializeProcessesRunningMetric(metric pdata.Metric, misc *load.MiscStat) { | ||
processesRunningDescriptor.CopyTo(metric.MetricDescriptor()) | ||
|
||
ddps := metric.Int64DataPoints() | ||
ddps.Resize(1) | ||
ddps.At(0).SetTimestamp(pdata.TimestampUnixNano(uint64(time.Now().UnixNano()))) | ||
ddps.At(0).SetValue(int64(misc.ProcsRunning)) | ||
} | ||
|
||
func initializeProcessesBlockedMetric(metric pdata.Metric, misc *load.MiscStat) { | ||
processesBlockedDescriptor.CopyTo(metric.MetricDescriptor()) | ||
|
||
ddps := metric.Int64DataPoints() | ||
ddps.Resize(1) | ||
ddps.At(0).SetTimestamp(pdata.TimestampUnixNano(uint64(time.Now().UnixNano()))) | ||
ddps.At(0).SetValue(int64(misc.ProcsBlocked)) | ||
} |
Oops, something went wrong.