Repository: stephenhillier/starlette_exporter

Issue:
Add option to use custom buckets
It would be nice if we could specify the default buckets for the histogram like this:

```python
buckets = (.5, 1, 3, 5, 10, 15, 20, 30, 45, 60, 80, 100, 125, 150, 175, 200, 250, 300, 400)
app.add_middleware(PrometheusMiddleware, group_paths=True, buckets=buckets)
app.add_route("/metrics", handle_metrics)
```
They provide something similar in the flask_exporter: https://github.com/rycus86/prometheus_flask_exporter/blob/master/prometheus_flask_exporter/__init__.py

Is this something I could put a pull request in for?

Discussion:

Patch:
diff --git a/setup.py b/setup.py
index 441e13f..1d286cc 100644
--- a/setup.py
+++ b/setup.py
@@ -2,7 +2,7 @@ from setuptools import setup
 
 setup(
     name='starlette_exporter',
-    version='0.5.0',
+    version='0.5.1',
     author='Stephen Hillier',
     author_email='stephenhillier@gmail.com',
     packages=['starlette_exporter'],
diff --git a/starlette_exporter/middleware.py b/starlette_exporter/middleware.py
index aeeadec..4b04cd6 100644
--- a/starlette_exporter/middleware.py
+++ b/starlette_exporter/middleware.py
@@ -18,12 +18,15 @@ class PrometheusMiddleware:
 
     def __init__(
         self, app: ASGIApp, group_paths: bool = False, app_name: str = "starlette",
-        prefix="starlette"
+        prefix="starlette", buckets=None
     ):
         self.app = app
         self.group_paths = group_paths
         self.app_name = app_name
         self.prefix = prefix
+        self.kwargs = {}
+        if buckets is not None:
+            self.kwargs['buckets'] = buckets
 
     # Starlette initialises middleware multiple times, so store metrics on the class
     @property
@@ -45,6 +48,7 @@ class PrometheusMiddleware:
                 metric_name,
                 "HTTP request duration, in seconds",
                 ("method", "path", "status_code", "app_name"),
+                **self.kwargs,
             )
         return PrometheusMiddleware._metrics[metric_name]
 


Test patch:
diff --git a/tests/test_middleware.py b/tests/test_middleware.py
index 4ec92d6..776b8ab 100644
--- a/tests/test_middleware.py
+++ b/tests/test_middleware.py
@@ -109,6 +109,33 @@ class TestMiddleware:
             in metrics
         )
 
+    def test_histogram_custom_buckets(self, testapp):
+        """ test that custom histogram buckets appear after making requests """
+
+        buckets = (10, 20, 30, 40, 50)
+        client = TestClient(testapp(buckets=buckets))
+        client.get('/200')
+        client.get('/500')
+        try:
+            client.get('/unhandled')
+        except:
+            pass
+
+        metrics = client.get('/metrics').content.decode()
+
+        assert (
+            """starlette_request_duration_seconds_bucket{app_name="starlette",le="50.0",method="GET",path="/200",status_code="200"}"""
+            in metrics
+        )
+        assert (
+            """starlette_request_duration_seconds_bucket{app_name="starlette",le="50.0",method="GET",path="/500",status_code="500"}"""
+            in metrics
+        )
+        assert (
+            """starlette_request_duration_seconds_bucket{app_name="starlette",le="50.0",method="GET",path="/unhandled",status_code="500"}"""
+            in metrics
+        )
+
     def test_app_name(self, testapp):
         """ test that app_name label is populated correctly """
         client = TestClient(testapp(app_name="testing"))