Skip to content

Commit

Permalink
Initial commit of host metrics network scraper (open-telemetry#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-bebbington authored and wyTrivail committed Jul 13, 2020
1 parent 522fc19 commit 4966208
Show file tree
Hide file tree
Showing 9 changed files with 500 additions and 4 deletions.
1 change: 1 addition & 0 deletions receiver/hostmetricsreceiver/example_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ receivers:
memory:
disk:
filesystem:
network:

exporters:
logging:
Expand Down
2 changes: 2 additions & 0 deletions receiver/hostmetricsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"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"
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/networkscraper"
)

// This file implements Factory for HostMetrics receiver.
Expand All @@ -54,6 +55,7 @@ func NewFactory() *Factory {
diskscraper.TypeStr: &diskscraper.Factory{},
filesystemscraper.TypeStr: &filesystemscraper.Factory{},
memoryscraper.TypeStr: &memoryscraper.Factory{},
networkscraper.TypeStr: &networkscraper.Factory{},
},
}
}
Expand Down
20 changes: 16 additions & 4 deletions receiver/hostmetricsreceiver/hostmetrics_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"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"
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/networkscraper"
)

var standardMetrics = []string{
Expand All @@ -40,6 +41,11 @@ var standardMetrics = []string{
"host/disk/ops",
"host/disk/time",
"host/filesystem/used",
"host/network/packets",
"host/network/dropped_packets",
"host/network/errors",
"host/network/bytes",
"host/network/tcp_connections",
}

var systemSpecificMetrics = map[string][]string{
Expand All @@ -53,20 +59,25 @@ var systemSpecificMetrics = map[string][]string{
func TestGatherMetrics_EndToEnd(t *testing.T) {
sink := &exportertest.SinkMetricsExporter{}

configSettings := internal.ConfigSettings{CollectionIntervalValue: 100 * time.Millisecond}

config := &Config{
Scrapers: map[string]internal.Config{
cpuscraper.TypeStr: &cpuscraper.Config{
ConfigSettings: internal.ConfigSettings{CollectionIntervalValue: 100 * time.Millisecond},
ConfigSettings: configSettings,
ReportPerCPU: true,
},
diskscraper.TypeStr: &diskscraper.Config{
ConfigSettings: internal.ConfigSettings{CollectionIntervalValue: 100 * time.Millisecond},
ConfigSettings: configSettings,
},
filesystemscraper.TypeStr: &filesystemscraper.Config{
ConfigSettings: internal.ConfigSettings{CollectionIntervalValue: 100 * time.Millisecond},
ConfigSettings: configSettings,
},
memoryscraper.TypeStr: &memoryscraper.Config{
ConfigSettings: internal.ConfigSettings{CollectionIntervalValue: 100 * time.Millisecond},
ConfigSettings: configSettings,
},
networkscraper.TypeStr: &networkscraper.Config{
ConfigSettings: configSettings,
},
},
}
Expand All @@ -76,6 +87,7 @@ func TestGatherMetrics_EndToEnd(t *testing.T) {
diskscraper.TypeStr: &diskscraper.Factory{},
filesystemscraper.TypeStr: &filesystemscraper.Factory{},
memoryscraper.TypeStr: &memoryscraper.Factory{},
networkscraper.TypeStr: &networkscraper.Factory{},
}

receiver, err := NewHostMetricsReceiver(context.Background(), zap.NewNop(), config, factories, sink)
Expand Down
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 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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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{}
}

// 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"
)

// network 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 4966208

Please sign in to comment.