public override async Task <TResponse> UnaryServerHandler <TRequest, TResponse>(TRequest request,
                                                                                        ServerCallContext context,
                                                                                        UnaryServerMethod <TRequest, TResponse> continuation)
        {
            GrpcMethodInfo method = new GrpcMethodInfo(context.Method, MethodType.Unary);

            metrics.RequestCounterInc(method);

            Stopwatch watch = new Stopwatch();

            watch.Start();

            try
            {
                TResponse result = await continuation(request, context);

                metrics.ResponseCounterInc(method, context.Status.StatusCode);
                return(result);
            }
            catch (RpcException e)
            {
                metrics.ResponseCounterInc(method, e.Status.StatusCode);
                throw;
            }
            finally
            {
                watch.Stop();
                if (enableLatencyMetrics)
                {
                    metrics.RecordLatency(method, watch.Elapsed.TotalSeconds);
                }
            }
        }
 /// <summary>
 /// Increments <see cref="StreamReceivedCounter"/>
 /// </summary>
 /// <param name="method">Information about the call</param>
 /// <param name="inc">Indicates by how much counter should be incremented. By default it's set to 1</param>
 public static void StreamReceivedCounterInc(this IMetricsRoot metrics, GrpcMethodInfo method, double inc = 1d)
 {
     metrics.Measure.Counter.Increment(MetricsRegistry.StreamReceivedCounter,
                                       new MetricTags(new string[]
     {
         "grpc_type", "grpc_service", "grpc_method"
     }, new string[]
     {
         method.MethodType, method.ServiceName, method.Name
     }));
 }
        public override Task DuplexStreamingServerHandler <TRequest, TResponse>(
            IAsyncStreamReader <TRequest> requestStream,
            IServerStreamWriter <TResponse> responseStream, ServerCallContext context,
            DuplexStreamingServerMethod <TRequest, TResponse> continuation)
        {
            GrpcMethodInfo method = new GrpcMethodInfo(context.Method, MethodType.DuplexStreaming);

            metrics.RequestCounterInc(method);

            Stopwatch watch = new Stopwatch();

            watch.Start();

            Task result;

            try
            {
                result = continuation(
                    new WrapperStreamReader <TRequest>(requestStream,
                                                       () => { metrics.StreamReceivedCounterInc(method); }),
                    new WrapperServerStreamWriter <TResponse>(responseStream,
                                                              () => { metrics.StreamSentCounterInc(method); }), context);

                metrics.ResponseCounterInc(method, StatusCode.OK);
            }
            catch (RpcException e)
            {
                metrics.ResponseCounterInc(method, e.Status.StatusCode);

                throw;
            }
            finally
            {
                watch.Stop();
                if (enableLatencyMetrics)
                {
                    metrics.RecordLatency(method, watch.Elapsed.TotalSeconds);
                }
            }

            return(result);
        }
 /// <summary>
 /// Records latency recorded during the call
 /// </summary>
 /// <param name="method">Infromation about the call</param>
 /// <param name="value">Value that should be recorded</param>
 public static void RecordLatency(this IMetricsRoot metrics, GrpcMethodInfo method, double value)
 {
     metrics.Measure.Histogram.Update(MetricsRegistry.LatencyHistogram, new MetricTags(new string[] { "grpc_type", "grpc_service", "grpc_method" }, new string[] { method.MethodType, method.ServiceName, method.Name }), (long)value);
 }
 /// <summary>
 /// Increments <see cref="ResponseCounter"/>
 /// </summary>
 /// <param name="method">Information about the call</param>
 /// <param name="code">Response status code</param>
 /// <param name="inc">Indicates by how much counter should be incremented. By default it's set to 1</param>
 public static void ResponseCounterInc(this IMetricsRoot metrics, GrpcMethodInfo method, StatusCode code, double inc = 1d)
 {
     metrics.Measure.Counter.Increment(MetricsRegistry.ResponseCounter, new MetricTags(new string[] { "grpc_type", "grpc_service", "grpc_method", "grpc_code" }, new string[] { method.MethodType, method.ServiceName, method.Name, code.ToString() }));
 }