|
| 1 | +// Copyright 2020 New Relic, Inc. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using NewRelic.Agent.Api; |
| 5 | +using NewRelic.Agent.Extensions.Providers.Wrapper; |
| 6 | + |
| 7 | +namespace NewRelic.Providers.Wrapper.AzureFunction; |
| 8 | + |
| 9 | +public class FunctionsHttpProxyingMiddlewareWrapper : IWrapper |
| 10 | +{ |
| 11 | + private const string WrapperName = "FunctionsHttpProxyingMiddlewareWrapper"; |
| 12 | + |
| 13 | + public bool IsTransactionRequired => false; |
| 14 | + |
| 15 | + public CanWrapResponse CanWrap(InstrumentedMethodInfo methodInfo) |
| 16 | + { |
| 17 | + return new CanWrapResponse(WrapperName.Equals(methodInfo.RequestedWrapperName)); |
| 18 | + } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Gets request method / path for Azure function HttpTrigger invocations |
| 22 | + /// in apps that use the Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore package |
| 23 | + /// </summary> |
| 24 | + public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction) |
| 25 | + { |
| 26 | + if (agent.Configuration.AzureFunctionModeEnabled) |
| 27 | + { |
| 28 | + dynamic httpContext; |
| 29 | + switch (instrumentedMethodCall.MethodCall.Method.MethodName) |
| 30 | + { |
| 31 | + case "AddHttpContextToFunctionContext": |
| 32 | + httpContext = instrumentedMethodCall.MethodCall.MethodArguments[1]; |
| 33 | + |
| 34 | + agent.CurrentTransaction.SetRequestMethod(httpContext.Request.Method); |
| 35 | + agent.CurrentTransaction.SetUri(httpContext.Request.Path); |
| 36 | + break; |
| 37 | + case "TryHandleHttpResult": |
| 38 | + if (!agent.CurrentTransaction.HasHttpResponseStatusCode) // these handlers seem to get called more than once; only set the status code one time |
| 39 | + { |
| 40 | + httpContext = instrumentedMethodCall.MethodCall.MethodArguments[2]; |
| 41 | + agent.CurrentTransaction.SetHttpResponseStatusCode(httpContext.Response.StatusCode); |
| 42 | + } |
| 43 | + break; |
| 44 | + case "TryHandleOutputBindingsHttpResult": |
| 45 | + if (!agent.CurrentTransaction.HasHttpResponseStatusCode) // these handlers seem to get called more than once; only set the status code one time |
| 46 | + { |
| 47 | + httpContext = instrumentedMethodCall.MethodCall.MethodArguments[1]; |
| 48 | + agent.CurrentTransaction.SetHttpResponseStatusCode(httpContext.Response.StatusCode); |
| 49 | + } |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return Delegates.NoOp; |
| 55 | + } |
| 56 | +} |
0 commit comments