Skip to content

Commit 290a079

Browse files
committed
chore: improve runtime configuration
1 parent 4387fb8 commit 290a079

File tree

4 files changed

+116
-5
lines changed

4 files changed

+116
-5
lines changed

src/Altinn.App.Api/Extensions/ServiceCollectionExtensions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ private static void AddOpenTelemetry(IServiceCollection services, IConfiguration
232232
DistributedContextPropagator.Current = new AspNetCorePropagator();
233233

234234
var appInsightsConnectionString = GetAppInsightsConnectionStringForOtel(config, env);
235+
var useOpenTelemetryCollector = config.GetValue<bool?>("AppSettings:UseOpenTelemetryCollector");
235236

236237
services
237238
.AddOpenTelemetry()
@@ -249,12 +250,13 @@ private static void AddOpenTelemetry(IServiceCollection services, IConfiguration
249250
.AddAspNetCoreInstrumentation(opts =>
250251
{
251252
opts.RecordException = true;
253+
opts.Filter = httpContext => !httpContext.Request.Path.StartsWithSegments("/health");
252254
});
253255

254256
if (isTest)
255257
return;
256258

257-
if (!string.IsNullOrWhiteSpace(appInsightsConnectionString))
259+
if (useOpenTelemetryCollector is not true && !string.IsNullOrWhiteSpace(appInsightsConnectionString))
258260
{
259261
builder = builder.AddAzureMonitorTraceExporter(options =>
260262
{
@@ -277,7 +279,7 @@ private static void AddOpenTelemetry(IServiceCollection services, IConfiguration
277279
if (isTest)
278280
return;
279281

280-
if (!string.IsNullOrWhiteSpace(appInsightsConnectionString))
282+
if (useOpenTelemetryCollector is not true && !string.IsNullOrWhiteSpace(appInsightsConnectionString))
281283
{
282284
builder = builder.AddAzureMonitorMetricExporter(options =>
283285
{
@@ -299,7 +301,7 @@ private static void AddOpenTelemetry(IServiceCollection services, IConfiguration
299301
if (isTest)
300302
return;
301303

302-
if (!string.IsNullOrWhiteSpace(appInsightsConnectionString))
304+
if (useOpenTelemetryCollector is not true && !string.IsNullOrWhiteSpace(appInsightsConnectionString))
303305
{
304306
options.AddAzureMonitorLogExporter(options =>
305307
{

src/Altinn.App.Api/Extensions/WebHostBuilderExtensions.cs

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
using Altinn.App.Core.Configuration;
12
using Altinn.App.Core.Extensions;
23
using Altinn.App.Core.Features.Maskinporten.Extensions;
4+
using Microsoft.Extensions.Configuration.Json;
5+
using Microsoft.Extensions.FileProviders;
36

47
namespace Altinn.App.Api.Extensions;
58

@@ -29,19 +32,111 @@ public static void ConfigureAppWebHost(this IWebHostBuilder builder, string[] ar
2932

3033
configBuilder.AddInMemoryCollection(config);
3134

35+
var runtimeSecretsDirectory = context.Configuration["AppSettings:RuntimeSecretsDirectory"];
36+
if (string.IsNullOrWhiteSpace(runtimeSecretsDirectory))
37+
{
38+
runtimeSecretsDirectory = AppSettings.DefaultRuntimeSecretsDirectory;
39+
}
40+
3241
configBuilder.AddMaskinportenSettingsFile(
3342
context,
3443
"MaskinportenSettingsFilepath",
35-
"/mnt/app-secrets/maskinporten-settings.json"
44+
Path.Join(runtimeSecretsDirectory, "maskinporten-settings.json")
3645
);
3746
configBuilder.AddMaskinportenSettingsFile(
3847
context,
3948
"MaskinportenSettingsInternalFilepath",
40-
"/mnt/app-secrets/maskinporten-settings-internal.json"
49+
Path.Join(runtimeSecretsDirectory, "maskinporten-settings-internal.json")
4150
);
4251

52+
configBuilder.AddRuntimeConfigFiles(context.HostingEnvironment, runtimeSecretsDirectory);
4353
configBuilder.LoadAppConfig(args);
4454
}
4555
);
4656
}
57+
58+
private static void AddRuntimeConfigFiles(
59+
this IConfigurationBuilder configBuilder,
60+
IHostEnvironment hostEnvironment,
61+
string secretsDirectory
62+
)
63+
{
64+
if (hostEnvironment.IsDevelopment())
65+
{
66+
return;
67+
}
68+
69+
ArgumentException.ThrowIfNullOrWhiteSpace(secretsDirectory);
70+
const string overrideFileNameFragment = "override";
71+
if (!Directory.Exists(secretsDirectory))
72+
{
73+
return;
74+
}
75+
76+
string[] jsonFiles = Directory.GetFiles(secretsDirectory, "*.json", SearchOption.TopDirectoryOnly);
77+
Array.Sort(jsonFiles, StringComparer.OrdinalIgnoreCase);
78+
79+
PhysicalFileProvider? secretsFileProvider = null;
80+
HashSet<string> existingJsonFilePaths = [];
81+
82+
foreach (JsonConfigurationSource source in configBuilder.Sources.OfType<JsonConfigurationSource>())
83+
{
84+
if (source.FileProvider is null || string.IsNullOrWhiteSpace(source.Path))
85+
{
86+
continue;
87+
}
88+
89+
string? existingJsonFilePath = source.FileProvider.GetFileInfo(source.Path).PhysicalPath;
90+
if (string.IsNullOrWhiteSpace(existingJsonFilePath))
91+
{
92+
continue;
93+
}
94+
95+
existingJsonFilePaths.Add(Path.GetFullPath(existingJsonFilePath));
96+
}
97+
98+
foreach (string jsonFile in jsonFiles)
99+
{
100+
string jsonFilePath = Path.GetFullPath(jsonFile);
101+
if (existingJsonFilePaths.Contains(jsonFilePath))
102+
{
103+
continue;
104+
}
105+
106+
string jsonFileName = Path.GetFileName(jsonFile);
107+
if (jsonFileName.Contains(overrideFileNameFragment, StringComparison.OrdinalIgnoreCase))
108+
{
109+
continue;
110+
}
111+
112+
configBuilder.AddJsonFile(
113+
provider: secretsFileProvider ??= new PhysicalFileProvider(secretsDirectory),
114+
path: jsonFileName,
115+
optional: true,
116+
reloadOnChange: true
117+
);
118+
}
119+
120+
foreach (string jsonFile in jsonFiles)
121+
{
122+
string jsonFilePath = Path.GetFullPath(jsonFile);
123+
if (existingJsonFilePaths.Contains(jsonFilePath))
124+
{
125+
continue;
126+
}
127+
128+
string jsonFileName = Path.GetFileName(jsonFile);
129+
if (!jsonFileName.Contains(overrideFileNameFragment, StringComparison.OrdinalIgnoreCase))
130+
{
131+
continue;
132+
}
133+
134+
configBuilder.AddJsonFile(
135+
provider: secretsFileProvider ??= new PhysicalFileProvider(secretsDirectory),
136+
path: jsonFileName,
137+
optional: true,
138+
reloadOnChange: true
139+
);
140+
}
141+
}
47142
}

src/Altinn.App.Core/Configuration/AppSettings.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,16 @@ public class AppSettings
220220
/// Improves instrumentation throughout the Altinn app libraries.
221221
/// </summary>
222222
public bool UseOpenTelemetry { get; set; }
223+
224+
/// <summary>
225+
/// Use OpenTelemetry collector via OTLP exporter instead of Azure Monitor exporters.
226+
/// </summary>
227+
public bool UseOpenTelemetryCollector { get; set; }
228+
229+
internal const string DefaultRuntimeSecretsDirectory = "/mnt/app-secrets";
230+
231+
/// <summary>
232+
/// Directory containing runtime secrets JSON files.
233+
/// </summary>
234+
public string RuntimeSecretsDirectory { get; set; } = DefaultRuntimeSecretsDirectory;
223235
}

test/Altinn.App.Core.Tests/PublicApiTests.PublicApi_ShouldNotChange_Unintentionally.verified.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ namespace Altinn.App.Core.Configuration
4040
public string RuntimeAppFileName { get; set; }
4141
public string RuntimeCookieName { get; set; }
4242
public string RuntimeCssFileName { get; set; }
43+
public string RuntimeSecretsDirectory { get; set; }
4344
public string ServiceStylesConfigFileName { get; set; }
4445
public string TextFolder { get; set; }
4546
public string UiFolder { get; set; }
4647
public bool UseOpenTelemetry { get; set; }
48+
public bool UseOpenTelemetryCollector { get; set; }
4749
public string ValidationConfigurationFileName { get; set; }
4850
}
4951
public class CacheSettings

0 commit comments

Comments
 (0)