diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index 87e524f9..e2f10a1c 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -6,6 +6,7 @@ import socket import time import threading +from contextlib import closing from . import core try: @@ -125,6 +126,6 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout): def instance_ip_grouping_key(): '''Grouping key with instance set to the IP Address of this host.''' - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.connect(('localhost', 0)) - return {'instance': s.getsockname()[0]} + with closing(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) as s: + s.connect(('localhost', 0)) + return {'instance': s.getsockname()[0]}