Skip to content

Conversation

@khvn26
Copy link
Member

@khvn26 khvn26 commented Mar 24, 2025

Overview

  • Add Prometheus support
  • Cover Gunicorn with request metrics
  • Add support for custom application metrics
  • Add flagsmith-test-tools pytest plugin and assert_metric fixture
  • Add flagsmith start api package entrypoint
  • Improve packaging

Prometheus

Instead of using django-prometheus, I decided to introduce the Prometheus client directly, and gradually instrument parts of our stack, starting from the HTTP server. One advantage of this decision is that, unlike django-prometheus users, we are able to label HTTP metrics by request path without performance or cardinality risks. See PrometheusGunicornLoggerMiddleware and PrometheusGunicornLogger code for details.

I've also added a couple of developer tools to help us implement metrics in an organised, robust manner:

  • The assert_metric Pytest fixture is installed as part of the flagsmith-test-tools Pytest plugin along with this package.
  • The with_labels utility function makes sure you always send a default "unknown" value to Prometheus for missing or null metric values. This greatly simplifies metric usage.
  • metrics.py modules contained in your Django application are autodiscovered, which makes it possible to advertise your metrics globally — see flagsmith_build_info for an example.

Existing default metrics and a guideline for adding user metrics are added in the README.

Entrypoint

The library now installs a flagsmith entrypoint, which is essentially a manage.py script that we can modify as needed. I've added a flagsmith start api command that runs the Gunicorn web server, ensuring some important defaults:

  • Our custom logger class with JSON and Prometheus support
  • Correct support for Prometheus multiprocess mode

The idea is that any flagsmith start subcommand command will accept Gunicorn arguments, as the base endpoints, including health*, version and metrics will always be served by Gunicorn.

The next PR will add an implementation of the flagsmith start task-processor command.

My hope is that we'll be able to eventually replace run-docker.sh with this, which should help us ship binaries for customers that can't support Docker.

Packaging

I've added the following improvements ahead of publishing the package on PyPI:

  • Proper PEP 621 compatible packaging configuration
  • Classifiers, license information, up-to-date author and maintainer lists
  • Loosened dependencies

khvn26 added 8 commits March 23, 2025 20:39
- Add Prometheus support
- Cover Gunicorn with request metrics
- Add support for custom application metrics
- Add `flagsmith-test-tools` pytest plugin and `assert_metric` fixture
- Add `flagsmith start api` package entrypoint
- Improve packaging
@khvn26 khvn26 requested a review from matthewelwell March 24, 2025 09:45
@khvn26 khvn26 changed the title Feat/core feat: Prometheus support, core entrypoint, packaging improvements Mar 24, 2025
@khvn26 khvn26 requested a review from gagantrivedi March 24, 2025 10:06
Copy link
Contributor

@matthewelwell matthewelwell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a couple of minor comments, but it looks solid to me.

Copy link
Contributor

@rolodato rolodato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall, great stuff! I'd like to test how this looks when setting up a Grafana dashboard for these metrics.

@khvn26 khvn26 merged commit efc98ef into main Mar 25, 2025
1 check passed
khvn26 added a commit that referenced this pull request Mar 25, 2025
- Add Prometheus support
- Cover Gunicorn with request metrics
- Add support for custom application metrics
- Add `flagsmith-test-tools` pytest plugin and `assert_metric` fixture
- Add `flagsmith start api` package entrypoint
- Improve packaging

---------

Co-authored-by: Rodrigo López Dato <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants