Skip to content

Commit 21a6d99

Browse files
Hyunuk LimJamesJHPark
Hyunuk Lim
andauthored
[Prometheus Remote Write Exporter] Handling Staleness flag from OTLP. (#6679)
* implement to handle staleness marker flag and added test cases for gauge and sum metric data types * feat: handling staleness marker for histogram and summary * modify test case implementation * feat: add test cases for histogram and summary * fix: lint error * fix: impi error * fix: remove else statement for the readability Co-authored-by: James Park <[email protected]>
1 parent 60440ca commit 21a6d99

File tree

4 files changed

+153
-5
lines changed

4 files changed

+153
-5
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
## 💡 Enhancements 💡
6+
7+
- `prometheusremotewriteexporter`: Handling Staleness flag from OTLP (#6679)
8+
59
## 🛑 Breaking changes 🛑
610

711
- `memcachedreceiver`: Update metric names (#6594)

exporter/prometheusremotewriteexporter/exporter_test.go

+88-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/gogo/protobuf/proto"
2727
"github.com/golang/snappy"
28+
"github.com/prometheus/prometheus/pkg/value"
2829
"github.com/prometheus/prometheus/prompb"
2930
"github.com/stretchr/testify/assert"
3031
"github.com/stretchr/testify/require"
@@ -380,7 +381,20 @@ func Test_PushMetrics(t *testing.T) {
380381

381382
emptySummaryBatch := getMetricsFromMetricList(invalidMetrics[emptySummary])
382383

383-
checkFunc := func(t *testing.T, r *http.Request, expected int) {
384+
// staleNaN cases
385+
staleNaNHistogramBatch := getMetricsFromMetricList(staleNaNMetrics[staleNaNHistogram])
386+
387+
staleNaNSummaryBatch := getMetricsFromMetricList(staleNaNMetrics[staleNaNSummary])
388+
389+
staleNaNIntGaugeBatch := getMetricsFromMetricList(staleNaNMetrics[staleNaNIntGauge])
390+
391+
staleNaNDoubleGaugeBatch := getMetricsFromMetricList(staleNaNMetrics[staleNaNDoubleGauge])
392+
393+
staleNaNIntSumBatch := getMetricsFromMetricList(staleNaNMetrics[staleNaNIntSum])
394+
395+
staleNaNSumBatch := getMetricsFromMetricList(staleNaNMetrics[staleNaNSum])
396+
397+
checkFunc := func(t *testing.T, r *http.Request, expected int, isStaleMarker bool) {
384398
body, err := ioutil.ReadAll(r.Body)
385399
if err != nil {
386400
t.Fatal(err)
@@ -397,15 +411,19 @@ func Test_PushMetrics(t *testing.T) {
397411
ok := proto.Unmarshal(dest, wr)
398412
require.Nil(t, ok)
399413
assert.EqualValues(t, expected, len(wr.Timeseries))
414+
if isStaleMarker {
415+
assert.True(t, value.IsStaleNaN(wr.Timeseries[0].Samples[0].Value))
416+
}
400417
}
401418

402419
tests := []struct {
403420
name string
404421
md *pdata.Metrics
405-
reqTestFunc func(t *testing.T, r *http.Request, expected int)
422+
reqTestFunc func(t *testing.T, r *http.Request, expected int, isStaleMarker bool)
406423
expectedTimeSeries int
407424
httpResponseCode int
408425
returnErr bool
426+
isStaleMarker bool
409427
}{
410428
{
411429
"invalid_type_case",
@@ -414,6 +432,7 @@ func Test_PushMetrics(t *testing.T) {
414432
0,
415433
http.StatusAccepted,
416434
true,
435+
false,
417436
},
418437
{
419438
"intSum_case",
@@ -422,6 +441,7 @@ func Test_PushMetrics(t *testing.T) {
422441
2,
423442
http.StatusAccepted,
424443
false,
444+
false,
425445
},
426446
{
427447
"doubleSum_case",
@@ -430,6 +450,7 @@ func Test_PushMetrics(t *testing.T) {
430450
2,
431451
http.StatusAccepted,
432452
false,
453+
false,
433454
},
434455
{
435456
"doubleGauge_case",
@@ -438,6 +459,7 @@ func Test_PushMetrics(t *testing.T) {
438459
2,
439460
http.StatusAccepted,
440461
false,
462+
false,
441463
},
442464
{
443465
"intGauge_case",
@@ -446,6 +468,7 @@ func Test_PushMetrics(t *testing.T) {
446468
2,
447469
http.StatusAccepted,
448470
false,
471+
false,
449472
},
450473
{
451474
"histogram_case",
@@ -454,6 +477,7 @@ func Test_PushMetrics(t *testing.T) {
454477
12,
455478
http.StatusAccepted,
456479
false,
480+
false,
457481
},
458482
{
459483
"summary_case",
@@ -462,6 +486,7 @@ func Test_PushMetrics(t *testing.T) {
462486
10,
463487
http.StatusAccepted,
464488
false,
489+
false,
465490
},
466491
{
467492
"unmatchedBoundBucketHist_case",
@@ -470,6 +495,7 @@ func Test_PushMetrics(t *testing.T) {
470495
5,
471496
http.StatusAccepted,
472497
false,
498+
false,
473499
},
474500
{
475501
"5xx_case",
@@ -478,6 +504,7 @@ func Test_PushMetrics(t *testing.T) {
478504
5,
479505
http.StatusServiceUnavailable,
480506
true,
507+
false,
481508
},
482509
{
483510
"emptyGauge_case",
@@ -486,6 +513,7 @@ func Test_PushMetrics(t *testing.T) {
486513
0,
487514
http.StatusAccepted,
488515
true,
516+
false,
489517
},
490518
{
491519
"emptyCumulativeSum_case",
@@ -494,6 +522,7 @@ func Test_PushMetrics(t *testing.T) {
494522
0,
495523
http.StatusAccepted,
496524
true,
525+
false,
497526
},
498527
{
499528
"emptyCumulativeHistogram_case",
@@ -502,6 +531,7 @@ func Test_PushMetrics(t *testing.T) {
502531
0,
503532
http.StatusAccepted,
504533
true,
534+
false,
505535
},
506536
{
507537
"emptySummary_case",
@@ -510,14 +540,69 @@ func Test_PushMetrics(t *testing.T) {
510540
0,
511541
http.StatusAccepted,
512542
true,
543+
false,
544+
},
545+
{
546+
"staleNaNIntGauge_case",
547+
&staleNaNIntGaugeBatch,
548+
checkFunc,
549+
1,
550+
http.StatusAccepted,
551+
false,
552+
true,
553+
},
554+
{
555+
"staleNaNDoubleGauge_case",
556+
&staleNaNDoubleGaugeBatch,
557+
checkFunc,
558+
1,
559+
http.StatusAccepted,
560+
false,
561+
true,
562+
},
563+
{
564+
"staleNaNIntSum_case",
565+
&staleNaNIntSumBatch,
566+
checkFunc,
567+
1,
568+
http.StatusAccepted,
569+
false,
570+
true,
571+
},
572+
{
573+
"staleNaNSum_case",
574+
&staleNaNSumBatch,
575+
checkFunc,
576+
1,
577+
http.StatusAccepted,
578+
false,
579+
true,
580+
},
581+
{
582+
"staleNaNHistogram_case",
583+
&staleNaNHistogramBatch,
584+
checkFunc,
585+
6,
586+
http.StatusAccepted,
587+
false,
588+
true,
589+
},
590+
{
591+
"staleNaNSummary_case",
592+
&staleNaNSummaryBatch,
593+
checkFunc,
594+
5,
595+
http.StatusAccepted,
596+
false,
597+
true,
513598
},
514599
}
515600

516601
for _, tt := range tests {
517602
t.Run(tt.name, func(t *testing.T) {
518603
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
519604
if tt.reqTestFunc != nil {
520-
tt.reqTestFunc(t, r, tt.expectedTimeSeries)
605+
tt.reqTestFunc(t, r, tt.expectedTimeSeries, tt.isStaleMarker)
521606
}
522607
w.WriteHeader(tt.httpResponseCode)
523608
}))

exporter/prometheusremotewriteexporter/helper.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/prometheus/common/model"
2828
"github.com/prometheus/prometheus/pkg/timestamp"
29+
"github.com/prometheus/prometheus/pkg/value"
2930
"github.com/prometheus/prometheus/prompb"
3031
"go.opentelemetry.io/collector/model/pdata"
3132
)
@@ -329,6 +330,9 @@ func addSingleNumberDataPoint(pt pdata.NumberDataPoint, resource pdata.Resource,
329330
case pdata.MetricValueTypeDouble:
330331
sample.Value = pt.DoubleVal()
331332
}
333+
if pt.Flags().HasFlag(pdata.MetricDataPointFlagNoRecordedValue) {
334+
sample.Value = math.Float64frombits(value.StaleNaN)
335+
}
332336
addSample(tsMap, sample, labels, metric)
333337
}
334338

@@ -344,6 +348,9 @@ func addSingleHistogramDataPoint(pt pdata.HistogramDataPoint, resource pdata.Res
344348
Value: pt.Sum(),
345349
Timestamp: time,
346350
}
351+
if pt.Flags().HasFlag(pdata.MetricDataPointFlagNoRecordedValue) {
352+
sum.Value = math.Float64frombits(value.StaleNaN)
353+
}
347354

348355
sumlabels := createAttributes(resource, pt.Attributes(), externalLabels, nameStr, baseName+sumStr)
349356
addSample(tsMap, sum, sumlabels, metric)
@@ -353,6 +360,10 @@ func addSingleHistogramDataPoint(pt pdata.HistogramDataPoint, resource pdata.Res
353360
Value: float64(pt.Count()),
354361
Timestamp: time,
355362
}
363+
if pt.Flags().HasFlag(pdata.MetricDataPointFlagNoRecordedValue) {
364+
count.Value = math.Float64frombits(value.StaleNaN)
365+
}
366+
356367
countlabels := createAttributes(resource, pt.Attributes(), externalLabels, nameStr, baseName+countStr)
357368
addSample(tsMap, count, countlabels, metric)
358369

@@ -373,6 +384,9 @@ func addSingleHistogramDataPoint(pt pdata.HistogramDataPoint, resource pdata.Res
373384
Value: float64(cumulativeCount),
374385
Timestamp: time,
375386
}
387+
if pt.Flags().HasFlag(pdata.MetricDataPointFlagNoRecordedValue) {
388+
bucket.Value = math.Float64frombits(value.StaleNaN)
389+
}
376390
boundStr := strconv.FormatFloat(bound, 'f', -1, 64)
377391
labels := createAttributes(resource, pt.Attributes(), externalLabels, nameStr, baseName+bucketStr, leStr, boundStr)
378392
sig := addSample(tsMap, bucket, labels, metric)
@@ -385,6 +399,9 @@ func addSingleHistogramDataPoint(pt pdata.HistogramDataPoint, resource pdata.Res
385399
Value: float64(cumulativeCount),
386400
Timestamp: time,
387401
}
402+
if pt.Flags().HasFlag(pdata.MetricDataPointFlagNoRecordedValue) {
403+
infBucket.Value = math.Float64frombits(value.StaleNaN)
404+
}
388405
infLabels := createAttributes(resource, pt.Attributes(), externalLabels, nameStr, baseName+bucketStr, leStr, pInfStr)
389406
sig := addSample(tsMap, infBucket, infLabels, metric)
390407

@@ -431,7 +448,9 @@ func addSingleSummaryDataPoint(pt pdata.SummaryDataPoint, resource pdata.Resourc
431448
Value: pt.Sum(),
432449
Timestamp: time,
433450
}
434-
451+
if pt.Flags().HasFlag(pdata.MetricDataPointFlagNoRecordedValue) {
452+
sum.Value = math.Float64frombits(value.StaleNaN)
453+
}
435454
sumlabels := createAttributes(resource, pt.Attributes(), externalLabels, nameStr, baseName+sumStr)
436455
addSample(tsMap, sum, sumlabels, metric)
437456

@@ -440,6 +459,9 @@ func addSingleSummaryDataPoint(pt pdata.SummaryDataPoint, resource pdata.Resourc
440459
Value: float64(pt.Count()),
441460
Timestamp: time,
442461
}
462+
if pt.Flags().HasFlag(pdata.MetricDataPointFlagNoRecordedValue) {
463+
count.Value = math.Float64frombits(value.StaleNaN)
464+
}
443465
countlabels := createAttributes(resource, pt.Attributes(), externalLabels, nameStr, baseName+countStr)
444466
addSample(tsMap, count, countlabels, metric)
445467

@@ -450,6 +472,9 @@ func addSingleSummaryDataPoint(pt pdata.SummaryDataPoint, resource pdata.Resourc
450472
Value: qt.Value(),
451473
Timestamp: time,
452474
}
475+
if pt.Flags().HasFlag(pdata.MetricDataPointFlagNoRecordedValue) {
476+
quantile.Value = math.Float64frombits(value.StaleNaN)
477+
}
453478
percentileStr := strconv.FormatFloat(qt.Quantile(), 'f', -1, 64)
454479
qtlabels := createAttributes(resource, pt.Attributes(), externalLabels, nameStr, baseName, quantileStr, percentileStr)
455480
addSample(tsMap, quantile, qtlabels, metric)

0 commit comments

Comments
 (0)