Skip to content

Commit 4ad6a0d

Browse files
committed
Added basic instructions to use the recommended library over one being phased out
1 parent 0ed9c2b commit 4ad6a0d

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

README.md

Lines changed: 18 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 anything into `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`.

0 commit comments

Comments
 (0)