From 27ffca34aacefb8d1935b6386190e8ef8b724daf Mon Sep 17 00:00:00 2001 From: Peyton Walters Date: Tue, 9 Jul 2024 12:01:52 -0400 Subject: [PATCH] Clean up push gateway metrics on exit (#805) This commit adds a cleanup step to the pushgateway application. This will make it so stale metrics don't hang around. --- 10_integrations/pushgateway.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/10_integrations/pushgateway.py b/10_integrations/pushgateway.py index b629bba11..f006ed268 100644 --- a/10_integrations/pushgateway.py +++ b/10_integrations/pushgateway.py @@ -86,6 +86,7 @@ def run_pushgateway(): from prometheus_client import ( CollectorRegistry, Counter, + delete_from_gateway, push_to_gateway, ) @@ -103,6 +104,16 @@ def init(self): registry=self.registry, ) + # We must explicitly clean up the metric when the app exits so Prometheus doesn't + # keep stale metrics around. + @modal.exit() + def cleanup(self): + delete_from_gateway( + self.web_url, + job="hello", + grouping_key={"instance": self.instance_id}, + ) + @web_endpoint() def hello(self): self.counter.inc()