Skip to content

Commit fdd9e5b

Browse files
authored
Network Scraper Translation to ES Metrics (open-telemetry#3)
* Network scrapper * Add network scrapper to hostmetrics * Add system.network.packets * Add more metrics to network scrapper * Move attribute addition to the network.go file * Add code for Network Scraper * remove disk for now * comment a particular part * Have just a single metric * Uncomment the code * update process.go * Improve the code of Network Scraper * Change in network scraper code * Update the metric.attributes
1 parent 80c19e9 commit fdd9e5b

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

processor/elasticprocessor/internal/hostmetrics/hostmetrics.go

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ func AddElasticSystemMetrics(scopeMetrics pmetric.ScopeMetrics, resource pcommon
5151
return addProcessMetrics(scopeMetrics.Metrics(), resource, dataset)
5252
case "processes":
5353
return addProcessSummaryMetrics(scopeMetrics.Metrics(), resource, dataset)
54+
case "network":
55+
return addNetworkMetrics(scopeMetrics.Metrics(), resource, dataset)
5456
default:
5557
return fmt.Errorf("no matching transform function found for scope '%s'", scope.Name())
5658
}

processor/elasticprocessor/internal/hostmetrics/metric.go

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type metric struct {
1919
startTimestamp pcommon.Timestamp
2020
intValue *int64
2121
doubleValue *float64
22+
attributes *pcommon.Map
2223
}
2324

2425
func addMetrics(ms pmetric.MetricSlice, resource pcommon.Resource, dataset string, metrics ...metric) {
@@ -47,7 +48,11 @@ func addMetrics(ms pmetric.MetricSlice, resource pcommon.Resource, dataset strin
4748
dp.SetStartTimestamp(metric.startTimestamp)
4849
}
4950

51+
if metric.attributes != nil {
52+
metric.attributes.CopyTo(dp.Attributes())
53+
}
5054
if dataset == "system.process" {
55+
// Add resource attribute as an attribute to each datapoint
5156
addProcessAttributes(resource, dp)
5257
}
5358

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package hostmetrics
2+
3+
import (
4+
"fmt"
5+
6+
"go.opentelemetry.io/collector/pdata/pcommon"
7+
"go.opentelemetry.io/collector/pdata/pmetric"
8+
)
9+
10+
func addNetworkMetrics(metrics pmetric.MetricSlice, resource pcommon.Resource, dataset string) error {
11+
for i := 0; i < metrics.Len(); i++ {
12+
metric := metrics.At(i)
13+
dataPoints := metric.Sum().DataPoints()
14+
for j := 0; j < dataPoints.Len(); j++ {
15+
dp := dataPoints.At(j)
16+
17+
var device string
18+
if d, ok := dp.Attributes().Get("device"); ok {
19+
device = d.Str()
20+
} else {
21+
continue
22+
}
23+
24+
if direction, ok := dp.Attributes().Get("direction"); ok {
25+
name := metric.Name()
26+
timestamp := dp.Timestamp()
27+
value := dp.IntValue()
28+
29+
switch direction.Str() {
30+
case "receive":
31+
addDeviceMetric(metrics, resource, dataset, name, device, "in", timestamp, value)
32+
case "transmit":
33+
addDeviceMetric(metrics, resource, dataset, name, device, "out", timestamp, value)
34+
}
35+
}
36+
}
37+
}
38+
39+
return nil
40+
}
41+
42+
func addDeviceMetric(metrics pmetric.MetricSlice, resource pcommon.Resource,
43+
dataset, name, device, direction string, timestamp pcommon.Timestamp, value int64) {
44+
45+
metricsToAdd := map[string]string{
46+
"system.network.io": "system.network.%s.bytes",
47+
"system.network.packets": "system.network.%s.packets",
48+
"system.network.dropped": "system.network.%s.dropped",
49+
"system.network.errors": "system.network.%s.errors",
50+
}
51+
52+
if metricNetworkES, ok := metricsToAdd[name]; ok {
53+
attributes := pcommon.NewMap()
54+
attributes.PutStr("system.network.name", device)
55+
56+
addMetrics(metrics, resource, dataset,
57+
metric{
58+
dataType: Sum,
59+
name: fmt.Sprintf(metricNetworkES, direction),
60+
timestamp: timestamp,
61+
intValue: &value,
62+
attributes: &attributes,
63+
})
64+
}
65+
}

0 commit comments

Comments
 (0)