Skip to content

Commit

Permalink
Separate metrics port (#288)
Browse files Browse the repository at this point in the history
* Make Metrics configurable through appsettings.json and move endpoint to separate port

* add missing trailing linebreaks
  • Loading branch information
tjololo authored Aug 17, 2023
1 parent 86a7377 commit 6ce0799
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 28 deletions.
18 changes: 16 additions & 2 deletions src/Altinn.App.Api/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,6 +20,7 @@
using Microsoft.Extensions.Hosting;
using Microsoft.FeatureManagement;
using Microsoft.IdentityModel.Tokens;
using Prometheus;

namespace Altinn.App.Api.Extensions
{
Expand Down Expand Up @@ -74,6 +77,7 @@ public static void AddAltinnAppServices(this IServiceCollection services, IConfi
services.AddHttpClient<AuthorizationApiClient>();

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddMetricsServer(config);
}

private static void AddApplicationInsights(IServiceCollection services, IConfiguration config, IWebHostEnvironment env)
Expand Down Expand Up @@ -144,5 +148,15 @@ private static void AddAntiforgery(IServiceCollection services)

services.TryAddSingleton<ValidateAntiforgeryTokenIfAuthCookieAuthorizationFilter>();
}

private static void AddMetricsServer(this IServiceCollection services, IConfiguration config)
{
var metricsSettings = config.GetSection("MetricsSettings").Get<MetricsSettings>() ?? new MetricsSettings();
if (metricsSettings.Enabled)
{
ushort port = metricsSettings.Port;
services.AddMetricServer(options => { options.Port = port; });
}
}
}
}
}
30 changes: 22 additions & 8 deletions src/Altinn.App.Api/Extensions/WebApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#nullable enable
using System.Reflection;
using Altinn.App.Api.Configuration;
using Altinn.App.Api.Helpers;
using Prometheus;

Expand All @@ -16,20 +18,15 @@ public static class WebApplicationBuilderExtensions
/// <returns></returns>
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<string, string>()
{
{ "application_id", appId },
{ "nuget_package_version", version }
});
app.UseDefaultSecurityHeaders();
app.UseRouting();
app.UseStaticFiles('/' + appId);
Expand All @@ -43,4 +40,21 @@ public static IApplicationBuilder UseAltinnAppCommonConfiguration(this IApplicat
app.UseHealthChecks("/health");
return app;
}
}

private static void UseAltinnPrometheus(this WebApplication webApp, string appId)
{
var metricsSettings = webApp.Configuration.GetSection("MetricsSettings")?.Get<MetricsSettings>() ?? new MetricsSettings();
if (!metricsSettings.Enabled)
{
return;
}

webApp.UseHttpMetrics();
var version = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown";
Metrics.DefaultRegistry.SetStaticLabels(new Dictionary<string, string>()
{
{ "application_id", appId },
{ "nuget_package_version", version }
});
}
}
16 changes: 16 additions & 0 deletions src/Altinn.App.Core/Configuration/MetricsSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Altinn.App.Api.Configuration;

/// <summary>
/// Metric settings for Altinn Apps
/// </summary>
public class MetricsSettings
{
/// <summary>
/// Gets or sets a value indicating whether metrics is enabled or not
/// </summary>
public bool Enabled { get; set; } = true;
/// <summary>
/// Gets or sets the port for the metrics server is exposed
/// </summary>
public ushort Port { get; set; } = 5006;
}
14 changes: 12 additions & 2 deletions src/Altinn.App.Core/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -80,7 +81,6 @@ public static void AddPlatformServices(this IServiceCollection services, IConfig
services.AddHttpClient<IDataClient, DataClient>();
services.AddHttpClient<IOrganizationClient, RegisterERClient>();
services.AddHttpClient<IInstanceClient, InstanceClient>();
services.Decorate<IInstanceClient, InstanceClientMetricsDecorator>();
services.AddHttpClient<IInstanceEventClient, InstanceEventClient>();
services.AddHttpClient<IEventsClient, EventsClient>();
services.AddHttpClient<IPDF, PDFClient>();
Expand Down Expand Up @@ -162,6 +162,7 @@ public static void AddAppServices(this IServiceCollection services, IConfigurati
AddProcessServices(services);
AddFileAnalyserServices(services);
AddFileValidatorServices(services);
AddMetricsDecorators(services, configuration);

if (!env.IsDevelopment())
{
Expand Down Expand Up @@ -236,7 +237,6 @@ private static void AddAppOptions(IServiceCollection services)
private static void AddProcessServices(IServiceCollection services)
{
services.TryAddTransient<IProcessEngine, ProcessEngine>();
services.Decorate<IProcessEngine, ProcessEngineMetricsDecorator>();
services.TryAddTransient<IProcessNavigator, ProcessNavigator>();
services.TryAddSingleton<IProcessReader, ProcessReader>();
services.TryAddTransient<IProcessEventDispatcher, ProcessEventDispatcher>();
Expand Down Expand Up @@ -264,5 +264,15 @@ private static void AddFileValidatorServices(IServiceCollection services)
services.TryAddTransient<IFileValidationService, FileValidationService>();
services.TryAddTransient<IFileValidatorFactory, FileValidatorFactory>();
}

private static void AddMetricsDecorators(IServiceCollection services, IConfiguration configuration)
{
MetricsSettings metricsSettings = configuration.GetSection("MetricsSettings")?.Get<MetricsSettings>() ?? new MetricsSettings();
if (metricsSettings.Enabled)
{
services.Decorate<IInstanceClient, InstanceClientMetricsDecorator>();
services.Decorate<IProcessEngine, ProcessEngineMetricsDecorator>();
}
}
}
}
18 changes: 2 additions & 16 deletions test/Altinn.App.Api.Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6ce0799

Please sign in to comment.