Skip to content

Commit

Permalink
Add Application Insights
Browse files Browse the repository at this point in the history
  • Loading branch information
Alina Popa committed May 3, 2016
1 parent 925de6d commit 77981b7
Show file tree
Hide file tree
Showing 10 changed files with 2,805 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Microsoft.Azure.Mobile.Server.Config;
using MyDrivingService.Models;
using Owin;
using Microsoft.ApplicationInsights;
using System.Web.Http.ExceptionHandling;

namespace MyDrivingService
{
Expand All @@ -17,6 +19,7 @@ public partial class Startup
public static void ConfigureMobileApp(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
config.Services.Add(typeof(IExceptionLogger), new AiExceptionLogger());

//For more information on Web API tracing, see http://go.microsoft.com/fwlink/?LinkId=620686
config.EnableSystemDiagnosticsTracing();
Expand Down Expand Up @@ -48,6 +51,20 @@ public static void ConfigureMobileApp(IAppBuilder app)
}
}

public class AiExceptionLogger : ExceptionLogger
{
public override void Log(ExceptionLoggerContext context)
{
if (context != null && context.Exception != null)
{
// Note: A single instance of telemetry client is sufficient to track multiple telemetry items.
var ai = new TelemetryClient();
ai.TrackException(context.Exception);
}
base.Log(context);
}
}

/* public class MyDrivingInitializer : DropCreateDatabaseAlways<MyDrivingContext>
{
public override void InitializeDatabase(MyDrivingContext context)
Expand Down
61 changes: 61 additions & 0 deletions src/MobileAppService/MyDrivingService/ApplicationInsights.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
<TelemetryModules>
<Add Type="Microsoft.ApplicationInsights.DependencyCollector.DependencyTrackingTelemetryModule, Microsoft.AI.DependencyCollector" />
<Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.PerformanceCollectorModule, Microsoft.AI.PerfCounterCollector">
<!--
Use the following syntax here to collect additional performance counters:
<Counters>
<Add PerformanceCounter="\Process(??APP_WIN32_PROC??)\Handle Count" ReportAs="Process handle count" />
...
</Counters>
PerformanceCounter must be either \CategoryName(InstanceName)\CounterName or \CategoryName\CounterName
Counter names may only contain letters, round brackets, forward slashes, hyphens, underscores, spaces and dots.
You may provide an optional ReportAs attribute which will be used as the metric name when reporting counter data.
For the purposes of reporting, metric names will be sanitized by removing all invalid characters from the resulting metric name.
NOTE: performance counters configuration will be lost upon NuGet upgrade.
The following placeholders are supported as InstanceName:
??APP_WIN32_PROC?? - instance name of the application process for Win32 counters.
??APP_W3SVC_PROC?? - instance name of the application IIS worker process for IIS/ASP.NET counters.
??APP_CLR_PROC?? - instance name of the application CLR process for .NET counters.
-->
</Add>
<Add Type="Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing.DiagnosticsTelemetryModule, Microsoft.ApplicationInsights" />
<Add Type="Microsoft.ApplicationInsights.WindowsServer.DeveloperModeWithDebuggerAttachedTelemetryModule, Microsoft.AI.WindowsServer" />
<Add Type="Microsoft.ApplicationInsights.Web.RequestTrackingTelemetryModule, Microsoft.AI.Web" />
<Add Type="Microsoft.ApplicationInsights.Web.ExceptionTrackingTelemetryModule, Microsoft.AI.Web" />
</TelemetryModules>
<TelemetryChannel Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel, Microsoft.AI.ServerTelemetryChannel" />
<!--
Learn more about Application Insights configuration with ApplicationInsights.config here:
http://go.microsoft.com/fwlink/?LinkID=513840
Note: If not present, please add <InstrumentationKey>Your Key</InstrumentationKey> to the top of this file.
-->
<TelemetryInitializers>
<Add Type="Microsoft.ApplicationInsights.WindowsServer.AzureRoleEnvironmentTelemetryInitializer, Microsoft.AI.WindowsServer" />
<Add Type="Microsoft.ApplicationInsights.WindowsServer.DomainNameRoleInstanceTelemetryInitializer, Microsoft.AI.WindowsServer" />
<Add Type="Microsoft.ApplicationInsights.WindowsServer.BuildInfoConfigComponentVersionTelemetryInitializer, Microsoft.AI.WindowsServer" />
<Add Type="Microsoft.ApplicationInsights.WindowsServer.DeviceTelemetryInitializer, Microsoft.AI.WindowsServer" />
<Add Type="Microsoft.ApplicationInsights.Web.SyntheticTelemetryInitializer, Microsoft.AI.Web" />
<Add Type="Microsoft.ApplicationInsights.Web.ClientIpHeaderTelemetryInitializer, Microsoft.AI.Web" />
<Add Type="Microsoft.ApplicationInsights.Web.UserAgentTelemetryInitializer, Microsoft.AI.Web" />
<Add Type="Microsoft.ApplicationInsights.Web.OperationNameTelemetryInitializer, Microsoft.AI.Web" />
<Add Type="Microsoft.ApplicationInsights.Web.OperationIdTelemetryInitializer, Microsoft.AI.Web" />
<Add Type="Microsoft.ApplicationInsights.Web.UserTelemetryInitializer, Microsoft.AI.Web" />
<Add Type="Microsoft.ApplicationInsights.Web.SessionTelemetryInitializer, Microsoft.AI.Web" />
</TelemetryInitializers>
<!--
Learn more about Application Insights configuration with ApplicationInsights.config here:
http://go.microsoft.com/fwlink/?LinkID=513840
Note: If not present, please add <InstrumentationKey>Your Key</InstrumentationKey> to the top of this file.
-->
<!-- This key is for Application Insights resource 'mydriving' in resource group 'mydriving-dataanalytics' -->
<InstrumentationKey>Your Key</InstrumentationKey>
</ApplicationInsights>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Azure.Mobile.Server;
using Microsoft.Azure.Mobile.Server.Config;
using MyDrivingService.Models;
using Microsoft.ApplicationInsights;

namespace MyDrivingService.Controllers
{
Expand All @@ -35,6 +36,7 @@ public async Task<IEnumerable<Device>> Get()
[Authorize]
public async Task<IHttpActionResult> Post(string userId, string deviceName)
{
var aiTelemetry = new TelemetryClient();
Device device = null;
EnsureRegistryManagerInitialized();
MobileAppSettingsDictionary settings = Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings();
Expand All @@ -56,6 +58,7 @@ public async Task<IHttpActionResult> Post(string userId, string deviceName)

if (curUser.Devices.Count >= maxDevices)
{
aiTelemetry.TrackEvent("Max number of devices reached for user = " + curUser.Id);
return BadRequest("You already have more than the maximum number of devices");
}

Expand All @@ -64,9 +67,9 @@ public async Task<IHttpActionResult> Post(string userId, string deviceName)
{
device = await registryManager.GetDeviceAsync(deviceName);
}
catch (Exception)
catch (Exception e)
{

aiTelemetry.TrackException(e);
}

if (device == null) //device not found
Expand All @@ -82,15 +85,20 @@ public async Task<IHttpActionResult> Post(string userId, string deviceName)
}
else //registration failed
{
aiTelemetry.TrackEvent(String.Format("Registration failed for device {0}", deviceName));
return BadRequest("Error. Cannot register device");
}
}
catch (Exception e)
{
aiTelemetry.TrackException(e);
aiTelemetry.TrackEvent(String.Format("Registration failed for device {0}", deviceName));
return BadRequest("Device provisioning failed on server with exception " + e.Message);
}
aiTelemetry.TrackEvent(String.Format("New device {0} registered for user {1}. Total devices: {2}", deviceName, curUser.Id, curUser.Devices.Count));
}


return Created("api/provision", device?.Authentication?.SymmetricKey?.PrimaryKey);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using MyDriving.DataObjects;
using MyDrivingService.Helpers;
using MyDrivingService.Models;
using Microsoft.ApplicationInsights;

namespace MyDrivingService.Controllers
{
Expand Down Expand Up @@ -82,6 +83,11 @@ public async Task<IHttpActionResult> PostTrip(Trip trip)
dbContext.SaveChanges();
}

//track large trips
var aiTelemetry = new TelemetryClient();
var pointsCount = trip?.Points?.Count ?? 0;
if(pointsCount > 1000)
aiTelemetry.TrackEvent(string.Format("Saved trip {0}. Points:{1}", current.Id, pointsCount));

return CreatedAtRoute("Tables", new {id = current.Id}, current);
}
Expand Down
42 changes: 42 additions & 0 deletions src/MobileAppService/MyDrivingService/MyDrivingService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
<ApplicationInsightsResourceId>your_ApplicationInsightsResourceId</ApplicationInsightsResourceId>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -65,6 +66,34 @@
</VisualStudio>
</ProjectExtensions>
<ItemGroup>
<Reference Include="Microsoft.AI.Agent.Intercept, Version=1.2.0.1011, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ApplicationInsights.Agent.Intercept.1.2.0\lib\net45\Microsoft.AI.Agent.Intercept.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.AI.DependencyCollector, Version=1.2.3.246, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ApplicationInsights.DependencyCollector.1.2.3\lib\net45\Microsoft.AI.DependencyCollector.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.AI.PerfCounterCollector, Version=1.2.3.246, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ApplicationInsights.PerfCounterCollector.1.2.3\lib\net45\Microsoft.AI.PerfCounterCollector.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.AI.ServerTelemetryChannel, Version=1.2.3.246, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.1.2.3\lib\net45\Microsoft.AI.ServerTelemetryChannel.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.AI.Web, Version=1.2.3.246, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ApplicationInsights.Web.1.2.3\lib\net45\Microsoft.AI.Web.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.AI.WindowsServer, Version=1.2.3.246, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ApplicationInsights.WindowsServer.1.2.3\lib\net45\Microsoft.AI.WindowsServer.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ApplicationInsights, Version=1.2.3.490, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.ApplicationInsights.1.2.3\lib\net45\Microsoft.ApplicationInsights.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Amqp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Azure.Amqp.1.0.0\lib\net451\Microsoft.Azure.Amqp.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -210,6 +239,8 @@
<Compile Include="Controllers\UserProfileController.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="scripts\ai.0.15.0-build58334.js" />
<Content Include="scripts\ai.0.15.0-build58334.min.js" />
<Content Include="Web.config">
<SubType>Designer</SubType>
</Content>
Expand All @@ -224,10 +255,21 @@
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="ApplicationInsights.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="Properties\PublishProfiles\motzMyDriving - Web Deploy.pubxml" />
<None Include="Properties\PublishProfiles\mydriving - Web Deploy %282%29.pubxml" />
<None Include="Properties\PublishProfiles\mydriving - Web Deploy.pubxml" />
<None Include="Properties\PublishProfiles\mydriving-test - Web Deploy %282%29.pubxml" />
<None Include="Properties\PublishProfiles\mydriving-test - Web Deploy %283%29.pubxml" />
<None Include="Properties\PublishProfiles\mydriving-test - Web Deploy.pubxml" />
<None Include="Properties\PublishProfiles\SmartKar - Web Deploy.pubxml" />
<None Include="Properties\PublishProfiles\MyDriving - Web Deploy.pubxml" />
<Content Include="Service References\Application Insights\ConnectedService.json" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Service References\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ProviderId": "Microsoft.ApplicationInsights.ConnectedService.ConnectedServiceProvider",
"Version": "4.1.60107.3",
"GettingStartedDocument": {
"Uri": "http://go.microsoft.com/fwlink/?LinkID=613413"
}
}
8 changes: 7 additions & 1 deletion src/MobileAppService/MyDrivingService/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,16 @@
<add assembly="System.Web.Http.Tracing" />
</assemblies>
</compilation>
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<modules runAllManagedModulesForAllRequests="true">
<remove name="ApplicationInsightsWebTracking" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
Expand Down
8 changes: 8 additions & 0 deletions src/MobileAppService/MyDrivingService/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
<packages>
<package id="AutoMapper" version="3.3.1" targetFramework="net45" />
<package id="EntityFramework" version="6.1.3" targetFramework="net45" />
<package id="Microsoft.ApplicationInsights" version="1.2.3" targetFramework="net451" />
<package id="Microsoft.ApplicationInsights.Agent.Intercept" version="1.2.0" targetFramework="net451" />
<package id="Microsoft.ApplicationInsights.DependencyCollector" version="1.2.3" targetFramework="net451" />
<package id="Microsoft.ApplicationInsights.JavaScript" version="0.15.0-build58334" targetFramework="net451" />
<package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="1.2.3" targetFramework="net451" />
<package id="Microsoft.ApplicationInsights.Web" version="1.2.3" targetFramework="net451" />
<package id="Microsoft.ApplicationInsights.WindowsServer" version="1.2.3" targetFramework="net451" />
<package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="1.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.OData" version="5.5.1" targetFramework="net45" />
Expand Down
Loading

0 comments on commit 77981b7

Please sign in to comment.