Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable Application Insights server telemetry on WebApi project that uses OWIN?

We are having a bunch of problems (read long response times) with a couple of projects in production and wanted to see exactly what was happening on the server. I then proceeded to add Application Insights to all of our projects by following this article. The problem is that both of our WebAPI projects are not sending server data to the Azure portal, while all other projects (MVC 5) are.

This is what is shown when I access the corresponding Application Insights blade on Azure:

enter image description here

I tried to disable and re-enable data collection in the Application Insights Status Monitor in our Azure VMs, restarted IIS a few times all while making requests to the API, to no avail. When I enable it on a MVC project, I can see the data almost instantly on the Azure portal when I open pages on the site.

When I saw that data was not being sent from our Azure VMs for these specific projects, I tried to setup the same collections in our dev environment, which is hosted in our own infrastructure, and the exact same situation repeated itself, ruling out the possibility that this is related to projects being hosted in Azure VMs.

I'm not exactly sure what is preventing these projects from sending data to Azure, but by taking a look at the working projects vs the non working ones, I think it might be somehow related to the fact that our WebAPI projects use the new OWIN pipeline while the MVC ones are standard MVC projects. I checked both the web.config file and the bin folder for both project types and they seem to be modified correctly by the Insights Monitor (I can see the same new dlls added to the bin folder and the same http module added to the web.config).

With that in mind, how do I enable server side telemetry using Application Insights for WebAPI projects that rely on the OWIN/Katana pipeline? What could I do to find out what exactly is causing the project to not send data to Azure in this case?

like image 627
julealgon Avatar asked Apr 06 '15 12:04

julealgon


People also ask

How do I enable Insights for web app?

Select File | New Project. Select Visual C# | . NET Core | ASP.NET Core Web Application, ensure to mark the checkbox Add Application Insights to Project. Click Ok on the next screen.

How do I add app Insights to my project?

Add Application Insights automatically From within your ASP.NET web app project in Visual Studio: Select Project > Add Application Insights Telemetry > Application Insights Sdk (local) > Next > Finish > Close.

How do I enable application Insights in Azure?

In your function app, select Configuration under Settings, and then select Application settings. If you see a setting named APPINSIGHTS_INSTRUMENTATIONKEY , Application Insights integration is enabled for your function app running in Azure.


2 Answers

This is an old question but it was still in the top 3 results on searches for "web api application insights owin". After lots of searching and not a lot of answers that didn't require us to write our own middleware or explictly instrumenting everything. We came across an extension package that made things super simple:

Here's the Github Repository for it and the associated NuGet Package

For those too lazy to look at the links, all that was needed to be added was:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseApplicationInsights();

        // rest of the config here...
    }
}

and add this to your ApplicationInsights.Config

<TelemetryInitializers>
    <!-- other initializers.. -->
    <Add Type="ApplicationInsights.OwinExtensions.OperationIdTelemetryInitializer, ApplicationInsights.OwinExtensions"/>
</TelemetryInitializers>
like image 65
Norrec Avatar answered Oct 13 '22 02:10

Norrec


AI uses httpmodule to collect information on begin request and send it on end request. As described here Owin/Katana uses middelwares to execute logic on a different stages. As most of AI auto collection logic is internal you cannot reuse it in your middleware. But you can instrument your code yourself. Create TelemetryClient from your code and start sending Request, Traces and Exceptions (like described here)

like image 25
Anastasia Black Avatar answered Oct 13 '22 01:10

Anastasia Black