Add telemetry to power service

This commit is contained in:
2021-06-15 15:24:59 -04:00
parent 0eca404329
commit 78b0757527
5 changed files with 34 additions and 2 deletions

View File

@@ -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);

View File

@@ -26,6 +26,8 @@
<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.90" />
<PackageReference Include="JetBrains.Annotations" Version="2021.1.0" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.17.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.17.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="5.0.6" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.3" />
<PackageReference Include="RestSharp" Version="106.11.7" />

View File

@@ -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<ITelemetryInitializer, TelemetryInitializer>();
services.AddApplicationInsightsTelemetry();
services.AddTransient<Database>();
services.AddHostedService<PowerReader>();

View File

@@ -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;
}
}
}

View File

@@ -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