|
| 1 | +using System; |
| 2 | +using System.Text.Json; |
| 3 | +using System.Text.Json.Serialization; |
| 4 | +using System.Threading.Channels; |
| 5 | +using Microsoft.Azure.Functions.Worker.Diagnostics; |
| 6 | +using Microsoft.Azure.Functions.Worker.Grpc.Messages; |
| 7 | + |
| 8 | +namespace Microsoft.Azure.Functions.Worker.Grpc |
| 9 | +{ |
| 10 | + |
| 11 | + internal class GrpcWorkerDiagnostics : IWorkerDiagnostics |
| 12 | + { |
| 13 | + private ChannelWriter<StreamingMessage> _outputChannel; |
| 14 | + |
| 15 | + internal static readonly JsonSerializerOptions SerializerOptions; |
| 16 | + |
| 17 | + static GrpcWorkerDiagnostics() |
| 18 | + { |
| 19 | + SerializerOptions = new JsonSerializerOptions |
| 20 | + { |
| 21 | + WriteIndented = true, |
| 22 | + }; |
| 23 | + |
| 24 | + SerializerOptions.Converters.Add(new JsonStringEnumConverter()); |
| 25 | + SerializerOptions.Converters.Add(new TypeNameConverter()); |
| 26 | + } |
| 27 | + |
| 28 | + public GrpcWorkerDiagnostics(GrpcHostChannel hostChannel) |
| 29 | + { |
| 30 | + if (hostChannel == null) |
| 31 | + { |
| 32 | + throw new ArgumentNullException(nameof(hostChannel)); |
| 33 | + } |
| 34 | + |
| 35 | + if (hostChannel.Channel == null) |
| 36 | + { |
| 37 | + throw new InvalidOperationException($"{nameof(Channel)} cannot be null."); |
| 38 | + } |
| 39 | + |
| 40 | + _outputChannel = hostChannel.Channel.Writer ?? throw new InvalidOperationException($"Writer cannot be null."); |
| 41 | + } |
| 42 | + |
| 43 | + public void OnApplicationCreated(WorkerInformation workerInfo) |
| 44 | + { |
| 45 | + var message = new StreamingMessage |
| 46 | + { |
| 47 | + RpcLog = new RpcLog |
| 48 | + { |
| 49 | + EventId = nameof(OnApplicationCreated), |
| 50 | + Level = RpcLog.Types.Level.Debug, |
| 51 | + LogCategory = RpcLog.Types.RpcLogCategory.System, |
| 52 | + Message = JsonSerializer.Serialize(workerInfo, SerializerOptions) |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + _outputChannel.TryWrite(message); |
| 57 | + } |
| 58 | + |
| 59 | + public void OnFunctionLoaded(FunctionDefinition definition) |
| 60 | + { |
| 61 | + var message = new StreamingMessage |
| 62 | + { |
| 63 | + RpcLog = new RpcLog |
| 64 | + { |
| 65 | + EventId = nameof(OnFunctionLoaded), |
| 66 | + Level = RpcLog.Types.Level.Debug, |
| 67 | + LogCategory = RpcLog.Types.RpcLogCategory.System, |
| 68 | + Message = JsonSerializer.Serialize(definition, SerializerOptions) |
| 69 | + } |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + private class TypeNameConverter : JsonConverter<Type> |
| 74 | + { |
| 75 | + public override Type? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 76 | + { |
| 77 | + // We'll never deserialize. |
| 78 | + throw new NotSupportedException(); |
| 79 | + } |
| 80 | + |
| 81 | + public override void Write(Utf8JsonWriter writer, Type value, JsonSerializerOptions options) |
| 82 | + { |
| 83 | + writer.WriteStringValue(value.FullName); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments