{"id":20556423,"url":"https://github.com/flagsmith/flagsmith-common","last_synced_at":"2025-10-06T12:17:08.494Z","repository":{"id":248718412,"uuid":"829471300","full_name":"Flagsmith/flagsmith-common","owner":"Flagsmith","description":"A repository for including code that is required in multiple flagsmith repositories","archived":false,"fork":false,"pushed_at":"2025-02-26T18:33:47.000Z","size":93,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-02-26T19:34:08.427Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Flagsmith.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-16T13:48:11.000Z","updated_at":"2025-01-27T17:10:05.000Z","dependencies_parsed_at":"2024-07-16T18:39:35.802Z","dependency_job_id":"b433da8b-d893-41ea-8c64-78685a98411c","html_url":"https://github.com/Flagsmith/flagsmith-common","commit_stats":null,"previous_names":["flagsmith/flagsmith-common"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flagsmith%2Fflagsmith-common","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flagsmith%2Fflagsmith-common/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flagsmith%2Fflagsmith-common/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flagsmith%2Fflagsmith-common/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Flagsmith","download_url":"https://codeload.github.com/Flagsmith/flagsmith-common/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242163854,"owners_count":20082224,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T03:27:39.448Z","updated_at":"2025-10-06T12:17:08.489Z","avatar_url":"https://github.com/Flagsmith.png","language":"Python","readme":"# flagsmith-common\n\n[![Coverage](https://codecov.io/gh/Flagsmith/flagsmith-common/graph/badge.svg?token=L3OGOXH86K)](https://codecov.io/gh/Flagsmith/flagsmith-common)\n\nFlagsmith's common library\n\n### Development Setup\n\nThis project uses [Poetry](https://python-poetry.org/) for dependency management and includes a Makefile to simplify common development tasks.\n\n#### Prerequisites\n\n- Python \u003e= 3.11\n- Make\n\n#### Installation\n\nYou can set up your development environment using the provided Makefile:\n\n```bash\n# Install everything (pip, poetry, and project dependencies)\nmake install\n\n# Individual installation steps are also available\nmake install-pip       # Upgrade pip\nmake install-poetry    # Install Poetry\nmake install-packages  # Install project dependencies\n```\n\n#### Development\n\nRun linting checks using pre-commit:\n\n```bash\nmake lint\n```\n\nAdditional options can be passed to the `install-packages` target:\n\n```bash\n# Install with development dependencies\nmake install-packages opts=\"--with dev\"\n\n# Install with specific extras\nmake install-packages opts=\"--extras 'feature1 feature2'\"\n```\n\n### Usage\n\n#### Installation\n\n1. `poetry add flagsmith-common`\n\n2. `poetry add --G dev flagsmith-common[test-tools]` — this will enable the Pytest fixtures. Skipping this step will make Pytest collection fail due to missing dependencies.\n\n3. Make sure `\"common.core\"` is in the `INSTALLED_APPS` of your settings module.\nThis enables the `manage.py flagsmith` commands.\n\n4. Add `\"common.gunicorn.middleware.RouteLoggerMiddleware\"` to `MIDDLEWARE` in your settings module.\nThis enables the `route` label for Prometheus HTTP metrics.\n\n5. To enable the `/metrics` endpoint, set the `PROMETHEUS_ENABLED` setting to `True`.\n\n#### Test tools\n\n##### Fixtures\n\n###### `assert_metric`\n\nTo test your metrics using the `assert_metric` fixture:\n\n```python\nfrom common.test_tools import AssertMetricFixture\n\ndef test_my_code__expected_metrics(assert_metric: AssertMetricFixture) -\u003e None:\n    # When\n    my_code()\n\n    # Then\n    assert_metric(\n        name=\"flagsmith_distance_from_earth_au_sum\",\n        labels={\"engine_type\": \"solar_sail\"},\n        value=1.0,\n    )\n```\n\n###### `saas_mode`\n\nThe `saas_mode` fixture makes all `common.core.utils.is_saas` calls return `True`.\n\n###### `enterprise_mode`\n\nThe `enterprise_mode` fixture makes all `common.core.utils.is_enterprise` calls return `True`.\n\n##### Markers\n\n###### `pytest.mark.saas_mode`\n\nUse this mark to auto-use the `saas_mode` fixture.\n\n###### `pytest.mark.enterprise_mode`\n\nUse this mark to auto-use the `enterprise_mode` fixture.\n\n#### Metrics\n\nFlagsmith uses Prometheus to track performance metrics.\n\nThe following default metrics are exposed:\n\n##### Common metrics\n\n- `flagsmith_build_info`: Has the labels `version` and `ci_commit_sha`.\n- `flagsmith_http_server_request_duration_seconds`: Histogram labeled with `method`, `route`, and `response_status`.\n- `flagsmith_http_server_requests_total`: Counter labeled with `method`, `route`, and `response_status`.\n- `flagsmith_http_server_response_size_bytes`:Histogram labeled with `method`, `route`, and `response_status`.\n- `flagsmith_task_processor_enqueued_tasks_total`: Counter labeled with `task_identifier`.\n\n##### Task Processor metrics\n\n- `flagsmith_task_processor_finished_tasks_total`: Counter labeled with `task_identifier`, `task_type` (`\"recurring\"`, `\"standard\"`) and `result` (`\"success\"`, `\"failure\"`).\n- `flagsmith_task_processor_task_duration_seconds`: Histogram labeled with `task_identifier`, `task_type` (`\"recurring\"`, `\"standard\"`) and `result` (`\"success\"`, `\"failure\"`).\n\n##### Guidelines\n\nTry to come up with meaningful metrics to cover your feature with when developing it. Refer to [Prometheus best practices][1] when naming your metric and labels.\n\nAs a reasonable default, Flagsmith metrics are expected to be namespaced with the `\"flagsmith_\"` prefix.\n\nDefine your metrics in a `metrics.py` module of your Django application — see [example][2]. Contrary to Prometheus Python client examples and documentation, please name a metric variable exactly as your metric name.\n\nIt'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:\n\n```python\nimport prometheus_client\nfrom django.conf import settings\n\nflagsmith_distance_from_earth_au = prometheus_client.Histogram(\n    \"flagsmith_distance_from_earth_au\",\n    \"Distance from Earth in astronomical units\",\n    labels=[\"engine_type\"],\n    buckets=settings.DISTANCE_FROM_EARTH_AU_HISTOGRAM_BUCKETS,\n)\n```\n\nFor testing your metrics, refer to [`assert_metric` documentation][5].\n\n[1]: https://prometheus.io/docs/practices/naming/\n[2]: https://github.com/Flagsmith/flagsmith-common/blob/main/src/common/gunicorn/metrics.py\n[3]: https://docs.gunicorn.org/en/stable/design.html#server-model\n[4]: https://prometheus.github.io/client_python/multiprocess\n[5]: #assert_metric\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflagsmith%2Fflagsmith-common","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflagsmith%2Fflagsmith-common","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflagsmith%2Fflagsmith-common/lists"}