|
| 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 System; |
| 20 | +using Grpc.Core; |
| 21 | +using Grpc.Core.Interceptors; |
| 22 | + |
| 23 | +namespace Client |
| 24 | +{ |
| 25 | + public class ClientLoggerInterceptor : Interceptor |
| 26 | + { |
| 27 | + public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>( |
| 28 | + TRequest request, |
| 29 | + ClientInterceptorContext<TRequest, TResponse> context, |
| 30 | + AsyncUnaryCallContinuation<TRequest, TResponse> continuation) |
| 31 | + { |
| 32 | + LogCall(context.Method); |
| 33 | + AddCallerMetadata(ref context); |
| 34 | + |
| 35 | + return continuation(request, context); |
| 36 | + } |
| 37 | + |
| 38 | + public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>( |
| 39 | + ClientInterceptorContext<TRequest, TResponse> context, |
| 40 | + AsyncClientStreamingCallContinuation<TRequest, TResponse> continuation) |
| 41 | + { |
| 42 | + LogCall(context.Method); |
| 43 | + AddCallerMetadata(ref context); |
| 44 | + |
| 45 | + return continuation(context); |
| 46 | + } |
| 47 | + |
| 48 | + public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>( |
| 49 | + TRequest request, |
| 50 | + ClientInterceptorContext<TRequest, TResponse> context, |
| 51 | + AsyncServerStreamingCallContinuation<TRequest, TResponse> continuation) |
| 52 | + { |
| 53 | + LogCall(context.Method); |
| 54 | + AddCallerMetadata(ref context); |
| 55 | + |
| 56 | + return continuation(request, context); |
| 57 | + } |
| 58 | + |
| 59 | + public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>( |
| 60 | + ClientInterceptorContext<TRequest, TResponse> context, |
| 61 | + AsyncDuplexStreamingCallContinuation<TRequest, TResponse> continuation) |
| 62 | + { |
| 63 | + LogCall(context.Method); |
| 64 | + AddCallerMetadata(ref context); |
| 65 | + |
| 66 | + return continuation(context); |
| 67 | + } |
| 68 | + |
| 69 | + private void LogCall<TRequest, TResponse>(Method<TRequest, TResponse> method) |
| 70 | + where TRequest : class |
| 71 | + where TResponse : class |
| 72 | + { |
| 73 | + var initialColor = Console.ForegroundColor; |
| 74 | + Console.ForegroundColor = ConsoleColor.Green; |
| 75 | + Console.WriteLine($"Starting call. Type: {method.Type}. Request: {typeof(TRequest)}. Response: {typeof(TResponse)}"); |
| 76 | + Console.ForegroundColor = initialColor; |
| 77 | + } |
| 78 | + |
| 79 | + private void AddCallerMetadata<TRequest, TResponse>(ref ClientInterceptorContext<TRequest, TResponse> context) |
| 80 | + where TRequest : class |
| 81 | + where TResponse : class |
| 82 | + { |
| 83 | + var headers = context.Options.Headers; |
| 84 | + |
| 85 | + // Call doesn't have a headers collection to add to. |
| 86 | + // Need to create a new context with headers for the call. |
| 87 | + if (headers == null) |
| 88 | + { |
| 89 | + headers = new Metadata(); |
| 90 | + var options = context.Options.WithHeaders(headers); |
| 91 | + context = new ClientInterceptorContext<TRequest, TResponse>(context.Method, context.Host, options); |
| 92 | + } |
| 93 | + |
| 94 | + // Add caller metadata to call headers |
| 95 | + headers.Add("caller-user", Environment.UserName); |
| 96 | + headers.Add("caller-machine", Environment.MachineName); |
| 97 | + headers.Add("caller-os", Environment.OSVersion.ToString()); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments