Fluent Bytes

"The only source of knowledge is experience" - Albert Einstein-

How to Fix Application insights Live Metrics stream on Azure Mobile Apps

In preparation for Tech Days 2016, I was working with my colleagues on the Techdays app and for this we also have a backend system. This system is running for some time now and we want to have better insights into the usage of the website and the mobile app.

One of the latest additions to application insights is the capability of Live Metrics, where you can see the current traffic coming into your website or API.

For this you can simply add a NuGet package, which is currently in pre-release, but works great.

Once you install the package and run the website, you will see the Live Stream tile light up in the Azure portal and when you click it you will see a couple of live graphs showing actual traffic on the site or API

Of course we wanted to host our backend very simply on Azure Web apps and for this specific purpose on Azure Mobile Services, Since that is what we use as the mobile backend of the application. But apparently there are some subtle differences that prevent AppInsights to work properly if it comes to Live Metrics.

When running the web API on a local machine with app insights enabled, we could see a nice set of graphs as shown here below:

clip_image002

But when I deploy this to the Azure Mobile services, this results in a page that only shows the dependency calls, bet never any incoming requests and also no request duration. You only see a first request and after this the incoming requests are a flat liner.

clip_image001

After some inquiries at the app insights team, I got the information that since Azure Mobile Services is using OWIN, because the app insights HTTP modules are not loaded since that is not allowed and finally because performance counters are not exposed to the web application, these metrics will not show. After going back and forth on this, I got information that it would be possible to at least get the info for the requests on the screen, by adding a small piece of middleware, that can track the request instead of the standard HttpModule that is normally instantiated, but not allowed in a Mobile app.

So the only thing I needed to do is create a middleware component like the following:

And an extension method to register the middleware in the request pipeline at startup in the Startup class:

In the application startup class, ensure that you register this handler as the first as follows:

And that is it!

When you now deploy tot the azure mobile app, you will see the live metrics as we expect. We still don’t have the memory consumption and CPU usage stats, but this now at least shows all incoming requests in real time

Hope this helps!

Marcel

CTO at Xpirit, Microsoft Regional Director, Visual studio ALM MVP, Speaker, Pluralsight Author and IT Architect Consultant

3 Comments

  1. Thank you Marcelo for your excellent post!

    I have the same problem, but using a Web API in MVC 5, and implementing the request handler solved my problem.

    Good work!

  2. Marcel, great article!

    Was this an Azure mobile App or an Azure Mobile Service? I recently migrated my Azure mobile service using the migrate button on the portable. I am able to see my custom event tracking but no live stream. Is this related to having been an AMS originally or does AI just not support live streaming for Azure mobile “anything”?

    -Bob

  3. Integrating the owin mobile app inside asp.net core should be possible and by simply adding the appinsights configuration in the root of your app.

Leave a Reply

Your email address will not be published.

*

© 2017 Fluent Bytes

Theme by Anders NorenUp ↑