Skip to content

Commit

Permalink
Initial commit of host metrics network scraper
Browse files Browse the repository at this point in the history
  • Loading branch information
james-bebbington committed May 8, 2020
1 parent 51a6efd commit a4c32d0
Show file tree
Hide file tree
Showing 9 changed files with 528 additions and 11 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:
network:

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 @@ -30,6 +30,7 @@ import (
"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/memoryscraper"
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/networkscraper"
)

// This file implements Factory for HostMetrics receiver.
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{},
memoryscraper.TypeStr: &memoryscraper.Factory{},
networkscraper.TypeStr: &networkscraper.Factory{},
},
}
}
Expand Down
26 changes: 18 additions & 8 deletions receiver/hostmetricsreceiver/hostmetrics_receiver_test.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/scraper/cpuscraper"
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/diskscraper"
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/memoryscraper"
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/networkscraper"
)

func TestGatherMetrics_EndToEnd(t *testing.T) {
Expand All @@ -40,19 +41,23 @@ func TestGatherMetrics_EndToEnd(t *testing.T) {
ConfigSettings: internal.ConfigSettings{CollectionIntervalValue: 100 * time.Millisecond},
ReportPerCPU: true,
},
diskscraper.TypeStr: &diskscraper.Config{
ConfigSettings: internal.ConfigSettings{CollectionIntervalValue: 100 * time.Millisecond},
},
memoryscraper.TypeStr: &memoryscraper.Config{
ConfigSettings: internal.ConfigSettings{CollectionIntervalValue: 100 * time.Millisecond},
},
diskscraper.TypeStr: &diskscraper.Config{
networkscraper.TypeStr: &networkscraper.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{},
memoryscraper.TypeStr: &memoryscraper.Factory{},
networkscraper.TypeStr: &networkscraper.Factory{},
}

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

got := sink.AllMetrics()

// expect 3 MetricData objects
assert.Equal(t, 3, len(got))
// expect 4 MetricData objects
assert.Equal(t, 4, len(got))

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

// expect 5 metrics
assert.Equal(t, 5, len(metricNames))
// expect 10 metrics
assert.Equal(t, 10, 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")
assert.Contains(t, metricNames, "host/network/packets")
assert.Contains(t, metricNames, "host/network/dropped_packets")
assert.Contains(t, metricNames, "host/network/errors")
assert.Contains(t, metricNames, "host/network/bytes")
assert.Contains(t, metricNames, "host/network/tcp_connections")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 networkscraper

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

// Config relating to Network Metric Scraper.
type Config struct {
internal.ConfigSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct

// If `true`, stats will be generated for the system as a whole _as well
// as_ for each individual Network/core in the system and will be distinguished
// by the `network` dimension. If `false`, stats will only be generated for
// the system as a whole that will not include a `network` dimension.
ReportPerNetwork bool `mapstructure:"report_per_network"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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 networkscraper

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 Network scraper.

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

// 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{
ReportPerNetwork: true,
}
}

// 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 NewNetworkScraper(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 networkscraper

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,91 @@
// 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 networkscraper

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

// disk metric constants

const (
directionLabelName = "direction"
stateLabelName = "state"
)

const (
receiveDirectionLabelValue = "receive"
transmitDirectionLabelValue = "transmit"
)

var metricNetworkPacketsDescriptor = createMetricNetworkPacketsDescriptor()

func createMetricNetworkPacketsDescriptor() pdata.MetricDescriptor {
descriptor := pdata.NewMetricDescriptor()
descriptor.InitEmpty()
descriptor.SetName("host/network/packets")
descriptor.SetDescription("The number of packets transferred.")
descriptor.SetUnit("1")
descriptor.SetType(pdata.MetricTypeCounterInt64)
return descriptor
}

var metricNetworkDroppedPacketsDescriptor = createMetricNetworkDroppedPacketsDescriptor()

func createMetricNetworkDroppedPacketsDescriptor() pdata.MetricDescriptor {
descriptor := pdata.NewMetricDescriptor()
descriptor.InitEmpty()
descriptor.SetName("host/network/dropped_packets")
descriptor.SetDescription("The number of packets dropped.")
descriptor.SetUnit("1")
descriptor.SetType(pdata.MetricTypeCounterInt64)
return descriptor
}

var metricNetworkErrorsDescriptor = createMetricNetworkErrorsDescriptor()

func createMetricNetworkErrorsDescriptor() pdata.MetricDescriptor {
descriptor := pdata.NewMetricDescriptor()
descriptor.InitEmpty()
descriptor.SetName("host/network/errors")
descriptor.SetDescription("The number of errors encountered")
descriptor.SetUnit("1")
descriptor.SetType(pdata.MetricTypeCounterInt64)
return descriptor
}

var metricNetworkBytesDescriptor = createMetricNetworkBytesDescriptor()

func createMetricNetworkBytesDescriptor() pdata.MetricDescriptor {
descriptor := pdata.NewMetricDescriptor()
descriptor.InitEmpty()
descriptor.SetName("host/network/bytes")
descriptor.SetDescription("The number of bytes transmitted and received")
descriptor.SetUnit("bytes")
descriptor.SetType(pdata.MetricTypeCounterInt64)
return descriptor
}

var metricNetworkTCPConnectionDescriptor = createMetricNetworkTCPConnectionDescriptor()

func createMetricNetworkTCPConnectionDescriptor() pdata.MetricDescriptor {
descriptor := pdata.NewMetricDescriptor()
descriptor.InitEmpty()
descriptor.SetName("host/network/tcp_connections")
descriptor.SetDescription("The number of tcp connections")
descriptor.SetUnit("bytes")
descriptor.SetType(pdata.MetricTypeGaugeInt64)
return descriptor
}

0 comments on commit a4c32d0

Please sign in to comment.