2121import com .linecorp .armeria .common .HttpMethod ;
2222import com .linecorp .armeria .common .HttpStatus ;
2323import com .linecorp .armeria .common .RequestHeaders ;
24+ import com .sun .net .httpserver .Authenticator ;
25+ import com .sun .net .httpserver .HttpExchange ;
26+ import com .sun .net .httpserver .HttpPrincipal ;
2427import io .github .netmikey .logunit .api .LogCapturer ;
2528import io .opentelemetry .api .common .Attributes ;
2629import io .opentelemetry .api .metrics .DoubleHistogram ;
@@ -528,6 +531,15 @@ void toBuilder() {
528531 PrometheusRegistry prometheusRegistry = new PrometheusRegistry ();
529532 builder .setPrometheusRegistry (prometheusRegistry );
530533
534+ Authenticator authenticator =
535+ new Authenticator () {
536+ @ Override
537+ public Result authenticate (HttpExchange exchange ) {
538+ return new Success (new HttpPrincipal ("anonymous" , "public" ));
539+ }
540+ };
541+ builder .setAuthenticator (authenticator );
542+
531543 PrometheusHttpServer httpServer = builder .build ();
532544 PrometheusHttpServerBuilder fromOriginalBuilder = httpServer .toBuilder ();
533545 httpServer .close ();
@@ -538,7 +550,8 @@ void toBuilder() {
538550 .hasFieldOrPropertyWithValue ("otelScopeEnabled" , false )
539551 .hasFieldOrPropertyWithValue ("allowedResourceAttributesFilter" , resourceAttributesFilter )
540552 .hasFieldOrPropertyWithValue ("executor" , executor )
541- .hasFieldOrPropertyWithValue ("prometheusRegistry" , prometheusRegistry );
553+ .hasFieldOrPropertyWithValue ("prometheusRegistry" , prometheusRegistry )
554+ .hasFieldOrPropertyWithValue ("authenticator" , authenticator );
542555 }
543556
544557 /**
@@ -611,4 +624,45 @@ void histogramDefaultBase2ExponentialHistogram() throws IOException {
611624 prometheusServer .shutdown ();
612625 }
613626 }
627+
628+ @ Test
629+ void authenticator () {
630+ // given
631+ try (PrometheusHttpServer prometheusServer =
632+ PrometheusHttpServer .builder ()
633+ .setHost ("localhost" )
634+ .setPort (0 )
635+ .setAuthenticator (
636+ new Authenticator () {
637+ @ Override
638+ public Result authenticate (HttpExchange exchange ) {
639+ if ("/metrics" .equals (exchange .getRequestURI ().getPath ())) {
640+ return new Failure (401 );
641+ } else {
642+ return new Success (new HttpPrincipal ("anonymous" , "public" ));
643+ }
644+ }
645+ })
646+ .build ()) {
647+ prometheusServer .register (
648+ new CollectionRegistration () {
649+ @ Override
650+ public Collection <MetricData > collectAllMetrics () {
651+ return metricData .get ();
652+ }
653+ });
654+ WebClient client =
655+ WebClient .builder ("http://localhost:" + prometheusServer .getAddress ().getPort ())
656+ .decorator (RetryingClient .newDecorator (RetryRule .failsafe ()))
657+ .build ();
658+
659+ // when
660+ AggregatedHttpResponse metricsResponse = client .get ("/metrics" ).aggregate ().join ();
661+ AggregatedHttpResponse defaultResponse = client .get ("/" ).aggregate ().join ();
662+
663+ // then
664+ assertThat (metricsResponse .status ()).isEqualTo (HttpStatus .UNAUTHORIZED );
665+ assertThat (defaultResponse .status ()).isEqualTo (HttpStatus .OK );
666+ }
667+ }
614668}
0 commit comments