@@ -624,6 +624,73 @@ else if (configuration().metricsRecorder() != null) {
624
624
}
625
625
}
626
626
627
+ /**
628
+ * Whether to enable metrics to be collected and registered in Micrometer's
629
+ * {@link io.micrometer.core.instrument.Metrics#globalRegistry globalRegistry}
630
+ * under the name {@link reactor.netty.Metrics#HTTP_SERVER_PREFIX}.
631
+ * <p>{@code uriTagValue} function receives the actual uri and returns the uri tag value
632
+ * that will be used for the metrics with {@link reactor.netty.Metrics#URI} tag.
633
+ * For example instead of using the actual uri {@code "/users/1"} as uri tag value, templated uri
634
+ * {@code "/users/{id}"} can be used.
635
+ * <p><strong>Note:</strong>
636
+ * It is strongly recommended to provide template-like form for the URIs. Without a conversion to a template-like form,
637
+ * each distinct URI leads to the creation of a distinct tag, which takes a lot of memory for the metrics.
638
+ * <p><strong>Note:</strong>
639
+ * It is strongly recommended applications to configure an upper limit for the number of the URI tags.
640
+ * For example:
641
+ * <pre class="code">
642
+ * Metrics.globalRegistry
643
+ * .config()
644
+ * .meterFilter(MeterFilter.maximumAllowableTags(HTTP_SERVER_PREFIX, URI, 100, MeterFilter.deny()));
645
+ * </pre>
646
+ * <p>{@code methodTagValue} function receives the actual method name and returns the method tag value
647
+ * that will be used for the metrics with {@link reactor.netty.Metrics#METHOD} tag.
648
+ * <p>By default metrics are not enabled.
649
+ *
650
+ * @param enable true enables metrics collection; false disables it
651
+ * @param uriTagValue a function that receives the actual uri and returns the uri tag value
652
+ * that will be used for the metrics with {@link reactor.netty.Metrics#URI} tag
653
+ * @param methodTagValue a function that receives the actual method name and returns the method tag value
654
+ * that will be used for the metrics with {@link reactor.netty.Metrics#METHOD} tag
655
+ * @return a new {@link HttpServer}
656
+ * @since 1.0.39
657
+ */
658
+ public final HttpServer metrics (boolean enable , Function <String , String > uriTagValue , Function <String , String > methodTagValue ) {
659
+ if (enable ) {
660
+ Objects .requireNonNull (methodTagValue , "methodTagValue" );
661
+ if (!Metrics .isInstrumentationAvailable ()) {
662
+ throw new UnsupportedOperationException (
663
+ "To enable metrics, you must add the dependency `io.micrometer:micrometer-core`" +
664
+ " to the class path first" );
665
+ }
666
+ if (uriTagValue == Function .<String >identity ()) {
667
+ log .debug ("Metrics are enabled with [uriTagValue=Function#identity]. " +
668
+ "It is strongly recommended to provide template-like form for the URIs. " +
669
+ "Without a conversion to a template-like form, each distinct URI leads " +
670
+ "to the creation of a distinct tag, which takes a lot of memory for the metrics." );
671
+ }
672
+ if (methodTagValue == Function .<String >identity ()) {
673
+ log .debug ("Metrics are enabled with [methodTagValue=Function#identity]. " +
674
+ "It is strongly recommended to provide a function for transforming the method names." );
675
+ }
676
+ HttpServer dup = duplicate ();
677
+ dup .configuration ().metricsRecorder (() -> configuration ().defaultMetricsRecorder ());
678
+ dup .configuration ().methodTagValue = methodTagValue ;
679
+ dup .configuration ().uriTagValue = uriTagValue ;
680
+ return dup ;
681
+ }
682
+ else if (configuration ().metricsRecorder () != null ) {
683
+ HttpServer dup = duplicate ();
684
+ dup .configuration ().metricsRecorder (null );
685
+ dup .configuration ().methodTagValue = null ;
686
+ dup .configuration ().uriTagValue = null ;
687
+ return dup ;
688
+ }
689
+ else {
690
+ return this ;
691
+ }
692
+ }
693
+
627
694
@ Override
628
695
public final HttpServer metrics (boolean enable , Supplier <? extends ChannelMetricsRecorder > recorder ) {
629
696
return super .metrics (enable , recorder );
@@ -663,6 +730,49 @@ else if (configuration().metricsRecorder() != null) {
663
730
}
664
731
}
665
732
733
+ /**
734
+ * Specifies whether the metrics are enabled on the {@link HttpServer}.
735
+ * All generated metrics are provided to the specified recorder which is only
736
+ * instantiated if metrics are being enabled (the instantiation is not lazy,
737
+ * but happens immediately, while configuring the {@link HttpServer}).
738
+ * <p>{@code uriValue} function receives the actual uri and returns the uri value
739
+ * that will be used when the metrics are propagated to the recorder.
740
+ * For example instead of using the actual uri {@code "/users/1"} as uri value, templated uri
741
+ * {@code "/users/{id}"} can be used.
742
+ * <p>{@code methodValue} function receives the actual method name and returns the method value
743
+ * that will be used when the metrics are propagated to the recorder.
744
+ *
745
+ * @param enable true enables metrics collection; false disables it
746
+ * @param recorder a supplier for the metrics recorder that receives the collected metrics
747
+ * @param uriValue a function that receives the actual uri and returns the uri value
748
+ * that will be used when the metrics are propagated to the recorder.
749
+ * @param methodValue a function that receives the actual method name and returns the method value
750
+ * that will be used when the metrics are propagated to the recorder.
751
+ * @return a new {@link HttpServer}
752
+ * @since 1.0.39
753
+ */
754
+ public final HttpServer metrics (boolean enable , Supplier <? extends ChannelMetricsRecorder > recorder ,
755
+ Function <String , String > uriValue , Function <String , String > methodValue ) {
756
+ if (enable ) {
757
+ Objects .requireNonNull (methodValue , "methodTagValue" );
758
+ HttpServer dup = duplicate ();
759
+ dup .configuration ().metricsRecorder (recorder );
760
+ dup .configuration ().methodTagValue = methodValue ;
761
+ dup .configuration ().uriTagValue = uriValue ;
762
+ return dup ;
763
+ }
764
+ else if (configuration ().metricsRecorder () != null ) {
765
+ HttpServer dup = duplicate ();
766
+ dup .configuration ().metricsRecorder (null );
767
+ dup .configuration ().methodTagValue = null ;
768
+ dup .configuration ().uriTagValue = null ;
769
+ return dup ;
770
+ }
771
+ else {
772
+ return this ;
773
+ }
774
+ }
775
+
666
776
/**
667
777
* Removes any previously applied SSL configuration customization.
668
778
*
0 commit comments