Skip to content

Commit c40b4cb

Browse files
committed
App Insights - WIP
1 parent 24eec13 commit c40b4cb

16 files changed

+161
-20
lines changed

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
# msbuild /r /p:Configuration=Release /p:OutputPath=app /t:Publish
33

44
# You should run this file with the following parameters:
5-
# docker build . --build-args DOTNETTAG=<dotnettag> --build-arg OSTAG=<ostag> -t <image-tag>
5+
# docker build . --build-arg DOTNETTAG=<dotnettag> --build-arg OSTAG=<ostag> -t <image-tag>
66
# where:
77
# <dotnettag> is the tag of the dotnet aspnet runtime image
88
# <ostag> is the tag of the runtime for hass.io
99

10-
ARG DOTNETTAG
11-
ARG OSTAG
10+
ARG DOTNETTAG=3.0-alpine-arm64v8
11+
ARG OSTAG=
1212

1313
FROM mcr.microsoft.com/dotnet/core/aspnet:$DOTNETTAG
1414
EXPOSE 80

TELEMETRY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ The telemetry feature collects the following data:
2929
* OS & processor type (Windows/Linux) (amd64/arm)
3030
* Installation type (Docker or HASS.IO)
3131
* Versions: HASS.IO, Zigbee2Mqtt
32+
* Crash reports with anonymized details

Zigbee2MqttAssistant.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1212
azure-pipelines.yaml = azure-pipelines.yaml
1313
Dockerfile = Dockerfile
1414
README.md = README.md
15+
TELEMETRY.md = TELEMETRY.md
1516
EndProjectSection
1617
EndProject
1718
Global

Zigbee2MqttAssistant/Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# This dockerfile is only for development in VisualStudio!
2+
# It's not used for build in CI.
3+
4+
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS base
5+
WORKDIR /app
6+
EXPOSE 80
7+
EXPOSE 443
8+
9+
FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
10+
WORKDIR /src
11+
COPY ["Zigbee2MqttAssistant/Zigbee2MqttAssistant.csproj", "Zigbee2MqttAssistant/"]
12+
RUN dotnet restore "Zigbee2MqttAssistant/Zigbee2MqttAssistant.csproj"
13+
COPY . .
14+
WORKDIR "/src/Zigbee2MqttAssistant"
15+
RUN dotnet build "Zigbee2MqttAssistant.csproj" -c Release -o /app
16+
17+
FROM build AS publish
18+
RUN dotnet publish "Zigbee2MqttAssistant.csproj" -c Release -o /app
19+
20+
FROM base AS final
21+
WORKDIR /app
22+
COPY --from=publish /app .
23+
ENTRYPOINT ["dotnet", "Zigbee2MqttAssistant.dll"]

Zigbee2MqttAssistant/Models/Settings.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,5 @@ public partial class Settings
6464
/// https://github.com/yllibed/Zigbee2MqttAssistant/blob/master/TELEMETRY.md
6565
/// </remarks>
6666
public bool TelemetryOptOut { get; } = false;
67-
68-
/// <summary>
69-
/// Instrumentation Key for Azure AppInsights
70-
/// </summary>
71-
public string TelemetryInstrumentationKey { get; } = "a07cd338-3c1d-417a-890b-67e56efa2ae9";
7267
}
7368
}

Zigbee2MqttAssistant/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore;
77
using Microsoft.AspNetCore.Hosting;
88
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.DependencyInjection;
910
using Microsoft.Extensions.Logging;
1011

1112
namespace Zigbee2MqttAssistant
@@ -14,7 +15,9 @@ public class Program
1415
{
1516
public static void Main(string[] args)
1617
{
17-
CreateWebHostBuilder(args).Build().Run();
18+
CreateWebHostBuilder(args)
19+
.Build()
20+
.Run();
1821
}
1922

2023
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Microsoft.ApplicationInsights;
5+
using Microsoft.ApplicationInsights.Channel;
6+
using Microsoft.ApplicationInsights.Extensibility;
7+
using Microsoft.Extensions.Hosting;
8+
using Zigbee2MqttAssistant.Models;
9+
10+
namespace Zigbee2MqttAssistant.Services
11+
{
12+
public class AppTelemetry : IAppTelemetry, ITelemetryInitializer, IDisposable
13+
{
14+
private readonly TelemetryClient _client;
15+
16+
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
17+
18+
public AppTelemetry(ISettingsService settings, ISystemInformation systemInformation)
19+
{
20+
TelemetryConfiguration.Active.TelemetryInitializers.Add(this);
21+
22+
_client = new TelemetryClient(SettingsToTelemetryConfig(settings.CurrentSettings));
23+
}
24+
25+
private static TelemetryConfiguration SettingsToTelemetryConfig(Settings settings)
26+
{
27+
return null;
28+
//return new TelemetryConfiguration(settings.TelemetryInstrumentationKey);
29+
}
30+
31+
public async void ReportStart()
32+
{
33+
while (!_cts.IsCancellationRequested)
34+
{
35+
_client.TrackEvent("test");
36+
37+
38+
await Task.Delay(TimeSpan.FromMinutes(15), _cts.Token);
39+
}
40+
}
41+
42+
public void ReportStop()
43+
{
44+
}
45+
46+
public void ReportError(string message, Exception exception)
47+
{
48+
}
49+
50+
void ITelemetryInitializer.Initialize(Microsoft.ApplicationInsights.Channel.ITelemetry telemetry)
51+
{
52+
}
53+
54+
public void Dispose()
55+
{
56+
_cts.Dispose();
57+
}
58+
}
59+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace Zigbee2MqttAssistant.Services
4+
{
5+
public interface IAppTelemetry
6+
{
7+
void ReportStart();
8+
void ReportStop();
9+
10+
void ReportError(string message, Exception exception);
11+
}
12+
}

Zigbee2MqttAssistant/Services/ISystemInformation.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Zigbee2MqttAssistant.Services
1+
using System;
2+
3+
namespace Zigbee2MqttAssistant.Services
24
{
35
public interface ISystemInformation
46
{
@@ -20,5 +22,6 @@ public interface ISystemInformation
2022
bool MqttBrokerConnected { get; }
2123
int NumberOfDevices { get; }
2224
bool Telemetry { get; }
25+
DateTimeOffset StartTime { get; }
2326
}
2427
}

Zigbee2MqttAssistant/Services/SettingsService.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,32 @@ namespace Zigbee2MqttAssistant.Services
77
public class SettingsService : ISettingsService
88
{
99
public SettingsService(IConfiguration configuration, ILogger<SettingsService> logger)
10+
{
11+
var settings = GetFromConfiguration(configuration);
12+
if(settings == null)
13+
{
14+
logger.LogWarning(
15+
"Section 'settings' in configuration does not exists. Will use default settings instead.");
16+
CurrentSettings = Settings.Default;
17+
}
18+
else
19+
{
20+
CurrentSettings = settings;
21+
}
22+
}
23+
24+
internal static Settings GetFromConfiguration(IConfiguration configuration)
1025
{
1126
var section = configuration.GetSection("settings");
1227

1328
if (section.Exists())
1429
{
1530
var settingsBuilder = new Settings.Builder();
1631
section.Bind(settingsBuilder);
17-
CurrentSettings = settingsBuilder;
18-
}
19-
else
20-
{
21-
logger.LogWarning(
22-
"Section 'settings' in configuration does not exists. Will use default settings instead.");
23-
CurrentSettings = Settings.Default;
32+
return settingsBuilder;
2433
}
34+
35+
return null;
2536
}
2637

2738
public Settings CurrentSettings { get; }

0 commit comments

Comments
 (0)