• /
  • EnglishEspañol日本語한국어Português
  • Inicia sesiónComenzar ahora

Add detail to transactions via XML (.NET)

New Relic collects and reports information on web browser transactions and non-web transactions (background tasks). Normally the .NET agent produces complete information automatically, without any need for you to modify your application code. However, if New Relic does not support your framework, you may need to add custom instrumentation.

New Relic uses method tracers to implement custom instrumentation. A method tracer is a software probe that you can put on a method of any class. The .NET agent loads the instrumentation directives that define which methods should be traced from all XML files in the extensions directory.

This document describes instrumenting work when the agent is already collecting transactions, but you want to add more detail to those transactions. To instrument work when the agent isn't creating transactions automatically, see Create transactions via XML. You can also add detail and create transactions by decorating your code with attributes.

Add detail with custom instrumentation

Extension files define a number of tracer factories in an instrumentation element. Each tracer factory contains match elements that define the assembly, fully qualified class name, and method name to match. To define a new custom instrumentation XML file:

  1. Create a new .xml file in the extensions directory used by the .NET agent to read every XML file and define its instrumentation set. For the .NET Framework agent, use the following location:

    C:\ProgramData\New Relic\.NET Agent\Extensions

    Importante

    Do not put the .xml file in the C:\Program Files\New Relic\.NET agent\Extensions directory. The file must be in the ProgramData file location for custom instrumentation to work.

    Do not modify the distributed xml files. These files are overwritten whenever the agent is upgraded.

  2. Copy this template into the file you created use the sample as an example. This instruments two methods but you can add methods to your instrumentation file.

    Sugerencia

    The values AssemblyName, NameSpace.ClassName, and MethodName are case sensitive.

    <?xml version="1.0" encoding="utf-8"?>
    <extension xmlns="urn:newrelic-extension">
    <instrumentation>
    <!-- These methods appear in the transactions breakdown table and in transaction traces -->
    <tracerFactory metricName="Category/Name">
    <match assemblyName="AssemblyName" className="NameSpace.ClassName">
    <exactMethodMatcher methodName="MethodName" />
    </match>
    </tracerFactory>
    <tracerFactory metricName="Category/Name2">
    <match assemblyName="AssemblyName" className="NameSpace.ClassName2">
    <exactMethodMatcher methodName="MethodName2" />
    </match>
    </tracerFactory>
    </instrumentation>
    </extension>
  3. Validate the .xml instrumentation file against extension.xsd.

  4. Non-IIS apps (such as a console app or background process): You must also create transactions via XML to contain the methods you instrument. Custom methods instrumented outside of a transaction will not be reported to New Relic.

  5. Importante

    If your .NET agent version is 7.0 or higher, this is the end of the procedure.

    If your .NET agent version is lower than 7.0, continue to the next steps.

  6. If your app is IIS-hosted, restart IIS.

  7. For non-IIS applications, restart your application's host process or the application itself.

Importante

If some of your methods still don't show up in traces after adding XML custom instrumentation, disable method inlining for those methods with [MethodImpl(MethodImplOptions.NoInlining)].

Ignore a transaction

You can stop a transaction from being reported by using a custom instrumentation file. Whenever an ignored method is called, the .NET agent ignores the entire parent transaction. This is the same as calling IgnoreTransaction().

To ignore a transaction:

  1. Add custom instrumentation.

  2. Add a tracerFactory whose name is NewRelic.Agent.Core.Tracer.Factories.IgnoreTransactionTracerFactory:

    <tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.IgnoreTransactionTracerFactory">
    <match assemblyName="System.Web.Extensions" className="System.Web.Handlers.ScriptResourceHandler">
    <exactMethodMatcher methodName="Throw404" />
    </match>
    </tracerFactory>

Track async work in a separate transaction

In some cases, asynchronous work can be tracked as a separate transaction by applying the AsyncForceNewTransactionWrapper instrumentation:

<tracerFactory name="AsyncForceNewTransactionWrapper">
<match assemblyName="AssemblyName" className="Namespace.ClassName">
<exactMethodMatcher methodName="MethodName" />
</match>
</tracerFactory>

Async usage considerations

Comments

Instrumented method

The instrumented method must be invoked using Task.Run, Task.Factory.StartNew, or new Thread(). It cannot be invoked using the await keyword.

Return type

The instrumented method is not required to be async. However, if it is async, it must have a return type of Task or Task<T>. It cannot have a void return type.

Attribute instrumentation

The instrumented method cannot have attribute instrumentation applied to it. It cannot be decorated with the [Transaction] or [Trace] attributes.

Example MyInstrumentation.xml

This example instruments two methods and ignores another method:

  • Instrument: CustomInstrumentDemo.Controllers.FirstController.FirstExample()
  • Instrument: CustomInstrumentDemo.Controllers.SecondController.SecondExample()
  • Ignore: CustomInstrumentDemo.Controllers.SecondController.ThirdExample()
  • Instrument: CustomInstrumentDemo.Controllers.SecondController.FourthExample(int id, string name)

To implement this custom instrumentation scheme, use the following example file and instrumented methods:

Name metrics

Metrics created from tracers will be named using the class name and method name of the matched method. You can override this name with the metricName attribute. Begin metricName with Custom/ (for example, metricName="Custom/OrderSubmissions").

<!-- instruments MyCompany.Order.Submit() and creates a metric named Custom/OrderSubmissions -->
<tracerFactory metricName="Custom/OrderSubmissions">
<match assemblyName="MyCompany" className="MyCompany.Order">
<exactMethodMatcher methodName="Submit" />
</match>
</tracerFactory>

Name transactions

The agent names transactions using the tracer in the transaction with the highest naming priority.

  • Web transactions may be named using the HTTP handler, ASP name, MVC controller name, or web service name, depending on the tracers invoked in the transaction's execution.
  • Background transactions with no tracer that explicitly names the transaction are rolled up into a single transaction name.

Use the transactionNamingPriority attribute to tell the agent to give a tracer transaction naming priority. Valid values are 1 to 7, where 7 takes precedence over 1 to 6. Also, the metricName attribute must begin with Custom/ (for example, metricName="Custom/instance").

<!-- instructs the agent to create a metric for MyControllerBase.Execute and to name the transaction using this tracer's metric name -->
<tracerFactory metricName="Custom/instance" transactionNamingPriority="7">
<match assemblyName="MyCompany" className="MyCompany.MyControllerBase">
<exactMethodMatcher methodName="Execute" />
</match>
</tracerFactory>

XSD validation

The XML instrumentation file can be checked against the XSD file (located at C:\ProgramData\New Relic\.NET Agent\Extensions\extension.xsd) with any XSD validator.

Troubleshooting

The .NET agent will write a log message to NewRelic.Profiler.####.log (where #### is the PID of the instrumented process) as it rewrites methods. This helps to verify that custom instrumentation is being read and that the proper methods are being instrumented.

Sugerencia

Avoid using the name attribute of the tracerFactory element in custom instrumentation. For more information, review the extension.xsd schema file in the agent directory.

Copyright © 2024 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.