Skip to content

Commit d5dd355

Browse files
authored
Revert #37 and add validation check for metric (#39)
* Revert #37 and add validation check for metric * Add check for sum and gauge metric type
1 parent 7f55be0 commit d5dd355

File tree

4 files changed

+183
-4
lines changed

4 files changed

+183
-4
lines changed

remappers/hostmetrics/hostmetrics_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,21 @@ func doTestRemap(t *testing.T, id string, remapOpts ...Option) {
116116
{Type: Gauge, Name: "system.cpu.system.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.68), Attrs: outAttr("cpu")}},
117117
{Type: Gauge, Name: "system.cpu.user.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.5), Attrs: outAttr("cpu")}},
118118
{Type: Gauge, Name: "system.cpu.steal.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.15), Attrs: outAttr("cpu")}},
119+
{Type: Gauge, Name: "system.cpu.iowait.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.0), Attrs: outAttr("cpu")}},
120+
{Type: Gauge, Name: "system.cpu.nice.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.0), Attrs: outAttr("cpu")}},
121+
{Type: Gauge, Name: "system.cpu.irq.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.0), Attrs: outAttr("cpu")}},
122+
{Type: Gauge, Name: "system.cpu.softirq.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.0), Attrs: outAttr("cpu")}},
119123
{Type: Sum, Name: "system.cpu.cores", DP: testutils.TestDP{Ts: now, Int: testutils.Ptr(int64(4)), Attrs: outAttr("cpu")}},
120124
{Type: Sum, Name: "system.load.cores", DP: testutils.TestDP{Ts: now, Int: testutils.Ptr(int64(4)), Attrs: outAttr("cpu")}},
121125
{Type: Gauge, Name: "system.cpu.total.norm.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.3325), Attrs: outAttr("cpu")}},
122126
{Type: Gauge, Name: "system.cpu.idle.norm.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.205), Attrs: outAttr("cpu")}},
123127
{Type: Gauge, Name: "system.cpu.system.norm.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.17), Attrs: outAttr("cpu")}},
124128
{Type: Gauge, Name: "system.cpu.user.norm.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.125), Attrs: outAttr("cpu")}},
125129
{Type: Gauge, Name: "system.cpu.steal.norm.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.0375), Attrs: outAttr("cpu")}},
130+
{Type: Gauge, Name: "system.cpu.iowait.norm.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.0), Attrs: outAttr("cpu")}},
131+
{Type: Gauge, Name: "system.cpu.nice.norm.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.0), Attrs: outAttr("cpu")}},
132+
{Type: Gauge, Name: "system.cpu.irq.norm.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.0), Attrs: outAttr("cpu")}},
133+
{Type: Gauge, Name: "system.cpu.softirq.norm.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.0), Attrs: outAttr("cpu")}},
126134
},
127135
},
128136
{
@@ -144,6 +152,10 @@ func doTestRemap(t *testing.T, id string, remapOpts ...Option) {
144152
{Type: Gauge, Name: "system.cpu.system.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.68), Attrs: outAttr("cpu")}},
145153
{Type: Gauge, Name: "system.cpu.user.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.5), Attrs: outAttr("cpu")}},
146154
{Type: Gauge, Name: "system.cpu.steal.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.15), Attrs: outAttr("cpu")}},
155+
{Type: Gauge, Name: "system.cpu.iowait.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.0), Attrs: outAttr("cpu")}},
156+
{Type: Gauge, Name: "system.cpu.nice.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.0), Attrs: outAttr("cpu")}},
157+
{Type: Gauge, Name: "system.cpu.irq.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.0), Attrs: outAttr("cpu")}},
158+
{Type: Gauge, Name: "system.cpu.softirq.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.0), Attrs: outAttr("cpu")}},
147159
},
148160
},
149161
{
@@ -217,6 +229,7 @@ func doTestRemap(t *testing.T, id string, remapOpts ...Option) {
217229
{Type: Sum, Name: "process.disk.operations", DP: testutils.TestDP{Ts: now, Int: testutils.Ptr(int64(20)), Attrs: map[string]any{"direction": "write"}}},
218230
},
219231
expected: []testutils.TestMetric{
232+
{Type: Sum, Name: "process.cpu.start_time", DP: testutils.TestDP{Ts: now, Int: testutils.Ptr(int64(0)), Attrs: outAttr("process")}},
220233
{Type: Sum, Name: "system.process.num_threads", DP: testutils.TestDP{Ts: now, Int: testutils.Ptr(int64(7)), Attrs: outAttr("process")}},
221234
{Type: Gauge, Name: "system.process.memory.rss.pct", DP: testutils.TestDP{Ts: now, Dbl: testutils.Ptr(0.15), Attrs: outAttr("process")}},
222235
{Type: Sum, Name: "system.process.memory.rss.bytes", DP: testutils.TestDP{Ts: now, Int: testutils.Ptr(int64(2048)), Attrs: outAttr("process")}},

remappers/internal/remappedmetric/metric.go

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

2020
import (
21-
"math"
22-
2321
"github.com/elastic/opentelemetry-lib/remappers/common"
2422
"go.opentelemetry.io/collector/pdata/pcommon"
2523
"go.opentelemetry.io/collector/pdata/pmetric"
@@ -38,6 +36,30 @@ type Metric struct {
3836
DataType pmetric.MetricType
3937
}
4038

39+
// Valid returns true if the metric is valid and set properly. A
40+
// metric is properly set if the value and the timestamp are set.
41+
// In addition, only gauge and sum metric are considered valid
42+
// as these are the only two currently supported metric type.
43+
func (m *Metric) Valid() bool {
44+
if m == nil {
45+
return false
46+
}
47+
if m.Name == "" {
48+
return false
49+
}
50+
if m.IntValue == nil && m.DoubleValue == nil {
51+
return false
52+
}
53+
if m.Timestamp == 0 {
54+
return false
55+
}
56+
switch m.DataType {
57+
case pmetric.MetricTypeGauge, pmetric.MetricTypeSum:
58+
return true
59+
}
60+
return false
61+
}
62+
4163
// Add adds a list of remapped OTel metric to the give MetricSlice.
4264
func Add(
4365
ms pmetric.MetricSlice,
@@ -49,8 +71,7 @@ func Add(
4971

5072
for _, metric := range metrics {
5173

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))) {
74+
if !metric.Valid() {
5475
continue
5576
}
5677

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package remappedmetric
19+
20+
import (
21+
"testing"
22+
"time"
23+
24+
"github.com/elastic/opentelemetry-lib/remappers/internal/testutils"
25+
"github.com/google/go-cmp/cmp"
26+
"github.com/stretchr/testify/assert"
27+
"go.opentelemetry.io/collector/pdata/pcommon"
28+
"go.opentelemetry.io/collector/pdata/pmetric"
29+
)
30+
31+
func TestAdd(t *testing.T) {
32+
dataset := "test"
33+
now := pcommon.NewTimestampFromTime(time.Now())
34+
attrs := map[string]any{
35+
"data_stream.dataset": dataset,
36+
"otel_remapped": true,
37+
}
38+
39+
for _, tc := range []struct {
40+
name string
41+
input []Metric
42+
mutator func(pmetric.NumberDataPoint)
43+
expected pmetric.MetricSlice
44+
}{
45+
{
46+
name: "empty",
47+
input: nil,
48+
mutator: EmptyMutator,
49+
expected: pmetric.NewMetricSlice(),
50+
},
51+
{
52+
name: "no_value_set",
53+
input: []Metric{
54+
{
55+
Name: "invalid",
56+
},
57+
{
58+
// value and datatype not set
59+
Timestamp: now,
60+
},
61+
{
62+
// timestamp and datatype not set
63+
IntValue: testutils.Ptr(int64(10)),
64+
},
65+
{
66+
// timestamp and datatype not set
67+
DoubleValue: testutils.Ptr(10.4),
68+
},
69+
{
70+
// timestamp and value not set
71+
DataType: pmetric.MetricTypeSum,
72+
},
73+
{
74+
Name: "invalid_histo",
75+
DataType: pmetric.MetricTypeHistogram,
76+
Timestamp: now,
77+
IntValue: testutils.Ptr(int64(10)),
78+
},
79+
{
80+
Name: "invalid_exp_histo",
81+
DataType: pmetric.MetricTypeExponentialHistogram,
82+
Timestamp: now,
83+
IntValue: testutils.Ptr(int64(10)),
84+
},
85+
{
86+
Name: "invalid_summary",
87+
DataType: pmetric.MetricTypeSummary,
88+
Timestamp: now,
89+
IntValue: testutils.Ptr(int64(10)),
90+
},
91+
},
92+
mutator: EmptyMutator,
93+
expected: pmetric.NewMetricSlice(),
94+
},
95+
{
96+
name: "valid",
97+
input: []Metric{
98+
{
99+
Name: "valid_int",
100+
DataType: pmetric.MetricTypeSum,
101+
Timestamp: now,
102+
IntValue: testutils.Ptr(int64(10)),
103+
},
104+
{
105+
Name: "valid_double",
106+
DataType: pmetric.MetricTypeSum,
107+
Timestamp: now,
108+
DoubleValue: testutils.Ptr(10.4),
109+
},
110+
},
111+
mutator: EmptyMutator,
112+
expected: func() pmetric.MetricSlice {
113+
s := pmetric.NewMetricSlice()
114+
m := s.AppendEmpty()
115+
m.SetName("valid_int")
116+
dp := m.SetEmptySum().DataPoints().AppendEmpty()
117+
dp.SetIntValue(10)
118+
dp.SetTimestamp(now)
119+
dp.Attributes().FromRaw(attrs)
120+
121+
m = s.AppendEmpty()
122+
m.SetName("valid_double")
123+
dp = m.SetEmptySum().DataPoints().AppendEmpty()
124+
dp.SetDoubleValue(10.4)
125+
dp.SetTimestamp(now)
126+
dp.Attributes().FromRaw(attrs)
127+
128+
return s
129+
}(),
130+
},
131+
} {
132+
actual := pmetric.NewMetricSlice()
133+
Add(actual, dataset, tc.mutator, tc.input...)
134+
135+
assert.Empty(t, cmp.Diff(
136+
testutils.MetricSliceToTestMetric(t, tc.expected),
137+
testutils.MetricSliceToTestMetric(t, actual),
138+
))
139+
}
140+
}

remappers/kubernetesmetrics/k8smetrics_test.go

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

0 commit comments

Comments
 (0)