Skip to content

Commit 9dea281

Browse files
author
Gabriel Filion
committed
Add debug logging
This can help one in identifying what the cache filters identified in each category, which is useful both for debugging the code itself and for identifying what builtin command-line apt search filters may not be identifying in a similar manner. One example of this is the obsolete package filter, which can sometimes catch more packages than `apt list "?obsolete"`
1 parent 39498fb commit 9dea281

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

apt_info.py

+43-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import apt
2626
import apt_pkg
2727
import collections
28+
import logging
2829
import os
30+
import sys
2931
from prometheus_client import CollectorRegistry, Gauge, generate_latest
3032

3133
_UpgradeInfo = collections.namedtuple("_UpgradeInfo", ["labels", "count"])
@@ -58,10 +60,16 @@ def _convert_candidates_to_upgrade_infos(candidates):
5860
return changes_list
5961

6062

61-
def _write_pending_upgrades(registry, cache):
63+
def _write_pending_upgrades(registry, cache, logger):
6264
candidates = {
6365
p.candidate for p in cache if p.is_upgradable and not p.phasing_applied
6466
}
67+
for candidate in candidates:
68+
logger.debug(
69+
"pending upgrade: %s / %s",
70+
candidate.package,
71+
candidate.architecture,
72+
)
6573
upgrade_list = _convert_candidates_to_upgrade_infos(candidates)
6674

6775
if upgrade_list:
@@ -71,7 +79,7 @@ def _write_pending_upgrades(registry, cache):
7179
g.labels(change.labels['origin'], change.labels['arch']).set(change.count)
7280

7381

74-
def _write_held_upgrades(registry, cache):
82+
def _write_held_upgrades(registry, cache, logger):
7583
held_candidates = {
7684
p.candidate for p in cache
7785
if (
@@ -80,6 +88,12 @@ def _write_held_upgrades(registry, cache):
8088
and not p.phasing_applied
8189
)
8290
}
91+
for candidate in held_candidates:
92+
logger.debug(
93+
"held upgrade: %s / %s",
94+
candidate.package,
95+
candidate.architecture,
96+
)
8397
upgrade_list = _convert_candidates_to_upgrade_infos(held_candidates)
8498

8599
if upgrade_list:
@@ -89,22 +103,37 @@ def _write_held_upgrades(registry, cache):
89103
g.labels(change.labels['origin'], change.labels['arch']).set(change.count)
90104

91105

92-
def _write_obsolete_packages(registry, cache):
106+
def _write_obsolete_packages(registry, cache, logger):
93107
# This corresponds to the apt filter "?obsolete"
94108
obsoletes = [p for p in cache if p.is_installed and (
95109
p.candidate is None or
96110
not p.candidate.origins or
97111
(len(p.candidate.origins) == 1 and
98112
p.candidate.origins[0].origin in ['', "/var/lib/dpkg/status"])
99113
)]
114+
for package in obsoletes:
115+
if package.candidate is None:
116+
logger.debug("obsolete package with no candidate: %s", package)
117+
else:
118+
logger.debug(
119+
"obsolete package: %s / %s",
120+
package,
121+
package.candidate.architecture,
122+
)
100123

101124
g = Gauge('apt_packages_obsolete_count', "Apt packages which are obsolete",
102125
registry=registry)
103126
g.set(len(obsoletes))
104127

105128

106-
def _write_autoremove_pending(registry, cache):
107-
autoremovable_packages = {p for p in cache if p.is_auto_removable}
129+
def _write_autoremove_pending(registry, cache, logger):
130+
autoremovable_packages = {p.candidate for p in cache if p.is_auto_removable}
131+
for candidate in autoremovable_packages:
132+
logger.debug(
133+
"autoremovable package: %s / %s",
134+
candidate.package,
135+
candidate.architecture,
136+
)
108137
g = Gauge('apt_autoremove_pending', "Apt packages pending autoremoval.",
109138
registry=registry)
110139
g.set(len(autoremovable_packages))
@@ -147,13 +176,18 @@ def _write_reboot_required(registry):
147176

148177

149178
def _main():
179+
logging.basicConfig(stream=sys.stderr)
180+
logger = logging.getLogger(__name__)
181+
if os.environ.get("DEBUG"):
182+
logger.setLevel(logging.DEBUG)
183+
150184
cache = apt.cache.Cache()
151185

152186
registry = CollectorRegistry()
153-
_write_pending_upgrades(registry, cache)
154-
_write_held_upgrades(registry, cache)
155-
_write_obsolete_packages(registry, cache)
156-
_write_autoremove_pending(registry, cache)
187+
_write_pending_upgrades(registry, cache, logger)
188+
_write_held_upgrades(registry, cache, logger)
189+
_write_obsolete_packages(registry, cache, logger)
190+
_write_autoremove_pending(registry, cache, logger)
157191
_write_installed_packages_per_origin(registry, cache)
158192
_write_cache_timestamps(registry)
159193
_write_reboot_required(registry)

0 commit comments

Comments
 (0)