From e653b190856de49349fea8b50a9a01b30b1eb60b Mon Sep 17 00:00:00 2001 From: geffzhang Date: Fri, 28 Jun 2024 13:51:35 +0800 Subject: [PATCH] feat : add aspire support --- BotSharp.sln | 22 +++- src/BotSharp.AppHost/BotSharp.AppHost.csproj | 22 ++++ src/BotSharp.AppHost/Program.cs | 13 ++ .../Properties/launchSettings.json | 29 +++++ .../appsettings.Development.json | 8 ++ src/BotSharp.AppHost/appsettings.json | 9 ++ .../BotSharp.ServiceDefaults.csproj | 22 ++++ src/BotSharp.ServiceDefaults/Extensions.cs | 119 ++++++++++++++++++ .../BotSharp.Plugin.MongoStorage.csproj | 1 + .../BotSharp.Plugin.Qdrant.csproj | 1 + src/WebStarter/Program.cs | 3 + src/WebStarter/WebStarter.csproj | 2 + 12 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 src/BotSharp.AppHost/BotSharp.AppHost.csproj create mode 100644 src/BotSharp.AppHost/Program.cs create mode 100644 src/BotSharp.AppHost/Properties/launchSettings.json create mode 100644 src/BotSharp.AppHost/appsettings.Development.json create mode 100644 src/BotSharp.AppHost/appsettings.json create mode 100644 src/BotSharp.ServiceDefaults/BotSharp.ServiceDefaults.csproj create mode 100644 src/BotSharp.ServiceDefaults/Extensions.cs diff --git a/BotSharp.sln b/BotSharp.sln index 645b6060..9725dec4 100644 --- a/BotSharp.sln +++ b/BotSharp.sln @@ -85,10 +85,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.Dashboard", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.SparkDesk", "src\Plugins\BotSharp.Plugin.SparkDesk\BotSharp.Plugin.SparkDesk.csproj", "{289E25C8-63F1-4D52-9909-207724DB40CB}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.MetaGLM", "src\Plugins\BotSharp.Plugin.MetaGLM\BotSharp.Plugin.MetaGLM.csproj", "{CCF745F2-0C95-4ED0-983B-507C528B39EA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.MetaGLM", "src\Plugins\BotSharp.Plugin.MetaGLM\BotSharp.Plugin.MetaGLM.csproj", "{CCF745F2-0C95-4ED0-983B-507C528B39EA}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.AnthropicAI", "src\Plugins\BotSharp.Plugin.AnthropicAI\BotSharp.Plugin.AnthropicAI.csproj", "{806A0B0E-FEFF-420E-B5B2-C9FCBF890A8C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.ServiceDefaults", "src\BotSharp.ServiceDefaults\BotSharp.ServiceDefaults.csproj", "{6406DC61-0F30-42E8-A1DB-B38CDF454273}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.AppHost", "src\BotSharp.AppHost\BotSharp.AppHost.csproj", "{E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -353,6 +357,22 @@ Global {806A0B0E-FEFF-420E-B5B2-C9FCBF890A8C}.Release|Any CPU.Build.0 = Release|Any CPU {806A0B0E-FEFF-420E-B5B2-C9FCBF890A8C}.Release|x64.ActiveCfg = Release|Any CPU {806A0B0E-FEFF-420E-B5B2-C9FCBF890A8C}.Release|x64.Build.0 = Release|Any CPU + {6406DC61-0F30-42E8-A1DB-B38CDF454273}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6406DC61-0F30-42E8-A1DB-B38CDF454273}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6406DC61-0F30-42E8-A1DB-B38CDF454273}.Debug|x64.ActiveCfg = Debug|Any CPU + {6406DC61-0F30-42E8-A1DB-B38CDF454273}.Debug|x64.Build.0 = Debug|Any CPU + {6406DC61-0F30-42E8-A1DB-B38CDF454273}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6406DC61-0F30-42E8-A1DB-B38CDF454273}.Release|Any CPU.Build.0 = Release|Any CPU + {6406DC61-0F30-42E8-A1DB-B38CDF454273}.Release|x64.ActiveCfg = Release|Any CPU + {6406DC61-0F30-42E8-A1DB-B38CDF454273}.Release|x64.Build.0 = Release|Any CPU + {E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Debug|x64.ActiveCfg = Debug|Any CPU + {E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Debug|x64.Build.0 = Debug|Any CPU + {E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Release|Any CPU.Build.0 = Release|Any CPU + {E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Release|x64.ActiveCfg = Release|Any CPU + {E04FBBEF-744E-4EF3-B634-42AD9F8B68B1}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/BotSharp.AppHost/BotSharp.AppHost.csproj b/src/BotSharp.AppHost/BotSharp.AppHost.csproj new file mode 100644 index 00000000..6e8a2ae3 --- /dev/null +++ b/src/BotSharp.AppHost/BotSharp.AppHost.csproj @@ -0,0 +1,22 @@ + + + + Exe + net8.0 + enable + enable + true + e4bd9862-7edd-45f0-9a89-b8e3b3339c71 + + + + + + + + + + + + + diff --git a/src/BotSharp.AppHost/Program.cs b/src/BotSharp.AppHost/Program.cs new file mode 100644 index 00000000..9c371bf4 --- /dev/null +++ b/src/BotSharp.AppHost/Program.cs @@ -0,0 +1,13 @@ +var builder = DistributedApplication.CreateBuilder(args); + +var apiService = builder.AddProject("apiservice") + .WithExternalHttpEndpoints(); + +builder.AddNpmApp("BotSharpUI", "../../../BotSharp-UI") + .WithReference(apiService) + .WithHttpEndpoint(env: "PORT") + .WithExternalHttpEndpoints() + .PublishAsDockerFile(); + +builder.Build().Run(); + diff --git a/src/BotSharp.AppHost/Properties/launchSettings.json b/src/BotSharp.AppHost/Properties/launchSettings.json new file mode 100644 index 00000000..c315179c --- /dev/null +++ b/src/BotSharp.AppHost/Properties/launchSettings.json @@ -0,0 +1,29 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17248;http://localhost:15140", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21247", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22140" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15140", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19185", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20069" + } + } + } +} diff --git a/src/BotSharp.AppHost/appsettings.Development.json b/src/BotSharp.AppHost/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/src/BotSharp.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/src/BotSharp.AppHost/appsettings.json b/src/BotSharp.AppHost/appsettings.json new file mode 100644 index 00000000..31c092aa --- /dev/null +++ b/src/BotSharp.AppHost/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + } +} diff --git a/src/BotSharp.ServiceDefaults/BotSharp.ServiceDefaults.csproj b/src/BotSharp.ServiceDefaults/BotSharp.ServiceDefaults.csproj new file mode 100644 index 00000000..51ceffa1 --- /dev/null +++ b/src/BotSharp.ServiceDefaults/BotSharp.ServiceDefaults.csproj @@ -0,0 +1,22 @@ + + + + net8.0 + enable + enable + true + + + + + + + + + + + + + + + diff --git a/src/BotSharp.ServiceDefaults/Extensions.cs b/src/BotSharp.ServiceDefaults/Extensions.cs new file mode 100644 index 00000000..c404e894 --- /dev/null +++ b/src/BotSharp.ServiceDefaults/Extensions.cs @@ -0,0 +1,119 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.ServiceDiscovery; +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 IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder) + { + 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 IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder) + { + builder.Logging.AddOpenTelemetry(logging => + { + logging.IncludeFormattedMessage = true; + logging.IncludeScopes = true; + }); + + builder.Services.AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddRuntimeInstrumentation(); + }) + .WithTracing(tracing => + { + tracing.AddAspNetCoreInstrumentation() + // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) + //.AddGrpcClientInstrumentation() + .AddHttpClientInstrumentation(); + }); + + builder.AddOpenTelemetryExporters(); + + return builder; + } + + private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder) + { + 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 IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder) + { + 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/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj b/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj index f4f1860e..350ef58b 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj @@ -11,6 +11,7 @@ + diff --git a/src/Plugins/BotSharp.Plugin.Qdrant/BotSharp.Plugin.Qdrant.csproj b/src/Plugins/BotSharp.Plugin.Qdrant/BotSharp.Plugin.Qdrant.csproj index 9c5aec5b..817c90c5 100644 --- a/src/Plugins/BotSharp.Plugin.Qdrant/BotSharp.Plugin.Qdrant.csproj +++ b/src/Plugins/BotSharp.Plugin.Qdrant/BotSharp.Plugin.Qdrant.csproj @@ -11,6 +11,7 @@ + diff --git a/src/WebStarter/Program.cs b/src/WebStarter/Program.cs index f30882a4..5924ab30 100644 --- a/src/WebStarter/Program.cs +++ b/src/WebStarter/Program.cs @@ -36,6 +36,9 @@ string[] allowedOrigins = builder.Configuration.GetSection("AllowedOrigins").Get }).AddBotSharpOpenAPI(builder.Configuration, allowedOrigins, builder.Environment, true) .AddBotSharpLogger(builder.Configuration); +// Add service defaults & Aspire components. +builder.AddServiceDefaults(); + // Add SignalR for WebSocket builder.Services.AddSignalR(); diff --git a/src/WebStarter/WebStarter.csproj b/src/WebStarter/WebStarter.csproj index c6912c14..8a1f2496 100644 --- a/src/WebStarter/WebStarter.csproj +++ b/src/WebStarter/WebStarter.csproj @@ -27,6 +27,7 @@ + @@ -61,6 +62,7 @@ +