@@ -46,13 +46,56 @@ make install-packages opts="--extras 'feature1 feature2'"
4646
4747#### Installation
4848
49- 1 . Make sure ` "common.core" ` is in the ` INSTALLED_APPS ` of your settings module.
49+ 1 . To make use of the ` test_tools ` Pytest plugin, install the packages with the ` test-tools ` extra, e.g. ` pip install flagsmith-common[test-tools] ` .
50+
51+ 2 . Make sure ` "common.core" ` is in the ` INSTALLED_APPS ` of your settings module.
5052This enables the ` manage.py flagsmith ` commands.
5153
52- 2 . Add ` "common.gunicorn.middleware.RouteLoggerMiddleware" ` to ` MIDDLEWARE ` in your settings module.
54+ 3 . Add ` "common.gunicorn.middleware.RouteLoggerMiddleware" ` to ` MIDDLEWARE ` in your settings module.
5355This enables the ` route ` label for Prometheus HTTP metrics.
5456
55- 3 . To enable the ` /metrics ` endpoint, set the ` PROMETHEUS_ENABLED ` setting to ` True ` .
57+ 4 . To enable the ` /metrics ` endpoint, set the ` PROMETHEUS_ENABLED ` setting to ` True ` .
58+
59+ #### Test tools
60+
61+ ##### Fixtures
62+
63+ ###### ` assert_metric `
64+
65+ To test your metrics using the ` assert_metric ` fixture:
66+
67+ ``` python
68+ from common.test_tools import AssertMetricFixture
69+
70+ def test_my_code__expected_metrics (assert_metric : AssertMetricFixture) -> None :
71+ # When
72+ my_code()
73+
74+ # Then
75+ assert_metric(
76+ name = " flagsmith_distance_from_earth_au_sum" ,
77+ labels = {" engine_type" : " solar_sail" },
78+ value = 1.0 ,
79+ )
80+ ```
81+
82+ ###### ` saas_mode `
83+
84+ The ` saas_mode ` fixture makes all ` common.core.utils.is_saas ` calls return ` True ` .
85+
86+ ###### ` enterprise_mode `
87+
88+ The ` enterprise_mode ` fixture makes all ` common.core.utils.is_enterprise ` calls return ` True ` .
89+
90+ ##### Markers
91+
92+ ###### ` pytest.mark.saas_mode `
93+
94+ Use this mark to auto-use the ` saas_mode ` fixture.
95+
96+ ###### ` pytest.mark.enterprise_mode `
97+
98+ Use this mark to auto-use the ` enterprise_mode ` fixture.
5699
57100#### Metrics
58101
@@ -87,14 +130,18 @@ It's generally a good idea to allow users to define histogram buckets of their o
87130import prometheus_client
88131from django.conf import settings
89132
90- flagsmith_distance_from_earth_au = prometheus .Histogram(
133+ flagsmith_distance_from_earth_au = prometheus_client .Histogram(
91134 " flagsmith_distance_from_earth_au" ,
92135 " Distance from Earth in astronomical units" ,
136+ labels = [" engine_type" ],
93137 buckets = settings.DISTANCE_FROM_EARTH_AU_HISTOGRAM_BUCKETS ,
94138)
95139```
96140
141+ For testing your metrics, refer to [ ` assert_metric ` documentation] [ 5 ] .
142+
97143[ 1 ] : https://prometheus.io/docs/practices/naming/
98144[ 2 ] : https://github.com/Flagsmith/flagsmith-common/blob/main/src/common/gunicorn/metrics.py
99145[ 3 ] : https://docs.gunicorn.org/en/stable/design.html#server-model
100146[ 4 ] : https://prometheus.github.io/client_python/multiprocess
147+ [ 5 ] : #assert_metric
0 commit comments