Skip to content

Commit 662bae0

Browse files
committed
Added a failover loop on docker information gathering
1 parent 9e80c05 commit 662bae0

File tree

1 file changed

+63
-48
lines changed

1 file changed

+63
-48
lines changed

treecript/collector.py

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -421,56 +421,71 @@ def process_metrics_collector(
421421

422422
possible_docker_pids = set()
423423
if docker_cli is not None:
424-
containers = docker_cli.containers.list()
425-
426-
docker_prev_pids = set(docker_following_pids.keys())
427-
docker_prev_notpids = set(docker_avoided_pids)
428-
for container in containers:
429-
container_pid = container.attrs.get("State", {}).get("Pid")
430-
if container_pid is not None:
431-
if container_pid in docker_prev_pids:
432-
docker_prev_pids.remove(container_pid)
433-
elif container_pid in docker_prev_notpids:
434-
docker_prev_notpids.remove(container_pid)
435-
else:
436-
possible_docker_pids.add(container_pid)
437-
container_created = container.attrs["Created"]
438-
if sys.version_info < (3, 11):
439-
matched = re.search(
440-
r"\:(\d\d)(?:\.\d+)?Z$", container_created
441-
)
442-
if matched is not None:
443-
container_created = (
444-
container_created[0 : matched.span()[0]]
445-
+ ":"
446-
+ matched.group(1)
447-
+ "+00:00"
424+
tries = 5
425+
while tries > 0:
426+
try:
427+
containers = docker_cli.containers.list()
428+
429+
docker_prev_pids = set(docker_following_pids.keys())
430+
docker_prev_notpids = set(docker_avoided_pids)
431+
for container in containers:
432+
container_pid = container.attrs.get("State", {}).get("Pid")
433+
if container_pid is not None:
434+
if container_pid in docker_prev_pids:
435+
docker_prev_pids.remove(container_pid)
436+
elif container_pid in docker_prev_notpids:
437+
docker_prev_notpids.remove(container_pid)
438+
else:
439+
possible_docker_pids.add(container_pid)
440+
container_created = container.attrs["Created"]
441+
if sys.version_info < (3, 11):
442+
matched = re.search(
443+
r"\:(\d\d)(?:\.\d+)?Z$", container_created
444+
)
445+
if matched is not None:
446+
container_created = (
447+
container_created[0 : matched.span()[0]]
448+
+ ":"
449+
+ matched.group(1)
450+
+ "+00:00"
451+
)
452+
container_data.append(
453+
(
454+
container.id,
455+
datetime.datetime.fromisoformat(
456+
container_created
457+
).timestamp(),
458+
container.attrs["Config"].get("Image"),
459+
container_pid,
460+
)
448461
)
449-
container_data.append(
450-
(
451-
container.id,
452-
datetime.datetime.fromisoformat(
453-
container_created
454-
).timestamp(),
455-
container.attrs["Config"].get("Image"),
456-
container_pid,
457-
)
458-
)
459-
460-
# Keeping the internal lists clean
461-
if len(docker_prev_notpids) > 0:
462-
docker_avoided_pids -= docker_prev_notpids
463462

464-
# Keeping the internal lists clean
465-
if len(docker_prev_pids) > 0:
466-
for prev_pid in docker_prev_pids:
467-
parent_pid = docker_following_pids.pop(prev_pid)
468-
if parent_pid in docker_followed_pids:
469-
del docker_followed_pids[parent_pid]
470-
471-
# Sorting in place by date
472-
if len(container_data) > 1:
473-
container_data.sort(key=lambda c: c[1])
463+
# Keeping the internal lists clean
464+
if len(docker_prev_notpids) > 0:
465+
docker_avoided_pids -= docker_prev_notpids
466+
467+
# Keeping the internal lists clean
468+
if len(docker_prev_pids) > 0:
469+
for prev_pid in docker_prev_pids:
470+
parent_pid = docker_following_pids.pop(prev_pid)
471+
if parent_pid in docker_followed_pids:
472+
del docker_followed_pids[parent_pid]
473+
474+
# Sorting in place by date
475+
if len(container_data) > 1:
476+
container_data.sort(key=lambda c: c[1])
477+
478+
break
479+
except docker.errors.NotFound:
480+
tries -= 1
481+
if tries == 0:
482+
logger.warning(
483+
"Failed getting the list of docker instances after 5 tries"
484+
)
485+
break
486+
except BaseException:
487+
logger.warning("Failed to get the list of docker instances")
488+
break
474489

475490
timestamp_str = datetime.datetime.now().strftime(timestamp_format)
476491

0 commit comments

Comments
 (0)