|
| 1 | +#region Copyright notice and license |
| 2 | + |
| 3 | +// Copyright 2019 The gRPC Authors |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +#endregion |
| 18 | + |
| 19 | +using Grpc.Core; |
| 20 | +using Grpc.Core.Interceptors; |
| 21 | +using Microsoft.Extensions.Logging; |
| 22 | + |
| 23 | +namespace Grpc.Net.Client.Tests.Infrastructure; |
| 24 | + |
| 25 | +public sealed class ClientLoggerInterceptor : Interceptor |
| 26 | +{ |
| 27 | + private readonly ILogger<ClientLoggerInterceptor> _logger; |
| 28 | + |
| 29 | + public ClientLoggerInterceptor(ILoggerFactory loggerFactory) |
| 30 | + { |
| 31 | + _logger = loggerFactory.CreateLogger<ClientLoggerInterceptor>(); |
| 32 | + } |
| 33 | + |
| 34 | + public override TResponse BlockingUnaryCall<TRequest, TResponse>( |
| 35 | + TRequest request, |
| 36 | + ClientInterceptorContext<TRequest, TResponse> context, |
| 37 | + BlockingUnaryCallContinuation<TRequest, TResponse> continuation) |
| 38 | + { |
| 39 | + LogCall(context.Method); |
| 40 | + AddCallerMetadata(ref context); |
| 41 | + |
| 42 | + try |
| 43 | + { |
| 44 | + return continuation(request, context); |
| 45 | + } |
| 46 | + catch (Exception ex) |
| 47 | + { |
| 48 | + LogError(ex); |
| 49 | + throw; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>( |
| 54 | + TRequest request, |
| 55 | + ClientInterceptorContext<TRequest, TResponse> context, |
| 56 | + AsyncUnaryCallContinuation<TRequest, TResponse> continuation) |
| 57 | + { |
| 58 | + LogCall(context.Method); |
| 59 | + AddCallerMetadata(ref context); |
| 60 | + |
| 61 | + try |
| 62 | + { |
| 63 | + var call = continuation(request, context); |
| 64 | + |
| 65 | + return new AsyncUnaryCall<TResponse>(HandleResponse(call.ResponseAsync), call.ResponseHeadersAsync, call.GetStatus, call.GetTrailers, call.Dispose); |
| 66 | + } |
| 67 | + catch (Exception ex) |
| 68 | + { |
| 69 | + LogError(ex); |
| 70 | + throw; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private async Task<TResponse> HandleResponse<TResponse>(Task<TResponse> t) |
| 75 | + { |
| 76 | + try |
| 77 | + { |
| 78 | + var response = await t; |
| 79 | + _logger.LogInformation($"Response received: {response}"); |
| 80 | + return response; |
| 81 | + } |
| 82 | + catch (Exception ex) |
| 83 | + { |
| 84 | + LogError(ex); |
| 85 | + throw; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>( |
| 90 | + ClientInterceptorContext<TRequest, TResponse> context, |
| 91 | + AsyncClientStreamingCallContinuation<TRequest, TResponse> continuation) |
| 92 | + { |
| 93 | + LogCall(context.Method); |
| 94 | + AddCallerMetadata(ref context); |
| 95 | + |
| 96 | + try |
| 97 | + { |
| 98 | + return continuation(context); |
| 99 | + } |
| 100 | + catch (Exception ex) |
| 101 | + { |
| 102 | + LogError(ex); |
| 103 | + throw; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>( |
| 108 | + TRequest request, |
| 109 | + ClientInterceptorContext<TRequest, TResponse> context, |
| 110 | + AsyncServerStreamingCallContinuation<TRequest, TResponse> continuation) |
| 111 | + { |
| 112 | + LogCall(context.Method); |
| 113 | + AddCallerMetadata(ref context); |
| 114 | + |
| 115 | + try |
| 116 | + { |
| 117 | + return continuation(request, context); |
| 118 | + } |
| 119 | + catch (Exception ex) |
| 120 | + { |
| 121 | + LogError(ex); |
| 122 | + throw; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>( |
| 127 | + ClientInterceptorContext<TRequest, TResponse> context, |
| 128 | + AsyncDuplexStreamingCallContinuation<TRequest, TResponse> continuation) |
| 129 | + { |
| 130 | + LogCall(context.Method); |
| 131 | + AddCallerMetadata(ref context); |
| 132 | + |
| 133 | + try |
| 134 | + { |
| 135 | + return continuation(context); |
| 136 | + } |
| 137 | + catch (Exception ex) |
| 138 | + { |
| 139 | + LogError(ex); |
| 140 | + throw; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private void LogCall<TRequest, TResponse>(Method<TRequest, TResponse> method) |
| 145 | + where TRequest : class |
| 146 | + where TResponse : class |
| 147 | + { |
| 148 | + _logger.LogInformation($"Starting call. Name: {method.Name}. Type: {method.Type}. Request: {typeof(TRequest)}. Response: {typeof(TResponse)}"); |
| 149 | + } |
| 150 | + |
| 151 | + private void AddCallerMetadata<TRequest, TResponse>(ref ClientInterceptorContext<TRequest, TResponse> context) |
| 152 | + where TRequest : class |
| 153 | + where TResponse : class |
| 154 | + { |
| 155 | + var headers = context.Options.Headers; |
| 156 | + |
| 157 | + // Call doesn't have a headers collection to add to. |
| 158 | + // Need to create a new context with headers for the call. |
| 159 | + if (headers == null) |
| 160 | + { |
| 161 | + headers = new Metadata(); |
| 162 | + var options = context.Options.WithHeaders(headers); |
| 163 | + context = new ClientInterceptorContext<TRequest, TResponse>(context.Method, context.Host, options); |
| 164 | + } |
| 165 | + |
| 166 | + // Add caller metadata to call headers |
| 167 | + headers.Add("caller-user", Environment.UserName); |
| 168 | + headers.Add("caller-machine", Environment.MachineName); |
| 169 | + headers.Add("caller-os", Environment.OSVersion.ToString()); |
| 170 | + } |
| 171 | + |
| 172 | + private void LogError(Exception ex) |
| 173 | + { |
| 174 | + _logger.LogError(ex, $"Call error: {ex.Message}"); |
| 175 | + } |
| 176 | +} |
| 177 | + |
0 commit comments