Flagsmith's common library
This project uses Poetry for dependency management and includes a Makefile to simplify common development tasks.
- Python >= 3.11
- Make
You can set up your development environment using the provided Makefile:
# Install everything (pip, poetry, and project dependencies)
make install
# Individual installation steps are also available
make install-pip # Upgrade pip
make install-poetry # Install Poetry
make install-packages # Install project dependencies
Run linting checks using pre-commit:
make lint
Additional options can be passed to the install-packages
target:
# Install with development dependencies
make install-packages opts="--with dev"
# Install with specific extras
make install-packages opts="--extras 'feature1 feature2'"
-
Make sure
"common.core"
is in theINSTALLED_APPS
of your settings module. This enables themanage.py flagsmith
commands. -
Add
"common.gunicorn.middleware.RouteLoggerMiddleware"
toMIDDLEWARE
in your settings module. This enables theroute
label for Prometheus HTTP metrics. -
To enable the
/metrics
endpoint, set thePROMETHEUS_ENABLED
setting toTrue
.
Flagsmith uses Prometheus to track performance metrics.
The following default metrics are exposed:
flagsmith_build_info
: Has the labelsversion
andci_commit_sha
.http_server_request_duration_seconds
: Histogram labeled withmethod
,route
, andresponse_status
.http_server_requests_total
: Counter labeled withmethod
,route
, andresponse_status
.
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.
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
distance_from_earth_au = prometheus.Histogram(
"distance_from_earth_au",
"Distance from Earth in astronomical units",
buckets=settings.DISTANCE_FROM_EARTH_AU_HISTOGRAM_BUCKETS,
)