@@ -11,6 +11,7 @@ import (
11
11
"net/http/httptest"
12
12
"reflect"
13
13
"runtime/debug"
14
+ "strings"
14
15
"testing"
15
16
16
17
"go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp"
@@ -122,7 +123,7 @@ func TestMetricExporterPrometheusInvalidPort(t *testing.T) {
122
123
assert .ErrorContains (t , err , "binding" )
123
124
}
124
125
125
- func TestMetricProducerPrometheus (t * testing.T ) {
126
+ func TestMetricProducerPrometheusWithOTLPExporter (t * testing.T ) {
126
127
assertNoOtelHandleErrors (t )
127
128
128
129
requestWaitChan := make (chan struct {})
@@ -150,6 +151,7 @@ func TestMetricProducerPrometheus(t *testing.T) {
150
151
assert .IsType (t , & metric.PeriodicReader {}, r )
151
152
152
153
// Register it with a meter provider to ensure it is used.
154
+ // mp.Shutdown errors out because r.Shutdown closes the reader.
153
155
metric .NewMeterProvider (metric .WithReader (r ))
154
156
155
157
// Shutdown actually makes an export call.
@@ -159,3 +161,34 @@ func TestMetricProducerPrometheus(t *testing.T) {
159
161
ts .Close ()
160
162
goleak .VerifyNone (t )
161
163
}
164
+
165
+ func TestMetricProducerPrometheusWithPrometheusExporter (t * testing.T ) {
166
+ assertNoOtelHandleErrors (t )
167
+
168
+ t .Setenv ("OTEL_METRICS_EXPORTER" , "prometheus" )
169
+ t .Setenv ("OTEL_EXPORTER_PROMETHEUS_PORT" , "0" )
170
+ t .Setenv ("OTEL_METRICS_PRODUCERS" , "prometheus" )
171
+
172
+ r , err := NewMetricReader (context .Background ())
173
+ assert .NoError (t , err )
174
+
175
+ // pull-based exporters like Prometheus need to be registered
176
+ mp := metric .NewMeterProvider (metric .WithReader (r ))
177
+
178
+ rws , ok := r .(readerWithServer )
179
+ if ! ok {
180
+ t .Errorf ("expected readerWithServer but got %v" , r )
181
+ }
182
+
183
+ resp , err := http .Get (fmt .Sprintf ("http://%s/metrics" , rws .addr ))
184
+ assert .NoError (t , err )
185
+ body , err := io .ReadAll (resp .Body )
186
+ assert .NoError (t , err )
187
+
188
+ // By default there are two metrics exporter. target_info and promhttp_metric_handler_errors_total.
189
+ // But by including the prometheus producer we should have more.
190
+ assert .Greater (t , strings .Count (string (body ), "# HELP" ), 2 )
191
+
192
+ assert .NoError (t , mp .Shutdown (context .Background ()))
193
+ goleak .VerifyNone (t )
194
+ }
0 commit comments