You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+29-11Lines changed: 29 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -128,22 +128,29 @@ We need to improve this:
128
128
129
129
Within your resource group create a new Application Insights resource.
130
130
131
-
Find the connection string (containing a sensitive "instrumentation key") on the overview page of your Application Insights resource. Rather than hardcoding the connection string in the Python code, configure it securely by using an environment variable called `APPLICATIONINSIGHTS_CONNECTION_STRING`. Set this on the 'Configuration' page of the App Service.
131
+
Find the connection string (containing a sensitive "instrumentation key") on the overview page of your Application Insights resource. Rather than hardcoding the connection string in the Python code, configure it securely by using an environment variable called `APPLICATIONINSIGHTS_CONNECTION_STRING`. Set this on the 'Environment Variables' page of the App Service.
132
132
133
-
> Don't forget to click the `Save` button at the top of the Configuration page after making changes!
133
+
> Don't forget to click the `Apply` button at the bottom of the Environment Variables page after making changes!
134
134
135
-
To actually send logs to Application Insights you'll need to add the Python packages `opencensus-ext-azure` and `opencensus-ext-flask` to requirements.txt.
136
-
It's a good habit to specify a version when adding these: you can find the latest version specified on the Python Package Index page for [the Azure extension](https://pypi.org/project/opencensus-ext-azure/) and [the Flask extension](https://pypi.org/project/opencensus-ext-flask/) respectively.
135
+
To actually send logs to Application Insights you'll need to add the Python packages `azure-monitor-opentelemetry` to requirements.txt.
136
+
It's a good habit to specify a version when adding these: you can find the latest version specified on the Python Package Index page for [the Azure extension](https://pypi.org/project/azure-monitor-opentelemetry/).
137
137
138
-
Next, add the middleware to`app.py` by adapting this sample code: <https://docs.microsoft.com/en-us/azure/azure-monitor/app/opencensus-python-request#tracking-flask-applications>. That middleware will log all requests to your Flask app (like every time you view the webpage) and the `AzureExporter` will send those logs to Application Insights.
138
+
Next, we configure the logging in`app.py` by adapting this sample code: <https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry/samples/tracing/http_flask.py>. This will log all requests to your Flask app (like every time you view the webpage) and the in-built exporter will send those logs to Application Insights.
139
139
140
-
> The environment variable gets picked up automatically so your code doesn't need to pass anything into `AzureExporter()`. Just make sure the application setting in Azure is the whole connection string, including `InstrumentationKey=`!
140
+
> The environment variable gets picked up automatically so your code doesn't need to pass it directly into the `configure_azure_monitor()` call. Just make sure the application setting in Azure is the whole connection string, including `InstrumentationKey=`!
141
141
142
-
Finally, to send `logger.info` messages there too, register a "log handler". Import `AzureLogHandler` from `opencensus.ext.azure.log_exporter`, then add it as a handler for our Flask app logs:
143
-
144
-
```python
145
-
app.logger.addHandler(AzureLogHandler())
146
-
```
142
+
> Note that the auto-configuration relies on a strict ordering and flask needs to be _imported_ after we have run the configuration - so you may have code like:
A few minutes after deploying your changes (App Insights batches up log messages) you can see the logs.
149
156
Go the App Insights resource and then navigate to `Logs`.
@@ -183,6 +190,17 @@ Back in App Insights under Alerts you'll see an alert is raised.
183
190
With the default settings this will take up to 5 minutes.
184
191
If you set up emails above you'll start to receive these.
185
192
193
+
## Add Live Metrics
194
+
195
+
You may have noticed that even once the app is reporting logs to App Insights they lag the system slightly, e.g. searching your traces may not show logging statements from more than a minute ago. In many cases this is sufficient, but there may be times that we want lower latency on our stats - for example whilst monitoring a release.
196
+
197
+
Azure offers ["Live Metrics"](https://learn.microsoft.com/en-us/azure/azure-monitor/app/live-stream?tabs=otel) to help with this. Try turning that on now by adjusting your setup command to:
198
+
```python
199
+
configure_azure_monitor(enable_live_metrics=True)
200
+
```
201
+
202
+
Once that's deployed, check out the "Live Metrics" tab (under the "Investigate" menu) in Application Insights. Again, it might take a few minutes to appear, but once it does you'll see that the traces on display are coming through within a second or two of being generated.
203
+
186
204
## Improve the queue
187
205
188
206
You'll now receive an email every 5 minutes until Jan 1st 3000.
0 commit comments