From 02600f68f9d7655ee066f4eda07150d06f7d9dfa Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 31 May 2022 17:15:01 +0200 Subject: [PATCH] Create rule S6423: Always log failures in Azure Functions (#960) --- rules/S6423/csharp/metadata.json | 18 +++++++++++ rules/S6423/csharp/rule.adoc | 54 ++++++++++++++++++++++++++++++++ rules/S6423/metadata.json | 2 ++ 3 files changed, 74 insertions(+) create mode 100644 rules/S6423/csharp/metadata.json create mode 100644 rules/S6423/csharp/rule.adoc create mode 100644 rules/S6423/metadata.json diff --git a/rules/S6423/csharp/metadata.json b/rules/S6423/csharp/metadata.json new file mode 100644 index 00000000000..f0217f1c627 --- /dev/null +++ b/rules/S6423/csharp/metadata.json @@ -0,0 +1,18 @@ +{ + "title": "Azure Functions should log all failures.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "error-handling" + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-6423", + "sqKey": "S6423", + "scope": "Main", + "defaultQualityProfiles": [ ], + "quickfix": "unknown" +} diff --git a/rules/S6423/csharp/rule.adoc b/rules/S6423/csharp/rule.adoc new file mode 100644 index 00000000000..c15d7a5283a --- /dev/null +++ b/rules/S6423/csharp/rule.adoc @@ -0,0 +1,54 @@ +Capturing and logging errors is critical to monitoring the health of your Azure Functions application. + +Each `catch` block inside an Azure Function should log helpful details about the failure. Moreover, the logging should not be done at `Debug` or `Trace` level. + +Consider using the built-in integration with Application Insights for better monitoring of your Application. + +// If you want to factorize the description uncomment the following line and create the file. +//include::../description.adoc[] + +== Noncompliant Code Example + +[source,csharp] +---- +[FunctionName("Foo")] +public static async Task Run( + [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, + ILogger log) +{ + try + { + // do stuff that can fail + } + catch (Exception ex) + { + // the failure is not logged at all OR is logged at DEBUG/TRACE level + } +} +---- + +== Compliant Solution + +[source,csharp] +---- +[FunctionName("Foo")] +public static async Task Run( + [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, + ILogger log) +{ + try + { + // do stuff that can fail + } + catch (Exception ex) + { + log.LogError("Give details that will help investigations", ex); + } +} +---- + +== See + +* https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-error-pages?tabs=csharp[Azure Functions error handling and retries] +* https://docs.microsoft.com/en-us/azure/azure-functions/functions-monitoring[Monitor Azure Functions] +* https://docs.microsoft.com/en-us/azure/azure-monitor/app/azure-functions-supported-features[Application Insights for Azure Functions supported features] diff --git a/rules/S6423/metadata.json b/rules/S6423/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S6423/metadata.json @@ -0,0 +1,2 @@ +{ +}