Skip to content

Commit

Permalink
Initial commit of host metrics filesystem scraper (#926)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-bebbington committed May 8, 2020
1 parent cfc9936 commit 5a2f2a4
Show file tree
Hide file tree
Showing 11 changed files with 549 additions and 19 deletions.
1 change: 1 addition & 0 deletions receiver/hostmetricsreceiver/example_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ receivers:
report_per_cpu: true
memory:
disk:
filesystem:

exporters:
logging:
Expand Down
8 changes: 5 additions & 3 deletions receiver/hostmetricsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal"
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/cpuscraper"
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/diskscraper"
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper"
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/memoryscraper"
)

Expand All @@ -49,9 +50,10 @@ type Factory struct {
func NewFactory() *Factory {
return &Factory{
scraperFactories: map[string]internal.Factory{
cpuscraper.TypeStr: &cpuscraper.Factory{},
diskscraper.TypeStr: &diskscraper.Factory{},
memoryscraper.TypeStr: &memoryscraper.Factory{},
cpuscraper.TypeStr: &cpuscraper.Factory{},
diskscraper.TypeStr: &diskscraper.Factory{},
filesystemscraper.TypeStr: &filesystemscraper.Factory{},
memoryscraper.TypeStr: &memoryscraper.Factory{},
},
}
}
Expand Down
52 changes: 36 additions & 16 deletions receiver/hostmetricsreceiver/hostmetrics_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package hostmetricsreceiver

import (
"context"
"runtime"
"testing"
"time"

Expand All @@ -28,9 +29,27 @@ import (
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal"
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/cpuscraper"
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/diskscraper"
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper"
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/memoryscraper"
)

var standardMetrics = []string{
"host/cpu/time",
"host/memory/used",
"host/disk/bytes",
"host/disk/ops",
"host/disk/time",
"host/filesystem/used",
}

var systemSpecificMetrics = map[string][]string{
"linux": {"host/filesystem/inodes/used"},
"darwin": {"host/filesystem/inodes/used"},
"freebsd": {"host/filesystem/inodes/used"},
"openbsd": {"host/filesystem/inodes/used"},
"solaris": {"host/filesystem/inodes/used"},
}

func TestGatherMetrics_EndToEnd(t *testing.T) {
sink := &exportertest.SinkMetricsExporter{}

Expand All @@ -40,19 +59,23 @@ func TestGatherMetrics_EndToEnd(t *testing.T) {
ConfigSettings: internal.ConfigSettings{CollectionIntervalValue: 100 * time.Millisecond},
ReportPerCPU: true,
},
memoryscraper.TypeStr: &memoryscraper.Config{
diskscraper.TypeStr: &diskscraper.Config{
ConfigSettings: internal.ConfigSettings{CollectionIntervalValue: 100 * time.Millisecond},
},
diskscraper.TypeStr: &diskscraper.Config{
filesystemscraper.TypeStr: &filesystemscraper.Config{
ConfigSettings: internal.ConfigSettings{CollectionIntervalValue: 100 * time.Millisecond},
},
memoryscraper.TypeStr: &memoryscraper.Config{
ConfigSettings: internal.ConfigSettings{CollectionIntervalValue: 100 * time.Millisecond},
},
},
}

factories := map[string]internal.Factory{
cpuscraper.TypeStr: &cpuscraper.Factory{},
memoryscraper.TypeStr: &memoryscraper.Factory{},
diskscraper.TypeStr: &diskscraper.Factory{},
cpuscraper.TypeStr: &cpuscraper.Factory{},
diskscraper.TypeStr: &diskscraper.Factory{},
filesystemscraper.TypeStr: &filesystemscraper.Factory{},
memoryscraper.TypeStr: &memoryscraper.Factory{},
}

receiver, err := NewHostMetricsReceiver(context.Background(), zap.NewNop(), config, factories, sink)
Expand All @@ -67,8 +90,8 @@ func TestGatherMetrics_EndToEnd(t *testing.T) {

got := sink.AllMetrics()

// expect 3 MetricData objects
assert.Equal(t, 3, len(got))
// expect a MetricData object for each configured scraper
assert.Equal(t, len(config.Scrapers), len(got))

// extract the names of all returned metrics
metricNames := make(map[string]bool)
Expand All @@ -79,13 +102,10 @@ func TestGatherMetrics_EndToEnd(t *testing.T) {
}
}

// expect 5 metrics
assert.Equal(t, 5, len(metricNames))

// expected metric names
assert.Contains(t, metricNames, "host/cpu/time")
assert.Contains(t, metricNames, "host/memory/used")
assert.Contains(t, metricNames, "host/disk/bytes")
assert.Contains(t, metricNames, "host/disk/ops")
assert.Contains(t, metricNames, "host/disk/time")
// the expected list of metrics returned is os dependent
expectedMetrics := append(standardMetrics, systemSpecificMetrics[runtime.GOOS]...)
assert.Equal(t, len(expectedMetrics), len(metricNames))
for _, expected := range expectedMetrics {
assert.Contains(t, metricNames, expected)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2020, 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 filesystemscraper

import "github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal"

// Config relating to FileSystem Metric Scraper.
type Config struct {
internal.ConfigSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2020, 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 filesystemscraper

import (
"context"

"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector/consumer"
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal"
)

// This file implements Factory for FileSystem scraper.

const (
// The value of "type" key in configuration.
TypeStr = "filesystem"
)

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

// Type gets the type of the scraper config created by this Factory.
func (f *Factory) Type() string {
return TypeStr
}

// 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,
logger *zap.Logger,
config internal.Config,
consumer consumer.MetricsConsumer,
) (internal.Scraper, error) {
cfg := config.(*Config)
return NewFileSystemScraper(ctx, cfg, consumer)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2020, 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 filesystemscraper

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)

func TestCreateMetricsScraper(t *testing.T) {
factory := &Factory{}
cfg := &Config{}

scraper, err := factory.CreateMetricsScraper(context.Background(), zap.NewNop(), cfg, nil)

assert.NoError(t, err)
assert.NotNil(t, scraper)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2020, 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 filesystemscraper

import (
"github.com/open-telemetry/opentelemetry-collector/consumer/pdata"
)

// filesystem metric constants

const (
deviceLabelName = "device"
stateLabelName = "state"
)

const (
freeLabelValue = "free"
usedLabelValue = "used"
reservedLabelValue = "reserved"
)

var metricFilesystemUsedDescriptor = createMetricFilesystemUsedDescriptor()

func createMetricFilesystemUsedDescriptor() pdata.MetricDescriptor {
descriptor := pdata.NewMetricDescriptor()
descriptor.InitEmpty()
descriptor.SetName("host/filesystem/used")
descriptor.SetDescription("Filesystem bytes used.")
descriptor.SetUnit("bytes")
descriptor.SetType(pdata.MetricTypeGaugeInt64)
return descriptor
}

var metricFilesystemINodesUsedDescriptor = createMetricFilesystemINodesUsedDescriptor()

func createMetricFilesystemINodesUsedDescriptor() pdata.MetricDescriptor {
descriptor := pdata.NewMetricDescriptor()
descriptor.InitEmpty()
descriptor.SetName("host/filesystem/inodes/used")
descriptor.SetDescription("FileSystem operations count.")
descriptor.SetUnit("1")
descriptor.SetType(pdata.MetricTypeGaugeInt64)
return descriptor
}
Loading

0 comments on commit 5a2f2a4

Please sign in to comment.