/**
* A gRPC server interceptor that will collect metrics using the given
* {@link MeterRegistry}.
*
* <p>
* <b>Usage:</b>
* </p>
*
* <pre>
* Server server = ServerBuilder.forPort(8080)
* .intercept(new MetricCollectingServerInterceptor(meterRegistry))
* .build();
*
* server.start()
* </pre>
*
* @author Daniel Theuke (daniel.theuke@heuboe.de)
* @since 1.7.0
*/publicclassMetricCollectingServerInterceptorextendsAbstractMetricCollectingInterceptorimplementsServerInterceptor{publicMetricCollectingServerInterceptor(finalMeterRegistry registry){super(registry);}publicMetricCollectingServerInterceptor(finalMeterRegistry registry,finalUnaryOperator<Counter.Builder> counterCustomizer,finalUnaryOperator<Timer.Builder> timerCustomizer,finalCode... eagerInitializedCodes){super(registry, counterCustomizer, timerCustomizer, eagerInitializedCodes);}publicvoidpreregisterService(finalBindableService service){preregisterService(service.bindService());}publicvoidpreregisterService(finalServerServiceDefinition serviceDefinition){preregisterService(serviceDefinition.getServiceDescriptor());}@OverrideprotectedCounternewRequestCounterFor(finalMethodDescriptor<?,?> method){returnthis.counterCustomizer.apply(prepareCounterFor(method, METRIC_NAME_SERVER_REQUESTS_RECEIVED,"The total number of requests received")).register(this.registry);}@OverrideprotectedCounternewResponseCounterFor(finalMethodDescriptor<?,?> method){returnthis.counterCustomizer.apply(prepareCounterFor(method, METRIC_NAME_SERVER_RESPONSES_SENT,"The total number of responses sent")).register(this.registry);}@OverrideprotectedFunction<Code,Timer>newTimerFunction(finalMethodDescriptor<?,?> method){returnasTimerFunction(()->this.timerCustomizer.apply(prepareTimerFor(method,
METRIC_NAME_SERVER_PROCESSING_DURATION,"The total time taken for the server to complete the call")));}@Overridepublic<Q,A>ServerCall.Listener<Q>interceptCall(finalServerCall<Q,A> call,finalMetadata requestHeaders,finalServerCallHandler<Q,A> next){finalMetricSet metrics =metricsFor(call.getMethodDescriptor());finalConsumer<Status.Code> responseStatusTiming = metrics.newProcessingDurationTiming(this.registry);finalMetricCollectingServerCall<Q,A> monitoringCall =newMetricCollectingServerCall<>(call,
metrics.getResponseCounter());returnnewMetricCollectingServerCallListener<>(next.startCall(monitoringCall, requestHeaders),
metrics.getRequestCounter(), monitoringCall::getResponseCode, responseStatusTiming);}}