Skip to content

Commit

Permalink
Store cloudfoundry.container.cpu.pct in decimal form (#24219)
Browse files Browse the repository at this point in the history
Percentages should be stored in decimal form, and can be stored as
scaled floats.

Some code is refactored to remove previous helper that makes less sense
now.

(cherry picked from commit 2cf650c)
  • Loading branch information
jsoriano committed Feb 25, 2021
1 parent af24f00 commit 7615de1
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 108 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Change types of numeric metrics from Kubelet summary api to double so as to cover big numbers. {pull}23335[23335]
- Add container.image.name and containe.name ECS fields for state_container. {pull}23802[23802]
- Add support for the MemoryPressure, DiskPressure, OutOfDisk and PIDPressure status conditions in state_node. {pull}[23905]
- Store `cloudfoundry.container.cpu.pct` in decimal form and as `scaled_float`. {pull}24219[24219]

*Packetbeat*

Expand Down
2 changes: 1 addition & 1 deletion metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -7078,7 +7078,7 @@ type: long
CPU usage percentage.


type: float
type: scaled_float

--

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
description: >
Index of the instance the metric belongs to.
- name: cpu.pct
type: float
type: scaled_float
description: >
CPU usage percentage.
- name: memory.bytes
Expand Down
14 changes: 10 additions & 4 deletions x-pack/metricbeat/module/cloudfoundry/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package container

import (
"fmt"
"math"

"github.com/elastic/beats/v7/x-pack/metricbeat/module/cloudfoundry"

Expand Down Expand Up @@ -53,12 +54,17 @@ type containerReporter struct {

func (r *containerReporter) Event(event mb.Event) bool {
cpuPctKey := "cloudfoundry.container.cpu.pct"
found, err := cloudfoundry.HasNonNumericFloat(event.RootFields, cpuPctKey)
value, err := event.RootFields.GetValue(cpuPctKey)
if err != nil {
r.logger.Debugf("Unexpected failure while checking for non-numeric values: %v", err)
}
if found {
event.RootFields.Delete(cpuPctKey)
} else {
if value, ok := value.(float64); ok {
if math.IsNaN(value) || math.IsInf(value, 0) {
event.RootFields.Delete(cpuPctKey)
} else {
event.RootFields.Put(cpuPctKey, value/100)
}
}
}
return r.PushReporterV2.Event(event)
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestMetricSet(t *testing.T) {

expectedFields := common.MapStr{
"cloudfoundry.app.id": "1234",
"cloudfoundry.container.cpu.pct": float64(12.34),
"cloudfoundry.container.cpu.pct": float64(0.1234),
"cloudfoundry.container.disk.bytes": uint64(0),
"cloudfoundry.container.disk.quota.bytes": uint64(0),
"cloudfoundry.container.instance_index": int32(0),
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestMetricValuesAreNumbers(t *testing.T) {
case "1234":
v, err := e.RootFields.GetValue(cpuPctKey)
if assert.NoError(t, err, "checking cpu pct") {
assert.Equal(t, 12.34, v.(float64))
assert.Equal(t, 0.1234, v.(float64))
}
default:
t.Errorf("unexpected app: %s", app)
Expand Down
2 changes: 1 addition & 1 deletion x-pack/metricbeat/module/cloudfoundry/fields.go

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

27 changes: 0 additions & 27 deletions x-pack/metricbeat/module/cloudfoundry/util.go

This file was deleted.

70 changes: 0 additions & 70 deletions x-pack/metricbeat/module/cloudfoundry/util_test.go

This file was deleted.

5 changes: 3 additions & 2 deletions x-pack/metricbeat/module/cloudfoundry/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package value

import (
"fmt"
"math"

"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/beats/v7/metricbeat/mb"
Expand Down Expand Up @@ -51,11 +52,11 @@ type valueReporter struct {
}

func (r *valueReporter) Event(event mb.Event) bool {
found, err := cloudfoundry.HasNonNumericFloat(event.RootFields, "cloudfoundry.value.value")
value, err := event.RootFields.GetValue("cloudfoundry.value.value")
if err != nil {
r.logger.Debugf("Unexpected failure while checking for non-numeric values: %v", err)
}
if found {
if value, ok := value.(float64); ok && (math.IsNaN(value) || math.IsInf(value, 0)) {
r.logger.Debugf("Ignored event with float value that is not a number: %+v", event)
return true
}
Expand Down

0 comments on commit 7615de1

Please sign in to comment.