Skip to content

Commit 31712e7

Browse files
committed
feat: Generate metrics documentation
1 parent 8872fb8 commit 31712e7

File tree

5 files changed

+89
-2
lines changed

5 files changed

+89
-2
lines changed

settings/dev.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88

99
env = Env()
1010

11+
TEMPLATES = [
12+
{
13+
"BACKEND": "django.template.backends.django.DjangoTemplates",
14+
"DIRS": ["templates"],
15+
"APP_DIRS": True,
16+
},
17+
]
18+
1119
# Settings expected by `mypy_django_plugin`
1220
AWS_SES_REGION_ENDPOINT: str
1321
SEGMENT_RULES_CONDITIONS_LIMIT: int
@@ -48,7 +56,8 @@
4856
TASK_DELETE_RETENTION_DAYS = 15
4957
TASK_DELETE_RUN_EVERY = timedelta(days=1)
5058
TASK_DELETE_RUN_TIME = time(5, 0, 0)
51-
TASK_PROCESSOR_MODE = False
59+
TASK_PROCESSOR_MODE = env.bool("RUN_BY_PROCESSOR", default=False)
60+
DOCGEN_MODE = env.bool("DOCGEN_MODE", default=False)
5261
TASK_RUN_METHOD = TaskRunMethod.TASK_PROCESSOR
5362

5463
# Avoid models.W042 warnings

src/common/core/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ def ensure_cli_env() -> typing.Generator[None, None, None]:
5252
)
5353
os.environ["PROMETHEUS_MULTIPROC_DIR"] = prometheus_multiproc_dir_name
5454

55+
if "docgen" in sys.argv:
56+
os.environ["DOCGEN_MODE"] = "true"
57+
5558
if "task-processor" in sys.argv:
5659
# A hacky way to signal we're not running the API
5760
os.environ["RUN_BY_PROCESSOR"] = "true"
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from operator import itemgetter
2+
from typing import Any, Callable
3+
4+
import prometheus_client
5+
from django.core.management import BaseCommand, CommandParser
6+
from django.template.loader import get_template
7+
from django.utils.module_loading import autodiscover_modules
8+
from prometheus_client.metrics import MetricWrapperBase
9+
10+
11+
class Command(BaseCommand):
12+
help = "Generate documentation for the Flagsmith codebase."
13+
14+
def add_arguments(self, parser: CommandParser) -> None:
15+
subparsers = parser.add_subparsers(
16+
title="sub-commands",
17+
required=True,
18+
)
19+
20+
metric_parser = subparsers.add_parser(
21+
"metrics",
22+
help="Start the Metrics service.",
23+
)
24+
metric_parser.set_defaults(handle_method=self.handle_metrics)
25+
26+
def initialise(self) -> None:
27+
from common.gunicorn import metrics
28+
29+
autodiscover_modules(
30+
"metrics",
31+
)
32+
33+
def handle(
34+
self,
35+
*args: Any,
36+
handle_method: Callable[..., None],
37+
**options: Any,
38+
) -> None:
39+
self.initialise()
40+
handle_method(*args, **options)
41+
42+
def handle_metrics(self, *args: Any, **options: Any) -> None:
43+
template = get_template("docgen-metrics.md")
44+
45+
flagsmith_metrics = sorted(
46+
(
47+
{
48+
"name": collector._name,
49+
"documentation": collector._documentation,
50+
"labels": collector._labelnames,
51+
"type": collector._type,
52+
}
53+
for collector in prometheus_client.REGISTRY._collector_to_names
54+
if isinstance(collector, MetricWrapperBase)
55+
and collector._name.startswith("flagsmith")
56+
),
57+
key=itemgetter("name"),
58+
)
59+
60+
self.stdout.write(
61+
template.render(
62+
context={"flagsmith_metrics": flagsmith_metrics},
63+
)
64+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Prometheus metrics
2+
3+
Flagsmith exports Prometheus metrics described below.
4+
{% for metric in flagsmith_metrics %}
5+
## `{{ metric.name }}` {{ metric.type }}
6+
7+
{{ metric.documentation }}
8+
9+
Labels:
10+
{% for label in metric.labels %} - `{{ label }}`
11+
{% endfor %}{% endfor %}

src/task_processor/metrics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
["task_identifier"],
1010
)
1111

12-
if settings.TASK_PROCESSOR_MODE:
12+
if settings.DOCGEN_MODE or settings.TASK_PROCESSOR_MODE:
1313
flagsmith_task_processor_finished_tasks_total = prometheus_client.Counter(
1414
"flagsmith_task_processor_finished_tasks_total",
1515
"Total number of finished tasks",

0 commit comments

Comments
 (0)