|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using System.Runtime.CompilerServices; |
| 9 | +using Azure; |
| 10 | + |
| 11 | +namespace Microsoft.Azure.WebJobs.Script.Diagnostics.HealthChecks |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// A helper for providing data with a health check result. |
| 15 | + /// </summary> |
| 16 | + internal partial class HealthCheckData |
| 17 | + { |
| 18 | + // exposed to the HealthCheckResult through IReadOnlyDictionary. |
| 19 | + private readonly Dictionary<string, object> _data = []; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Gets or sets the area of the health check data failure. |
| 23 | + /// </summary> |
| 24 | + /// <remarks> |
| 25 | + /// This is the area that has failed. Such as "configuration", "connectivity", etc. |
| 26 | + /// </remarks> |
| 27 | + public string Area |
| 28 | + { |
| 29 | + get => GetOrDefault<string>(); |
| 30 | + set => Set(value); |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Gets or sets the configuration section related to the health check data. |
| 35 | + /// </summary> |
| 36 | + /// <remarks> |
| 37 | + /// Useful for when the component being checked is related to a specific configuration section. |
| 38 | + /// </remarks> |
| 39 | + public string ConfigurationSection |
| 40 | + { |
| 41 | + get => GetOrDefault<string>(); |
| 42 | + set => Set(value); |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Gets or sets the status code related to the health check data. |
| 47 | + /// For HTTP related related checks, this is the HTTP status code. |
| 48 | + /// </summary> |
| 49 | + public int StatusCode |
| 50 | + { |
| 51 | + get => GetOrDefault<int>(); |
| 52 | + set => Set(value); |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Gets or sets the error code related to the health check data. |
| 57 | + /// </summary> |
| 58 | + /// <remarks> |
| 59 | + /// For Azure SDK related checks, this is typically the RequestFailedException.ErrorCode value. |
| 60 | + /// </remarks> |
| 61 | + public string ErrorCode |
| 62 | + { |
| 63 | + get => GetOrDefault<string>(); |
| 64 | + set => Set(value); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Sets exception details into the health check data. |
| 69 | + /// </summary> |
| 70 | + /// <param name="ex">The exception to set details from.</param> |
| 71 | + /// <remarks> |
| 72 | + /// This will set various properties based on the type of exception. |
| 73 | + /// </remarks> |
| 74 | + public void SetExceptionDetails(Exception ex) |
| 75 | + { |
| 76 | + ArgumentNullException.ThrowIfNull(ex); |
| 77 | + if (ex is AggregateException aggregate) |
| 78 | + { |
| 79 | + // Azure SDK will retry a few times in some cases, leading to multiple inner exceptions. |
| 80 | + // We only care about the last one. |
| 81 | + ex = aggregate.InnerExceptions.Last(); |
| 82 | + } |
| 83 | + |
| 84 | + if (ex is TimeoutException) |
| 85 | + { |
| 86 | + ErrorCode = "Timeout"; |
| 87 | + } |
| 88 | + else if (ex is OperationCanceledException) |
| 89 | + { |
| 90 | + ErrorCode = "OperationCanceled"; |
| 91 | + } |
| 92 | + else if (ex is RequestFailedException rfe) |
| 93 | + { |
| 94 | + StatusCode = rfe.Status; |
| 95 | + ErrorCode = rfe.ErrorCode; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private void Set<T>(T value, [CallerMemberName] string key = null) |
| 100 | + { |
| 101 | + _data[key] = value; |
| 102 | + } |
| 103 | + |
| 104 | + private T GetOrDefault<T>([CallerMemberName] string key = null, T defaultValue = default) |
| 105 | + { |
| 106 | + if (_data.TryGetValue(key, out var value) && value is T typedValue) |
| 107 | + { |
| 108 | + return typedValue; |
| 109 | + } |
| 110 | + |
| 111 | + return defaultValue; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // Partial class down here to separate IReadOnlyDictionary implementation details. |
| 116 | + internal partial class HealthCheckData : IReadOnlyDictionary<string, object> |
| 117 | + { |
| 118 | + IEnumerable<string> IReadOnlyDictionary<string, object>.Keys |
| 119 | + => _data.Keys; |
| 120 | + |
| 121 | + IEnumerable<object> IReadOnlyDictionary<string, object>.Values |
| 122 | + => _data.Values; |
| 123 | + |
| 124 | + int IReadOnlyCollection<KeyValuePair<string, object>>.Count |
| 125 | + => _data.Count; |
| 126 | + |
| 127 | + object IReadOnlyDictionary<string, object>.this[string key] |
| 128 | + => _data[key]; |
| 129 | + |
| 130 | + bool IReadOnlyDictionary<string, object>.ContainsKey(string key) |
| 131 | + => _data.ContainsKey(key); |
| 132 | + |
| 133 | + IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator() |
| 134 | + => _data.GetEnumerator(); |
| 135 | + |
| 136 | + IEnumerator IEnumerable.GetEnumerator() |
| 137 | + => _data.GetEnumerator(); |
| 138 | + |
| 139 | + bool IReadOnlyDictionary<string, object>.TryGetValue(string key, out object value) |
| 140 | + => _data.TryGetValue(key, out value); |
| 141 | + } |
| 142 | +} |
0 commit comments