| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
|
Closing to reopen with a proper description. |
Sorry, something went wrong.
Addresses follow-up feedback on prometheus#1169 from @calestyo: the existing yield explanation mentioned lazy iteration but omitted memory as a secondary benefit and gave no guidance on variable reuse for GC efficiency. Added a paragraph explaining that yielding inline lets Python reclaim the object as soon as the registry advances, while a named variable stays alive until rebound. Also added a short loop example showing how reusing the same variable name across iterations gives the GC the opportunity to reclaim each object before the next is allocated. Signed-off-by: k1chik <107162115+k1chik@users.noreply.github.com>
Signed-off-by: k1chik <107162115+k1chik@users.noreply.github.com>
|
Some comments on this... or maybe... questions: First what I often have (actually in a exporter I'm developing right now) is something like: labels = ("id", )
metric_info = InfoMetricFamily(self.name_prefix + "space_reservation", "Information about a space reservation.",
labels=labels)
metric_space_total = GaugeMetricFamily(self.name_prefix + "space_reservation_space_total", "The total space of a space reservation in bytes.",
unit="bytes",
labels=labels)
metric_space_used = GaugeMetricFamily(self.name_prefix + "space_reservation_space_used", "The used space of a space reservation in bytes.",
unit="bytes",
labels=labels)
metric_space_allocated = GaugeMetricFamily(self.name_prefix + "space_reservation_space_allocated", "The allocated space of a space reservation in bytes.",
unit="bytes",
labels=labels)
for s in raw_data.space_reservations:
s_labels = ( str(s["id"]), )
metric_info.add_metric(labels=s_labels,
value={"description": s["description"]}
)
metric_space_total.add_metric(s_labels,
value=s["sizeInBytes"]
)
metric_space_used.add_metric(s_labels,
value=s["usedSizeInBytes"]
)
metric_space_allocated.add_metric(s_labels,
value=s["allocatedSpaceInBytes"]
)
yield metric_info
yield metric_space_total
yield metric_space_used
yield metric_space_allocatedThat's not the final code I'm going to use (because it's actually a bit stupid with respect to the GC thingy. What I mean to tell is the (IMO general) use case that I iterate over something and draw multiple metrics from it. I think using a single var metric (for all) is also impossible or at least stupid: While I could of course yield in the for loop, using a single metric var, would also mean that I'd need to define the GaugeMetricFamily in ever iteration of the loop, which is of course also bad. So my point here is: My "solution" (though) it's not perfect right now is:
metrics = SimpleNamespace(
info=InfoMetricFamily(self.name_prefix + "cell", "Information about a cell.",
labels=l),
other_metric= ...
)One could of course also use a tuple (which probably would be faster), but if one has many metrics in one go, referring them via numbers might be prone to human errors. metrics = namedtuple(typename="metrics", field_names=["info","other_metric"])(InfoMetricFamily(...),...)The downside with SimpleNamespace is that it's not directly iterable but one can do e.g. yield from metrics.__dict__.values(). dict would of course work, and is probably not slower than SimpleNamespace which also uses a dict (or even faster)... but it felt a bit more ugly to write for me. So yeah... that might be something to include in the docs.... Second, and that's more of a question... We try here to educate the users how to make their collection faster, right!? So:
AFAICS, all these classes simply store their metrics in .samples, which is a list, everything else seems static. |
Sorry, something went wrong.
|
Yeah... as you can see I made a draft PR for the 2nd point above... would be happy if you could have a look at it. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Closes points made from comment section in #1169.
Changes
is a secondary benefit of using `yield`: yielding inline lets Python
reclaim the object as soon as the registry advances, while a named variable
stays alive until it is rebound.
accumulating into a list keeps at most one object alive at a time.
cc @calestyo @csmarchbanks