Skip to content

Commit 3c02288

Browse files
authored
[chore][prometheusreceiver]: safer and quicker tests (open-telemetry#43906)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Some tests set the global registry to enable native histograms, which is not safe for t.Parallel(). Fix this by only using the feature gate in the receiver factory. This makes TestNativeVsClassicHistogramScrapeViaProtobuf 4x faster on my laptop. Signed-off-by: György Krajcsovits <[email protected]>
1 parent aef9425 commit 3c02288

File tree

4 files changed

+13
-20
lines changed

4 files changed

+13
-20
lines changed

receiver/prometheusreceiver/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ type Config struct {
4747
// server in agent mode. This allows the user to call the endpoint to get
4848
// the config, service discovery, and targets for debugging purposes.
4949
APIServer APIServer `mapstructure:"api_server"`
50+
51+
// From feature gate.
52+
enableNativeHistograms bool
5053
}
5154

5255
// Validate checks the receiver configuration is valid.

receiver/prometheusreceiver/factory.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func createMetricsReceiver(
6363
nextConsumer consumer.Metrics,
6464
) (receiver.Metrics, error) {
6565
configWarnings(set.Logger, cfg.(*Config))
66+
cfg.(*Config).enableNativeHistograms = enableNativeHistogramsGate.IsEnabled()
6667
return newPrometheusReceiver(set, cfg.(*Config), nextConsumer)
6768
}
6869

receiver/prometheusreceiver/metrics_receiver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func newPrometheusReceiver(set receiver.Settings, cfg *Config, next consumer.Met
9898
set,
9999
cfg.TargetAllocator.Get(),
100100
&baseCfg,
101-
enableNativeHistogramsGate.IsEnabled(),
101+
cfg.enableNativeHistograms,
102102
),
103103
}
104104
return pr, nil
@@ -176,7 +176,7 @@ func (r *pReceiver) initPrometheusComponents(ctx context.Context, logger *slog.L
176176
r.cfg.UseStartTimeMetric,
177177
startTimeMetricRegex,
178178
useCreatedMetricGate.IsEnabled(),
179-
enableNativeHistogramsGate.IsEnabled(),
179+
r.cfg.enableNativeHistograms,
180180
r.cfg.PrometheusConfig.GlobalConfig.ExternalLabels,
181181
r.cfg.TrimMetricSuffixes,
182182
)
@@ -231,7 +231,7 @@ func (r *pReceiver) initScrapeOptions() *scrape.Options {
231231
commonconfig.WithUserAgent(r.settings.BuildInfo.Command + "/" + r.settings.BuildInfo.Version),
232232
},
233233
EnableCreatedTimestampZeroIngestion: enableCreatedTimestampZeroIngestionGate.IsEnabled(),
234-
EnableNativeHistogramsIngestion: enableNativeHistogramsGate.IsEnabled(),
234+
EnableNativeHistogramsIngestion: r.cfg.enableNativeHistograms,
235235
}
236236

237237
return opts

receiver/prometheusreceiver/metrics_receiver_protobuf_test.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/prometheus/prometheus/config"
1212
dto "github.com/prometheus/prometheus/prompb/io/prometheus/client"
1313
"github.com/stretchr/testify/require"
14-
"go.opentelemetry.io/collector/featuregate"
1514
"go.opentelemetry.io/collector/pdata/pmetric"
1615
)
1716

@@ -496,14 +495,9 @@ func TestNativeVsClassicHistogramScrapeViaProtobuf(t *testing.T) {
496495
},
497496
}
498497

499-
defer func() {
500-
_ = featuregate.GlobalRegistry().Set("receiver.prometheusreceiver.EnableNativeHistograms", false)
501-
}()
502-
503498
for name, tc := range testCases {
504499
t.Run(name, func(t *testing.T) {
505-
err := featuregate.GlobalRegistry().Set("receiver.prometheusreceiver.EnableNativeHistograms", tc.enableNativeHistograms)
506-
require.NoError(t, err)
500+
t.Parallel()
507501

508502
targets := []*testData{
509503
{
@@ -522,6 +516,7 @@ func TestNativeVsClassicHistogramScrapeViaProtobuf(t *testing.T) {
522516
mutCfg = func(*PromConfig) {}
523517
}
524518
testComponent(t, targets, func(c *Config) {
519+
c.enableNativeHistograms = tc.enableNativeHistograms
525520
c.PrometheusConfig.GlobalConfig.ScrapeProtocols = []config.ScrapeProtocol{config.PrometheusProto}
526521
}, mutCfg)
527522
})
@@ -650,12 +645,9 @@ func TestStaleExponentialHistogram(t *testing.T) {
650645
},
651646
},
652647
}
653-
err := featuregate.GlobalRegistry().Set("receiver.prometheusreceiver.EnableNativeHistograms", true)
654-
require.NoError(t, err)
655-
defer func() {
656-
_ = featuregate.GlobalRegistry().Set("receiver.prometheusreceiver.EnableNativeHistograms", false)
657-
}()
648+
658649
testComponent(t, targets, func(c *Config) {
650+
c.enableNativeHistograms = true
659651
c.PrometheusConfig.GlobalConfig.ScrapeProtocols = []config.ScrapeProtocol{config.PrometheusProto}
660652
})
661653
}
@@ -715,12 +707,9 @@ func TestFloatCounterHistogram(t *testing.T) {
715707
},
716708
},
717709
}
718-
err := featuregate.GlobalRegistry().Set("receiver.prometheusreceiver.EnableNativeHistograms", true)
719-
require.NoError(t, err)
720-
defer func() {
721-
_ = featuregate.GlobalRegistry().Set("receiver.prometheusreceiver.EnableNativeHistograms", false)
722-
}()
710+
723711
testComponent(t, targets, func(c *Config) {
712+
c.enableNativeHistograms = true
724713
c.PrometheusConfig.GlobalConfig.ScrapeProtocols = []config.ScrapeProtocol{config.PrometheusProto}
725714
})
726715
}

0 commit comments

Comments
 (0)