Skip to content

Commit

Permalink
feat: send incoming requests to Azure Monitor logs
Browse files Browse the repository at this point in the history
  • Loading branch information
afeld committed Jun 22, 2022
1 parent 4db21eb commit 5f53549
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
7 changes: 2 additions & 5 deletions benefits/logging.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import os


def get_config(level):
def get_config(level, enable_azure=False):
config = {
"version": 1,
"disable_existing_loggers": False,
Expand Down Expand Up @@ -30,7 +27,7 @@ def get_config(level):
},
}

if "APPLICATIONINSIGHTS_CONNECTION_STRING" in os.environ:
if enable_azure:
# enable Azure Insights logging

# https://docs.microsoft.com/en-us/azure/azure-monitor/app/opencensus-python#configure-logging-for-django-applications
Expand Down
21 changes: 19 additions & 2 deletions benefits/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,24 @@ def _filter_empty(ls):
)

if DEBUG:
MIDDLEWARE.extend(["benefits.core.middleware.DebugSession"])
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
if ENABLE_AZURE_INSIGHTS:
MIDDLEWARE.append("opencensus.ext.django.middleware.OpencensusMiddleware")

# only used if enabled above
OPENCENSUS = {
"TRACE": {
"SAMPLER": "opencensus.trace.samplers.ProbabilitySampler(rate=1)",
"EXPORTER": "opencensus.ext.azure.trace_exporter.AzureExporter()",
}
}


CSRF_COOKIE_AGE = None
CSRF_COOKIE_SAMESITE = "Strict"
Expand Down Expand Up @@ -194,7 +211,7 @@ 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)
LOGGING = benefits.logging.get_config(LOG_LEVEL, enable_azure=ENABLE_AZURE_INSIGHTS)

# Analytics configuration

Expand Down
3 changes: 2 additions & 1 deletion docs/deployment/infrastructure.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ We send application logs to [Azure Monitor Logs](https://docs.microsoft.com/en-u
1. [Open Application Insights.](https://portal.azure.com/#view/HubsExtension/BrowseResource/resourceType/microsoft.insights%2Fcomponents)
1. Click the resource corresponding to the environment.
1. In the navigation, under `Monitoring`, click `Logs`.
1. In the Query Editor, type `traces`, and click `Run`.
1. In the Query Editor, type `requests` or `traces`, and click `Run`.
- [What each means](https://docs.microsoft.com/en-us/azure/azure-monitor/app/opencensus-python#telemetry-type-mappings)

You should see recent log output. Note [there is some latency](https://docs.microsoft.com/en-us/azure/azure-monitor/logs/data-ingestion-time).

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ django-csp==3.7
git+https://github.com/cal-itp/eligibility-api#egg=eligibility_api
gunicorn==20.1.0
opencensus-ext-azure==1.1.4
opencensus-ext-django==0.7.6
requests==2.28.0
six==1.16.0

0 comments on commit 5f53549

Please sign in to comment.