Prometheus integration for Streamlit app metrics

You can now integrate Prometheus to your Streamlit apps for monitoring and analytics with a new streamlit extra: 📊 Prometheus - streamlit-extras

Prometheus is a popular tool for collecting metrics on apps and services hosted in cloud environments. Typical use cases include usage analytics, performance data and health / reliability. This integration provides a slight hack to allow the app developer to write custom metrics using the normal prometheus python SDK which are then exposed on a web server endpoint for scraping.

# Slightly simplified example for reference
from streamlit_extras import prometheus
from prometheus_client import Counter

# For a real usage, put in a separate file and import for persistence
SLIDER_COUNT = Counter(
    name="slider_counter",
    documentation="Sum of slider values",
    registry=prometheus.streamlit_registry()
)

latest = st.slider("Latest value", 0, 20, 3)
if st.button("Submit"):
    SLIDER_COUNT.inc(latest)
streamlit run my_slider_app.py
curl localhost:8501/_stcore/metrics

Output of curl (can be picked up by an automated scraper):

# TYPE slider_counter counter
# HELP slider_counter Sum of slider values
slider_counter_total{} 14.0
slider_counter_created{} 1.7042185907557938e+09

You can see a full example here. This works best on Streamlit 1.31.

Note: If you already use OpenTelemetry, it should be possible to run an OTel Collector alongside the Streamlit app to convert from Prometheus to OpenTelemetry format. Here’s an example for Google Cloud Run. I am hoping to write up a short doc on this with a Streamlit specific use case when I have some time :slight_smile:

What’s next?

As a product manager for Streamlit :person_with_crown: I want to explore deeper, native integrations for app telemetry including OpenTelemetry which are geared towards an organization’s internal apps, as well as tools like Google Analytics which have been widely requested. Expect to hear more about this in the coming months.

I see this integration as a quick win and a first step in that direction. If you try it out, I would love to hear from you about how it works and what you’re doing with it! Or what breaks :sweat_smile: . This will inform our roadmap and how we prioritize any native integrations and telemetry features. Thanks!!

8 Likes

Underrated feature - thank you! I’m definitely going to try this out with my enterprise’s Prometheus-based observability platform.

2 Likes

Hey there,
Thanks a lot for the feature.

However, I am facing some difficulty connecting it with Prometheus installed locally to view visualizations, similar to the post you have shared here. I am on Ubuntu 23.10.
The curl command works well with the example in the github repo, but I am looking for the steps to connect it to Prometheus for the graph monitoring.

It would be great if someone could guide me on this part.