Skip to content

Commit 6d438c0

Browse files
Fix timestamp issue (#37)
* Fix timestamp issue * Update k8s tests * Update remappers/internal/metric.go Co-authored-by: Vishal Raj <vishal.raj@elastic.co> * Update remappers/internal/metric.go Co-authored-by: Vishal Raj <vishal.raj@elastic.co> * import math package * Add the correct check --------- Co-authored-by: Vishal Raj <vishal.raj@elastic.co>
1 parent 5f9cc14 commit 6d438c0

File tree

3 files changed

+23
-31
lines changed

3 files changed

+23
-31
lines changed

remappers/hostmetrics/hostmetrics_test.go

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,13 @@ func doTestRemap(t *testing.T, id string, remapOpts ...Option) {
116116
{Type: Gauge, Name: "system.cpu.system.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.68), Attrs: outAttr("cpu")}},
117117
{Type: Gauge, Name: "system.cpu.user.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.5), Attrs: outAttr("cpu")}},
118118
{Type: Gauge, Name: "system.cpu.steal.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.15), Attrs: outAttr("cpu")}},
119-
{Type: Gauge, Name: "system.cpu.iowait.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("cpu")}},
120-
{Type: Gauge, Name: "system.cpu.nice.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("cpu")}},
121-
{Type: Gauge, Name: "system.cpu.irq.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("cpu")}},
122-
{Type: Gauge, Name: "system.cpu.softirq.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("cpu")}},
123119
{Type: Sum, Name: "system.cpu.cores", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(4)), Attrs: outAttr("cpu")}},
124120
{Type: Sum, Name: "system.load.cores", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(4)), Attrs: outAttr("cpu")}},
125121
{Type: Gauge, Name: "system.cpu.total.norm.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.3325), Attrs: outAttr("cpu")}},
126122
{Type: Gauge, Name: "system.cpu.idle.norm.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.205), Attrs: outAttr("cpu")}},
127123
{Type: Gauge, Name: "system.cpu.system.norm.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.17), Attrs: outAttr("cpu")}},
128124
{Type: Gauge, Name: "system.cpu.user.norm.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.125), Attrs: outAttr("cpu")}},
129125
{Type: Gauge, Name: "system.cpu.steal.norm.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0375), Attrs: outAttr("cpu")}},
130-
{Type: Gauge, Name: "system.cpu.iowait.norm.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("cpu")}},
131-
{Type: Gauge, Name: "system.cpu.nice.norm.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("cpu")}},
132-
{Type: Gauge, Name: "system.cpu.irq.norm.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("cpu")}},
133-
{Type: Gauge, Name: "system.cpu.softirq.norm.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("cpu")}},
134126
},
135127
},
136128
{
@@ -152,10 +144,6 @@ func doTestRemap(t *testing.T, id string, remapOpts ...Option) {
152144
{Type: Gauge, Name: "system.cpu.system.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.68), Attrs: outAttr("cpu")}},
153145
{Type: Gauge, Name: "system.cpu.user.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.5), Attrs: outAttr("cpu")}},
154146
{Type: Gauge, Name: "system.cpu.steal.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.15), Attrs: outAttr("cpu")}},
155-
{Type: Gauge, Name: "system.cpu.iowait.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("cpu")}},
156-
{Type: Gauge, Name: "system.cpu.nice.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("cpu")}},
157-
{Type: Gauge, Name: "system.cpu.irq.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("cpu")}},
158-
{Type: Gauge, Name: "system.cpu.softirq.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("cpu")}},
159147
},
160148
},
161149
{
@@ -220,28 +208,29 @@ func doTestRemap(t *testing.T, id string, remapOpts ...Option) {
220208
{Type: Sum, Name: "process.memory.usage", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(2048))}},
221209
{Type: Sum, Name: "process.memory.virtual", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(128))}},
222210
{Type: Sum, Name: "process.open_file_descriptors", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(10))}},
223-
{Type: Sum, Name: "process.cpu.time", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(3)), Attrs: map[string]any{"state": "system"}}},
224-
{Type: Sum, Name: "process.cpu.time", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(4)), Attrs: map[string]any{"state": "user"}}},
225-
{Type: Sum, Name: "process.cpu.time", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(5)), Attrs: map[string]any{"state": "wait"}}},
226-
{Type: Sum, Name: "process.disk.io", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(1024))}},
227-
{Type: Sum, Name: "process.disk.operations", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(10))}},
211+
{Type: Sum, Name: "process.cpu.time", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.3345), Attrs: map[string]any{"state": "system"}}},
212+
{Type: Sum, Name: "process.cpu.time", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.5546), Attrs: map[string]any{"state": "user"}}},
213+
{Type: Sum, Name: "process.cpu.time", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.8752), Attrs: map[string]any{"state": "wait"}}},
214+
{Type: Sum, Name: "process.disk.io", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(1024)), Attrs: map[string]any{"direction": "read"}}},
215+
{Type: Sum, Name: "process.disk.io", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(2048)), Attrs: map[string]any{"direction": "write"}}},
216+
{Type: Sum, Name: "process.disk.operations", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(10)), Attrs: map[string]any{"direction": "read"}}},
217+
{Type: Sum, Name: "process.disk.operations", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(20)), Attrs: map[string]any{"direction": "write"}}},
228218
},
229219
expected: []internal.TestMetric{
230-
{Type: Sum, Name: "process.cpu.start_time", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(0)), Attrs: outAttr("process")}},
231220
{Type: Sum, Name: "system.process.num_threads", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(7)), Attrs: outAttr("process")}},
232221
{Type: Gauge, Name: "system.process.memory.rss.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.15), Attrs: outAttr("process")}},
233222
{Type: Sum, Name: "system.process.memory.rss.bytes", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(2048)), Attrs: outAttr("process")}},
234223
{Type: Sum, Name: "system.process.memory.size", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(128)), Attrs: outAttr("process")}},
235224
{Type: Sum, Name: "system.process.fd.open", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(10)), Attrs: outAttr("process")}},
236225
{Type: Gauge, Name: "process.memory.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.15), Attrs: outAttr("process")}},
237-
{Type: Sum, Name: "system.process.cpu.total.value", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("process")}},
238-
{Type: Sum, Name: "system.process.cpu.system.ticks", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("process")}},
239-
{Type: Sum, Name: "system.process.cpu.user.ticks", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("process")}},
240-
{Type: Sum, Name: "system.process.cpu.total.ticks", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("process")}},
241-
{Type: Sum, Name: "system.process.io.read_bytes", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(0)), Attrs: outAttr("process")}},
242-
{Type: Sum, Name: "system.process.io.write_bytes", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(0)), Attrs: outAttr("process")}},
243-
{Type: Sum, Name: "system.process.io.read_ops", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(0)), Attrs: outAttr("process")}},
244-
{Type: Sum, Name: "system.process.io.write_ops", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(0)), Attrs: outAttr("process")}},
226+
{Type: Sum, Name: "system.process.cpu.total.value", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(1764.3), Attrs: outAttr("process")}},
227+
{Type: Sum, Name: "system.process.cpu.system.ticks", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(334.5), Attrs: outAttr("process")}},
228+
{Type: Sum, Name: "system.process.cpu.user.ticks", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(554.6), Attrs: outAttr("process")}},
229+
{Type: Sum, Name: "system.process.cpu.total.ticks", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(1764.3), Attrs: outAttr("process")}},
230+
{Type: Sum, Name: "system.process.io.read_bytes", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(1024)), Attrs: outAttr("process")}},
231+
{Type: Sum, Name: "system.process.io.write_bytes", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(2048)), Attrs: outAttr("process")}},
232+
{Type: Sum, Name: "system.process.io.read_ops", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(10)), Attrs: outAttr("process")}},
233+
{Type: Sum, Name: "system.process.io.write_ops", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(20)), Attrs: outAttr("process")}},
245234
{Type: Gauge, Name: "system.process.cpu.total.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("process")}},
246235
},
247236
},

remappers/internal/metric.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package internal
1919

2020
import (
21+
"math"
22+
2123
"github.com/elastic/opentelemetry-lib/remappers/common"
2224
"go.opentelemetry.io/collector/pdata/pcommon"
2325
"go.opentelemetry.io/collector/pdata/pmetric"
@@ -46,6 +48,12 @@ func AddMetrics(
4648
ms.EnsureCapacity(ms.Len() + len(metrics))
4749

4850
for _, metric := range metrics {
51+
52+
//The translated metric should not be generated for a null or negative value
53+
if (metric.IntValue != nil && *metric.IntValue <= 0) || (metric.DoubleValue != nil && (*metric.DoubleValue <= 0 || math.IsInf(*metric.DoubleValue, 0))) {
54+
continue
55+
}
56+
4957
m := ms.AppendEmpty()
5058
m.SetName(metric.Name)
5159

remappers/kubernetesmetrics/k8smetrics_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,9 @@ func doTestRemap(t *testing.T, id string, remapOpts ...Option) {
8484
expected: []internal.TestMetric{
8585
{Type: Gauge, Name: "kubernetes.pod.cpu.usage.limit.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.26), Attrs: outAttr("kubeletstatsreceiver")}},
8686
{Type: Gauge, Name: "kubernetes.pod.cpu.usage.node.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.12), Attrs: outAttr("kubeletstatsreceiver")}},
87-
{Type: Gauge, Name: "kubernetes.pod.memory.usage.node.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("kubeletstatsreceiver")}},
8887
{Type: Gauge, Name: "kubernetes.pod.memory.usage.limit.pct", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.18), Attrs: outAttr("kubeletstatsreceiver")}},
8988
{Type: Sum, Name: "kubernetes.pod.network.tx.bytes", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(2048)), Attrs: outAttr("kubeletstatsreceiver")}},
9089
{Type: Sum, Name: "kubernetes.pod.network.rx.bytes", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(1024)), Attrs: outAttr("kubeletstatsreceiver")}},
91-
{Type: Gauge, Name: "kubernetes.node.cpu.usage.nanocores", DP: internal.TestDP{Ts: now, Dbl: internal.Ptr(0.0), Attrs: outAttr("kubeletstatsreceiver")}},
92-
{Type: Gauge, Name: "kubernetes.node.memory.usage.bytes", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(0)), Attrs: outAttr("kubeletstatsreceiver")}},
93-
{Type: Gauge, Name: "kubernetes.node.fs.capacity.bytes", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(0)), Attrs: outAttr("kubeletstatsreceiver")}},
94-
{Type: Gauge, Name: "kubernetes.node.fs.used.bytes", DP: internal.TestDP{Ts: now, Int: internal.Ptr(int64(0)), Attrs: outAttr("kubeletstatsreceiver")}},
9590
},
9691
},
9792
} {

0 commit comments

Comments
 (0)