|
| 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