Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Altinn.App.Api/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ private static void AddOpenTelemetry(IServiceCollection services, IConfiguration
DistributedContextPropagator.Current = new AspNetCorePropagator();

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

services
.AddOpenTelemetry()
Expand All @@ -249,12 +250,13 @@ private static void AddOpenTelemetry(IServiceCollection services, IConfiguration
.AddAspNetCoreInstrumentation(opts =>
{
opts.RecordException = true;
opts.Filter = httpContext => !httpContext.Request.Path.StartsWithSegments("/health");
});

if (isTest)
return;

if (!string.IsNullOrWhiteSpace(appInsightsConnectionString))
if (useOpenTelemetryCollector is not true && !string.IsNullOrWhiteSpace(appInsightsConnectionString))
{
builder = builder.AddAzureMonitorTraceExporter(options =>
{
Expand All @@ -277,7 +279,7 @@ private static void AddOpenTelemetry(IServiceCollection services, IConfiguration
if (isTest)
return;

if (!string.IsNullOrWhiteSpace(appInsightsConnectionString))
if (useOpenTelemetryCollector is not true && !string.IsNullOrWhiteSpace(appInsightsConnectionString))
{
builder = builder.AddAzureMonitorMetricExporter(options =>
{
Expand All @@ -299,7 +301,7 @@ private static void AddOpenTelemetry(IServiceCollection services, IConfiguration
if (isTest)
return;

if (!string.IsNullOrWhiteSpace(appInsightsConnectionString))
if (useOpenTelemetryCollector is not true && !string.IsNullOrWhiteSpace(appInsightsConnectionString))
{
options.AddAzureMonitorLogExporter(options =>
{
Expand Down
99 changes: 97 additions & 2 deletions src/Altinn.App.Api/Extensions/WebHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Altinn.App.Core.Configuration;
using Altinn.App.Core.Extensions;
using Altinn.App.Core.Features.Maskinporten.Extensions;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.FileProviders;

namespace Altinn.App.Api.Extensions;

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

configBuilder.AddInMemoryCollection(config);

var runtimeSecretsDirectory = context.Configuration["AppSettings:RuntimeSecretsDirectory"];
if (string.IsNullOrWhiteSpace(runtimeSecretsDirectory))
{
runtimeSecretsDirectory = AppSettings.DefaultRuntimeSecretsDirectory;
}

configBuilder.AddMaskinportenSettingsFile(
context,
"MaskinportenSettingsFilepath",
"/mnt/app-secrets/maskinporten-settings.json"
Path.Join(runtimeSecretsDirectory, "maskinporten-settings.json")
);
configBuilder.AddMaskinportenSettingsFile(
context,
"MaskinportenSettingsInternalFilepath",
"/mnt/app-secrets/maskinporten-settings-internal.json"
Path.Join(runtimeSecretsDirectory, "maskinporten-settings-internal.json")
);

configBuilder.AddRuntimeConfigFiles(context.HostingEnvironment, runtimeSecretsDirectory);
configBuilder.LoadAppConfig(args);
}
);
}

private static void AddRuntimeConfigFiles(
this IConfigurationBuilder configBuilder,
IHostEnvironment hostEnvironment,
string secretsDirectory
)
{
if (hostEnvironment.IsDevelopment())
{
return;
}

ArgumentException.ThrowIfNullOrWhiteSpace(secretsDirectory);
const string overrideFileNameFragment = "override";
if (!Directory.Exists(secretsDirectory))
{
return;
}

string[] jsonFiles = Directory.GetFiles(secretsDirectory, "*.json", SearchOption.TopDirectoryOnly);
Array.Sort(jsonFiles, StringComparer.OrdinalIgnoreCase);

PhysicalFileProvider? secretsFileProvider = null;
HashSet<string> existingJsonFilePaths = [];

foreach (JsonConfigurationSource source in configBuilder.Sources.OfType<JsonConfigurationSource>())
{
if (source.FileProvider is null || string.IsNullOrWhiteSpace(source.Path))
{
continue;
}

string? existingJsonFilePath = source.FileProvider.GetFileInfo(source.Path).PhysicalPath;
if (string.IsNullOrWhiteSpace(existingJsonFilePath))
{
continue;
}

existingJsonFilePaths.Add(Path.GetFullPath(existingJsonFilePath));
}

foreach (string jsonFile in jsonFiles)
{
string jsonFilePath = Path.GetFullPath(jsonFile);
if (existingJsonFilePaths.Contains(jsonFilePath))
{
continue;
}

string jsonFileName = Path.GetFileName(jsonFile);
if (jsonFileName.Contains(overrideFileNameFragment, StringComparison.OrdinalIgnoreCase))
{
continue;
}

configBuilder.AddJsonFile(
provider: secretsFileProvider ??= new PhysicalFileProvider(secretsDirectory),
path: jsonFileName,
optional: true,
reloadOnChange: true
);
}

foreach (string jsonFile in jsonFiles)
{
string jsonFilePath = Path.GetFullPath(jsonFile);
if (existingJsonFilePaths.Contains(jsonFilePath))
{
continue;
}

string jsonFileName = Path.GetFileName(jsonFile);
if (!jsonFileName.Contains(overrideFileNameFragment, StringComparison.OrdinalIgnoreCase))
{
continue;
}

configBuilder.AddJsonFile(
provider: secretsFileProvider ??= new PhysicalFileProvider(secretsDirectory),
path: jsonFileName,
optional: true,
reloadOnChange: true
);
}
}
}
12 changes: 12 additions & 0 deletions src/Altinn.App.Core/Configuration/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,16 @@ public class AppSettings
/// Improves instrumentation throughout the Altinn app libraries.
/// </summary>
public bool UseOpenTelemetry { get; set; }

/// <summary>
/// Use OpenTelemetry collector via OTLP exporter instead of Azure Monitor exporters.
/// </summary>
public bool UseOpenTelemetryCollector { get; set; }

internal const string DefaultRuntimeSecretsDirectory = "/mnt/app-secrets";

/// <summary>
/// Directory containing runtime secrets JSON files.
/// </summary>
public string RuntimeSecretsDirectory { get; set; } = DefaultRuntimeSecretsDirectory;
}
Loading