Skip to content

πŸ“ŠΒ Β Prometheus

Submitted by Joshua Carroll

Summary

Expose Prometheus metrics (https://prometheus.io) from your Streamlit app.

Functions

streamlit_registry

Expose Prometheus metrics (https://prometheus.io) from your Streamlit app.

Create and use Prometheus metrics in your app with registry=streamlit_registry(). The metrics will be exposed at Streamlit's existing /_stcore/metrics route.

Note: This extra works best with Streamlit >= 1.31. There are known bugs with some earlier Streamlit versions, especially 1.30.0.

See more example metrics in the Prometheus Python docs: https://prometheus.github.io/client_python/

To produce accurate metrics, you are responsible to ensure that unique metric objects are shared across app runs and sessions. We recommend either 1) initialize metrics in a separate file and import them in the main app script, or 2) initialize metrics in a cached function (and ensure the cache is not cleared during execution).

For an app running locally you can view the output with curl localhost:8501/_stcore/metrics or equivalent.

Source code in src/streamlit_extras/prometheus/__init__.py
@extra
def streamlit_registry() -> CollectorRegistry:
    """
    Expose Prometheus metrics (https://prometheus.io) from your Streamlit app.

    Create and use Prometheus metrics in your app with `registry=streamlit_registry()`.
    The metrics will be exposed at Streamlit's existing `/_stcore/metrics` route.

    **Note:** This extra works best with Streamlit >= 1.31. There are known bugs with
    some earlier Streamlit versions, especially 1.30.0.

    See more example metrics in the Prometheus Python docs:
    https://prometheus.github.io/client_python/

    To produce accurate metrics, you are responsible to ensure that unique metric
    objects are shared across app runs and sessions. We recommend either 1) initialize
    metrics in a separate file and import them in the main app script, or 2) initialize
    metrics in a cached function (and ensure the cache is not cleared during execution).

    For an app running locally you can view the output with
    `curl localhost:8501/_stcore/metrics` or equivalent.
    """
    from streamlit import runtime

    stats = runtime.get_instance().stats_mgr

    # Did we already register it elsewhere? If so, return that copy
    for prv in stats._cache_stats_providers:
        if isinstance(prv, PrometheusMetricsProvider):
            return prv.registry

    # This is the function was called, so create the registry
    # and hook it into Streamlit stats
    registry = CollectorRegistry(auto_describe=True)
    prv = PrometheusMetricsProvider(registry=registry)
    stats.register_provider(prv)
    return registry

Import:

from streamlit_extras.prometheus import streamlit_registry # (1)!
  1. You should add this to the top of your .py file πŸ› 

Examples

example

def example():
    import streamlit as st
    from prometheus_client import Counter

    @st.cache_resource
    def get_metric():
        registry = streamlit_registry()
        return Counter(
            name="my_counter",
            documentation="A cool counter",
            labelnames=("app_name",),
            registry=registry,  # important!!
        )

    SLIDER_COUNT = get_metric()

    app_name = st.text_input("App name", "prometheus_app")
    latest = st.slider("Latest value", 0, 20, 3)
    if st.button("Submit"):
        SLIDER_COUNT.labels(app_name).inc(latest)

    st.write(
        """
        View a fuller example that uses the (safer) import metrics method at:
        https://github.com/arnaudmiribel/streamlit-extras/tree/main/src/streamlit_extras/prometheus/example
        """
    )

    st.write(
        """
        ### Example output at `{host:port}/_stcore/metrics`
        ```
        # TYPE my_counter counter
        # HELP my_counter A cool counter
        my_counter_total{app_name="prometheus_app"} 14.0
        my_counter_created{app_name="prometheus_app"} 1.7042185907557938e+09
        ```
        """
    )