From 1f27886884120f1f0932e97019dd449aab794b4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20J=C3=B8rgen=20Skogstad?= Date: Wed, 13 Mar 2024 15:25:28 +0100 Subject: [PATCH] chore: Temp debugging, log Slack Webhook URL (#553) The Slack notifier function throws "invalid URI" once in a while, attempting to figure out what/why --- .../External/Slack/SlackClient.cs | 17 +++++++++++++---- .../ForwardAlertToSlack.cs | 1 + 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Digdir.Tool.Dialogporten.SlackNotifier/External/Slack/SlackClient.cs b/src/Digdir.Tool.Dialogporten.SlackNotifier/External/Slack/SlackClient.cs index d4f699b7f..deaa2f0db 100644 --- a/src/Digdir.Tool.Dialogporten.SlackNotifier/External/Slack/SlackClient.cs +++ b/src/Digdir.Tool.Dialogporten.SlackNotifier/External/Slack/SlackClient.cs @@ -10,10 +10,19 @@ internal sealed class SlackClient : ISlackClient public SlackClient(HttpClient httpClient, IOptions slackOptions) { - _httpClient = httpClient; - _slackOptions = slackOptions; + _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); + _slackOptions = slackOptions ?? throw new ArgumentNullException(nameof(slackOptions)); } - public Task SendAsync(SlackRequestDto message, CancellationToken cancellationToken) => - _httpClient.PostAsJsonAsync(_slackOptions.Value.WebhookUrl, message, cancellationToken); + public async Task SendAsync(SlackRequestDto message, CancellationToken cancellationToken) + { + // TEMP DEBUG + Console.WriteLine($"Slack WebhookURL: {_slackOptions.Value.WebhookUrl}"); + if (!Uri.TryCreate(_slackOptions.Value.WebhookUrl, UriKind.Absolute, out var uri)) + { + return; + } + + await _httpClient.PostAsJsonAsync(uri, message, cancellationToken); + } } diff --git a/src/Digdir.Tool.Dialogporten.SlackNotifier/Features/AzureAlertToSlackForwarder/ForwardAlertToSlack.cs b/src/Digdir.Tool.Dialogporten.SlackNotifier/Features/AzureAlertToSlackForwarder/ForwardAlertToSlack.cs index 3c6b507b6..65bbf1f12 100644 --- a/src/Digdir.Tool.Dialogporten.SlackNotifier/Features/AzureAlertToSlackForwarder/ForwardAlertToSlack.cs +++ b/src/Digdir.Tool.Dialogporten.SlackNotifier/Features/AzureAlertToSlackForwarder/ForwardAlertToSlack.cs @@ -31,6 +31,7 @@ await _slack.SendAsync(new SlackRequestDto ExceptionReport = appInsightsResponses.ToAsciiTableExceptionReport(), Link = azureAlertRequest.ToQueryLink() }, cancellationToken); + return req.CreateResponse(HttpStatusCode.OK); } }