From 12db31711078ee3c95b785dff59c13da81f621ca Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Tue, 27 Dec 2016 15:03:54 -0800 Subject: [PATCH] Report metrics to Application Insights --- src/Podsync/Controllers/DownloadController.cs | 8 ++++- src/Podsync/Controllers/FeedController.cs | 35 +++++++++++++++++-- src/Podsync/Helpers/Extensions.cs | 2 +- src/Podsync/Podsync.xproj | 1 + .../ConnectedService.json | 7 ++++ src/Podsync/Startup.cs | 5 +++ src/Podsync/Views/Shared/_Layout.cshtml | 3 +- src/Podsync/Views/_ViewImports.cshtml | 2 ++ src/Podsync/appsettings.json | 8 +++-- src/Podsync/project.json | 3 +- 10 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 src/Podsync/Service References/Application Insights/ConnectedService.json diff --git a/src/Podsync/Controllers/DownloadController.cs b/src/Podsync/Controllers/DownloadController.cs index 8e8d710..ac61730 100644 --- a/src/Podsync/Controllers/DownloadController.cs +++ b/src/Podsync/Controllers/DownloadController.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks; +using Microsoft.ApplicationInsights; using Microsoft.AspNetCore.Mvc; using Podsync.Helpers; using Podsync.Services.Links; @@ -14,12 +15,14 @@ namespace Podsync.Controllers private readonly IResolverService _resolverService; private readonly ILinkService _linkService; private readonly IStorageService _storageService; + private readonly TelemetryClient _telemetry; - public DownloadController(IResolverService resolverService, ILinkService linkService, IStorageService storageService) + public DownloadController(IResolverService resolverService, ILinkService linkService, IStorageService storageService, TelemetryClient telemetry) { _resolverService = resolverService; _linkService = linkService; _storageService = storageService; + _telemetry = telemetry; } // Main video download endpoint, don't forget to reflect any changes in LinkService.Download @@ -38,6 +41,9 @@ namespace Podsync.Controllers var redirectUrl = await _resolverService.Resolve(url, metadata.Quality); + // Report metrics + _telemetry.TrackEvent("Download"); + return Redirect(redirectUrl.ToString()); } } diff --git a/src/Podsync/Controllers/FeedController.cs b/src/Podsync/Controllers/FeedController.cs index b70554c..e00e6ac 100644 --- a/src/Podsync/Controllers/FeedController.cs +++ b/src/Podsync/Controllers/FeedController.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.Security.Claims; using System.Threading.Tasks; using System.Xml.Serialization; +using Microsoft.ApplicationInsights; using Microsoft.AspNetCore.Mvc; using Podsync.Helpers; using Podsync.Services; @@ -27,12 +29,14 @@ namespace Podsync.Controllers private readonly IRssBuilder _rssBuilder; private readonly ILinkService _linkService; private readonly IStorageService _storageService; + private readonly TelemetryClient _telemetry; - public FeedController(IRssBuilder rssBuilder, ILinkService linkService, IStorageService storageService) + public FeedController(IRssBuilder rssBuilder, ILinkService linkService, IStorageService storageService, TelemetryClient telemetry) { _rssBuilder = rssBuilder; _linkService = linkService; _storageService = storageService; + _telemetry = telemetry; } [HttpPost] @@ -51,14 +55,36 @@ namespace Podsync.Controllers PageSize = request.PageSize ?? DefaultPageSize }; - if (!User.EnablePatreonFeatures()) + // Check if user eligible for Patreon features + var enablePatreonFeatures = User.EnablePatreonFeatures(); + if (!enablePatreonFeatures) { feed.Quality = ResolveType.VideoHigh; feed.PageSize = DefaultPageSize; } var feedId = await _storageService.Save(feed); - return _linkService.Feed(Request.GetBaseUrl(), feedId); + var url = _linkService.Feed(Request.GetBaseUrl(), feedId); + + // Report metrics + var properties = new Dictionary + { + ["Provider"] = linkInfo.Provider.ToString(), + ["Patreon"] = enablePatreonFeatures.ToString(), + ["Format"] = feed.Quality == ResolveType.AudioHigh || feed.Quality == ResolveType.AudioLow ? "Audio" : "Video", + ["Quality"] = feed.Quality == ResolveType.AudioHigh || feed.Quality == ResolveType.VideoHigh ? "Hight" : "Low", + ["PageSize"] = feed.PageSize.ToString() + }; + + if (User.Identity.IsAuthenticated) + { + properties.Add("User", User.GetClaim(ClaimTypes.NameIdentifier)); + properties.Add("Email", User.GetClaim(ClaimTypes.Email)); + } + + _telemetry.TrackEvent("CreateFeed", properties); + + return url; } [HttpGet] @@ -90,6 +116,9 @@ namespace Podsync.Controllers body = writer.ToString(); } + // Report metrics + _telemetry.TrackEvent("GetFeed"); + return Content(body, "application/rss+xml; charset=UTF-8"); } } diff --git a/src/Podsync/Helpers/Extensions.cs b/src/Podsync/Helpers/Extensions.cs index 85ffa31..90c99ed 100644 --- a/src/Podsync/Helpers/Extensions.cs +++ b/src/Podsync/Helpers/Extensions.cs @@ -68,7 +68,7 @@ namespace Podsync.Helpers ?? "noname :("; } - private static string GetClaim(this ClaimsPrincipal claimsPrincipal, string type) + public static string GetClaim(this ClaimsPrincipal claimsPrincipal, string type) { return claimsPrincipal.Claims.FirstOrDefault(x => x.Type == type)?.Value; } diff --git a/src/Podsync/Podsync.xproj b/src/Podsync/Podsync.xproj index 655122b..0deb346 100644 --- a/src/Podsync/Podsync.xproj +++ b/src/Podsync/Podsync.xproj @@ -14,6 +14,7 @@ 2.0 + /subscriptions/6ced39ae-0008-4531-bf42-4c517d4fb309/resourcegroups/podsync_group/providers/microsoft.insights/components/podsync diff --git a/src/Podsync/Service References/Application Insights/ConnectedService.json b/src/Podsync/Service References/Application Insights/ConnectedService.json new file mode 100644 index 0000000..92d8ae9 --- /dev/null +++ b/src/Podsync/Service References/Application Insights/ConnectedService.json @@ -0,0 +1,7 @@ +{ + "ProviderId": "Microsoft.ApplicationInsights.ConnectedService.ConnectedServiceProvider", + "Version": "7.15.1215.1", + "GettingStartedDocument": { + "Uri": "https://go.microsoft.com/fwlink/?LinkID=798432" + } +} \ No newline at end of file diff --git a/src/Podsync/Startup.cs b/src/Podsync/Startup.cs index d32dc38..72585bc 100644 --- a/src/Podsync/Startup.cs +++ b/src/Podsync/Startup.cs @@ -32,6 +32,7 @@ namespace Podsync if (env.IsDevelopment()) { builder.AddUserSecrets(); + builder.AddApplicationInsightsSettings(true); } Configuration = builder.Build(); @@ -56,6 +57,7 @@ namespace Podsync services.AddAuthentication(config => config.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme); // Add framework services + services.AddApplicationInsightsTelemetry(Configuration); services.AddMvc(); } @@ -65,6 +67,9 @@ namespace Podsync loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); + app.UseApplicationInsightsRequestTelemetry(); + app.UseApplicationInsightsExceptionTelemetry(); + if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); diff --git a/src/Podsync/Views/Shared/_Layout.cshtml b/src/Podsync/Views/Shared/_Layout.cshtml index 23432db..85b4d42 100644 --- a/src/Podsync/Views/Shared/_Layout.cshtml +++ b/src/Podsync/Views/Shared/_Layout.cshtml @@ -21,7 +21,8 @@ - + + @Html.ApplicationInsightsJavaScript(TelemetryConfiguration)
diff --git a/src/Podsync/Views/_ViewImports.cshtml b/src/Podsync/Views/_ViewImports.cshtml index 28ee55d..61e8b96 100644 --- a/src/Podsync/Views/_ViewImports.cshtml +++ b/src/Podsync/Views/_ViewImports.cshtml @@ -1,2 +1,4 @@ @using Podsync @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers + +@inject Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration TelemetryConfiguration \ No newline at end of file diff --git a/src/Podsync/appsettings.json b/src/Podsync/appsettings.json index 0b89f9f..73856dd 100644 --- a/src/Podsync/appsettings.json +++ b/src/Podsync/appsettings.json @@ -1,4 +1,4 @@ -{ +{ "Logging": { "IncludeScopes": false, "LogLevel": { @@ -7,12 +7,14 @@ "Microsoft": "Information" } }, - "Podsync": { "YouTubeApiKey": "", "RedisConnectionString": "localhost", "BaseUrl": "", "PatreonClientId": "", "PatreonSecret": "" + }, + "ApplicationInsights": { + "InstrumentationKey": "" } -} +} \ No newline at end of file diff --git a/src/Podsync/project.json b/src/Podsync/project.json index d998496..e0d57f0 100644 --- a/src/Podsync/project.json +++ b/src/Podsync/project.json @@ -1,7 +1,8 @@ -{ +{ "dependencies": { "Google.Apis.YouTube.v3": "1.16.0.582", "Hashids.net": "1.2.2", + "Microsoft.ApplicationInsights.AspNetCore": "1.0.0", "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0", "Microsoft.AspNetCore.Authentication.OAuth": "1.0.0", "Microsoft.AspNetCore.Diagnostics": "1.0.0",