Flagsmith's common library
Project description
flagsmith-common
Flagsmith's common library
Local development
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.
Usage
Installation
-
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.
Test tools
Fixtures
assert_metric
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,
)
saas_mode
The saas_mode fixture makes all common.core.utils.is_saas calls return True.
enterprise_mode
The enterprise_mode fixture makes all common.core.utils.is_enterprise calls return True.
Markers
pytest.mark.saas_mode
Use this mark to auto-use the saas_mode fixture.
pytest.mark.enterprise_mode
Use this mark to auto-use the enterprise_mode fixture.
Metrics
Flagsmith uses Prometheus to track performance metrics.
The following default metrics are exposed:
Common metrics
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.
Task Processor metrics
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").
Guidelines
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flagsmith_common-3.0.0.tar.gz.
File metadata
- Download URL: flagsmith_common-3.0.0.tar.gz
- Upload date:
- Size: 35.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
353b95993653732ffcbf96c8172f4205091e14e2bae8013e33da2345e69d75b9
|
|
| MD5 |
e16599cff1cd73b505fd4049cff981dd
|
|
| BLAKE2b-256 |
390fe778af71fb34564090b8ea21ee0789f8769aac4690d3909e37cf297c8e39
|
Provenance
The following attestation bundles were made for flagsmith_common-3.0.0.tar.gz:
Publisher:
publish.yml on Flagsmith/flagsmith-common
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flagsmith_common-3.0.0.tar.gz -
Subject digest:
353b95993653732ffcbf96c8172f4205091e14e2bae8013e33da2345e69d75b9 - Sigstore transparency entry: 795777535
- Sigstore integration time:
-
Permalink:
Flagsmith/flagsmith-common@4f1b05a4d10c3fe5abfa8ef2bb6997173c4a57af -
Branch / Tag:
refs/tags/v3.0.0 - Owner: https://github.com/Flagsmith
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4f1b05a4d10c3fe5abfa8ef2bb6997173c4a57af -
Trigger Event:
release
-
Statement type:
File details
Details for the file flagsmith_common-3.0.0-py3-none-any.whl.
File metadata
- Download URL: flagsmith_common-3.0.0-py3-none-any.whl
- Upload date:
- Size: 65.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92008cf4cb31ebe12d75dba558529c80806c84c8ea6c2e3056d4eda2f7e0e9b4
|
|
| MD5 |
de8a4d95d684b7cc2d5ae2f08d0db0b4
|
|
| BLAKE2b-256 |
507cb054710003da05f1dd65bb7cfd3033c11a3e98559fd3c7ece73a0b3fa040
|
Provenance
The following attestation bundles were made for flagsmith_common-3.0.0-py3-none-any.whl:
Publisher:
publish.yml on Flagsmith/flagsmith-common
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flagsmith_common-3.0.0-py3-none-any.whl -
Subject digest:
92008cf4cb31ebe12d75dba558529c80806c84c8ea6c2e3056d4eda2f7e0e9b4 - Sigstore transparency entry: 795777571
- Sigstore integration time:
-
Permalink:
Flagsmith/flagsmith-common@4f1b05a4d10c3fe5abfa8ef2bb6997173c4a57af -
Branch / Tag:
refs/tags/v3.0.0 - Owner: https://github.com/Flagsmith
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4f1b05a4d10c3fe5abfa8ef2bb6997173c4a57af -
Trigger Event:
release
-
Statement type: