Skip to content

Commit

Permalink
[receiver/podman] Fix precision for cpu metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
tosuke committed Oct 6, 2024
1 parent fc405e3 commit 0af750b
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 33 deletions.
27 changes: 27 additions & 0 deletions .chloggen/fix-podman-cpu-precision.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: podmanreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Change cpu metric type to float to preserve precision."

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [35633]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
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 |
| ---- | ----------- | ---------- | ----------------------- | --------- |
| s | Sum | Int | Cumulative | true |
| s | Sum | Double | Cumulative | true |
#### Attributes
Expand All @@ -60,15 +60,15 @@ System CPU usage.
| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
| ---- | ----------- | ---------- | ----------------------- | --------- |
| s | Sum | Int | Cumulative | true |
| s | Sum | Double | Cumulative | true |
### container.cpu.usage.total
Total CPU time consumed.
| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
| ---- | ----------- | ---------- | ----------------------- | --------- |
| s | Sum | Int | Cumulative | true |
| s | Sum | Double | Cumulative | true |
### container.memory.percent
Expand Down
3 changes: 1 addition & 2 deletions receiver/podmanreceiver/generated_package_test.go

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

18 changes: 9 additions & 9 deletions receiver/podmanreceiver/internal/metadata/generated_metrics.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.

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

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

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

Expand All @@ -170,7 +170,7 @@ func (r *metricsReceiver) recordIOMetrics(now pcommon.Timestamp, stats *containe
r.mb.RecordContainerBlockioIoServiceBytesRecursiveWriteDataPoint(now, int64(stats.BlockOutput))
}

// nanoseconds to seconds conversion truncating the fractional part
func toSecondsWithNanosecondPrecision(nanoseconds uint64) uint64 {
return nanoseconds / 1e9
// nanoseconds to seconds conversion
func toSecondsWithNanosecondPrecision(nanoseconds uint64) float64 {
return float64(nanoseconds) / 1e9
}
12 changes: 8 additions & 4 deletions receiver/podmanreceiver/record_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,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: toSecondsWithNanosecondPrecision(podmanStats.CPUSystemNano)}})
assertMetricEqual(t, m, pmetric.MetricTypeSum, []point{{doubleVal: toSecondsWithNanosecondPrecision(podmanStats.CPUSystemNano)}})
case "container.cpu.usage.total":
assertMetricEqual(t, m, pmetric.MetricTypeSum, []point{{intVal: toSecondsWithNanosecondPrecision(podmanStats.CPUNano)}})
assertMetricEqual(t, m, pmetric.MetricTypeSum, []point{{doubleVal: 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: toSecondsWithNanosecondPrecision(v), attributes: map[string]string{"core": fmt.Sprintf("cpu%d", i)}}
points[i] = point{doubleVal: toSecondsWithNanosecondPrecision(v), attributes: map[string]string{"core": fmt.Sprintf("cpu%d", i)}}
}
assertMetricEqual(t, m, pmetric.MetricTypeSum, points)

Expand All @@ -79,6 +79,8 @@ func assertStatsEqualToMetrics(t *testing.T, podmanStats *containerStats, md pme
}

func assertMetricEqual(t *testing.T, m pmetric.Metric, dt pmetric.MetricType, pts []point) {
t.Helper()

assert.Equal(t, m.Type(), dt)
switch dt {
case pmetric.MetricTypeGauge:
Expand All @@ -99,11 +101,13 @@ func assertMetricEqual(t *testing.T, m pmetric.Metric, dt pmetric.MetricType, pt
}

func assertPoints(t *testing.T, dpts pmetric.NumberDataPointSlice, pts []point) {
t.Helper()

assert.Len(t, pts, dpts.Len())
for i, expected := range pts {
got := dpts.At(i)
assert.Equal(t, got.IntValue(), int64(expected.intVal))
assert.Equal(t, expected.doubleVal, got.DoubleValue())
assert.InDelta(t, expected.doubleVal, got.DoubleValue(), 0.01)
for k, expectedV := range expected.attributes {
gotV, exists := got.Attributes().Get(k)
assert.True(t, exists)
Expand Down

0 comments on commit 0af750b

Please sign in to comment.