Update Weather to .NET 8 and add telemetry

This commit is contained in:
2024-01-25 16:25:59 -05:00
parent 2394f613d2
commit 5d0bc2c31c
28 changed files with 856 additions and 775 deletions

View File

@@ -1,12 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using System;
namespace ChrisKaczor.HomeMonitor.Weather.SerialReader.Controllers
namespace ChrisKaczor.HomeMonitor.Weather.SerialReader.Controllers;
[Route("[controller]")]
[ApiController]
public class HealthController : ControllerBase
{
[Route("[controller]")]
[ApiController]
public class HealthController : ControllerBase
{
private readonly TimeSpan _checkTimeSpan = TimeSpan.FromSeconds(5);
[HttpGet("ready")]
@@ -23,5 +23,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.SerialReader.Controllers
return SerialReader.BoardStarted && timeSinceLastReading <= _checkTimeSpan ? Ok(lastReading) : BadRequest(lastReading);
}
}
}

View File

@@ -1,8 +1,8 @@
FROM mcr.microsoft.com/dotnet/aspnet:5.0-focal-arm32v7 AS base
FROM mcr.microsoft.com/dotnet/aspnet:8.0-jammy-arm32v7 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS build
FROM mcr.microsoft.com/dotnet/sdk:8.0-jammy AS build
WORKDIR /src
COPY ["./SerialReader.csproj", "./"]
RUN dotnet restore -r linux-arm "SerialReader.csproj"

View File

@@ -1,20 +1,116 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System;
using System.IO.Compression;
using System.Reflection;
using System.Text.Json.Serialization;
namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
namespace ChrisKaczor.HomeMonitor.Weather.SerialReader;
public static class Program
{
public static class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
var builder = WebApplication.CreateBuilder(args);
private static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
builder.Configuration.AddEnvironmentVariables();
builder.Services.AddControllers();
// ---
var openTelemetry = builder.Services.AddOpenTelemetry();
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var serviceName = Assembly.GetExecutingAssembly().GetName().Name;
openTelemetry.ConfigureResource(resource => resource.AddService(serviceName!));
openTelemetry.WithMetrics(meterProviderBuilder => meterProviderBuilder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddProcessInstrumentation()
.AddMeter("Microsoft.AspNetCore.Hosting")
.AddMeter("Microsoft.AspNetCore.Server.Kestrel"));
openTelemetry.WithTracing(tracerProviderBuilder =>
{
webBuilder.UseStartup<Startup>();
tracerProviderBuilder.AddAspNetCoreInstrumentation(instrumentationOptions => instrumentationOptions.RecordException = true);
tracerProviderBuilder.AddHttpClientInstrumentation(instrumentationOptions => instrumentationOptions.RecordException = true);
tracerProviderBuilder.AddSqlClientInstrumentation(o =>
{
o.RecordException = true;
o.SetDbStatementForText = true;
});
tracerProviderBuilder.AddSource(nameof(SerialReader));
tracerProviderBuilder.SetErrorStatusOnException();
tracerProviderBuilder.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(builder.Configuration["Telemetry:Endpoint"]!);
exporterOptions.Protocol = OtlpExportProtocol.Grpc;
});
});
builder.Services.AddLogging(loggingBuilder =>
{
loggingBuilder.SetMinimumLevel(LogLevel.Information);
loggingBuilder.AddOpenTelemetry(options =>
{
options.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(builder.Configuration["Telemetry:Endpoint"]!);
exporterOptions.Protocol = OtlpExportProtocol.Grpc;
});
}
);
});
builder.Services.AddHostedService<SerialReader>();
builder.Services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Optimal);
builder.Services.AddResponseCompression(options =>
{
options.Providers.Add<GzipCompressionProvider>();
options.EnableForHttps = true;
});
builder.Services.AddCors(o => o.AddPolicy("CorsPolicy", corsPolicyBuilder => corsPolicyBuilder.AllowAnyMethod().AllowAnyHeader().AllowCredentials().WithOrigins("http://localhost:4200")));
builder.Services.AddMvc().AddJsonOptions(configure => { configure.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); });
// ---
var app = builder.Build();
if (builder.Environment.IsDevelopment())
app.UseDeveloperExceptionPage();
app.UseCors("CorsPolicy");
app.UseResponseCompression();
app.UseRouting();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}

View File

@@ -8,23 +8,17 @@ using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
namespace ChrisKaczor.HomeMonitor.Weather.SerialReader;
public class SerialReader(IConfiguration configuration, ILogger<SerialReader> logger) : IHostedService
{
public class SerialReader : IHostedService
{
private readonly IConfiguration _configuration;
public static bool BoardStarted { get; private set; }
public static DateTimeOffset LastReading { get; private set; }
private CancellationToken _cancellationToken;
public SerialReader(IConfiguration configuration)
{
_configuration = configuration;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_cancellationToken = cancellationToken;
@@ -36,7 +30,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
private void Execute()
{
var baudRate = int.Parse(_configuration["Weather:Port:BaudRate"]);
var baudRate = configuration.GetValue<int>("Weather:Port:BaudRate");
while (!_cancellationToken.IsCancellationRequested)
{
@@ -51,7 +45,9 @@ namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
if (port == null)
continue;
using var serialPort = new SerialPort(port, baudRate) { NewLine = "\r\n" };
using var serialPort = new SerialPort(port, baudRate);
serialPort.NewLine = "\r\n";
WriteLog("Opening serial port");
@@ -61,9 +57,9 @@ namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
var factory = new ConnectionFactory
{
HostName = _configuration["Weather:Queue:Host"],
UserName = _configuration["Weather:Queue:User"],
Password = _configuration["Weather:Queue:Password"]
HostName = configuration["Weather:Queue:Host"],
UserName = configuration["Weather:Queue:User"],
Password = configuration["Weather:Queue:Password"]
};
WriteLog("Connecting to queue server");
@@ -73,7 +69,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
WriteLog("Declaring queue");
model.QueueDeclare(_configuration["Weather:Queue:Name"], true, false, false, null);
model.QueueDeclare(configuration["Weather:Queue:Name"], true, false, false, null);
WriteLog("Starting serial read loop");
@@ -122,7 +118,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
properties.Persistent = true;
model.BasicPublish(string.Empty, _configuration["Weather:Queue:Name"], properties, body);
model.BasicPublish(string.Empty, configuration["Weather:Queue:Name"], properties, body);
}
catch (TimeoutException)
{
@@ -133,7 +129,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
private string GetPort()
{
var portPrefix = _configuration["Weather:Port:Prefix"];
var portPrefix = configuration["Weather:Port:Prefix"]!;
while (!_cancellationToken.IsCancellationRequested)
{
@@ -157,14 +153,13 @@ namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
return null;
}
private static void WriteLog(string message)
private void WriteLog(string message)
{
Console.WriteLine(message);
logger.LogInformation(message);
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}

View File

@@ -2,8 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<CodeAnalysisRuleSet>../../ChrisKaczor.ruleset</CodeAnalysisRuleSet>
<TargetFramework>net8.0</TargetFramework>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
</PropertyGroup>
@@ -13,13 +12,16 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ChrisKaczor.HomeMonitor.Weather.Models" Version="1.1.6" />
<PackageReference Include="JetBrains.Annotations" Version="2021.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="RabbitMQ.Client" Version="6.2.1" />
<PackageReference Include="System.IO.Ports" Version="5.0.1" />
<PackageReference Include="ChrisKaczor.HomeMonitor.Weather.Models" Version="1.1.8" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
<PackageReference Include="OpenTelemetry.AutoInstrumentation" Version="1.3.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.0" />
<PackageReference Include="System.Security.Cryptography.Xml" Version="8.0.0" />
</ItemGroup>
</Project>

View File

@@ -1,34 +0,0 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddHostedService<SerialReader>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}

View File

@@ -7,5 +7,8 @@
"Queue": {
"Name": "weather"
}
},
"Telemetry": {
"Endpoint": "http://signoz-otel-collector.platform:4317/"
}
}

View File

@@ -97,7 +97,7 @@ spec:
livenessProbe:
httpGet:
path: health/health
port: 80
port: 8080
env:
- name: Weather__Queue__Host
value: weather-queue

View File

@@ -7,23 +7,16 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Controllers
namespace ChrisKaczor.HomeMonitor.Weather.Service.Controllers;
[Route("[controller]")]
[ApiController]
public class ReadingsController(Database database) : ControllerBase
{
[Route("[controller]")]
[ApiController]
public class ReadingsController : ControllerBase
{
private readonly Database _database;
public ReadingsController(Database database)
{
_database = database;
}
[HttpGet("recent")]
public async Task<ActionResult<WeatherReading>> GetRecent([FromQuery] string tz)
{
var recentReading = await _database.GetRecentReading();
var recentReading = await database.GetRecentReading();
if (string.IsNullOrWhiteSpace(tz))
return recentReading;
@@ -46,13 +39,13 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Controllers
[HttpGet("history")]
public async Task<ActionResult<List<WeatherReading>>> GetHistory(DateTimeOffset start, DateTimeOffset end)
{
return (await _database.GetReadingHistory(start, end)).ToList();
return (await database.GetReadingHistory(start, end)).ToList();
}
[HttpGet("aggregate")]
public async Task<ActionResult<WeatherAggregate>> GetHistoryAggregate(DateTimeOffset start, DateTimeOffset end)
{
var readings = (await _database.GetReadingHistory(start, end)).ToList();
var readings = (await database.GetReadingHistory(start, end)).ToList();
return readings.Any() ? new WeatherAggregate(readings) : null;
}
@@ -60,25 +53,24 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Controllers
[HttpGet("value-history")]
public async Task<ActionResult<List<WeatherValue>>> GetValueHistory(WeatherValueType weatherValueType, DateTimeOffset start, DateTimeOffset end)
{
return (await _database.GetReadingValueHistory(weatherValueType, start, end)).ToList();
return (await database.GetReadingValueHistory(weatherValueType, start, end)).ToList();
}
[HttpGet("value-history-grouped")]
public async Task<ActionResult<List<WeatherValueGrouped>>> GetValueHistoryGrouped(WeatherValueType weatherValueType, DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 2)
{
return (await _database.GetReadingValueHistoryGrouped(weatherValueType, start, end, bucketMinutes)).ToList();
return (await database.GetReadingValueHistoryGrouped(weatherValueType, start, end, bucketMinutes)).ToList();
}
[HttpGet("history-grouped")]
public async Task<ActionResult<List<WeatherReadingGrouped>>> GetHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 2)
{
return (await _database.GetReadingHistoryGrouped(start, end, bucketMinutes)).ToList();
return (await database.GetReadingHistoryGrouped(start, end, bucketMinutes)).ToList();
}
[HttpGet("wind-history-grouped")]
public async Task<ActionResult<List<WindHistoryGrouped>>> GetWindHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 60)
{
return (await _database.GetWindHistoryGrouped(start, end, bucketMinutes)).ToList();
}
return (await database.GetWindHistoryGrouped(start, end, bucketMinutes)).ToList();
}
}

View File

@@ -7,24 +7,17 @@ using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading.Tasks;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
namespace ChrisKaczor.HomeMonitor.Weather.Service.Data;
public class Database(IConfiguration configuration)
{
public class Database
{
private readonly IConfiguration _configuration;
public Database(IConfiguration configuration)
{
_configuration = configuration;
}
public void EnsureDatabase()
{
var connectionStringBuilder = new SqlConnectionStringBuilder
{
DataSource = _configuration["Weather:Database:Host"],
UserID = _configuration["Weather:Database:User"],
Password = _configuration["Weather:Database:Password"],
DataSource = configuration["Weather:Database:Host"],
UserID = configuration["Weather:Database:User"],
Password = configuration["Weather:Database:Password"],
InitialCatalog = "master"
};
@@ -35,22 +28,22 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
connection.Open();
// Check to see if the database exists
command.CommandText = $"SELECT CAST(1 as bit) from sys.databases WHERE name='{_configuration["Weather:Database:Name"]}'";
command.CommandText = $"SELECT CAST(1 as bit) from sys.databases WHERE name='{configuration["Weather:Database:Name"]}'";
var databaseExists = (bool?)command.ExecuteScalar();
// Create database if needed
if (!(databaseExists ?? false))
{
command.CommandText = $"CREATE DATABASE {_configuration["Weather:Database:Name"]}";
command.CommandText = $"CREATE DATABASE {configuration["Weather:Database:Name"]}";
command.ExecuteNonQuery();
}
// Switch to the database now that we're sure it exists
connection.ChangeDatabase(_configuration["Weather:Database:Name"]);
connection.ChangeDatabase(configuration["Weather:Database:Name"]!);
var schema = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.Schema.sql");
// Make sure the database is up to date
// Make sure the database is up-to-date
command.CommandText = schema;
command.ExecuteNonQuery();
}
@@ -59,10 +52,10 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
{
var connectionStringBuilder = new SqlConnectionStringBuilder
{
DataSource = _configuration["Weather:Database:Host"],
UserID = _configuration["Weather:Database:User"],
Password = _configuration["Weather:Database:Password"],
InitialCatalog = _configuration["Weather:Database:Name"]
DataSource = configuration["Weather:Database:Host"],
UserID = configuration["Weather:Database:User"],
Password = configuration["Weather:Database:Password"],
InitialCatalog = configuration["Weather:Database:Name"]
};
var connection = new SqlConnection(connectionStringBuilder.ConnectionString);
@@ -148,5 +141,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
return await connection.QueryAsync<WindHistoryGrouped>(query, new { Start = start, End = end, BucketMinutes = bucketMinutes });
}
}
}

View File

@@ -1,8 +1,8 @@
FROM mcr.microsoft.com/dotnet/aspnet:7.0-alpine AS base
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS base
WORKDIR /app
EXPOSE 80
EXPOSE 8080
FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
WORKDIR /src
COPY ["./Service.csproj", "./"]
RUN dotnet restore "Service.csproj"
@@ -11,7 +11,8 @@ WORKDIR "/src"
RUN dotnet publish "Service.csproj" -c Release -o /app
FROM base AS final
RUN apk add --no-cache tzdata
WORKDIR /app
COPY --from=build /app .
RUN apk add --no-cache icu-libs tzdata
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
ENTRYPOINT ["dotnet", "ChrisKaczor.HomeMonitor.Weather.Service.dll"]

View File

@@ -1,10 +1,10 @@
using System;
using DecimalMath;
using DecimalMath;
using System;
namespace ChrisKaczor.HomeMonitor.Weather.Service
namespace ChrisKaczor.HomeMonitor.Weather.Service;
public static class Extensions
{
public static class Extensions
{
public static bool IsBetween<T>(this T item, T start, T end) where T : IComparable
{
return item.CompareTo(start) >= 0 && item.CompareTo(end) <= 0;
@@ -14,5 +14,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service
{
return decimal.Truncate(value * DecimalEx.Pow(10, decimalPlaces)) / DecimalEx.Pow(10, decimalPlaces);
}
}
}

View File

@@ -13,28 +13,19 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ChrisKaczor.HomeMonitor.Weather.Service
{
[UsedImplicitly]
public class MessageHandler : IHostedService
{
private readonly IConfiguration _configuration;
private readonly Database _database;
namespace ChrisKaczor.HomeMonitor.Weather.Service;
[UsedImplicitly]
public class MessageHandler(IConfiguration configuration, Database database) : IHostedService
{
private IConnection _queueConnection;
private IModel _queueModel;
private HubConnection _hubConnection;
public MessageHandler(IConfiguration configuration, Database database)
{
_configuration = configuration;
_database = database;
}
public Task StartAsync(CancellationToken cancellationToken)
{
var host = _configuration["Weather:Queue:Host"];
var host = configuration["Weather:Queue:Host"];
if (string.IsNullOrEmpty(host))
return Task.CompletedTask;
@@ -44,22 +35,22 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service
var factory = new ConnectionFactory
{
HostName = host,
UserName = _configuration["Weather:Queue:User"],
Password = _configuration["Weather:Queue:Password"]
UserName = configuration["Weather:Queue:User"],
Password = configuration["Weather:Queue:Password"]
};
_queueConnection = factory.CreateConnection();
_queueModel = _queueConnection.CreateModel();
_queueModel.QueueDeclare(_configuration["Weather:Queue:Name"], true, false, false, null);
_queueModel.QueueDeclare(configuration["Weather:Queue:Name"], true, false, false, null);
var consumer = new EventingBasicConsumer(_queueModel);
consumer.Received += DeviceEventHandler;
_queueModel.BasicConsume(_configuration["Weather:Queue:Name"], true, consumer);
_queueModel.BasicConsume(configuration["Weather:Queue:Name"], true, consumer);
if (!string.IsNullOrEmpty(_configuration["Hub:Weather"]))
_hubConnection = new HubConnectionBuilder().WithUrl(_configuration["Hub:Weather"]).Build();
if (!string.IsNullOrEmpty(configuration["Hub:Weather"]))
_hubConnection = new HubConnectionBuilder().WithUrl(configuration["Hub:Weather"]).Build();
return Task.CompletedTask;
}
@@ -94,12 +85,12 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service
return;
}
_database.StoreWeatherData(weatherMessage);
database.StoreWeatherData(weatherMessage);
if (_hubConnection == null)
return;
var weatherUpdate = new WeatherUpdate(weatherMessage, _database);
var weatherUpdate = new WeatherUpdate(weatherMessage, database);
try
{
@@ -127,5 +118,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service
{
Console.WriteLine(message);
}
}
}

View File

@@ -1,25 +1,17 @@
using ChrisKaczor.HomeMonitor.Weather.Models;
using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
using ChrisKaczor.HomeMonitor.Weather.Models;
using JetBrains.Annotations;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models;
[PublicAPI]
public class ReadingAggregate(IEnumerable<WeatherReading> readings, Func<WeatherReading, decimal> selector, int decimalPlaces)
{
[PublicAPI]
public class ReadingAggregate
{
public decimal Min { get; set; }
public decimal Min { get; set; } = readings.Min(selector);
public decimal Max { get; set; }
public decimal Max { get; set; } = readings.Max(selector);
public decimal Average { get; set; }
public ReadingAggregate(IEnumerable<WeatherReading> readings, Func<WeatherReading, decimal> selector, int decimalPlaces)
{
Min = readings.Min(selector);
Max = readings.Max(selector);
Average = readings.Average(selector).Truncate(decimalPlaces);
}
}
public decimal Average { get; set; } = readings.Average(selector).Truncate(decimalPlaces);
}

View File

@@ -1,14 +1,14 @@
using ChrisKaczor.HomeMonitor.Weather.Models;
using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
using ChrisKaczor.HomeMonitor.Weather.Models;
using JetBrains.Annotations;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models;
[PublicAPI]
public class WeatherAggregate
{
[PublicAPI]
public class WeatherAggregate
{
public ReadingAggregate Humidity { get; set; }
public ReadingAggregate Temperature { get; set; }
@@ -45,5 +45,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
RainTotal = readings.Sum(r => r.Rain);
}
}
}

View File

@@ -1,11 +1,11 @@
using JetBrains.Annotations;
using System;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models;
[PublicAPI]
public class WeatherReadingGrouped
{
[PublicAPI]
public class WeatherReadingGrouped
{
public DateTimeOffset Bucket { get; set; }
public decimal AverageHumidity { get; set; }
@@ -17,5 +17,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
public decimal AverageLightLevel { get; set; }
public decimal RainTotal { get; set; }
}
}

View File

@@ -5,11 +5,11 @@ using JetBrains.Annotations;
using MathNet.Numerics;
using System.Linq;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models;
[PublicAPI]
public class WeatherUpdate : WeatherUpdateBase
{
[PublicAPI]
public class WeatherUpdate : WeatherUpdateBase
{
private readonly Database _database;
public WeatherUpdate(WeatherMessage weatherMessage, Database database)
@@ -97,5 +97,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
.00000199m * temperature * temperature * humidity * humidity;
}
}
}
}

View File

@@ -1,13 +1,12 @@
using JetBrains.Annotations;
using System;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models;
[PublicAPI]
public class WeatherValue
{
[PublicAPI]
public class WeatherValue
{
public DateTimeOffset Timestamp { get; set; }
public decimal Value { get; set; }
}
}

View File

@@ -1,13 +1,12 @@
using JetBrains.Annotations;
using System;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models;
[PublicAPI]
public class WeatherValueGrouped
{
[PublicAPI]
public class WeatherValueGrouped
{
public DateTimeOffset Bucket { get; set; }
public decimal AverageValue { get; set; }
}
}

View File

@@ -1,10 +1,10 @@
using JetBrains.Annotations;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models;
[PublicAPI]
public enum WeatherValueType
{
[PublicAPI]
public enum WeatherValueType
{
WindSpeed,
Humidity,
HumidityTemperature,
@@ -14,5 +14,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
LightLevel,
Altitude,
SatelliteCount
}
}

View File

@@ -1,11 +1,11 @@
using JetBrains.Annotations;
using System;
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
namespace ChrisKaczor.HomeMonitor.Weather.Service.Models;
[PublicAPI]
public class WindHistoryGrouped
{
[PublicAPI]
public class WindHistoryGrouped
{
public DateTimeOffset Bucket { get; set; }
public decimal MinimumSpeed { get; set; }
@@ -15,5 +15,4 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
public decimal MaximumSpeed { get; set; }
public decimal AverageDirection { get; set; }
}
}

View File

@@ -1,19 +1,125 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using ChrisKaczor.HomeMonitor.Weather.Service.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System;
using System.IO.Compression;
using System.Reflection;
using System.Text.Json.Serialization;
namespace ChrisKaczor.HomeMonitor.Weather.Service
namespace ChrisKaczor.HomeMonitor.Weather.Service;
public static class Program
{
public static class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
var builder = WebApplication.CreateBuilder(args);
private static IWebHostBuilder CreateWebHostBuilder(string[] args)
builder.Configuration.AddEnvironmentVariables();
builder.Services.AddControllers();
// ---
var openTelemetry = builder.Services.AddOpenTelemetry();
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var serviceName = Assembly.GetExecutingAssembly().GetName().Name;
openTelemetry.ConfigureResource(resource => resource.AddService(serviceName!));
openTelemetry.WithMetrics(meterProviderBuilder => meterProviderBuilder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddProcessInstrumentation()
.AddMeter("Microsoft.AspNetCore.Hosting")
.AddMeter("Microsoft.AspNetCore.Server.Kestrel"));
openTelemetry.WithTracing(tracerProviderBuilder =>
{
return WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration((_, config) => config.AddEnvironmentVariables()).UseStartup<Startup>();
tracerProviderBuilder.AddAspNetCoreInstrumentation(instrumentationOptions => instrumentationOptions.RecordException = true);
tracerProviderBuilder.AddHttpClientInstrumentation(instrumentationOptions => instrumentationOptions.RecordException = true);
tracerProviderBuilder.AddSqlClientInstrumentation(o =>
{
o.RecordException = true;
o.SetDbStatementForText = true;
});
tracerProviderBuilder.AddSource(nameof(MessageHandler));
tracerProviderBuilder.SetErrorStatusOnException();
tracerProviderBuilder.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(builder.Configuration["Telemetry:Endpoint"]!);
exporterOptions.Protocol = OtlpExportProtocol.Grpc;
});
});
builder.Services.AddLogging(loggingBuilder =>
{
loggingBuilder.SetMinimumLevel(LogLevel.Information);
loggingBuilder.AddOpenTelemetry(options =>
{
options.AddOtlpExporter(exporterOptions =>
{
exporterOptions.Endpoint = new Uri(builder.Configuration["Telemetry:Endpoint"]!);
exporterOptions.Protocol = OtlpExportProtocol.Grpc;
});
}
);
});
builder.Services.AddTransient<Database>();
builder.Services.AddHostedService<MessageHandler>();
builder.Services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Optimal);
builder.Services.AddResponseCompression(options =>
{
options.Providers.Add<GzipCompressionProvider>();
options.EnableForHttps = true;
});
builder.Services.AddCors(o => o.AddPolicy("CorsPolicy", corsPolicyBuilder => corsPolicyBuilder.AllowAnyMethod().AllowAnyHeader().AllowCredentials().WithOrigins("http://localhost:4200")));
builder.Services.AddMvc().AddJsonOptions(configure =>
{
configure.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
// ---
var app = builder.Build();
if (builder.Environment.IsDevelopment())
app.UseDeveloperExceptionPage();
var database = app.Services.GetRequiredService<Database>();
database.EnsureDatabase();
app.UseCors("CorsPolicy");
app.UseResponseCompression();
app.UseRouting();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}

View File

@@ -2,21 +2,20 @@
using System.IO;
using System.Reflection;
namespace ChrisKaczor.HomeMonitor.Weather.Service
namespace ChrisKaczor.HomeMonitor.Weather.Service;
public static class ResourceReader
{
public static class ResourceReader
{
public static string GetString(string resourceName)
{
var assembly = Assembly.GetExecutingAssembly();
using var stream = assembly.GetManifestResourceStream(resourceName);
if (stream == null)
throw new Exception($"Resource {resourceName} not found in {assembly.FullName}. Valid resources are: {string.Join(", ", assembly.GetManifestResourceNames())}.");
using var reader = new StreamReader(stream);
return reader.ReadToEnd();
}
}
}

View File

@@ -1,15 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<AssemblyName>ChrisKaczor.HomeMonitor.Weather.Service</AssemblyName>
<RootNamespace>ChrisKaczor.HomeMonitor.Weather.Service</RootNamespace>
<CodeAnalysisRuleSet>../../ChrisKaczor.ruleset</CodeAnalysisRuleSet>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
</PropertyGroup>
<ItemGroup>
<None Remove="C:\Users\chris\.nuget\packages\opentelemetry.autoinstrumentation\1.3.0\contentFiles\any\any\instrument.cmd" />
<None Remove="C:\Users\chris\.nuget\packages\opentelemetry.autoinstrumentation\1.3.0\contentFiles\any\any\instrument.sh" />
<None Remove="Data\Resources\CreateReading.sql" />
<None Remove="Data\Resources\GetReadingHistory.sql" />
<None Remove="Data\Resources\GetReadingHistoryGrouped.sql" />
@@ -35,12 +36,17 @@
<ItemGroup>
<PackageReference Include="ChrisKaczor.HomeMonitor.Weather.Models" Version="1.1.7" />
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="Dapper" Version="2.1.28" />
<PackageReference Include="DecimalMath.DecimalEx" Version="1.0.2" />
<PackageReference Include="MathNet.Numerics" Version="4.15.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="5.0.6" />
<PackageReference Include="RabbitMQ.Client" Version="6.2.4" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
<PackageReference Include="OpenTelemetry.AutoInstrumentation" Version="1.3.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.0" />
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.1" />
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
</ItemGroup>
</Project>

View File

@@ -1,54 +0,0 @@
using ChrisKaczor.HomeMonitor.Weather.Service.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.IO.Compression;
using System.Text.Json.Serialization;
namespace ChrisKaczor.HomeMonitor.Weather.Service
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<Database>();
services.AddHostedService<MessageHandler>();
services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Optimal);
services.AddResponseCompression(options =>
{
options.Providers.Add<GzipCompressionProvider>();
options.EnableForHttps = true;
});
services.AddCors(o => o.AddPolicy("CorsPolicy", builder => builder.AllowAnyMethod().AllowAnyHeader().AllowCredentials().WithOrigins("http://localhost:4200")));
services.AddMvc().AddJsonOptions(configure =>
{
configure.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
}
public void Configure(IApplicationBuilder applicationBuilder, IWebHostEnvironment environment)
{
if (environment.IsDevelopment())
applicationBuilder.UseDeveloperExceptionPage();
var database = applicationBuilder.ApplicationServices.GetService<Database>();
database.EnsureDatabase();
applicationBuilder.UseCors("CorsPolicy");
applicationBuilder.UseResponseCompression();
applicationBuilder.UseRouting();
applicationBuilder.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute());
}
}
}

View File

@@ -2,7 +2,6 @@
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}

View File

@@ -1,12 +1,14 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
"Default": "Warning",
"Microsoft": "Information"
}
},
"Weather": {
"Database": {
"Name": "Weather"
"Name": "Weather",
"TrustServerCertificate": true
},
"Queue": {
"Name": "weather"
@@ -14,5 +16,8 @@
},
"Hub": {
"Weather": "http://hub-server/weather"
},
"Telemetry": {
"Endpoint": "http://signoz-otel-collector.platform:4317/"
}
}

View File

@@ -132,7 +132,7 @@ metadata:
spec:
ports:
- name: client
port: 80
port: 8080
selector:
app: weather-service
type: ClusterIP
@@ -156,7 +156,7 @@ spec:
- kind: Service
name: weather-service
namespace: home-monitor
port: 80
port: 8080
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware