Open gorkaerana opened 8 months ago
+1
+1
Hi -- I wasn't familiar with Streamlit but it looks like it can re-run scripts from top to bottom in response to user interactions, reusing a single Python process. So you have to be careful with operations that are designed to happen only once per Python process, such as setting the global tracer provider or setting up an instrumentor.
@gorkaerana
As @pmcollins has mentioned above, certain components of OpenTelemtry and in addition components of Azure monitor built on top of OpenTelemetry are not meant to be instantiated more than once. Besides ...Provider
classes, we have no explicitly safeguards for this. I am also not too familiar with Streamlit
but if it's anything similar to databricks notebooks in which multiple runs of the same code are done using the same process and memory is saved between runs, this will not be the intended use case for our SDKs. Even if you do find a way to cache the ...Provider
classes, you will experience additional issues such as duplicate telemetry and every growing thread creation.
If you really want to get this scenario working, you must ensure that EACH component that is meant to be a singleton is used be only instantiated once. You have attempted to do this with tracer
and logger
, which actually can created more than once (usually tracers and loggers represent telemetry captured within a Python file since they are usually namespaced by __name__
. The components you actually want to cache are the ...Provider
classes from the OpenTelemetry side, as well as the exporter
classes within Azure monitor. Now, you are using configure_azure_monitor()
, which is an API of the Azure monitor distro, which is intended as a one-stop-shop solution that will instatiate everything for you using one api call. Now, while a simple api, this is probably not the component you want to be using for your use case, as we do not expose the underlying components that are being instantiated under the hood. You would be better off using the azure-monitor-opentelemetry-exporters, which the distro is built on top of. The components you need to cache are AzureMonitor<Trace/Log/Metrics>Exporter
. Please keep in mind that this is a uncommon scenario and you might have to do some hacky stuff to get it working, including taking dependencies on beta libraries.
Hi @gorkaerana ,
I have set up the same environment you want by simply following the instructions here: https://learn.microsoft.com/en-us/azure/azure-monitor/app/opentelemetry-enable?tabs=python
Then for Streamlit, use the cache_resource decorator: https://docs.streamlit.io/develop/api-reference/caching-and-state/st.cache_resource.
Example:
import streamlit as st
import logging
from azure.monitor.opentelemetry import configure_azure_monitor
@st.cache_resource
def _configure_logging():
configure_azure_monitor()
def main() -> None:
_configure_logging()
logging.error(f"This is an error.")
Hope this helps.
Hi @claorden,
Your minimal example does indeed work, thank you very much for your help 😃 I'll retrofit it to our code base one of these days and report back with the results.
Best, Gorka.
Hello,
We have a Streamlit app in production and we are hoping to add observability to it by leveraging
opentelemetry.instrumentation.logging.LoggingInstrumentor
to integrate the current logging and OpenTelemetry to publish logs to Azure Monitor. I believe Streamlit executing the same piece of code more than once at runtime results inOverriding of current TracerProvider is not allowed
messages, which in turn only publishesAttempting to instrument while already instrumented
to Azure Monitor. I have tried to circumvent the issue the following few ways:example.py
and runningstreamlit run example.py
.)opentelemetry._logs
, but the issue I'm hoping to describe seems to be intrinsic to how the OpenTelemetry Python SDK functions, so I don't think that would solve it either. Plus, that part of the API is experimental.opentelemetry.trace._TRACER_PROVIDER
viaopentelemetry.trace.set_tracer_provider
and related functions.Please do let me know if this is not the place to ask such questions. I tried asking in the Streamlit forum but I have received no answers so far. Any help would be very much appreciated 😊
Thank you in advance, Gorka.