| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Linq; |
| | 6 | | using System.Reflection; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using Azure.Core.Tests; |
| | 9 | | using Castle.DynamicProxy; |
| | 10 | |
|
| | 11 | | namespace Azure.Core.TestFramework |
| | 12 | | { |
| | 13 | | public class DiagnosticScopeValidatingInterceptor : IInterceptor |
| | 14 | | { |
| | 15 | | public void Intercept(IInvocation invocation) |
| | 16 | | { |
| 162866 | 17 | | var methodName = invocation.Method.Name; |
| 162866 | 18 | | if (methodName.EndsWith("Async")) |
| | 19 | | { |
| 74886 | 20 | | Type declaringType = invocation.Method.DeclaringType; |
| 74886 | 21 | | var ns = declaringType.Namespace; |
| 74886 | 22 | | var expectedName = declaringType.Name + "." + methodName.Substring(0, methodName.Length - 5); |
| 182818058 | 23 | | using ClientDiagnosticListener diagnosticListener = new ClientDiagnosticListener(s => s.StartsWith("Azur |
| 74886 | 24 | | invocation.Proceed(); |
| | 25 | |
|
| 74870 | 26 | | bool expectFailure = false; |
| 74870 | 27 | | bool skipChecks = false; |
| | 28 | |
|
| 150368 | 29 | | bool strict = !invocation.Method.GetCustomAttributes(true).Any(a => a.GetType().FullName == "Azure.Core. |
| 74870 | 30 | | if (invocation.Method.ReturnType.Name.Contains("Pageable") || |
| 74870 | 31 | | invocation.Method.ReturnType.Name.Contains("IAsyncEnumerable")) |
| | 32 | | { |
| 3252 | 33 | | return; |
| | 34 | | } |
| | 35 | |
|
| | 36 | | try |
| | 37 | | { |
| 71618 | 38 | | object returnValue = invocation.ReturnValue; |
| 71618 | 39 | | if (returnValue is Task t) |
| | 40 | | { |
| 71242 | 41 | | t.GetAwaiter().GetResult(); |
| | 42 | | } |
| | 43 | | else |
| | 44 | | { |
| | 45 | | // Await ValueTask |
| 376 | 46 | | Type returnType = returnValue.GetType(); |
| 376 | 47 | | MethodInfo getAwaiterMethod = returnType.GetMethod("GetAwaiter", BindingFlags.Instance | Binding |
| 376 | 48 | | MethodInfo getResultMethod = getAwaiterMethod.ReturnType.GetMethod("GetResult", BindingFlags.Ins |
| | 49 | |
|
| 376 | 50 | | getResultMethod.Invoke( |
| 376 | 51 | | getAwaiterMethod.Invoke(returnValue, Array.Empty<object>()), |
| 376 | 52 | | Array.Empty<object>()); |
| | 53 | | } |
| 68015 | 54 | | } |
| | 55 | | catch (Exception ex) |
| | 56 | | { |
| 3603 | 57 | | expectFailure = true; |
| | 58 | |
|
| 3603 | 59 | | if (ex is ArgumentException) |
| | 60 | | { |
| | 61 | | // Don't expect scope for argument validation failures |
| 580 | 62 | | skipChecks = true; |
| | 63 | | } |
| 3603 | 64 | | } |
| | 65 | | finally |
| | 66 | | { |
| | 67 | | // Remove subscribers before enumerating events. |
| 71618 | 68 | | diagnosticListener.Dispose(); |
| 71618 | 69 | | if (!skipChecks) |
| | 70 | | { |
| 71038 | 71 | | if (strict) |
| | 72 | | { |
| 141224 | 73 | | ClientDiagnosticListener.ProducedDiagnosticScope e = diagnosticListener.Scopes.FirstOrDefaul |
| | 74 | |
|
| 70612 | 75 | | if (e == default) |
| | 76 | | { |
| 4 | 77 | | throw new InvalidOperationException($"Expected diagnostic scope not created {expectedNam |
| | 78 | | } |
| | 79 | |
|
| 166357 | 80 | | if (!e.Activity.Tags.Any(tag => tag.Key == "az.namespace")) |
| | 81 | | { |
| 0 | 82 | | throw new InvalidOperationException($"All diagnostic scopes should have 'az.namespace' a |
| | 83 | | } |
| | 84 | |
|
| 70608 | 85 | | if (expectFailure && !e.IsFailed) |
| | 86 | | { |
| 8 | 87 | | throw new InvalidOperationException($"Expected scope {expectedName} to be marked as fail |
| | 88 | | } |
| | 89 | | } |
| | 90 | | else |
| | 91 | | { |
| 426 | 92 | | if (!diagnosticListener.Scopes.Any()) |
| | 93 | | { |
| 4 | 94 | | throw new InvalidOperationException($"Expected some diagnostic scopes to be created, fou |
| | 95 | | } |
| | 96 | | } |
| | 97 | | } |
| 71602 | 98 | | } |
| | 99 | | } |
| | 100 | | else |
| | 101 | | { |
| 87980 | 102 | | invocation.Proceed(); |
| | 103 | | } |
| 162825 | 104 | | } |
| | 105 | | } |
| | 106 | | } |