From f72883a8b1bd89530220a3ea47fbc3164d841892 Mon Sep 17 00:00:00 2001 From: Aidan Feldman Date: Sat, 11 Feb 2023 21:50:50 +0000 Subject: [PATCH 1/2] chore(Azure): remove Azure error monitoring Wasn't working anyway. --- benefits/core/middleware.py | 15 --------------- benefits/settings.py | 7 +------ docs/deployment/troubleshooting.md | 2 +- 3 files changed, 2 insertions(+), 22 deletions(-) diff --git a/benefits/core/middleware.py b/benefits/core/middleware.py index 8fb2a4bff..75f4b5f98 100644 --- a/benefits/core/middleware.py +++ b/benefits/core/middleware.py @@ -161,21 +161,6 @@ def process_view(self, request, view_func, view_args, view_kwargs): return redirect("oauth:login") -# https://github.com/census-instrumentation/opencensus-python/issues/766 -class LogErrorToAzure(MiddlewareMixin): - def __init__(self, get_response): - super().__init__(get_response) - # wait to do this here to be sure the handler is initialized - self.azure_logger = logging.getLogger("azure") - - def process_exception(self, request, exception): - # https://stackoverflow.com/a/45532289 - msg = getattr(exception, "message", repr(exception)) - self.azure_logger.exception(msg, exc_info=exception) - - return None - - class RecaptchaEnabled(MiddlewareMixin): """Middleware configures the request with required reCAPTCHA settings.""" diff --git a/benefits/settings.py b/benefits/settings.py index 3b7444977..857aafa9f 100644 --- a/benefits/settings.py +++ b/benefits/settings.py @@ -75,12 +75,7 @@ def _filter_empty(ls): ENABLE_AZURE_INSIGHTS = "APPLICATIONINSIGHTS_CONNECTION_STRING" in os.environ print("ENABLE_AZURE_INSIGHTS: ", ENABLE_AZURE_INSIGHTS) if ENABLE_AZURE_INSIGHTS: - MIDDLEWARE.extend( - [ - "opencensus.ext.django.middleware.OpencensusMiddleware", - "benefits.core.middleware.LogErrorToAzure", - ] - ) + MIDDLEWARE.append("opencensus.ext.django.middleware.OpencensusMiddleware") # only used if enabled above OPENCENSUS = { diff --git a/docs/deployment/troubleshooting.md b/docs/deployment/troubleshooting.md index 9e2e1f964..bb8cf8a22 100644 --- a/docs/deployment/troubleshooting.md +++ b/docs/deployment/troubleshooting.md @@ -31,7 +31,7 @@ The following [tables](https://docs.microsoft.com/en-us/azure/azure-monitor/app/ In the latter two, you should see recent log output. Note [there is some latency](https://docs.microsoft.com/en-us/azure/azure-monitor/logs/data-ingestion-time). -See [`Failures`](https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-exceptions#diagnose-failures-using-the-azure-portal) in the sidebar (or `exceptions` under `Logs`) for application errors/exceptions. +Note that we aren't [sending exceptions to Azure Monitor](https://learn.microsoft.com/en-us/azure/azure-monitor/app/opencensus-python#send-exceptions) (aside from the logs), so you won't see anything show up under `Failures` or `Logs`->`exceptions`. #### Live tail From 09a7beeeff3c96005d27e59c1eb30856bd640f3d Mon Sep 17 00:00:00 2001 From: Aidan Feldman Date: Thu, 16 Feb 2023 04:03:03 +0000 Subject: [PATCH 2/2] chore(Azure): remove Azure OpenCensus integration Now that we have Sentry, we determined that the App Service logs are sufficient for other logs, and thus it isn't worth the complexity/confusion of having the logs sent to Azure Monitor. --- appcontainer/requirements.txt | 2 - benefits/logging.py | 49 --------------------- benefits/settings.py | 46 +++++++++++-------- docs/configuration/environment-variables.md | 14 ------ docs/deployment/troubleshooting.md | 15 ------- tests/pytest/test_logging.py | 13 ------ 6 files changed, 27 insertions(+), 112 deletions(-) delete mode 100644 benefits/logging.py delete mode 100644 tests/pytest/test_logging.py diff --git a/appcontainer/requirements.txt b/appcontainer/requirements.txt index c1cf92276..2ac3b74a9 100644 --- a/appcontainer/requirements.txt +++ b/appcontainer/requirements.txt @@ -2,8 +2,6 @@ Authlib==1.2.0 Django==4.1.6 django-csp==3.7 eligibility-api==2023.01.1 -opencensus-ext-azure==1.1.8 -opencensus-ext-django==0.8.0 requests==2.28.2 sentry-sdk==1.15.0 six==1.16.0 diff --git a/benefits/logging.py b/benefits/logging.py deleted file mode 100644 index 8c92e13ce..000000000 --- a/benefits/logging.py +++ /dev/null @@ -1,49 +0,0 @@ -def get_config(level="INFO", enable_azure=False): - config = { - "version": 1, - "disable_existing_loggers": False, - "formatters": { - "default": { - "format": "[{asctime}] {levelname} {name}:{lineno} {message}", - "datefmt": "%d/%b/%Y %H:%M:%S", - "style": "{", - }, - }, - "handlers": { - "console": { - "class": "logging.StreamHandler", - "formatter": "default", - }, - }, - "root": { - "handlers": ["console"], - "level": level, - }, - "loggers": { - "django": { - "handlers": ["console"], - "propagate": False, - }, - }, - } - - print("ENABLE AZURE: ", enable_azure) - - if enable_azure: - # enable Azure Insights logging - - # https://docs.microsoft.com/en-us/azure/azure-monitor/app/opencensus-python#configure-logging-for-django-applications - config["handlers"]["azure"] = { - "class": "opencensus.ext.azure.log_exporter.AzureLogHandler", - # send all logs - "logging_sampling_rate": 1.0, - } - - # create custom logger - # https://github.com/census-instrumentation/opencensus-python/issues/1130#issuecomment-1161898856 - config["loggers"]["azure"] = { - "handlers": ["azure"], - "level": level, - } - - return config diff --git a/benefits/settings.py b/benefits/settings.py index 857aafa9f..97cf1cfd8 100644 --- a/benefits/settings.py +++ b/benefits/settings.py @@ -3,7 +3,6 @@ """ import os from benefits import sentry -import benefits.logging def _filter_empty(ls): @@ -69,23 +68,6 @@ def _filter_empty(ls): MIDDLEWARE.append("benefits.core.middleware.DebugSession") -# Azure Insights -# https://docs.microsoft.com/en-us/azure/azure-monitor/app/opencensus-python-request#tracking-django-applications - -ENABLE_AZURE_INSIGHTS = "APPLICATIONINSIGHTS_CONNECTION_STRING" in os.environ -print("ENABLE_AZURE_INSIGHTS: ", ENABLE_AZURE_INSIGHTS) -if ENABLE_AZURE_INSIGHTS: - MIDDLEWARE.append("opencensus.ext.django.middleware.OpencensusMiddleware") - -# only used if enabled above -OPENCENSUS = { - "TRACE": { - "SAMPLER": "opencensus.trace.samplers.AlwaysOnSampler()", - "EXPORTER": "opencensus.ext.azure.trace_exporter.AzureExporter()", - } -} - - CSRF_COOKIE_AGE = None CSRF_COOKIE_SAMESITE = "Strict" CSRF_COOKIE_HTTPONLY = True @@ -221,7 +203,33 @@ def _filter_empty(ls): # Logging configuration LOG_LEVEL = os.environ.get("DJANGO_LOG_LEVEL", "DEBUG" if DEBUG else "WARNING") -LOGGING = benefits.logging.get_config(LOG_LEVEL, enable_azure=ENABLE_AZURE_INSIGHTS) +LOGGING = { + "version": 1, + "disable_existing_loggers": False, + "formatters": { + "default": { + "format": "[{asctime}] {levelname} {name}:{lineno} {message}", + "datefmt": "%d/%b/%Y %H:%M:%S", + "style": "{", + }, + }, + "handlers": { + "console": { + "class": "logging.StreamHandler", + "formatter": "default", + }, + }, + "root": { + "handlers": ["console"], + "level": LOG_LEVEL, + }, + "loggers": { + "django": { + "handlers": ["console"], + "propagate": False, + }, + }, +} sentry.configure() diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.md index e78bce7d7..4f6a4e637 100644 --- a/docs/configuration/environment-variables.md +++ b/docs/configuration/environment-variables.md @@ -158,20 +158,6 @@ The base URL for the (running) application, against which all Cypress `.visit()` When Cypress is running inside the devcontainer, this should be `http://localhost:8000`. When Cypress is running outside the devcontainer, check the [`DJANGO_LOCAL_PORT`](#django_local_port). -## Azure - -### `APPLICATIONINSIGHTS_CONNECTION_STRING` - -!!! tldr "Azure docs" - - [Azure Monitor connection strings](https://docs.microsoft.com/en-us/azure/azure-monitor/app/sdk-connection-string) - -Enables [log collection](../../deployment/troubleshooting/#logs). Set the value in quotes, e.g. `APPLICATIONINSIGHTS_CONNECTION_STRING="InstrumentationKey=…"`. - -[app-service-config]: https://docs.microsoft.com/en-us/azure/app-service/configure-common?tabs=portal -[benefits-secrets]: https://github.com/cal-itp/benefits-secrets -[getting-started_create-env]: ../getting-started/README.md#create-an-environment-file - ## Sentry ### `SENTRY_DSN` diff --git a/docs/deployment/troubleshooting.md b/docs/deployment/troubleshooting.md index bb8cf8a22..0253b6439 100644 --- a/docs/deployment/troubleshooting.md +++ b/docs/deployment/troubleshooting.md @@ -8,8 +8,6 @@ We have [ping tests](https://docs.microsoft.com/en-us/azure/azure-monitor/app/mo ### Logs -Logs can be found a couple of places: - #### Azure App Service Logs [Open the `Logs` for the environment you are interested in.](https://docs.google.com/document/d/11EPDIROBvg7cRtU2V42c6VBxcW_o8HhcyORALNtL_XY/edit#heading=h.6pxjhslhxwvj) The following tables are likely of interest: @@ -20,19 +18,6 @@ Logs can be found a couple of places: For some pre-defined queries, click `Queries`, then `Group by: Query type`, and look under `Query pack queries`. -#### [Azure Monitor Logs](https://docs.microsoft.com/en-us/azure/azure-monitor/logs/data-platform-logs) - -[Open the `Logs` for the environment you are interested in.](https://docs.google.com/document/d/11EPDIROBvg7cRtU2V42c6VBxcW_o8HhcyORALNtL_XY/edit#heading=h.n0oq4r1jo7zs) - -The following [tables](https://docs.microsoft.com/en-us/azure/azure-monitor/app/opencensus-python#telemetry-type-mappings) are likely of interest: - -- `requests` -- `traces` - -In the latter two, you should see recent log output. Note [there is some latency](https://docs.microsoft.com/en-us/azure/azure-monitor/logs/data-ingestion-time). - -Note that we aren't [sending exceptions to Azure Monitor](https://learn.microsoft.com/en-us/azure/azure-monitor/app/opencensus-python#send-exceptions) (aside from the logs), so you won't see anything show up under `Failures` or `Logs`->`exceptions`. - #### Live tail After [setting up the Azure CLI](#making-changes), you can use the following command to [stream live logs](https://docs.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs#in-local-terminal): diff --git a/tests/pytest/test_logging.py b/tests/pytest/test_logging.py deleted file mode 100644 index 2fdcb06d9..000000000 --- a/tests/pytest/test_logging.py +++ /dev/null @@ -1,13 +0,0 @@ -from benefits.logging import get_config - - -def test_get_config_no_azure(): - config = get_config() - assert "azure" not in config["handlers"] - assert "azure" not in config["loggers"] - - -def test_get_config_with_azure(): - config = get_config(enable_azure=True) - assert "azure" in config["handlers"] - assert "azure" in config["loggers"]