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()