Skip to content

Commit 2c84c2a

Browse files
authored
docs: follow-up fixes for collect() generator examples (#1169) (#1172)
* 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> * docs: split InfoMetricFamily example into two separate blocks The single block with two yield statements looked like one collect() yielding both patterns. Split into labelled prose + code pairs to make clear they are alternatives, not sequential yields. Signed-off-by: k1chik <kkukdia@gmail.com> --------- Signed-off-by: k1chik <kkukdia@gmail.com>
1 parent 482656c commit 2c84c2a

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

docs/content/collector/custom.md

Lines changed: 15 additions & 2 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
@@ -232,11 +241,15 @@ InfoMetricFamily(name, documentation, value=None, labels=None)
232241
| `value` | `Dict[str, str]` | Key-value label pairs that form the info payload. |
233242
| `timestamp` | `float` or `Timestamp` | Optional Unix timestamp. |
234243

244+
Single unlabelled info metric:
245+
235246
```python
236-
# single unlabelled info metric
237247
yield InfoMetricFamily('build', 'Build metadata', value={'version': '1.2.3', 'commit': 'abc123'})
248+
```
249+
250+
Labelled — one info metric per service:
238251

239-
# labelled: one info metric per service
252+
```python
240253
i = InfoMetricFamily('service_build', 'Per-service build info', labels=['service'])
241254
i.add_metric(['auth'], {'version': '2.0.1', 'commit': 'def456'})
242255
i.add_metric(['api'], {'version': '1.9.0', 'commit': 'ghi789'})

0 commit comments

Comments
 (0)