diff --git a/src/Altinn.App.Api/Extensions/ServiceCollectionExtensions.cs b/src/Altinn.App.Api/Extensions/ServiceCollectionExtensions.cs index 0c7512386..a7417917c 100644 --- a/src/Altinn.App.Api/Extensions/ServiceCollectionExtensions.cs +++ b/src/Altinn.App.Api/Extensions/ServiceCollectionExtensions.cs @@ -1,4 +1,6 @@ -using System; +#nullable enable +using System; +using Altinn.App.Api.Configuration; using Altinn.App.Api.Controllers; using Altinn.App.Api.Infrastructure.Filters; using Altinn.App.Api.Infrastructure.Health; @@ -18,6 +20,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.FeatureManagement; using Microsoft.IdentityModel.Tokens; +using Prometheus; namespace Altinn.App.Api.Extensions { @@ -74,6 +77,7 @@ public static void AddAltinnAppServices(this IServiceCollection services, IConfi services.AddHttpClient(); services.AddSingleton(); + services.AddMetricsServer(config); } private static void AddApplicationInsights(IServiceCollection services, IConfiguration config, IWebHostEnvironment env) @@ -144,5 +148,15 @@ private static void AddAntiforgery(IServiceCollection services) services.TryAddSingleton(); } + + private static void AddMetricsServer(this IServiceCollection services, IConfiguration config) + { + var metricsSettings = config.GetSection("MetricsSettings").Get() ?? new MetricsSettings(); + if (metricsSettings.Enabled) + { + ushort port = metricsSettings.Port; + services.AddMetricServer(options => { options.Port = port; }); + } + } } -} \ No newline at end of file +} diff --git a/src/Altinn.App.Api/Extensions/WebApplicationBuilderExtensions.cs b/src/Altinn.App.Api/Extensions/WebApplicationBuilderExtensions.cs index 400dd87a8..d6fb4613d 100644 --- a/src/Altinn.App.Api/Extensions/WebApplicationBuilderExtensions.cs +++ b/src/Altinn.App.Api/Extensions/WebApplicationBuilderExtensions.cs @@ -1,4 +1,6 @@ +#nullable enable using System.Reflection; +using Altinn.App.Api.Configuration; using Altinn.App.Api.Helpers; using Prometheus; @@ -16,20 +18,15 @@ public static class WebApplicationBuilderExtensions /// public static IApplicationBuilder UseAltinnAppCommonConfiguration(this IApplicationBuilder app) { + var appId = StartupHelper.GetApplicationId(); if (app is WebApplication webApp && webApp.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); + webApp.UseAltinnPrometheus(appId); } app.UseHttpMetrics(); app.UseMetricServer(); - var appId = StartupHelper.GetApplicationId(); - var version = Assembly.GetExecutingAssembly().GetName().Version?.ToString(); - Metrics.DefaultRegistry.SetStaticLabels(new Dictionary() - { - { "application_id", appId }, - { "nuget_package_version", version } - }); app.UseDefaultSecurityHeaders(); app.UseRouting(); app.UseStaticFiles('/' + appId); @@ -43,4 +40,21 @@ public static IApplicationBuilder UseAltinnAppCommonConfiguration(this IApplicat app.UseHealthChecks("/health"); return app; } -} \ No newline at end of file + + private static void UseAltinnPrometheus(this WebApplication webApp, string appId) + { + var metricsSettings = webApp.Configuration.GetSection("MetricsSettings")?.Get() ?? new MetricsSettings(); + if (!metricsSettings.Enabled) + { + return; + } + + webApp.UseHttpMetrics(); + var version = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown"; + Metrics.DefaultRegistry.SetStaticLabels(new Dictionary() + { + { "application_id", appId }, + { "nuget_package_version", version } + }); + } +} diff --git a/src/Altinn.App.Core/Configuration/MetricsSettings.cs b/src/Altinn.App.Core/Configuration/MetricsSettings.cs new file mode 100644 index 000000000..3de05e3df --- /dev/null +++ b/src/Altinn.App.Core/Configuration/MetricsSettings.cs @@ -0,0 +1,16 @@ +namespace Altinn.App.Api.Configuration; + +/// +/// Metric settings for Altinn Apps +/// +public class MetricsSettings +{ + /// + /// Gets or sets a value indicating whether metrics is enabled or not + /// + public bool Enabled { get; set; } = true; + /// + /// Gets or sets the port for the metrics server is exposed + /// + public ushort Port { get; set; } = 5006; +} diff --git a/src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs b/src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs index 80cf71a9b..aad701e8e 100644 --- a/src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs +++ b/src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs @@ -1,3 +1,4 @@ +using Altinn.App.Api.Configuration; using Altinn.App.Core.Configuration; using Altinn.App.Core.Features; using Altinn.App.Core.Features.Action; @@ -80,7 +81,6 @@ public static void AddPlatformServices(this IServiceCollection services, IConfig services.AddHttpClient(); services.AddHttpClient(); services.AddHttpClient(); - services.Decorate(); services.AddHttpClient(); services.AddHttpClient(); services.AddHttpClient(); @@ -162,6 +162,7 @@ public static void AddAppServices(this IServiceCollection services, IConfigurati AddProcessServices(services); AddFileAnalyserServices(services); AddFileValidatorServices(services); + AddMetricsDecorators(services, configuration); if (!env.IsDevelopment()) { @@ -236,7 +237,6 @@ private static void AddAppOptions(IServiceCollection services) private static void AddProcessServices(IServiceCollection services) { services.TryAddTransient(); - services.Decorate(); services.TryAddTransient(); services.TryAddSingleton(); services.TryAddTransient(); @@ -264,5 +264,15 @@ private static void AddFileValidatorServices(IServiceCollection services) services.TryAddTransient(); services.TryAddTransient(); } + + private static void AddMetricsDecorators(IServiceCollection services, IConfiguration configuration) + { + MetricsSettings metricsSettings = configuration.GetSection("MetricsSettings")?.Get() ?? new MetricsSettings(); + if (metricsSettings.Enabled) + { + services.Decorate(); + services.Decorate(); + } + } } } diff --git a/test/Altinn.App.Api.Tests/Program.cs b/test/Altinn.App.Api.Tests/Program.cs index 1b6fa9e3f..520fc5a40 100644 --- a/test/Altinn.App.Api.Tests/Program.cs +++ b/test/Altinn.App.Api.Tests/Program.cs @@ -24,6 +24,7 @@ // external api's etc. should be mocked. WebApplicationBuilder builder = WebApplication.CreateBuilder(new WebApplicationOptions() { ApplicationName = "Altinn.App.Api.Tests" }); +builder.Configuration.GetSection("MetricsSettings:Enabled").Value = "false"; ConfigureServices(builder.Services, builder.Configuration); ConfigureMockServices(builder.Services, builder.Configuration); @@ -56,22 +57,7 @@ void ConfigureMockServices(IServiceCollection services, ConfigurationManager con void Configure() { - if (app.Environment.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - - app.UseDefaultSecurityHeaders(); - app.UseRouting(); - app.UseStaticFiles(); - app.UseAuthentication(); - app.UseAuthorization(); - - app.UseEndpoints(endpoints => - { - endpoints.MapControllers(); - }); - app.UseHealthChecks("/health"); + app.UseAltinnAppCommonConfiguration(); } // This "hack" (documentet by Microsoft) is done to