Skip to content

Commit 8b24818

Browse files
authored
Merge pull request #94 from Virtual-Finland-Development/feat/error-alerting
Subscribe errors to the monitoring/alerter
2 parents b0bad35 + c0a804a commit 8b24818

11 files changed

Lines changed: 89 additions & 86 deletions

File tree

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/Activities/User/Operations/GetSearchProfile.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public async Task<SearchProfile> Handle(Query request, CancellationToken cancell
5151

5252
if (userSearchProfile is null)
5353
{
54-
_logger.LogInformation("Failed to retrieve search profile: {RequestProfileId}", request.ProfileId);
5554
throw new NotFoundException($"Specified search profile not found by ID: {request.ProfileId}");
5655
}
5756

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/Helpers/Services/TestbedConsentSecurityService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public TestbedConsentSecurityService(ILogger<TestbedConsentSecurityService> logg
2424

2525
public async Task VerifyConsentTokenRequestHeaders(IHeaderDictionary headers, string dataSourceUri)
2626
{
27-
_logger.LogInformation("Verifying consent request");
27+
_logger.LogDebug("Verifying consent request");
2828
var idToken = headers.Authorization.ToString().Replace("Bearer ", string.Empty);
2929
if (string.IsNullOrEmpty(idToken))
3030
throw new NotAuthorizedException("Authorization token is missing");
@@ -36,12 +36,12 @@ public async Task VerifyConsentTokenRequestHeaders(IHeaderDictionary headers, st
3636
VerifyConsentToken(consentToken, idToken, dataSourceUri);
3737
await VerifyConsentTokenWithService(consentToken, idToken, dataSourceUri); // Checks for token revokation
3838

39-
_logger.LogInformation("Consent request verified");
39+
_logger.LogDebug("Consent request verified");
4040
}
4141

4242
public void VerifyConsentToken(string consentTokenRaw, string idTokenRaw, string dataSourceUri)
4343
{
44-
_logger.LogInformation("Verifying consent token");
44+
_logger.LogDebug("Verifying consent token");
4545

4646
var idToken = ParseJwtToken(idTokenRaw);
4747
if (idToken == null)
@@ -96,13 +96,13 @@ public void VerifyConsentToken(string consentTokenRaw, string idTokenRaw, string
9696
if (consentToken.Payload["dsi"] as string != dataSourceUri)
9797
throw new NotAuthorizedException("Token mismatch: dsi");
9898

99-
_logger.LogInformation("Consent token verified");
99+
_logger.LogDebug("Consent token verified");
100100
}
101101

102102

103103
public async Task VerifyConsentTokenWithService(string consentToken, string idToken, string dataSourceUri)
104104
{
105-
_logger.LogInformation("Verifying consent with Testbed Consent API");
105+
_logger.LogDebug("Verifying consent with Testbed Consent API");
106106
try
107107
{
108108
var httpClient = _httpClientFactory.CreateClient();
@@ -130,7 +130,7 @@ public async Task VerifyConsentTokenWithService(string consentToken, string idTo
130130
throw new NotAuthorizedException(e.Message);
131131
}
132132

133-
_logger.LogInformation("Consent verified by Testbed Consent API");
133+
_logger.LogDebug("Consent verified by Testbed Consent API");
134134
}
135135

136136

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/Middleware/ErrorHandlerMiddleware.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ public async Task Invoke(HttpContext context)
2323
}
2424
catch (Exception error)
2525
{
26+
// Debug mode logging
27+
_logger.LogDebug(error, "Debug");
28+
29+
// Attach exception to the request context for request logger middleware
30+
context.Items.Add("Exception", error);
31+
32+
// Write the error response
2633
switch (error)
2734
{
2835
case NotAuthorizedException:
@@ -40,7 +47,6 @@ public async Task Invoke(HttpContext context)
4047
}, HttpStatusCode.NotFound);
4148
break;
4249
case BadRequestException:
43-
_logger.LogError(error, "Request processing failure!");
4450
await WriteErrorResponse(context, new ErrorResponse()
4551
{
4652
Type = "BadRequest",
@@ -113,7 +119,6 @@ public async Task Invoke(HttpContext context)
113119
}, HttpStatusCode.RequestTimeout);
114120
break;
115121
default:
116-
_logger.LogError(error, "Request processing failure!");
117122
await WriteErrorResponse(context, new ErrorResponse()
118123
{
119124
Type = "InternalServerError",

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/Program.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,17 @@
170170
}
171171

172172

173-
app.UseSerilogRequestLogging();
173+
app.UseSerilogRequestLogging(options =>
174+
{
175+
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
176+
{
177+
if (httpContext.Items["Exception"] is Exception exception)
178+
{
179+
// Add the exception to the log context, omit the stack trace
180+
diagnosticContext.Set("@x", $"{exception.GetType().Name}: {exception.Message}");
181+
}
182+
};
183+
});
174184
app.UseMiddleware<RequestTracingMiddleware>();
175185
app.UseMiddleware<ErrorHandlerMiddleware>();
176186
app.UseHttpsRedirection();

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/appsettings.dev.json

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,4 @@
11
{
2-
"Serilog": {
3-
"Using": [
4-
"Serilog.Sinks.Console"
5-
],
6-
"MinimumLevel": {
7-
"Default": "Information",
8-
"Override": {
9-
"Microsoft": "Warning",
10-
"Microsoft.Hosting.Lifetime": "Warning",
11-
"Microsoft.AspNetCore": "Warning",
12-
"Microsoft.EntityFrameworkCore.Database.Command": "Warning"
13-
}
14-
},
15-
"WriteTo": [
16-
{
17-
"Name": "Console",
18-
"Args": {
19-
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
20-
}
21-
}
22-
]
23-
},
242
"Security": {
253
"Authorization": {
264
"SuomiFi": {

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/appsettings.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@
55
"Microsoft.AspNetCore": "Warning"
66
}
77
},
8+
"Serilog": {
9+
"Using": [
10+
"Serilog.Sinks.Console"
11+
],
12+
"MinimumLevel": {
13+
"Default": "Information",
14+
"Override": {
15+
"Microsoft": "Warning",
16+
"Microsoft.Hosting.Lifetime": "Warning",
17+
"Microsoft.AspNetCore": "Warning",
18+
"Microsoft.EntityFrameworkCore.Database.Command": "Warning"
19+
}
20+
},
21+
"WriteTo": [
22+
{
23+
"Name": "Console",
24+
"Args": {
25+
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
26+
}
27+
}
28+
]
29+
},
830
"AllowedHosts": "*",
931
"Security": {
1032
"Access": {

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/appsettings.local.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
{
22
"Serilog": {
3-
"Using": [
4-
"Serilog.Sinks.Console"
5-
],
63
"MinimumLevel": {
7-
"Default": "Information",
4+
"Default": "Debug",
85
"Override": {
9-
"Microsoft": "Warning",
10-
"Microsoft.Hosting.Lifetime": "Warning",
11-
"Microsoft.AspNetCore": "Warning",
12-
"Microsoft.EntityFrameworkCore.Database.Command": "Warning"
6+
"Microsoft.EntityFrameworkCore.Database.Command": "Information"
137
}
148
},
159
"WriteTo": [
1610
{
17-
"Name": "Console"
11+
"Name": "Console",
12+
"Args": {
13+
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
14+
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"
15+
}
1816
}
1917
]
2018
},

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/appsettings.mvp-staging.json

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,4 @@
11
{
2-
"Serilog": {
3-
"Using": [
4-
"Serilog.Sinks.Console"
5-
],
6-
"MinimumLevel": {
7-
"Default": "Information",
8-
"Override": {
9-
"Microsoft": "Warning",
10-
"Microsoft.Hosting.Lifetime": "Warning",
11-
"Microsoft.AspNetCore": "Warning",
12-
"Microsoft.EntityFrameworkCore.Database.Command": "Warning"
13-
}
14-
},
15-
"WriteTo": [
16-
{
17-
"Name": "Console",
18-
"Args": {
19-
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
20-
}
21-
}
22-
]
23-
},
242
"Security": {
253
"Authorization": {
264
"Sinuna": {

VirtualFinland.UserAPI/src/VirtualFinland.UsersAPI/appsettings.staging.json

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,4 @@
11
{
2-
"Serilog": {
3-
"Using": [
4-
"Serilog.Sinks.Console"
5-
],
6-
"MinimumLevel": {
7-
"Default": "Information",
8-
"Override": {
9-
"Microsoft": "Warning",
10-
"Microsoft.Hosting.Lifetime": "Warning",
11-
"Microsoft.AspNetCore": "Warning",
12-
"Microsoft.EntityFrameworkCore.Database.Command": "Warning"
13-
}
14-
},
15-
"WriteTo": [
16-
{
17-
"Name": "Console",
18-
"Args": {
19-
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
20-
}
21-
}
22-
]
23-
},
242
"Security": {
253
"Authorization": {
264
"SuomiFi": {

VirtualFinland.UsersAPI.Deployment/Common/Models/StackSetup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ public class StackSetup
1212
public string Region = default!;
1313
public string CreateResourceName(string name) => $"{ProjectName}-{name}-{Environment}".ToLower();
1414
public string GetInfrastructureStackName() => $"{Organization}/infrastructure/{Environment}";
15+
public string GetAlertingStackName() => $"{Organization}/cloudwatch-logs-alerts/{Environment}";
1516
}

0 commit comments

Comments
 (0)