<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2020 New Relic Corporation. All rights reserved.
SPDX-License-Identifier: Apache-2.0
-->

<extension xmlns="urn:newrelic-extension">
  <instrumentation>

    <tracerFactory name="AzureFunctionIsolatedInvokeAsyncWrapper">
      <match assemblyName="Microsoft.Azure.Functions.Worker.Core" className="Microsoft.Azure.Functions.Worker.FunctionsApplication">
        <exactMethodMatcher methodName="InvokeFunctionAsync" />
      </match>
    </tracerFactory>

    <tracerFactory name="AzureFunctionInProcessInvokeAsyncWrapper">
      <match assemblyName="Microsoft.Azure.WebJobs.Host" className="Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2">
        <exactMethodMatcher methodName="InvokeAsync" parameters="System.Object,System.Object[]" />
      </match>
    </tracerFactory>

    <tracerFactory name="AzureFunctionInProcessExecuteWithWatchersAsyncWrapper">
      <match assemblyName="Microsoft.Azure.WebJobs.Host" className="Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor">
        <exactMethodMatcher methodName="ExecuteWithWatchersAsync" parameters="Microsoft.Azure.WebJobs.Host.Executors.IFunctionInstanceEx,ParameterHelper,Microsoft.Extensions.Logging.ILogger,System.Threading.CancellationTokenSource" />
      </match>
    </tracerFactory>

    <!-- For Isolated functions, use a NoOpWrapper on HostBuilder.Build() to load the agent early in the startup cycle -->
    <tracerFactory name="NewRelic.Agent.Core.Wrapper.NoOpWrapper">
      <match assemblyName="Microsoft.Extensions.Hosting" className="Microsoft.Extensions.Hosting.HostBuilder">
        <exactMethodMatcher methodName="Build" />
      </match>
    </tracerFactory>
    
    <!-- TODO: can we early-load the agent for in-proc functions? -->

    <tracerFactory name="FunctionsHttpProxyingMiddlewareWrapper">
      <match assemblyName="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" className="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.FunctionsHttpProxyingMiddleware">
        <exactMethodMatcher methodName="AddHttpContextToFunctionContext" />
      </match>
      <match assemblyName="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" className="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.FunctionsHttpProxyingMiddleware">
        <exactMethodMatcher methodName="TryHandleHttpResult" />
      </match>
      <match assemblyName="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" className="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.FunctionsHttpProxyingMiddleware">
        <exactMethodMatcher methodName="TryHandleOutputBindingsHttpResult" />
      </match>
    </tracerFactory>
  </instrumentation>
</extension>
