Skip to content

Commit f393961

Browse files
committed
docs: clarify collect() generator usage and API Reference snippet context
Add a note to the collect() protocol section explaining that yield is idiomatic (generator iterates lazily, no state between scrapes) and a preamble to the API Reference section clarifying that code snippets belong inside a collect() method. Follows up on review feedback in #1169. Signed-off-by: k1chik <kkukdia@gmail.com>
1 parent 482656c commit f393961

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

docs/content/collector/custom.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ can also implement `describe`.
4747
Returns an iterable of metric family objects (`GaugeMetricFamily`,
4848
`CounterMetricFamily`, etc.). Called every time the registry is scraped.
4949

50+
Using `yield` is the idiomatic way to implement `collect()` — it turns the method
51+
into a generator, which the registry iterates lazily without building an intermediate
52+
list first. Each scrape calls `collect()` fresh, so no state carries over between
53+
scrapes.
54+
5055
### `describe()`
5156

5257
Returns an iterable of metric family objects used only to determine the metric
@@ -76,6 +81,10 @@ g.add_metric(['eu-west-1'], 5)
7681

7782
## API Reference
7883

84+
The examples below show usage inside a `collect()` method body. Each snippet is
85+
meant to be placed within a custom collector class as shown in the example at the
86+
top of this page.
87+
7988
### GaugeMetricFamily
8089

8190
```python

0 commit comments

Comments
 (0)