Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove New Relic .NET Agent RUM code from a specific request

Tags:

.net

newrelic

We are using the .NET version of New Relic. It appears as though upon output, New Relic parses the output to see if it contains any </head> elements and inserts its <script type="text/javascript">var NREUMQ=... script before it.

My problem is that on a particular closed source page there is a line of js that reads...

document.write("<html><head></head><body></body></html>");

...New Relic then turns this into...

document.write("<html><head><script type="text/javascript">var NREUMQ=NREUMQ||[];NREUMQ.push(["mark","firstbyte",new Date().getTime()]);</script></head><body></body></html>");

...which causes major headaches as the New Relic code is not escaped.

What I would like to do is somehow tell New Relic to not inject the code on this particular page as I cannot edit the page.

Cheers!

Here is the modified code in pretty format - for your viewing pleasure (not what is actually in the response)....

document.write("<html><head><script type="text/javascript">
                var NREUMQ=NREUMQ||[];
                NREUMQ.push(["mark","firstbyte",new Date().getTime()]);
                </script></head><body></body></html>");
like image 546
Michael Coxon Avatar asked Oct 03 '22 22:10

Michael Coxon


2 Answers

EDIT 2017-01-25
(new urls)

  • https://docs.newrelic.com/docs/agents/net-agent/net-agent-api/disablebrowsermonitoring-net-agent
  • https://docs.newrelic.com/docs/agents/net-agent/net-agent-api/getbrowsertimingheader-net-agent

ORIGINAL POST
I have received an answer from New Relic support. Here it is...

Hello Michael,

This is a fantastic question! There is a very direct way of accomplishing what you want to do.

The .Net Agent injects snippets of javascript into the header and footer of pages sent to the users browser to measure the page load time on the user's end. We call this feature Real User Monitoring or RUM for short. More info about that is here: https://newrelic.com/docs/features/how-does-real-user-monitoring-work

If you wish to completely disable RUM on all pages you can edit newrelic.config and change this line:

<browserMonitoring autoInstrument="true"/>

to this:

<browserMonitoring autoInstrument="false"/>

You can disable RUM on individual pages and/or manually specify where the RUM header/footer snippets are injected into the page by using the .Net Agent API calls. To use this library, add a reference to NewRelic.Api.Agent.dll to your project. The DLL should be found in this folder: "%ProgramFiles%\New Relic\.NET Agent"

Then you can disable the RUM on specific pages using this API call:

  • https://newrelic.com/docs/dotnet/the-net-agent-api#disable_browser

Or you can manually inject the header and foot where you would like them to be using these API calls:

  • https://newrelic.com/docs/dotnet/the-net-agent-api#timing_header
  • https://newrelic.com/docs/dotnet/the-net-agent-api#timing_footer
like image 80
Michael Coxon Avatar answered Oct 16 '22 22:10

Michael Coxon


I ran into the same problem today. I was able to fix it by having the Javascript combine "he" and "ad" on the client. This prevents the New Relic RUM from seeing a HEAD tag.

document.write("<html><he" + "ad></he" + "ad><body></body></html>");

like image 1
gerrard00 Avatar answered Oct 16 '22 23:10

gerrard00