From c7950a933a03af000292c0527dd0368b0c3300cb Mon Sep 17 00:00:00 2001 From: Tom Kerkhove Date: Thu, 16 May 2019 13:56:26 +0200 Subject: [PATCH] Provide capability to configure log verbosity (#553) * Provide docs * Provide capability to configure log verbosity * PROMITOR_LOGGING_MINIMUMLOGLEVEL -> PROMITOR_LOGGING_MINIMUMLEVEL * Docs - Change headers --- docs/configuration/index.md | 8 +++++- docs/index.md | 2 +- src/Promitor.Core.Telemetry/Loggers/Logger.cs | 26 +++++++++++++++++++ .../Loggers/RuntimeLogger.cs | 11 ++++++++ .../Loggers/ValidationLogger.cs | 11 ++++++++ src/Promitor.Core.Telemetry/RuntimeLogger.cs | 12 --------- src/Promitor.Core/EnvironmentVariables.cs | 5 ++++ src/Promitor.Scraper.Host/Startup.cs | 1 + .../Validation/RuntimeValidator.cs | 6 ++--- src/docker-compose.override.yml | 1 + 10 files changed, 66 insertions(+), 17 deletions(-) create mode 100644 src/Promitor.Core.Telemetry/Loggers/Logger.cs create mode 100644 src/Promitor.Core.Telemetry/Loggers/RuntimeLogger.cs create mode 100644 src/Promitor.Core.Telemetry/Loggers/ValidationLogger.cs delete mode 100644 src/Promitor.Core.Telemetry/RuntimeLogger.cs diff --git a/docs/configuration/index.md b/docs/configuration/index.md index 7c90ea8f9..08bc79bd9 100644 --- a/docs/configuration/index.md +++ b/docs/configuration/index.md @@ -30,7 +30,13 @@ This information can be found on the newly created AD Application as documented The entity in the Azure AD needs to have `Monitoring Reader` permission on the resource group that will be queried. More information can be found [here](https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/monitoring-roles-permissions-security). -# Telemetry +# Logging +We provide insights in how our runtime is doing and is written to `stdout`. + +This can be controlled via the following environment variables: +- **PROMITOR_LOGGING_MINIMUMLEVEL** - Defines the minimum log level that should be logged. If none is configured, `Warning` will be used. Allowed values are `Trace`, `Debug`, `Information`, `Warning`, `Error`, `Critical`, `None` ordered from most to least verbose. + +## External Providers Promitor can send telemetry to Azure Application Insights when there is a need to. It currently supports: diff --git a/docs/index.md b/docs/index.md index 1e777b5cf..c9cf8eb55 100644 --- a/docs/index.md +++ b/docs/index.md @@ -47,7 +47,7 @@ And there is more on the way - Check our [backlog](https://github.com/tomkerkhov - [Runtime](configuration#runtime) - [Scraping](configuration#scraping) - [Authentication with Azure Monitor](configuration#authentication-with-azure-monitor) - - [Telemetry](configuration#telemetry) + - [Logging & External Providers](configuration#logging) - **Operations** - [Azure Resource Manager API - Consumption & Throttling](operations#azure-resource-manager-api---consumption--throttling) - [Health](operations#health) diff --git a/src/Promitor.Core.Telemetry/Loggers/Logger.cs b/src/Promitor.Core.Telemetry/Loggers/Logger.cs new file mode 100644 index 000000000..33a4addaf --- /dev/null +++ b/src/Promitor.Core.Telemetry/Loggers/Logger.cs @@ -0,0 +1,26 @@ +using System; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Console; + +#pragma warning disable 618 + +namespace Promitor.Core.Telemetry.Loggers +{ + public class Logger : ConsoleLogger + { + public Logger(string name) : base(name, (loggerName, logLevel) => IsFilteringRequired(logLevel), includeScopes: true) + { + } + + private static bool IsFilteringRequired(LogLevel usedLogLevel) + { + var rawMinimalLogLevel = Environment.GetEnvironmentVariable(EnvironmentVariables.Logging.MinimumLogLevel); + if (Enum.TryParse(rawMinimalLogLevel, out LogLevel minimalLogLevel)) + { + return minimalLogLevel <= usedLogLevel; + } + + return LogLevel.Warning <= usedLogLevel; + } + } +} \ No newline at end of file diff --git a/src/Promitor.Core.Telemetry/Loggers/RuntimeLogger.cs b/src/Promitor.Core.Telemetry/Loggers/RuntimeLogger.cs new file mode 100644 index 000000000..a83aaa553 --- /dev/null +++ b/src/Promitor.Core.Telemetry/Loggers/RuntimeLogger.cs @@ -0,0 +1,11 @@ +#pragma warning disable 618 + +namespace Promitor.Core.Telemetry.Loggers +{ + public class RuntimeLogger : Logger + { + public RuntimeLogger() : base("Runtime") + { + } + } +} \ No newline at end of file diff --git a/src/Promitor.Core.Telemetry/Loggers/ValidationLogger.cs b/src/Promitor.Core.Telemetry/Loggers/ValidationLogger.cs new file mode 100644 index 000000000..835c32ed8 --- /dev/null +++ b/src/Promitor.Core.Telemetry/Loggers/ValidationLogger.cs @@ -0,0 +1,11 @@ +#pragma warning disable 618 + +namespace Promitor.Core.Telemetry.Loggers +{ + public class ValidationLogger : Logger + { + public ValidationLogger() : base("Validation") + { + } + } +} \ No newline at end of file diff --git a/src/Promitor.Core.Telemetry/RuntimeLogger.cs b/src/Promitor.Core.Telemetry/RuntimeLogger.cs deleted file mode 100644 index 0f87ac564..000000000 --- a/src/Promitor.Core.Telemetry/RuntimeLogger.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Microsoft.Extensions.Logging.Console; -#pragma warning disable 618 - -namespace Promitor.Core.Telemetry -{ - public class RuntimeLogger : ConsoleLogger - { - public RuntimeLogger() : base("Runtime", (loggerName, logLevel) => true, includeScopes: true) - { - } - } -} \ No newline at end of file diff --git a/src/Promitor.Core/EnvironmentVariables.cs b/src/Promitor.Core/EnvironmentVariables.cs index 97831057c..fcdb734c6 100644 --- a/src/Promitor.Core/EnvironmentVariables.cs +++ b/src/Promitor.Core/EnvironmentVariables.cs @@ -28,5 +28,10 @@ public class Telemetry { public const string InstrumentationKey = "PROMITOR_TELEMETRY_INSTRUMENTATIONKEY"; } + + public class Logging + { + public const string MinimumLogLevel = "PROMITOR_LOGGING_MINIMUMLEVEL"; + } } } \ No newline at end of file diff --git a/src/Promitor.Scraper.Host/Startup.cs b/src/Promitor.Scraper.Host/Startup.cs index 29c4f4889..62eb9e7df 100644 --- a/src/Promitor.Scraper.Host/Startup.cs +++ b/src/Promitor.Scraper.Host/Startup.cs @@ -8,6 +8,7 @@ using Promitor.Core.Scraping.Configuration.Providers.Interfaces; using Promitor.Core.Telemetry; using Promitor.Core.Telemetry.Interfaces; +using Promitor.Core.Telemetry.Loggers; using Promitor.Core.Telemetry.Metrics; using Promitor.Core.Telemetry.Metrics.Interfaces; using Promitor.Scraper.Host.Extensions; diff --git a/src/Promitor.Scraper.Host/Validation/RuntimeValidator.cs b/src/Promitor.Scraper.Host/Validation/RuntimeValidator.cs index 65d2cfe14..664c05558 100644 --- a/src/Promitor.Scraper.Host/Validation/RuntimeValidator.cs +++ b/src/Promitor.Scraper.Host/Validation/RuntimeValidator.cs @@ -1,8 +1,8 @@ using System.Collections.Generic; using System.Linq; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Console; using Promitor.Core.Scraping.Configuration.Providers; +using Promitor.Core.Telemetry.Loggers; using Promitor.Scraper.Host.Validation.Exceptions; using Promitor.Scraper.Host.Validation.Interfaces; using Promitor.Scraper.Host.Validation.Steps; @@ -17,7 +17,7 @@ public class RuntimeValidator public RuntimeValidator() { - _validationLogger = new ConsoleLogger("Validation", (message, logLevel) => true, includeScopes: true); + _validationLogger = new ValidationLogger(); var scrapeConfigurationProvider = new MetricsDeclarationProvider(_validationLogger); _validationSteps = new List @@ -31,7 +31,7 @@ public RuntimeValidator() public void Run() { - var validationLogger = new ConsoleLogger("Validation", (message, logLevel) => true, includeScopes: true); + var validationLogger = new ValidationLogger(); validationLogger.LogInformation("Starting validation of Promitor setup"); var validationResults = RunValidationSteps(); diff --git a/src/docker-compose.override.yml b/src/docker-compose.override.yml index 1d7329e2e..5a17659d3 100644 --- a/src/docker-compose.override.yml +++ b/src/docker-compose.override.yml @@ -8,6 +8,7 @@ services: - PROMITOR_AUTH_APPID=ceb249a3-44ce-4c90-8863-6776336f5b7e - PROMITOR_AUTH_APPKEY=ZgLy6zYNh9SEmIl0B+rv+ZuQQ2wJyQi/tTXnp2Wp9PM= - PROMITOR_TELEMETRY_INSTRUMENTATIONKEY= + - PROMITOR_LOGGING_MINIMUMLEVEL=Trace - "SECRETS_STORAGEQUEUE_SAS=?sv=2018-03-28&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-07-28T02:33:14Z&st=2019-03-24T18:33:14Z&spr=https&sig=OiwNEYueCWlOhveapM1K6cRgV%2Be21gNhoq%2FDZqJEMZE%3D" ports: - "88" \ No newline at end of file