From 308a0d475cd8898dd623aaedfe6011afe525f587 Mon Sep 17 00:00:00 2001 From: Marcio Date: Sat, 7 Dec 2024 19:41:12 -0300 Subject: [PATCH] Add Aspire --- UptimeKumaRemoteProbe.sln | 12 ++ src/UKRP.AppHost/Program.cs | 5 + .../Properties/launchSettings.json | 30 +++++ src/UKRP.AppHost/UKRP.AppHost.csproj | 25 ++++ src/UKRP.AppHost/appsettings.Development.json | 8 ++ src/UKRP.AppHost/appsettings.json | 9 ++ src/UKRP.ServiceDefaults/Extensions.cs | 118 ++++++++++++++++++ .../UKRP.ServiceDefaults.csproj | 25 ++++ src/UptimeKumaRemoteProbe/Dockerfile | 2 +- .../Dockerfile.multiarch | 2 +- src/UptimeKumaRemoteProbe/Program.cs | 2 + .../UptimeKumaRemoteProbe.csproj | 6 +- src/UptimeKumaRemoteProbe/appsettings.json | 23 +--- 13 files changed, 245 insertions(+), 22 deletions(-) create mode 100644 src/UKRP.AppHost/Program.cs create mode 100644 src/UKRP.AppHost/Properties/launchSettings.json create mode 100644 src/UKRP.AppHost/UKRP.AppHost.csproj create mode 100644 src/UKRP.AppHost/appsettings.Development.json create mode 100644 src/UKRP.AppHost/appsettings.json create mode 100644 src/UKRP.ServiceDefaults/Extensions.cs create mode 100644 src/UKRP.ServiceDefaults/UKRP.ServiceDefaults.csproj diff --git a/UptimeKumaRemoteProbe.sln b/UptimeKumaRemoteProbe.sln index bb4ca28..a826b5e 100644 --- a/UptimeKumaRemoteProbe.sln +++ b/UptimeKumaRemoteProbe.sln @@ -15,6 +15,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UptimeKumaRemoteProbe", "src\UptimeKumaRemoteProbe\UptimeKumaRemoteProbe.csproj", "{291D4A53-0E98-4E7B-861D-C43973C0B5D1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UKRP.AppHost", "src\UKRP.AppHost\UKRP.AppHost.csproj", "{07875E62-655F-44E9-8781-1F24DB832BA1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UKRP.ServiceDefaults", "src\UKRP.ServiceDefaults\UKRP.ServiceDefaults.csproj", "{03B064F8-A9A3-4380-9F77-2D025C51185E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -25,6 +29,14 @@ Global {291D4A53-0E98-4E7B-861D-C43973C0B5D1}.Debug|Any CPU.Build.0 = Debug|Any CPU {291D4A53-0E98-4E7B-861D-C43973C0B5D1}.Release|Any CPU.ActiveCfg = Release|Any CPU {291D4A53-0E98-4E7B-861D-C43973C0B5D1}.Release|Any CPU.Build.0 = Release|Any CPU + {07875E62-655F-44E9-8781-1F24DB832BA1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {07875E62-655F-44E9-8781-1F24DB832BA1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {07875E62-655F-44E9-8781-1F24DB832BA1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {07875E62-655F-44E9-8781-1F24DB832BA1}.Release|Any CPU.Build.0 = Release|Any CPU + {03B064F8-A9A3-4380-9F77-2D025C51185E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {03B064F8-A9A3-4380-9F77-2D025C51185E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {03B064F8-A9A3-4380-9F77-2D025C51185E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {03B064F8-A9A3-4380-9F77-2D025C51185E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/UKRP.AppHost/Program.cs b/src/UKRP.AppHost/Program.cs new file mode 100644 index 0000000..5acb0b9 --- /dev/null +++ b/src/UKRP.AppHost/Program.cs @@ -0,0 +1,5 @@ +var builder = DistributedApplication.CreateBuilder(args); + +builder.AddProject("uptimekumaremoteprobe"); + +builder.Build().Run(); diff --git a/src/UKRP.AppHost/Properties/launchSettings.json b/src/UKRP.AppHost/Properties/launchSettings.json new file mode 100644 index 0000000..9759936 --- /dev/null +++ b/src/UKRP.AppHost/Properties/launchSettings.json @@ -0,0 +1,30 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17047;http://localhost:15113", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21296", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22162" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15113", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19132", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20111", + "ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true" + } + } + } +} diff --git a/src/UKRP.AppHost/UKRP.AppHost.csproj b/src/UKRP.AppHost/UKRP.AppHost.csproj new file mode 100644 index 0000000..4245a3d --- /dev/null +++ b/src/UKRP.AppHost/UKRP.AppHost.csproj @@ -0,0 +1,25 @@ + + + + + + Exe + net8.0 + enable + enable + true + 9b25896d-67a2-4379-9411-47b0e205791d + $(PackageVersion) + $(PackageVersion) + 1.0.0.0 + + + + + + + + + + + diff --git a/src/UKRP.AppHost/appsettings.Development.json b/src/UKRP.AppHost/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/src/UKRP.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/src/UKRP.AppHost/appsettings.json b/src/UKRP.AppHost/appsettings.json new file mode 100644 index 0000000..31c092a --- /dev/null +++ b/src/UKRP.AppHost/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + } +} diff --git a/src/UKRP.ServiceDefaults/Extensions.cs b/src/UKRP.ServiceDefaults/Extensions.cs new file mode 100644 index 0000000..8cf2bbd --- /dev/null +++ b/src/UKRP.ServiceDefaults/Extensions.cs @@ -0,0 +1,118 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Logging; +using OpenTelemetry; +using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; + +namespace Microsoft.Extensions.Hosting; + +// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry. +// This project should be referenced by each service project in your solution. +// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults +public static class Extensions +{ + public static TBuilder AddServiceDefaults(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.ConfigureOpenTelemetry(); + + builder.AddDefaultHealthChecks(); + + builder.Services.AddServiceDiscovery(); + + builder.Services.ConfigureHttpClientDefaults(http => + { + // Turn on resilience by default + http.AddStandardResilienceHandler(); + + // Turn on service discovery by default + http.AddServiceDiscovery(); + }); + + // Uncomment the following to restrict the allowed schemes for service discovery. + // builder.Services.Configure(options => + // { + // options.AllowedSchemes = ["https"]; + // }); + + return builder; + } + + public static TBuilder ConfigureOpenTelemetry(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Logging.AddOpenTelemetry(logging => + { + logging.IncludeFormattedMessage = true; + logging.IncludeScopes = true; + }); + + builder.Services.AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddRuntimeInstrumentation(); + }) + .WithTracing(tracing => + { + tracing.AddSource(builder.Environment.ApplicationName) + .AddAspNetCoreInstrumentation() + // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) + //.AddGrpcClientInstrumentation() + .AddHttpClientInstrumentation(); + }); + + builder.AddOpenTelemetryExporters(); + + return builder; + } + + private static TBuilder AddOpenTelemetryExporters(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); + + if (useOtlpExporter) + { + builder.Services.AddOpenTelemetry().UseOtlpExporter(); + } + + // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) + //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) + //{ + // builder.Services.AddOpenTelemetry() + // .UseAzureMonitor(); + //} + + return builder; + } + + public static TBuilder AddDefaultHealthChecks(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Services.AddHealthChecks() + // Add a default liveness check to ensure app is responsive + .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); + + return builder; + } + + public static WebApplication MapDefaultEndpoints(this WebApplication app) + { + // Adding health checks endpoints to applications in non-development environments has security implications. + // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. + if (app.Environment.IsDevelopment()) + { + // All health checks must pass for app to be considered ready to accept traffic after starting + app.MapHealthChecks("/health"); + + // Only health checks tagged with the "live" tag must pass for app to be considered alive + app.MapHealthChecks("/alive", new HealthCheckOptions + { + Predicate = r => r.Tags.Contains("live") + }); + } + + return app; + } +} diff --git a/src/UKRP.ServiceDefaults/UKRP.ServiceDefaults.csproj b/src/UKRP.ServiceDefaults/UKRP.ServiceDefaults.csproj new file mode 100644 index 0000000..1e71425 --- /dev/null +++ b/src/UKRP.ServiceDefaults/UKRP.ServiceDefaults.csproj @@ -0,0 +1,25 @@ + + + + net8.0 + enable + enable + true + $(PackageVersion) + $(PackageVersion) + 1.0.0.0 + + + + + + + + + + + + + + + diff --git a/src/UptimeKumaRemoteProbe/Dockerfile b/src/UptimeKumaRemoteProbe/Dockerfile index 4cddd5c..3add1c4 100644 --- a/src/UptimeKumaRemoteProbe/Dockerfile +++ b/src/UptimeKumaRemoteProbe/Dockerfile @@ -1,6 +1,6 @@ #See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. -FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base WORKDIR /app FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build diff --git a/src/UptimeKumaRemoteProbe/Dockerfile.multiarch b/src/UptimeKumaRemoteProbe/Dockerfile.multiarch index 2becd14..c20cb8f 100644 --- a/src/UptimeKumaRemoteProbe/Dockerfile.multiarch +++ b/src/UptimeKumaRemoteProbe/Dockerfile.multiarch @@ -15,7 +15,7 @@ RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \ fi \ && dotnet publish "src/UptimeKumaRemoteProbe/UptimeKumaRemoteProbe.csproj" -c Release -o /app/publish -r $RID --self-contained false -FROM mcr.microsoft.com/dotnet/runtime:8.0 AS final +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final WORKDIR /app COPY --from=build /app/publish . diff --git a/src/UptimeKumaRemoteProbe/Program.cs b/src/UptimeKumaRemoteProbe/Program.cs index 133757c..72d984a 100644 --- a/src/UptimeKumaRemoteProbe/Program.cs +++ b/src/UptimeKumaRemoteProbe/Program.cs @@ -1,4 +1,6 @@ var builder = Host.CreateApplicationBuilder(args); + +builder.AddServiceDefaults(); builder.Services.AddHttpClient("Default"); builder.Services.AddHttpClient("IgnoreSSL") .ConfigurePrimaryHttpMessageHandler(() => diff --git a/src/UptimeKumaRemoteProbe/UptimeKumaRemoteProbe.csproj b/src/UptimeKumaRemoteProbe/UptimeKumaRemoteProbe.csproj index fe35208..ff6d2ad 100644 --- a/src/UptimeKumaRemoteProbe/UptimeKumaRemoteProbe.csproj +++ b/src/UptimeKumaRemoteProbe/UptimeKumaRemoteProbe.csproj @@ -9,7 +9,7 @@ uptime-kuma.ico $(PackageVersion) $(PackageVersion) - 6.0.1.1 + 6.0.1.2 Zimbres.Com https://github.com/zimbres/UptimeKumaRemoteProbe https://github.com/zimbres/UptimeKumaRemoteProbe @@ -45,4 +45,8 @@ + + + + diff --git a/src/UptimeKumaRemoteProbe/appsettings.json b/src/UptimeKumaRemoteProbe/appsettings.json index 146fa3e..e57b452 100644 --- a/src/UptimeKumaRemoteProbe/appsettings.json +++ b/src/UptimeKumaRemoteProbe/appsettings.json @@ -3,28 +3,13 @@ "LogLevel": { "Default": "Information", "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - }, - "Console": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - }, - "FormatterName": "json", - "FormatterOptions": { - "SingleLine": true, - "IncludeScopes": false, - "TimestampFormat": "dd/MM/yyyy HH:mm:ss", - "UseUtcTimestamp": false, - "JsonWriterOptions": { - "Indented": false - } - } + "Microsoft.Hosting.Lifetime": "Information", + "System.Net.Http.HttpClient": "Warning", + "Polly": "Warning" } }, "Configurations": { - "Url": "http://localhost:3001/", + "Url": "http://192.168.100.190:3001/", "Username": "admin", "Password": "Admin123", "ProbeName": "Home",