Skip to content

Commit

Permalink
receiver/hostmetrics: update labels to attributes (#3811)
Browse files Browse the repository at this point in the history
Updating `LabelsMap` to `Attributes` for hostmetrics receiver.

**Link to tracking Issue:** Part of #3535
  • Loading branch information
alrex committed Aug 13, 2021
1 parent 7f8f321 commit ffda9d5
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ func initializeCPUTimeMetric(metric pdata.Metric, startTime, now pdata.Timestamp
const gopsCPUTotal string = "cpu-total"

func initializeCPUTimeDataPoint(dataPoint pdata.NumberDataPoint, startTime, now pdata.Timestamp, cpuLabel string, stateLabel string, value float64) {
labelsMap := dataPoint.LabelsMap()
// ignore cpu label if reporting "total" cpu usage
attributes := dataPoint.Attributes()
// ignore cpu attribute if reporting "total" cpu usage
if cpuLabel != gopsCPUTotal {
labelsMap.Insert(metadata.Labels.Cpu, cpuLabel)
attributes.InsertString(metadata.Labels.Cpu, cpuLabel)
}
labelsMap.Insert(metadata.Labels.CPUState, stateLabel)
attributes.InsertString(metadata.Labels.CPUState, stateLabel)

dataPoint.SetStartTimestamp(startTime)
dataPoint.SetTimestamp(now)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ func initializeDiskPendingOperationsMetric(metric pdata.Metric, now pdata.Timest
}

func initializeDiskPendingDataPoint(dataPoint pdata.NumberDataPoint, now pdata.Timestamp, deviceLabel string, value int64) {
labelsMap := dataPoint.LabelsMap()
labelsMap.Insert(metadata.Labels.DiskDevice, deviceLabel)
dataPoint.Attributes().InsertString(metadata.Labels.DiskDevice, deviceLabel)
dataPoint.SetTimestamp(now)
dataPoint.SetIntVal(value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ func initializeDiskPendingOperationsMetric(metric pdata.Metric, now pdata.Timest
}

func initializeDiskPendingDataPoint(dataPoint pdata.NumberDataPoint, now pdata.Timestamp, deviceLabel string, value int64) {
labelsMap := dataPoint.LabelsMap()
labelsMap.Insert(metadata.Labels.DiskDevice, deviceLabel)
dataPoint.Attributes().InsertString(metadata.Labels.DiskDevice, deviceLabel)
dataPoint.SetTimestamp(now)
dataPoint.SetIntVal(value)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ import (
)

func initializeNumberDataPointAsInt(dataPoint pdata.NumberDataPoint, startTime, now pdata.Timestamp, deviceLabel string, directionLabel string, value int64) {
labelsMap := dataPoint.LabelsMap()
labelsMap.Insert(metadata.Labels.DiskDevice, deviceLabel)
attributes := dataPoint.Attributes()
attributes.InsertString(metadata.Labels.DiskDevice, deviceLabel)
if directionLabel != "" {
labelsMap.Insert(metadata.Labels.DiskDirection, directionLabel)
attributes.InsertString(metadata.Labels.DiskDirection, directionLabel)
}
dataPoint.SetStartTimestamp(startTime)
dataPoint.SetTimestamp(now)
dataPoint.SetIntVal(value)
}

func initializeNumberDataPointAsDouble(dataPoint pdata.NumberDataPoint, startTime, now pdata.Timestamp, deviceLabel string, directionLabel string, value float64) {
labelsMap := dataPoint.LabelsMap()
labelsMap.Insert(metadata.Labels.DiskDevice, deviceLabel)
attributes := dataPoint.Attributes()
attributes.InsertString(metadata.Labels.DiskDevice, deviceLabel)
if directionLabel != "" {
labelsMap.Insert(metadata.Labels.DiskDirection, directionLabel)
attributes.InsertString(metadata.Labels.DiskDirection, directionLabel)
}
dataPoint.SetStartTimestamp(startTime)
dataPoint.SetTimestamp(now)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ func initializeFileSystemUsageMetric(metric pdata.Metric, now pdata.Timestamp, d
}

func initializeFileSystemUsageDataPoint(dataPoint pdata.NumberDataPoint, now pdata.Timestamp, partition disk.PartitionStat, stateLabel string, value int64) {
labelsMap := dataPoint.LabelsMap()
labelsMap.Insert(metadata.Labels.FilesystemDevice, partition.Device)
labelsMap.Insert(metadata.Labels.FilesystemType, partition.Fstype)
labelsMap.Insert(metadata.Labels.FilesystemMode, getMountMode(partition.Opts))
labelsMap.Insert(metadata.Labels.FilesystemMountpoint, partition.Mountpoint)
labelsMap.Insert(metadata.Labels.FilesystemState, stateLabel)
attributes := dataPoint.Attributes()
attributes.InsertString(metadata.Labels.FilesystemDevice, partition.Device)
attributes.InsertString(metadata.Labels.FilesystemType, partition.Fstype)
attributes.InsertString(metadata.Labels.FilesystemMode, getMountMode(partition.Opts))
attributes.InsertString(metadata.Labels.FilesystemMountpoint, partition.Mountpoint)
attributes.InsertString(metadata.Labels.FilesystemState, stateLabel)
dataPoint.SetTimestamp(now)
dataPoint.SetIntVal(value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ func initializeMemoryUsageMetric(metric pdata.Metric, now pdata.Timestamp, memIn
}

func initializeMemoryUsageDataPoint(dataPoint pdata.NumberDataPoint, now pdata.Timestamp, stateLabel string, value int64) {
labelsMap := dataPoint.LabelsMap()
labelsMap.Insert(metadata.Labels.MemState, stateLabel)
dataPoint.Attributes().InsertString(metadata.Labels.MemState, stateLabel)
dataPoint.SetTimestamp(now)
dataPoint.SetIntVal(value)
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ func initializeNetworkIOMetric(metric pdata.Metric, metricIntf metadata.MetricIn
}

func initializeNetworkDataPoint(dataPoint pdata.NumberDataPoint, startTime, now pdata.Timestamp, deviceLabel, directionLabel string, value int64) {
labelsMap := dataPoint.LabelsMap()
labelsMap.Insert(metadata.Labels.NetworkDevice, deviceLabel)
labelsMap.Insert(metadata.Labels.NetworkDirection, directionLabel)
attributes := dataPoint.Attributes()
attributes.InsertString(metadata.Labels.NetworkDevice, deviceLabel)
attributes.InsertString(metadata.Labels.NetworkDirection, directionLabel)
dataPoint.SetStartTimestamp(startTime)
dataPoint.SetTimestamp(now)
dataPoint.SetIntVal(value)
Expand Down Expand Up @@ -215,9 +215,9 @@ func initializeNetworkConnectionsMetric(metric pdata.Metric, now pdata.Timestamp
}

func initializeNetworkConnectionsDataPoint(dataPoint pdata.NumberDataPoint, now pdata.Timestamp, protocolLabel, stateLabel string, value int64) {
labelsMap := dataPoint.LabelsMap()
labelsMap.Insert(metadata.Labels.NetworkProtocol, protocolLabel)
labelsMap.Insert(metadata.Labels.NetworkState, stateLabel)
attributes := dataPoint.Attributes()
attributes.InsertString(metadata.Labels.NetworkProtocol, protocolLabel)
attributes.InsertString(metadata.Labels.NetworkState, stateLabel)
dataPoint.SetTimestamp(now)
dataPoint.SetIntVal(value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ func initializePagingUsageMetric(metric pdata.Metric, now pdata.Timestamp, vmem
}

func initializePagingUsageDataPoint(dataPoint pdata.NumberDataPoint, now pdata.Timestamp, stateLabel string, value int64) {
labelsMap := dataPoint.LabelsMap()
labelsMap.Insert(metadata.Labels.PagingState, stateLabel)
dataPoint.Attributes().InsertString(metadata.Labels.PagingState, stateLabel)
dataPoint.SetTimestamp(now)
dataPoint.SetIntVal(value)
}
Expand Down Expand Up @@ -134,9 +133,9 @@ func initializePagingOperationsMetric(metric pdata.Metric, startTime, now pdata.
}

func initializePagingOperationsDataPoint(dataPoint pdata.NumberDataPoint, startTime, now pdata.Timestamp, typeLabel string, directionLabel string, value int64) {
labelsMap := dataPoint.LabelsMap()
labelsMap.Insert(metadata.Labels.PagingType, typeLabel)
labelsMap.Insert(metadata.Labels.PagingDirection, directionLabel)
attributes := dataPoint.Attributes()
attributes.InsertString(metadata.Labels.PagingType, typeLabel)
attributes.InsertString(metadata.Labels.PagingDirection, directionLabel)
dataPoint.SetStartTimestamp(startTime)
dataPoint.SetTimestamp(now)
dataPoint.SetIntVal(value)
Expand All @@ -152,7 +151,7 @@ func initializePageFaultsMetric(metric pdata.Metric, startTime, now pdata.Timest
}

func initializePageFaultDataPoint(dataPoint pdata.NumberDataPoint, startTime, now pdata.Timestamp, typeLabel string, value int64) {
dataPoint.LabelsMap().Insert(metadata.Labels.PagingType, typeLabel)
dataPoint.Attributes().InsertString(metadata.Labels.PagingType, typeLabel)
dataPoint.SetStartTimestamp(startTime)
dataPoint.SetTimestamp(now)
dataPoint.SetIntVal(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ func (s *scraper) initializePagingUsageMetric(metric pdata.Metric, now pdata.Tim
}

func initializePagingUsageDataPoint(dataPoint pdata.NumberDataPoint, now pdata.Timestamp, deviceLabel string, stateLabel string, value int64) {
labelsMap := dataPoint.LabelsMap()
labelsMap.Insert(metadata.Labels.PagingDevice, deviceLabel)
labelsMap.Insert(metadata.Labels.PagingState, stateLabel)
attributes := dataPoint.Attributes()
attributes.InsertString(metadata.Labels.PagingDevice, deviceLabel)
attributes.InsertString(metadata.Labels.PagingState, stateLabel)
dataPoint.SetTimestamp(now)
dataPoint.SetIntVal(value)
}
Expand Down Expand Up @@ -165,9 +165,9 @@ func initializePagingOperationsMetric(metric pdata.Metric, startTime, now pdata.
}

func initializePagingOperationsDataPoint(dataPoint pdata.NumberDataPoint, startTime, now pdata.Timestamp, directionLabel string, value int64) {
labelsMap := dataPoint.LabelsMap()
labelsMap.Insert(metadata.Labels.PagingType, metadata.LabelPagingType.Major)
labelsMap.Insert(metadata.Labels.PagingDirection, directionLabel)
attributes := dataPoint.Attributes()
attributes.InsertString(metadata.Labels.PagingType, metadata.LabelPagingType.Major)
attributes.InsertString(metadata.Labels.PagingDirection, directionLabel)
dataPoint.SetStartTimestamp(startTime)
dataPoint.SetTimestamp(now)
dataPoint.SetIntVal(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,5 @@ func assertProcessesCreatedMetricValid(t *testing.T, metric pdata.Metric, startT
}
internal.AssertDescriptorEqual(t, metadata.Metrics.SystemProcessesCreated.New(), metric)
assert.Equal(t, 1, metric.Sum().DataPoints().Len())
assert.Equal(t, 0, metric.Sum().DataPoints().At(0).LabelsMap().Len())
assert.Equal(t, 0, metric.Sum().DataPoints().At(0).Attributes().Len())
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ func initializeProcessesCountMetric(metric pdata.Metric, startTime pdata.Timesta
}

func initializeProcessesCountDataPoint(dataPoint pdata.NumberDataPoint, startTime pdata.Timestamp, now pdata.Timestamp, statusLabel string, value int64) {
labelsMap := dataPoint.LabelsMap()
labelsMap.Insert(metadata.Labels.ProcessesStatus, statusLabel)
dataPoint.Attributes().InsertString(metadata.Labels.ProcessesStatus, statusLabel)
dataPoint.SetStartTimestamp(startTime)
dataPoint.SetTimestamp(now)
dataPoint.SetIntVal(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ func initializeDiskIOMetric(metric pdata.Metric, startTime, now pdata.Timestamp,
}

func initializeDiskIODataPoint(dataPoint pdata.NumberDataPoint, startTime, now pdata.Timestamp, value int64, directionLabel string) {
labelsMap := dataPoint.LabelsMap()
labelsMap.Insert(metadata.Labels.ProcessDirection, directionLabel)
dataPoint.Attributes().InsertString(metadata.Labels.ProcessDirection, directionLabel)
dataPoint.SetStartTimestamp(startTime)
dataPoint.SetTimestamp(now)
dataPoint.SetIntVal(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ func appendCPUTimeStateDataPoints(ddps pdata.NumberDataPointSlice, startTime, no
}

func initializeCPUTimeDataPoint(dataPoint pdata.NumberDataPoint, startTime, now pdata.Timestamp, value float64, stateLabel string) {
labelsMap := dataPoint.LabelsMap()
labelsMap.Insert(metadata.Labels.ProcessState, stateLabel)
dataPoint.Attributes().InsertString(metadata.Labels.ProcessState, stateLabel)
dataPoint.SetStartTimestamp(startTime)
dataPoint.SetTimestamp(now)
dataPoint.SetDoubleVal(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ func appendCPUTimeStateDataPoints(ddps pdata.NumberDataPointSlice, startTime, no
}

func initializeCPUTimeDataPoint(dataPoint pdata.NumberDataPoint, startTime, now pdata.Timestamp, value float64, stateLabel string) {
labelsMap := dataPoint.LabelsMap()
labelsMap.Insert(metadata.Labels.ProcessState, stateLabel)
dataPoint.Attributes().InsertString(metadata.Labels.ProcessState, stateLabel)
dataPoint.SetStartTimestamp(startTime)
dataPoint.SetTimestamp(now)
dataPoint.SetDoubleVal(value)
Expand Down
10 changes: 5 additions & 5 deletions receiver/hostmetricsreceiver/internal/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ func AssertDescriptorEqual(t *testing.T, expected pdata.Metric, actual pdata.Met
}

func AssertSumMetricHasLabelValue(t *testing.T, metric pdata.Metric, index int, labelName string, expectedVal string) {
val, ok := metric.Sum().DataPoints().At(index).LabelsMap().Get(labelName)
assert.Truef(t, ok, "Missing label %q in metric %q", labelName, metric.Name())
assert.Equal(t, expectedVal, val)
val, ok := metric.Sum().DataPoints().At(index).Attributes().Get(labelName)
assert.Truef(t, ok, "Missing attribute %q in metric %q", labelName, metric.Name())
assert.Equal(t, expectedVal, val.StringVal())
}

func AssertSumMetricHasLabel(t *testing.T, metric pdata.Metric, index int, labelName string) {
_, ok := metric.Sum().DataPoints().At(index).LabelsMap().Get(labelName)
assert.Truef(t, ok, "Missing label %q in metric %q", labelName, metric.Name())
_, ok := metric.Sum().DataPoints().At(index).Attributes().Get(labelName)
assert.Truef(t, ok, "Missing attribute %q in metric %q", labelName, metric.Name())
}

func AssertSumMetricStartTimeEquals(t *testing.T, metric pdata.Metric, startTime pdata.Timestamp) {
Expand Down

0 comments on commit ffda9d5

Please sign in to comment.