diff --git a/docs/content/collector/custom.md b/docs/content/collector/custom.md index c1979109..27db4f6f 100644 --- a/docs/content/collector/custom.md +++ b/docs/content/collector/custom.md @@ -52,6 +52,21 @@ into a generator, which the registry iterates lazily without building an interme list first. Each scrape calls `collect()` fresh, so no state carries over between scrapes. +Memory is a secondary benefit. Yielding an object inline +(`yield GaugeMetricFamily(...)`) lets Python reclaim it as soon as the registry +advances past that point. A named variable keeps the object alive until the variable +is rebound. For most collectors this difference is negligible, but it can matter for +very large metric sets. Yielding inside a loop rather than accumulating into a list +keeps at most one object alive at a time: + +```python +def collect(self): + for name in _QUEUE_NAMES: + m = GaugeMetricFamily('queue_depth', 'Queue depth', labels=['queue']) + m.add_metric([name], fetch_depth(name)) + yield m +``` + ### `describe()` Returns an iterable of metric family objects used only to determine the metric