Flagsmith's common library
The project assumes the following tools installed:
To list available Makefile targets, run make help.
To set up local development environment, run make install.
To run linters, run make lint.
To run tests, run make test.
-
Install all runtime packages:
uv add flagsmith-common[common-core,task-processor] -
To enable the Pytest fixtures, run
uv add --G dev flagsmith-common[test-tools]. Skipping this step will make Pytest collection fail due to missing dependencies. -
Make sure
"common.core"is in theINSTALLED_APPSof your settings module. This enables themanage.py flagsmithcommands. -
Add
"common.gunicorn.middleware.RouteLoggerMiddleware"toMIDDLEWAREin your settings module. This enables theroutelabel for Prometheus HTTP metrics. -
To enable the
/metricsendpoint, set thePROMETHEUS_ENABLEDsetting toTrue.
To test your metrics using the assert_metric fixture:
from common.test_tools import AssertMetricFixture
def test_my_code__expected_metrics(assert_metric: AssertMetricFixture) -> None:
# When
my_code()
# Then
assert_metric(
name="flagsmith_distance_from_earth_au_sum",
labels={"engine_type": "solar_sail"},
value=1.0,
)The saas_mode fixture makes all common.core.utils.is_saas calls return True.
The enterprise_mode fixture makes all common.core.utils.is_enterprise calls return True.
Use this mark to auto-use the saas_mode fixture.
Use this mark to auto-use the enterprise_mode fixture.
Flagsmith uses Prometheus to track performance metrics.
The following default metrics are exposed:
flagsmith_build_info: Has the labelsversionandci_commit_sha.flagsmith_http_server_request_duration_seconds: Histogram labeled withmethod,route, andresponse_status.flagsmith_http_server_requests_total: Counter labeled withmethod,route, andresponse_status.flagsmith_http_server_response_size_bytes: Histogram labeled withmethod,route, andresponse_status.flagsmith_task_processor_enqueued_tasks_total: Counter labeled withtask_identifier.
flagsmith_task_processor_finished_tasks_total: Counter labeled withtask_identifier,task_type("recurring","standard") andresult("success","failure").flagsmith_task_processor_task_duration_seconds: Histogram labeled withtask_identifier,task_type("recurring","standard") andresult("success","failure").
Try to come up with meaningful metrics to cover your feature with when developing it. Refer to Prometheus best practices when naming your metric and labels.
As a reasonable default, Flagsmith metrics are expected to be namespaced with the "flagsmith_" prefix.
Define your metrics in a metrics.py module of your Django application — see example. Contrary to Prometheus Python client examples and documentation, please name a metric variable exactly as your metric name.
It's generally a good idea to allow users to define histogram buckets of their own. Flagsmith accepts a PROMETHEUS_HISTOGRAM_BUCKETS setting so users can customise their buckets. To honour the setting, use the common.prometheus.Histogram class when defining your histograms. When using prometheus_client.Histogram directly, please expose a dedicated setting like so:
import prometheus_client
from django.conf import settings
flagsmith_distance_from_earth_au = prometheus_client.Histogram(
"flagsmith_distance_from_earth_au",
"Distance from Earth in astronomical units",
labels=["engine_type"],
buckets=settings.DISTANCE_FROM_EARTH_AU_HISTOGRAM_BUCKETS,
)For testing your metrics, refer to assert_metric documentation.