Skip to content

Commit

Permalink
[pkg/otlp/metrics] Add support for NoRecordedValue flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mx-psi committed May 17, 2024
1 parent 796d82e commit 737cd6c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/otlp/metrics/metrics_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ func (t *Translator) mapNumberMetrics(

for i := 0; i < slice.Len(); i++ {
p := slice.At(i)
if p.Flags().NoRecordedValue() {
// No recorded value, skip.
continue
}

pointDims := dims.WithAttributeMap(p.Attributes())
var val float64
switch p.ValueType() {
Expand Down Expand Up @@ -203,6 +208,11 @@ func (t *Translator) mapNumberMonotonicMetrics(
) {
for i := 0; i < slice.Len(); i++ {
p := slice.At(i)
if p.Flags().NoRecordedValue() {
// No recorded value, skip.
continue
}

ts := uint64(p.Timestamp())
startTs := uint64(p.StartTimestamp())
pointDims := dims.WithAttributeMap(p.Attributes())
Expand Down Expand Up @@ -450,6 +460,11 @@ func (t *Translator) mapHistogramMetrics(
) {
for i := 0; i < slice.Len(); i++ {
p := slice.At(i)
if p.Flags().NoRecordedValue() {
// No recorded value, skip.
continue
}

startTs := uint64(p.StartTimestamp())
ts := uint64(p.Timestamp())
pointDims := dims.WithAttributeMap(p.Attributes())
Expand Down Expand Up @@ -554,6 +569,11 @@ func (t *Translator) mapSummaryMetrics(

for i := 0; i < slice.Len(); i++ {
p := slice.At(i)
if p.Flags().NoRecordedValue() {
// No recorded value, skip.
continue
}

startTs := uint64(p.StartTimestamp())
ts := uint64(p.Timestamp())
pointDims := dims.WithAttributeMap(p.Attributes())
Expand Down
34 changes: 34 additions & 0 deletions pkg/otlp/metrics/metrics_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,40 @@ func TestMapIntMonotonicWithRebootWithinSlice(t *testing.T) {
})
}

// This test checks that in the case of a reboot within a NumberDataPointSlice,
// we cache the value but we do NOT compute first value for the value at reset.
func TestMapIntMonotonicWithNoRecordedValueWithinSlice(t *testing.T) {

buildMonotonicWithNoRecorded := func() (slice pmetric.NumberDataPointSlice) {
values := []int64{0, 30, 0, 40}
slice = pmetric.NewNumberDataPointSlice()
slice.EnsureCapacity(len(values))

for i, val := range values {
point := slice.AppendEmpty()
point.SetTimestamp(seconds(i * 10))
point.SetIntValue(val)
}

var flags pmetric.DataPointFlags
slice.At(2).SetFlags(flags.WithNoRecordedValue(true))
return
}

slice := buildMonotonicWithNoRecorded()
ctx := context.Background()
tr := newTranslator(t, zap.NewNop())
consumer := &mockTimeSeriesConsumer{}
tr.mapNumberMonotonicMetrics(ctx, consumer, exampleDims, slice)
assert.ElementsMatch(t,
consumer.metrics,
[]metric{
newCount(exampleDims, uint64(seconds(10)), 30),
newCount(exampleDims, uint64(seconds(30)), 10),
},
)
}

// This test checks that in the case of a reboot at the first point in a NumberDataPointSlice:
// - diff: we cache the value AND compute first value
// - rate: we cache the value AND don't compute first value
Expand Down

0 comments on commit 737cd6c

Please sign in to comment.