Skip to content

Commit 52bc34b

Browse files
committed
feat: enable sending of logs to Azure Monitor
1 parent 891ac2a commit 52bc34b

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

benefits/logging.py

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import os
2+
3+
14
def get_config(level):
2-
return {
5+
config = {
36
"version": 1,
47
"disable_existing_loggers": False,
58
"formatters": {
@@ -10,19 +13,38 @@ def get_config(level):
1013
},
1114
},
1215
"handlers": {
13-
"default": {
16+
"console": {
1417
"class": "logging.StreamHandler",
1518
"formatter": "default",
1619
},
1720
},
1821
"root": {
19-
"handlers": ["default"],
22+
"handlers": ["console"],
2023
"level": level,
2124
},
2225
"loggers": {
2326
"django": {
24-
"handlers": ["default"],
27+
"handlers": ["console"],
2528
"propagate": False,
2629
},
2730
},
2831
}
32+
33+
if "APPLICATIONINSIGHTS_CONNECTION_STRING" in os.environ:
34+
# enable Azure Insights logging
35+
36+
# https://docs.microsoft.com/en-us/azure/azure-monitor/app/opencensus-python#configure-logging-for-django-applications
37+
config["handlers"]["azure"] = {
38+
"class": "opencensus.ext.azure.log_exporter.AzureLogHandler",
39+
# send all logs
40+
"logging_sampling_rate": 1.0,
41+
}
42+
43+
# create custom logger
44+
# https://github.com/census-instrumentation/opencensus-python/issues/1130#issuecomment-1161898856
45+
config["loggers"]["azure"] = {
46+
"handlers": ["azure"],
47+
"level": "INFO",
48+
}
49+
50+
return config

docs/configuration/environment-variables.md

+10
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,13 @@ devcontainer, check the [`DJANGO_LOCAL_PORT`](#django_local_port).
149149

150150
[deployment]: ../deployment/README.md
151151
[getting-started_create-env]: ../getting-started/README.md#create-an-environment-file
152+
153+
## Azure
154+
155+
### `APPLICATIONINSIGHTS_CONNECTION_STRING`
156+
157+
!!! tldr "Azure docs"
158+
159+
[Azure Monitor connection strings](https://docs.microsoft.com/en-us/azure/azure-monitor/app/sdk-connection-string)
160+
161+
Enables [log collection](../../deployment/infrastructure/#logs). Set the value in quotes, e.g. `APPLICATIONINSIGHTS_CONNECTION_STRING="InstrumentationKey=…"`.

docs/deployment/infrastructure.md

+11
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ flowchart LR
6363

6464
We have [ping tests](https://docs.microsoft.com/en-us/azure/azure-monitor/app/monitor-web-app-availability) set up to notify about availability of the dev, test, and prod deployments. Alerts go to [#benefits-notify](https://cal-itp.slack.com/archives/C022HHSEE3F).
6565

66+
## Logs
67+
68+
We send application logs to [Azure Monitor Logs](https://docs.microsoft.com/en-us/azure/azure-monitor/logs/data-platform-logs). To find them:
69+
70+
1. [Open Application Insights.](https://portal.azure.com/#view/HubsExtension/BrowseResource/resourceType/microsoft.insights%2Fcomponents)
71+
1. Click the resource corresponding to the environment.
72+
1. In the navigation, under `Monitoring`, click `Logs`.
73+
1. In the Query Editor, type `traces`, and click `Run`.
74+
75+
You should see recent log output. Note [there is some latency](https://docs.microsoft.com/en-us/azure/azure-monitor/logs/data-ingestion-time).
76+
6677
## Making changes
6778

6879
1. Get access to the Azure account through the DevSecOps team.

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ Django==3.2.13
33
django-csp==3.7
44
git+https://github.com/cal-itp/eligibility-api#egg=eligibility_api
55
gunicorn==20.1.0
6+
opencensus-ext-azure==1.1.4
67
requests==2.28.0
78
six==1.16.0

0 commit comments

Comments
 (0)