Skip to content

Commit

Permalink
add test case for exportCreatedGate disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
pokgak committed Nov 9, 2024
1 parent 9a5c587 commit f11490c
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/translator/prometheusremotewrite/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func (c *prometheusConverter) addHistogramDataPoints(dataPoints pmetric.Histogra
c.addExemplars(pt, bucketBounds)

startTimestamp := pt.StartTimestamp()
if settings.ExportCreatedMetric && startTimestamp != 0 {
if settings.ExportCreatedMetric && startTimestamp != 0 && exportCreatedMetricGate.IsEnabled() {
labels := createLabels(baseName+createdSuffix, baseLabels)
c.addTimeSeriesIfNeeded(labels, startTimestamp, pt.Timestamp())
}
Expand Down
98 changes: 90 additions & 8 deletions pkg/translator/prometheusremotewrite/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,12 +678,14 @@ func TestMostRecentTimestampInMetric(t *testing.T) {
func TestPrometheusConverter_AddSummaryDataPoints(t *testing.T) {
ts := pcommon.Timestamp(time.Now().UnixNano())
tests := []struct {
name string
metric func() pmetric.Metric
want func() map[uint64]*prompb.TimeSeries
name string
isGateEnabled bool
metric func() pmetric.Metric
want func() map[uint64]*prompb.TimeSeries
}{
{
name: "summary with start time",
name: "summary with start time",
isGateEnabled: true,
metric: func() pmetric.Metric {
metric := pmetric.NewMetric()
metric.SetName("test_summary")
Expand Down Expand Up @@ -728,14 +730,52 @@ func TestPrometheusConverter_AddSummaryDataPoints(t *testing.T) {
},
},
{
name: "summary without start time",
name: "summary without start time",
isGateEnabled: true,
metric: func() pmetric.Metric {
metric := pmetric.NewMetric()
metric.SetName("test_summary")
metric.SetEmptySummary()

dp := metric.Summary().DataPoints().AppendEmpty()
dp.SetTimestamp(ts)

return metric
},
want: func() map[uint64]*prompb.TimeSeries {
labels := []prompb.Label{
{Name: model.MetricNameLabel, Value: "test_summary" + countStr},
}
sumLabels := []prompb.Label{
{Name: model.MetricNameLabel, Value: "test_summary" + sumStr},
}
return map[uint64]*prompb.TimeSeries{
timeSeriesSignature(labels): {
Labels: labels,
Samples: []prompb.Sample{
{Value: 0, Timestamp: convertTimeStamp(ts)},
},
},
timeSeriesSignature(sumLabels): {
Labels: sumLabels,
Samples: []prompb.Sample{
{Value: 0, Timestamp: convertTimeStamp(ts)},
},
},
}
},
},
{
name: "summary with exportCreatedMetricGate disabled",
isGateEnabled: false,
metric: func() pmetric.Metric {
metric := pmetric.NewMetric()
metric.SetName("test_summary")
metric.SetEmptySummary()

dp := metric.Summary().DataPoints().AppendEmpty()
dp.SetTimestamp(ts)
dp.SetStartTimestamp(ts)

return metric
},
Expand Down Expand Up @@ -765,6 +805,10 @@ func TestPrometheusConverter_AddSummaryDataPoints(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
oldValue := exportCreatedMetricGate.IsEnabled()
testutil.SetFeatureGateForTest(t, exportCreatedMetricGate, tt.isGateEnabled)
defer testutil.SetFeatureGateForTest(t, exportCreatedMetricGate, oldValue)

metric := tt.metric()
converter := newPrometheusConverter()

Expand Down Expand Up @@ -875,16 +919,54 @@ func TestPrometheusConverter_AddHistogramDataPoints(t *testing.T) {
}
},
},
{
name: "histogram with exportCreatedMetricGate disabled",
isGateEnabled: false,
metric: func() pmetric.Metric {
metric := pmetric.NewMetric()
metric.SetName("test_hist")
metric.SetEmptyHistogram().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)

pt := metric.Histogram().DataPoints().AppendEmpty()
pt.SetTimestamp(ts)
pt.SetStartTimestamp(ts)

return metric
},
want: func() map[uint64]*prompb.TimeSeries {
labels := []prompb.Label{
{Name: model.MetricNameLabel, Value: "test_hist" + countStr},
}
infLabels := []prompb.Label{
{Name: model.MetricNameLabel, Value: "test_hist_bucket"},
{Name: model.BucketLabel, Value: "+Inf"},
}
return map[uint64]*prompb.TimeSeries{
timeSeriesSignature(infLabels): {
Labels: infLabels,
Samples: []prompb.Sample{
{Value: 0, Timestamp: convertTimeStamp(ts)},
},
},
timeSeriesSignature(labels): {
Labels: labels,
Samples: []prompb.Sample{
{Value: 0, Timestamp: convertTimeStamp(ts)},
},
},
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
metric := tt.metric()
converter := newPrometheusConverter()

oldValue := exportCreatedMetricGate.IsEnabled()
testutil.SetFeatureGateForTest(t, exportCreatedMetricGate, tt.isGateEnabled)
defer testutil.SetFeatureGateForTest(t, exportCreatedMetricGate, oldValue)

metric := tt.metric()
converter := newPrometheusConverter()

converter.addHistogramDataPoints(
metric.Histogram().DataPoints(),
pcommon.NewResource(),
Expand Down

0 comments on commit f11490c

Please sign in to comment.