From 78b07575278b8e7dff7e105db1cc7a4a1b0f4d3c Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Tue, 15 Jun 2021 15:24:59 -0400 Subject: [PATCH] Add telemetry to power service --- Power/Service/PowerReader.cs | 10 ++++++++-- Power/Service/Service.csproj | 2 ++ Power/Service/Startup.cs | 5 +++++ Power/Service/TelemetryInitializer.cs | 14 ++++++++++++++ Power/Service/deploy/manifest.yaml | 5 +++++ 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 Power/Service/TelemetryInitializer.cs diff --git a/Power/Service/PowerReader.cs b/Power/Service/PowerReader.cs index f618591..59dc3a3 100644 --- a/Power/Service/PowerReader.cs +++ b/Power/Service/PowerReader.cs @@ -1,12 +1,12 @@ using ChrisKaczor.HomeMonitor.Power.Service.Data; using ChrisKaczor.HomeMonitor.Power.Service.Models; using JetBrains.Annotations; +using Microsoft.ApplicationInsights; using Microsoft.AspNetCore.SignalR.Client; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using RestSharp; using System; -using System.Linq; using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -18,18 +18,22 @@ namespace ChrisKaczor.HomeMonitor.Power.Service { private readonly IConfiguration _configuration; private readonly Database _database; + private readonly TelemetryClient _telemetryClient; private HubConnection _hubConnection; private Timer _readTimer; - public PowerReader(IConfiguration configuration, Database database) + public PowerReader(IConfiguration configuration, Database database, TelemetryClient telemetryClient) { _configuration = configuration; _database = database; + _telemetryClient = telemetryClient; } public Task StartAsync(CancellationToken cancellationToken) { + _telemetryClient.TrackTrace($"{nameof(PowerReader)} - Start"); + _readTimer = new Timer(OnTimer, null, TimeSpan.Zero, TimeSpan.FromSeconds(1)); if (!string.IsNullOrEmpty(_configuration["Hub:Power"])) @@ -81,6 +85,8 @@ namespace ChrisKaczor.HomeMonitor.Power.Service public Task StopAsync(CancellationToken cancellationToken) { + _telemetryClient.TrackTrace($"{nameof(PowerReader)} - Stop"); + _readTimer.Dispose(); _hubConnection?.StopAsync(cancellationToken).Wait(cancellationToken); diff --git a/Power/Service/Service.csproj b/Power/Service/Service.csproj index 75c9f60..0197698 100644 --- a/Power/Service/Service.csproj +++ b/Power/Service/Service.csproj @@ -26,6 +26,8 @@ + + diff --git a/Power/Service/Startup.cs b/Power/Service/Startup.cs index 1533359..1391e82 100644 --- a/Power/Service/Startup.cs +++ b/Power/Service/Startup.cs @@ -1,4 +1,5 @@ using ChrisKaczor.HomeMonitor.Power.Service.Data; +using Microsoft.ApplicationInsights.Extensibility; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; @@ -13,6 +14,10 @@ namespace ChrisKaczor.HomeMonitor.Power.Service { public void ConfigureServices(IServiceCollection services) { + services.AddSingleton(); + + services.AddApplicationInsightsTelemetry(); + services.AddTransient(); services.AddHostedService(); diff --git a/Power/Service/TelemetryInitializer.cs b/Power/Service/TelemetryInitializer.cs new file mode 100644 index 0000000..157be8a --- /dev/null +++ b/Power/Service/TelemetryInitializer.cs @@ -0,0 +1,14 @@ +using Microsoft.ApplicationInsights.Channel; +using Microsoft.ApplicationInsights.Extensibility; +using System.Reflection; + +namespace ChrisKaczor.HomeMonitor.Power.Service +{ + public class TelemetryInitializer : ITelemetryInitializer + { + public void Initialize(ITelemetry telemetry) + { + telemetry.Context.Cloud.RoleName = Assembly.GetEntryAssembly()?.GetName().Name; + } + } +} diff --git a/Power/Service/deploy/manifest.yaml b/Power/Service/deploy/manifest.yaml index bd8f2ca..493ff3b 100644 --- a/Power/Service/deploy/manifest.yaml +++ b/Power/Service/deploy/manifest.yaml @@ -94,6 +94,11 @@ spec: securityContext: privileged: true env: + - name: ApplicationInsights__InstrumentationKey + valueFrom: + secretKeyRef: + name: telemetry + key: key - name: Power__Database__Host value: power-database - name: Power__Database__User