Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR in MVC skewing Application Insights

We just started using SignalR in an MVC application and now we're getting a bunch of alerts due to high average response time. I suspect this to be misleading as the application isn't experiencing performance degradation. It appears that SignalR uses this URL to make a connection. This url not a controller/action of the project and just the built in SignalR code in the js file. jquery.signalR-2.2.1.js is the file. I suspect that it is just leaving the websocket connection open while they are on this page and it's skewing our numbers. Is this accurate? If so, is there a way to filter it out of the application insights?

Here is the counter. Is this the expected behavior?

enter image description here

Here is the signalR jquery code where it builds it's url:

// BUG #2953: The url needs to be same otherwise it will cause a memory leak
    getUrl: function (connection, transport, reconnecting, poll, ajaxPost) {
        /// <summary>Gets the url for making a GET based connect request</summary>
        var baseUrl = transport === "webSockets" ? "" : connection.baseUrl,
            url = baseUrl + connection.appRelativeUrl,
            qs = "transport=" + transport;

        if (!ajaxPost && connection.groupsToken) {
            qs += "&groupsToken=" + window.encodeURIComponent(connection.groupsToken);
        }

        if (!reconnecting) {
            url += "/connect";
        } else {
            if (poll) {
                // longPolling transport specific
                url += "/poll";
            } else {
                url += "/reconnect";
            }

            if (!ajaxPost && connection.messageId) {
                qs += "&messageId=" + window.encodeURIComponent(connection.messageId);
            }
        }
        url += "?" + qs;
        url = transportLogic.prepareQueryString(connection, url);

        if (!ajaxPost) {
            url += "&tid=" + Math.floor(Math.random() * 11);
        }

        return url;
    },
like image 836
Papa Burgundy Avatar asked Jan 04 '17 15:01

Papa Burgundy


2 Answers

I fixed this by following the instructions on https://docs.microsoft.com/en-us/azure/application-insights/app-insights-api-filtering-sampling:

  1. Update your ApplicationInsights Nuget package to 2.0.0 or later.
  2. Create a class implementing ITelemetryProcessor:
    public class UnwantedTelemetryFilter : ITelemetryProcessor
    {
        private ITelemetryProcessor Next { get; set; }

        public UnwantedTelemetryFilter(ITelemetryProcessor next)
        {
            this.Next = next;
        }

        public void Process(ITelemetry item)
        {
            var request = item as RequestTelemetry;

            if (request != null && request.Name != null)
                if (request.Name.Contains("signalr"))
                    return;

            // Send everything else:
            this.Next.Process(item);
        }
    }
  1. Add the processor to your Application_Start() in Global.asax.cs:
    var builder = TelemetryConfiguration.Active.TelemetryProcessorChainBuilder;
    builder.Use((next) => new UnwantedTelemetryFilter(next));
    builder.Build();
like image 83
Phoexo Avatar answered Sep 20 '22 09:09

Phoexo


if the calls are coming from the C# part of the app, the easiest way is to write a custom telemetry processor: https://docs.microsoft.com/en-us/azure/application-insights/app-insights-api-filtering-sampling

public void Process(ITelemetry item)
{
    var request = item as RequestTelemetry;

    if (request != null && request.[some field here].Equals("[some signalr specific check here]", StringComparison.OrdinalIgnoreCase))
    {
        // To filter out an item, just terminate the chain:
        return;
    }
    // Send everything else:
    this.Next.Process(item);
}

and use that to explicitly filter out the signalr calls from being sent

or if the calls are coming from JS, then the telemetry initializer there does a similar thing to filter out telemetry if you return false in the initializer.

like image 33
John Gardner Avatar answered Sep 20 '22 09:09

John Gardner