Skip to content

Commit

Permalink
feat: move cpu usage metrics to seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
rogercoll committed Jan 14, 2024
1 parent afce91c commit a5a0099
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 20 deletions.
6 changes: 3 additions & 3 deletions receiver/podmanreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Total CPU time consumed per CPU-core.
| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
| ---- | ----------- | ---------- | ----------------------- | --------- |
| ns | Sum | Int | Cumulative | true |
| s | Sum | Int | Cumulative | true |
#### Attributes
Expand All @@ -60,15 +60,15 @@ System CPU usage.
| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
| ---- | ----------- | ---------- | ----------------------- | --------- |
| ns | Sum | Int | Cumulative | true |
| s | Sum | Int | Cumulative | true |
### container.cpu.usage.total
Total CPU time consumed.
| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
| ---- | ----------- | ---------- | ----------------------- | --------- |
| ns | Sum | Int | Cumulative | true |
| s | Sum | Int | Cumulative | true |
### container.memory.percent
Expand Down
4 changes: 2 additions & 2 deletions receiver/podmanreceiver/internal/metadata/generated_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions receiver/podmanreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ metrics:
container.cpu.usage.system:
enabled: true
description: "System CPU usage."
unit: ns
unit: s
sum:
value_type: int
monotonic: true
aggregation_temporality: cumulative
container.cpu.usage.total:
enabled: true
description: "Total CPU time consumed."
unit: ns
unit: s
sum:
value_type: int
monotonic: true
aggregation_temporality: cumulative
container.cpu.usage.percpu:
enabled: true
description: "Total CPU time consumed per CPU-core."
unit: ns
unit: s
sum:
value_type: int
monotonic: true
Expand Down
11 changes: 8 additions & 3 deletions receiver/podmanreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ func (r *metricsReceiver) recordContainerStats(now pcommon.Timestamp, container
}

func (r *metricsReceiver) recordCPUMetrics(now pcommon.Timestamp, stats *containerStats) {
r.mb.RecordContainerCPUUsageSystemDataPoint(now, int64(stats.CPUSystemNano))
r.mb.RecordContainerCPUUsageTotalDataPoint(now, int64(stats.CPUNano))
r.mb.RecordContainerCPUUsageSystemDataPoint(now, int64(toSecondsWithNanosecondPrecision(stats.CPUSystemNano)))
r.mb.RecordContainerCPUUsageTotalDataPoint(now, int64(toSecondsWithNanosecondPrecision(stats.CPUNano)))
r.mb.RecordContainerCPUPercentDataPoint(now, stats.CPU)

for i, cpu := range stats.PerCPU {
r.mb.RecordContainerCPUUsagePercpuDataPoint(now, int64(cpu), fmt.Sprintf("cpu%d", i))
r.mb.RecordContainerCPUUsagePercpuDataPoint(now, int64(toSecondsWithNanosecondPrecision(cpu)), fmt.Sprintf("cpu%d", i))
}

}
Expand All @@ -155,3 +155,8 @@ func (r *metricsReceiver) recordIOMetrics(now pcommon.Timestamp, stats *containe
r.mb.RecordContainerBlockioIoServiceBytesRecursiveReadDataPoint(now, int64(stats.BlockInput))
r.mb.RecordContainerBlockioIoServiceBytesRecursiveWriteDataPoint(now, int64(stats.BlockOutput))
}

// nanoseconds to seconds conversion truncating the fractional part
func toSecondsWithNanosecondPrecision(nanoseconds uint64) uint64 {
return nanoseconds / 1e9
}
6 changes: 3 additions & 3 deletions receiver/podmanreceiver/record_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ func assertStatsEqualToMetrics(t *testing.T, podmanStats *containerStats, md pme
assertMetricEqual(t, m, pmetric.MetricTypeSum, []point{{intVal: podmanStats.BlockInput}})

case "container.cpu.usage.system":
assertMetricEqual(t, m, pmetric.MetricTypeSum, []point{{intVal: podmanStats.CPUSystemNano}})
assertMetricEqual(t, m, pmetric.MetricTypeSum, []point{{intVal: toSecondsWithNanosecondPrecision(podmanStats.CPUSystemNano)}})
case "container.cpu.usage.total":
assertMetricEqual(t, m, pmetric.MetricTypeSum, []point{{intVal: podmanStats.CPUNano}})
assertMetricEqual(t, m, pmetric.MetricTypeSum, []point{{intVal: toSecondsWithNanosecondPrecision(podmanStats.CPUNano)}})
case "container.cpu.percent":
assertMetricEqual(t, m, pmetric.MetricTypeGauge, []point{{doubleVal: podmanStats.CPU}})
case "container.cpu.usage.percpu":
points := make([]point, len(podmanStats.PerCPU))
for i, v := range podmanStats.PerCPU {
points[i] = point{intVal: v, attributes: map[string]string{"core": fmt.Sprintf("cpu%d", i)}}
points[i] = point{intVal: toSecondsWithNanosecondPrecision(v), attributes: map[string]string{"core": fmt.Sprintf("cpu%d", i)}}
}
assertMetricEqual(t, m, pmetric.MetricTypeSum, points)

Expand Down

0 comments on commit a5a0099

Please sign in to comment.