Skip to content

Commit

Permalink
POC - razor page instrumentation. Needs work in transaction / segment…
Browse files Browse the repository at this point in the history
… naming
  • Loading branch information
tippmar-nr committed Oct 27, 2023
1 parent f1036f2 commit 8a50d96
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static void Initialize(string installPathExtensionsDirectory)
{ "BuildCommonServicesWrapper6Plus", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNetCore6Plus.dll") },
{ "GenericHostWebHostBuilderExtensionsWrapper6Plus", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNetCore6Plus.dll") },
{ "InvokeActionMethodAsyncWrapper6Plus", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNetCore6Plus.dll") },
{ "PageActionInvokeHandlerAsyncWrapper6Plus", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNetCore6Plus.dll") },

{ "ResolveAppWrapper", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Owin.dll") },

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@ SPDX-License-Identifier: Apache-2.0
<exactMethodMatcher methodName="InvokeActionMethodAsync" />
</match>
</tracerFactory>
<tracerFactory name="PageActionInvokeHandlerAsyncWrapper6Plus">
<match assemblyName="Microsoft.AspNetCore.Mvc.RazorPages" className="Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker" minVersion="6.0.0.0">
<exactMethodMatcher methodName="InvokeHandlerMethodAsync" />
</match>
</tracerFactory>
</instrumentation>
</extension>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.RazorPages;
using NewRelic.Agent.Api;
using NewRelic.Agent.Api.Experimental;
using NewRelic.Agent.Extensions.Providers.Wrapper;
using NewRelic.Reflection;

namespace NewRelic.Providers.Wrapper.AspNetCore6Plus
{
public class PageActionInvokeHandlerAsyncWrapper6Plus : IWrapper
{
private static Func<object, PageContext> _getPageContext;

static PageActionInvokeHandlerAsyncWrapper6Plus()
{
_getPageContext = VisibilityBypasser.Instance.GenerateFieldReadAccessor<PageContext>("Microsoft.AspNetCore.Mvc.RazorPages",
"Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker", "_pageContext");
}

public bool IsTransactionRequired => true;

public CanWrapResponse CanWrap(InstrumentedMethodInfo methodInfo)
{
return new CanWrapResponse("PageActionInvokeHandlerAsyncWrapper6Plus".Equals(methodInfo.RequestedWrapperName));
}

public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
{
if (instrumentedMethodCall.IsAsync)
{
transaction.AttachToAsync();
}

var pageContext = _getPageContext(instrumentedMethodCall.MethodCall.InvocationTarget);

var actionDescriptor = pageContext.ActionDescriptor;

var transactionName = CreateTransactionName(actionDescriptor);

transaction.SetWebTransactionName(WebTransactionType.Action, transactionName, TransactionNamePriority.FrameworkHigh);

var actionDescriptorPageTypeInfo = actionDescriptor.PageTypeInfo;
var pageTypeName = actionDescriptorPageTypeInfo.Name;

var segment = transaction.StartMethodSegment(instrumentedMethodCall.MethodCall, pageTypeName, "uhh_what_goes_here");

var segmentApi = segment.GetExperimentalApi();
segmentApi.UserCodeNamespace = actionDescriptorPageTypeInfo.FullName;
segmentApi.UserCodeFunction = "uhh_what_goes_here";

return Delegates.GetAsyncDelegateFor<Task>(agent, segment, TaskContinueWithOption.None);
}

private static string CreateTransactionName(CompiledPageActionDescriptor actionDescriptor)
{
var transactionName = $"Pages{actionDescriptor.DisplayName}";

return transactionName;
}
}
}

0 comments on commit 8a50d96

Please sign in to comment.