I have a Streamlit app running as a Docker container on Azure App Service. I’d like to start using Azure App Insights to log requests and errors. This requires adding telemetry code to the app, as described here.
To add temeltry to a Flask app, I would add the following to the top of the main app.py file:
from opencensus.ext.azure.trace_exporter import AzureExporter
from opencensus.ext.flask.flask_middleware import FlaskMiddleware
from opencensus.trace.samplers import ProbabilitySampler
app = Flask(__name__)
middleware = FlaskMiddleware(
app,
exporter=AzureExporter(connection_string=APP_INSIGHTS_CONN_STR),
sampler=ProbabilitySampler(rate=1.0),
)
Does anyone know how to do this for a Streamlit app? I suspect that I need a different way of defining app in the code above.
I do not have a complete solution to this but one option is to use the open census extensions to add tracing for what happens at the BE.
For example you can add open-census-ext-requests to be attached to any back end stuff. Or you can completely just use the open-census telemetry gatherer to attach to certain BE processes.
opencensus-ext-azure · PyPI - you can check out the requests extension in addition to it.
Some code snippet for adding the base logging tracing and initialing the requests. Check out the open census ext from Pypi for more inspiration on how to attach it to your specific needs.
from opencensus.ext.azure.trace_exporter import AzureExporter
from opencensus.trace import attributes_helper
from opencensus.trace import config_integration
from opencensus.trace import stack_trace
from opencensus.trace.samplers import ProbabilitySampler
from opencensus.trace.tracer import Tracer
"""
Tornado server logs are named: 'tornado.access', 'tornado.application', 'tornado.general' currently Not used.
"""
for log_name in ('streamlit', 'access', 'application', 'general'):
logger = logging.getLogger(log_name)
logger.setLevel(_config.get_logging_level())
if app_insights_connection := _config.get_app_insights_connection_string():
logger.addHandler(AzureLogHandler(connection_string=app_insights_connection))
Another code snippet for adding the requests tracer.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.