Skip to content

Commit cb27f29

Browse files
Initial commit of host metrics network scraper
1 parent 80ee563 commit cb27f29

File tree

9 files changed

+515
-4
lines changed

9 files changed

+515
-4
lines changed

receiver/hostmetricsreceiver/example_config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ receivers:
1111
memory:
1212
disk:
1313
filesystem:
14+
network:
1415

1516
exporters:
1617
logging:

receiver/hostmetricsreceiver/factory.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/diskscraper"
3232
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper"
3333
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/memoryscraper"
34+
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/networkscraper"
3435
)
3536

3637
// This file implements Factory for HostMetrics receiver.
@@ -54,6 +55,7 @@ func NewFactory() *Factory {
5455
diskscraper.TypeStr: &diskscraper.Factory{},
5556
filesystemscraper.TypeStr: &filesystemscraper.Factory{},
5657
memoryscraper.TypeStr: &memoryscraper.Factory{},
58+
networkscraper.TypeStr: &networkscraper.Factory{},
5759
},
5860
}
5961
}

receiver/hostmetricsreceiver/hostmetrics_receiver_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/diskscraper"
3232
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper"
3333
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/memoryscraper"
34+
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal/scraper/networkscraper"
3435
)
3536

3637
var standardMetrics = []string{
@@ -40,6 +41,11 @@ var standardMetrics = []string{
4041
"host/disk/ops",
4142
"host/disk/time",
4243
"host/filesystem/used",
44+
"host/network/packets",
45+
"host/network/dropped_packets",
46+
"host/network/errors",
47+
"host/network/bytes",
48+
"host/network/tcp_connections",
4349
}
4450

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

62+
configSettings := internal.ConfigSettings{CollectionIntervalValue: 100 * time.Millisecond}
63+
5664
config := &Config{
5765
Scrapers: map[string]internal.Config{
5866
cpuscraper.TypeStr: &cpuscraper.Config{
59-
ConfigSettings: internal.ConfigSettings{CollectionIntervalValue: 100 * time.Millisecond},
67+
ConfigSettings: configSettings,
6068
ReportPerCPU: true,
6169
},
6270
diskscraper.TypeStr: &diskscraper.Config{
63-
ConfigSettings: internal.ConfigSettings{CollectionIntervalValue: 100 * time.Millisecond},
71+
ConfigSettings: configSettings,
6472
},
6573
filesystemscraper.TypeStr: &filesystemscraper.Config{
66-
ConfigSettings: internal.ConfigSettings{CollectionIntervalValue: 100 * time.Millisecond},
74+
ConfigSettings: configSettings,
6775
},
6876
memoryscraper.TypeStr: &memoryscraper.Config{
69-
ConfigSettings: internal.ConfigSettings{CollectionIntervalValue: 100 * time.Millisecond},
77+
ConfigSettings: configSettings,
78+
},
79+
networkscraper.TypeStr: &networkscraper.Config{
80+
ConfigSettings: configSettings,
7081
},
7182
},
7283
}
@@ -76,6 +87,7 @@ func TestGatherMetrics_EndToEnd(t *testing.T) {
7687
diskscraper.TypeStr: &diskscraper.Factory{},
7788
filesystemscraper.TypeStr: &filesystemscraper.Factory{},
7889
memoryscraper.TypeStr: &memoryscraper.Factory{},
90+
networkscraper.TypeStr: &networkscraper.Factory{},
7991
}
8092

8193
receiver, err := NewHostMetricsReceiver(context.Background(), zap.NewNop(), config, factories, sink)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2020, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package networkscraper
16+
17+
import "github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal"
18+
19+
// Config relating to Network Metric Scraper.
20+
type Config struct {
21+
internal.ConfigSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
22+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2020, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package networkscraper
16+
17+
import (
18+
"context"
19+
20+
"go.uber.org/zap"
21+
22+
"github.com/open-telemetry/opentelemetry-collector/consumer"
23+
"github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/internal"
24+
)
25+
26+
// This file implements Factory for Network scraper.
27+
28+
const (
29+
// The value of "type" key in configuration.
30+
TypeStr = "network"
31+
)
32+
33+
// Factory is the Factory for scraper.
34+
type Factory struct {
35+
}
36+
37+
// CreateDefaultConfig creates the default configuration for the Scraper.
38+
func (f *Factory) CreateDefaultConfig() internal.Config {
39+
return &Config{}
40+
}
41+
42+
// CreateMetricsScraper creates a scraper based on provided config.
43+
func (f *Factory) CreateMetricsScraper(
44+
ctx context.Context,
45+
logger *zap.Logger,
46+
config internal.Config,
47+
consumer consumer.MetricsConsumer,
48+
) (internal.Scraper, error) {
49+
cfg := config.(*Config)
50+
return NewNetworkScraper(ctx, cfg, consumer)
51+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2020, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package networkscraper
16+
17+
import (
18+
"context"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
"go.uber.org/zap"
23+
)
24+
25+
func TestCreateMetricsScraper(t *testing.T) {
26+
factory := &Factory{}
27+
cfg := &Config{}
28+
29+
scraper, err := factory.CreateMetricsScraper(context.Background(), zap.NewNop(), cfg, nil)
30+
31+
assert.NoError(t, err)
32+
assert.NotNil(t, scraper)
33+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2020, OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package networkscraper
16+
17+
import (
18+
"github.com/open-telemetry/opentelemetry-collector/consumer/pdata"
19+
)
20+
21+
// network metric constants
22+
23+
const (
24+
directionLabelName = "direction"
25+
stateLabelName = "state"
26+
)
27+
28+
const (
29+
receiveDirectionLabelValue = "receive"
30+
transmitDirectionLabelValue = "transmit"
31+
)
32+
33+
var metricNetworkPacketsDescriptor = createMetricNetworkPacketsDescriptor()
34+
35+
func createMetricNetworkPacketsDescriptor() pdata.MetricDescriptor {
36+
descriptor := pdata.NewMetricDescriptor()
37+
descriptor.InitEmpty()
38+
descriptor.SetName("host/network/packets")
39+
descriptor.SetDescription("The number of packets transferred.")
40+
descriptor.SetUnit("1")
41+
descriptor.SetType(pdata.MetricTypeCounterInt64)
42+
return descriptor
43+
}
44+
45+
var metricNetworkDroppedPacketsDescriptor = createMetricNetworkDroppedPacketsDescriptor()
46+
47+
func createMetricNetworkDroppedPacketsDescriptor() pdata.MetricDescriptor {
48+
descriptor := pdata.NewMetricDescriptor()
49+
descriptor.InitEmpty()
50+
descriptor.SetName("host/network/dropped_packets")
51+
descriptor.SetDescription("The number of packets dropped.")
52+
descriptor.SetUnit("1")
53+
descriptor.SetType(pdata.MetricTypeCounterInt64)
54+
return descriptor
55+
}
56+
57+
var metricNetworkErrorsDescriptor = createMetricNetworkErrorsDescriptor()
58+
59+
func createMetricNetworkErrorsDescriptor() pdata.MetricDescriptor {
60+
descriptor := pdata.NewMetricDescriptor()
61+
descriptor.InitEmpty()
62+
descriptor.SetName("host/network/errors")
63+
descriptor.SetDescription("The number of errors encountered")
64+
descriptor.SetUnit("1")
65+
descriptor.SetType(pdata.MetricTypeCounterInt64)
66+
return descriptor
67+
}
68+
69+
var metricNetworkBytesDescriptor = createMetricNetworkBytesDescriptor()
70+
71+
func createMetricNetworkBytesDescriptor() pdata.MetricDescriptor {
72+
descriptor := pdata.NewMetricDescriptor()
73+
descriptor.InitEmpty()
74+
descriptor.SetName("host/network/bytes")
75+
descriptor.SetDescription("The number of bytes transmitted and received")
76+
descriptor.SetUnit("bytes")
77+
descriptor.SetType(pdata.MetricTypeCounterInt64)
78+
return descriptor
79+
}
80+
81+
var metricNetworkTCPConnectionDescriptor = createMetricNetworkTCPConnectionDescriptor()
82+
83+
func createMetricNetworkTCPConnectionDescriptor() pdata.MetricDescriptor {
84+
descriptor := pdata.NewMetricDescriptor()
85+
descriptor.InitEmpty()
86+
descriptor.SetName("host/network/tcp_connections")
87+
descriptor.SetDescription("The number of tcp connections")
88+
descriptor.SetUnit("bytes")
89+
descriptor.SetType(pdata.MetricTypeGaugeInt64)
90+
return descriptor
91+
}

0 commit comments

Comments
 (0)