Skip to content

Commit e29d2f6

Browse files
authored
[pkg/pdatatest] introduce IgnoreExemplars, IgnoreExemplarSlice and ChangeDatapointAttributeValue to CompareMetricsOption (#39005)
<!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes #39004 --------- Signed-off-by: odubajDT <[email protected]>
1 parent b462a9e commit e29d2f6

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

.chloggen/exemplars-mask.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/pdatatest
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Introduce IgnoreExemplars, IgnoreExemplarSlice and ChangeDatapointAttributeValue to CompareMetricsOption"
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [39004]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [api]

pkg/pdatatest/pmetrictest/options.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,96 @@ func roundDataPointSliceValues(dataPoints pmetric.NumberDataPointSlice, precisio
159159
}
160160
}
161161

162+
// IgnoreExemplars is a CompareMetricsOption that clears exemplar fields on all data points.
163+
func IgnoreExemplars() CompareMetricsOption {
164+
return compareMetricsOptionFunc(func(expected, actual pmetric.Metrics) {
165+
maskExemplars(expected)
166+
maskExemplars(actual)
167+
})
168+
}
169+
170+
func maskExemplars(metrics pmetric.Metrics) {
171+
emptyExemplar := pmetric.NewExemplar()
172+
for i := 0; i < metrics.ResourceMetrics().Len(); i++ {
173+
for j := 0; j < metrics.ResourceMetrics().At(i).ScopeMetrics().Len(); j++ {
174+
for g := 0; g < metrics.ResourceMetrics().At(i).ScopeMetrics().At(j).Metrics().Len(); g++ {
175+
m := metrics.ResourceMetrics().At(i).ScopeMetrics().At(j).Metrics().At(g)
176+
switch m.Type() {
177+
case pmetric.MetricTypeGauge:
178+
datapoints := m.Gauge().DataPoints()
179+
for k := 0; k < datapoints.Len(); k++ {
180+
for f := 0; f < datapoints.At(k).Exemplars().Len(); f++ {
181+
emptyExemplar.CopyTo(datapoints.At(k).Exemplars().At(f))
182+
}
183+
}
184+
case pmetric.MetricTypeSum:
185+
datapoints := m.Sum().DataPoints()
186+
for k := 0; k < datapoints.Len(); k++ {
187+
for f := 0; f < datapoints.At(k).Exemplars().Len(); f++ {
188+
emptyExemplar.CopyTo(datapoints.At(k).Exemplars().At(f))
189+
}
190+
}
191+
case pmetric.MetricTypeHistogram:
192+
datapoints := m.Histogram().DataPoints()
193+
for k := 0; k < datapoints.Len(); k++ {
194+
for f := 0; f < datapoints.At(k).Exemplars().Len(); f++ {
195+
emptyExemplar.CopyTo(datapoints.At(k).Exemplars().At(f))
196+
}
197+
}
198+
case pmetric.MetricTypeExponentialHistogram:
199+
datapoints := m.ExponentialHistogram().DataPoints()
200+
for k := 0; k < datapoints.Len(); k++ {
201+
for f := 0; f < datapoints.At(k).Exemplars().Len(); f++ {
202+
emptyExemplar.CopyTo(datapoints.At(k).Exemplars().At(f))
203+
}
204+
}
205+
}
206+
}
207+
}
208+
}
209+
}
210+
211+
// IgnoreExemplarSlice is a CompareMetricsOption that clears exemplars slice on all data points.
212+
func IgnoreExemplarSlice() CompareMetricsOption {
213+
return compareMetricsOptionFunc(func(expected, actual pmetric.Metrics) {
214+
maskExemplarSlice(expected)
215+
maskExemplarSlice(actual)
216+
})
217+
}
218+
219+
func maskExemplarSlice(metrics pmetric.Metrics) {
220+
emptyExemplarSlice := pmetric.NewExemplarSlice()
221+
for i := 0; i < metrics.ResourceMetrics().Len(); i++ {
222+
for j := 0; j < metrics.ResourceMetrics().At(i).ScopeMetrics().Len(); j++ {
223+
for g := 0; g < metrics.ResourceMetrics().At(i).ScopeMetrics().At(j).Metrics().Len(); g++ {
224+
m := metrics.ResourceMetrics().At(i).ScopeMetrics().At(j).Metrics().At(g)
225+
switch m.Type() {
226+
case pmetric.MetricTypeGauge:
227+
datapoints := m.Gauge().DataPoints()
228+
for k := 0; k < datapoints.Len(); k++ {
229+
emptyExemplarSlice.CopyTo(datapoints.At(k).Exemplars())
230+
}
231+
case pmetric.MetricTypeSum:
232+
datapoints := m.Sum().DataPoints()
233+
for k := 0; k < datapoints.Len(); k++ {
234+
emptyExemplarSlice.CopyTo(datapoints.At(k).Exemplars())
235+
}
236+
case pmetric.MetricTypeHistogram:
237+
datapoints := m.Histogram().DataPoints()
238+
for k := 0; k < datapoints.Len(); k++ {
239+
emptyExemplarSlice.CopyTo(datapoints.At(k).Exemplars())
240+
}
241+
case pmetric.MetricTypeExponentialHistogram:
242+
datapoints := m.ExponentialHistogram().DataPoints()
243+
for k := 0; k < datapoints.Len(); k++ {
244+
emptyExemplarSlice.CopyTo(datapoints.At(k).Exemplars())
245+
}
246+
}
247+
}
248+
}
249+
}
250+
}
251+
162252
// IgnoreTimestamp is a CompareMetricsOption that clears Timestamp fields on all the data points.
163253
func IgnoreTimestamp() CompareMetricsOption {
164254
return compareMetricsOptionFunc(func(expected, actual pmetric.Metrics) {
@@ -571,6 +661,59 @@ func changeMetricsResourceAttributeValue(metrics pmetric.Metrics, attributeName
571661
}
572662
}
573663

664+
// ChangeDatapointAttributeValue changes the metric datapoint value with the specified key
665+
func ChangeDatapointAttributeValue(attributeName string, changeFn func(string) string) CompareMetricsOption {
666+
return compareMetricsOptionFunc(func(expected, actual pmetric.Metrics) {
667+
changeMetricsDatapointAttributeValue(expected, attributeName, changeFn)
668+
changeMetricsDatapointAttributeValue(actual, attributeName, changeFn)
669+
})
670+
}
671+
672+
func changeMetricsDatapointAttributeValue(metrics pmetric.Metrics, attributeName string, changeFn func(string) string) {
673+
rms := metrics.ResourceMetrics()
674+
for i := 0; i < rms.Len(); i++ {
675+
internal.ChangeResourceAttributeValue(rms.At(i).Resource(), attributeName, changeFn)
676+
}
677+
678+
for i := 0; i < metrics.ResourceMetrics().Len(); i++ {
679+
for j := 0; j < metrics.ResourceMetrics().At(i).ScopeMetrics().Len(); j++ {
680+
for g := 0; g < metrics.ResourceMetrics().At(i).ScopeMetrics().At(j).Metrics().Len(); g++ {
681+
m := metrics.ResourceMetrics().At(i).ScopeMetrics().At(j).Metrics().At(g)
682+
switch m.Type() {
683+
case pmetric.MetricTypeGauge:
684+
datapoints := m.Gauge().DataPoints()
685+
for k := 0; k < datapoints.Len(); k++ {
686+
if v, ok := datapoints.At(k).Attributes().Get(attributeName); ok {
687+
datapoints.At(k).Attributes().PutStr(attributeName, changeFn(v.Str()))
688+
}
689+
}
690+
case pmetric.MetricTypeSum:
691+
datapoints := m.Sum().DataPoints()
692+
for k := 0; k < datapoints.Len(); k++ {
693+
if v, ok := datapoints.At(k).Attributes().Get(attributeName); ok {
694+
datapoints.At(k).Attributes().PutStr(attributeName, changeFn(v.Str()))
695+
}
696+
}
697+
case pmetric.MetricTypeHistogram:
698+
datapoints := m.Histogram().DataPoints()
699+
for k := 0; k < datapoints.Len(); k++ {
700+
if v, ok := datapoints.At(k).Attributes().Get(attributeName); ok {
701+
datapoints.At(k).Attributes().PutStr(attributeName, changeFn(v.Str()))
702+
}
703+
}
704+
case pmetric.MetricTypeExponentialHistogram:
705+
datapoints := m.ExponentialHistogram().DataPoints()
706+
for k := 0; k < datapoints.Len(); k++ {
707+
if v, ok := datapoints.At(k).Attributes().Get(attributeName); ok {
708+
datapoints.At(k).Attributes().PutStr(attributeName, changeFn(v.Str()))
709+
}
710+
}
711+
}
712+
}
713+
}
714+
}
715+
}
716+
574717
// IgnoreSubsequentDataPoints is a CompareMetricsOption that ignores data points after the first.
575718
func IgnoreSubsequentDataPoints(metricNames ...string) CompareMetricsOption {
576719
return compareMetricsOptionFunc(func(expected, actual pmetric.Metrics) {

0 commit comments

Comments
 (0)