Skip to content

Commit 58f8435

Browse files
authored
Merge pull request corndeladmin#2 from corndeladmin/update-to-use-opentelemetry
Added basic instructions to use the recommended library
2 parents 0ed9c2b + c0b7339 commit 58f8435

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

README.md

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,29 @@ We need to improve this:
128128

129129
Within your resource group create a new Application Insights resource.
130130

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.
132132

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!
134134
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/).
137137

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.
139139

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=`!
141141
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:
143+
> ```python
144+
> from azure.monitor.opentelemetry import configure_azure_monitor
145+
> import logging
146+
>
147+
> logging.basicConfig(level=logging.INFO)
148+
> configure_azure_monitor()
149+
>
150+
> from flask import Flask, render_template, request
151+
> app = Flask(__name__)
152+
> ...
153+
> ```
147154
148155
A few minutes after deploying your changes (App Insights batches up log messages) you can see the logs.
149156
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.
183190
With the default settings this will take up to 5 minutes.
184191
If you set up emails above you'll start to receive these.
185192

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+
186204
## Improve the queue
187205

188206
You'll now receive an email every 5 minutes until Jan 1st 3000.

0 commit comments

Comments
 (0)