mirror of
https://chromium.googlesource.com/chromium/tools/depot_tools.git
synced 2026-01-11 10:41:31 +00:00
Telemetry: use print instead of logging
Using the logging library before UTR can set it up puts it in a weird state that consumes all output. Instead of using logging, just print anything from initializing telemetry. Also make some small updated to avoid features not supported by non-linux os's right now (these have been generating unnecessary warnings) Bug: 425298118 Change-Id: I1ccc749fb3c3410b105116ff25160fc23f0336df Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6648785 Commit-Queue: Struan Shrimpton <sshrimp@google.com> Reviewed-by: Ben Pastene <bpastene@chromium.org>
This commit is contained in:
committed by
LUCI CQ
parent
b51ce4941b
commit
712066553a
@@ -2,7 +2,6 @@
|
||||
# Use of this source code is governed by a BSD-style license that can be
|
||||
# found in the LICENSE file.
|
||||
from typing import Optional
|
||||
import logging
|
||||
import socket
|
||||
import sys
|
||||
|
||||
@@ -46,12 +45,11 @@ def get_host_name(fully_qualified: bool = False) -> str:
|
||||
try:
|
||||
hostname = socket.gethostbyaddr(hostname)[0]
|
||||
except (socket.gaierror, socket.herror) as e:
|
||||
logging.warning(
|
||||
'please check your /etc/hosts file; resolving your hostname'
|
||||
' (%s) failed: %s',
|
||||
hostname,
|
||||
e,
|
||||
)
|
||||
if sys.platform.startswith('linux'):
|
||||
print(
|
||||
'please check your /etc/hosts file; resolving your hostname'
|
||||
f' ({hostname}) failed: {e}',
|
||||
file=sys.stderr)
|
||||
|
||||
if fully_qualified:
|
||||
return hostname
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
# found in the LICENSE file.
|
||||
"""Defines the ResourceDetector to capture resource properties."""
|
||||
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
@@ -37,14 +36,22 @@ class ProcessDetector(resources.ResourceDetector):
|
||||
def detect(self) -> resources.Resource:
|
||||
env = os.environ
|
||||
resource = {
|
||||
PROCESS_CWD: os.getcwd(),
|
||||
PROCESS_RUNTIME_API_VERSION: sys.api_version,
|
||||
resources.PROCESS_PID: os.getpid(),
|
||||
resources.PROCESS_OWNER: os.geteuid(),
|
||||
resources.PROCESS_EXECUTABLE_NAME: Path(sys.executable).name,
|
||||
resources.PROCESS_EXECUTABLE_PATH: sys.executable,
|
||||
resources.PROCESS_COMMAND: sys.argv[0],
|
||||
resources.PROCESS_COMMAND_ARGS: sys.argv[1:],
|
||||
PROCESS_CWD:
|
||||
os.getcwd(),
|
||||
PROCESS_RUNTIME_API_VERSION:
|
||||
sys.api_version,
|
||||
resources.PROCESS_PID:
|
||||
os.getpid(),
|
||||
resources.PROCESS_OWNER:
|
||||
os.geteuid() if os.name.startswith('linux') else 'Unknown',
|
||||
resources.PROCESS_EXECUTABLE_NAME:
|
||||
Path(sys.executable).name,
|
||||
resources.PROCESS_EXECUTABLE_PATH:
|
||||
sys.executable,
|
||||
resources.PROCESS_COMMAND:
|
||||
sys.argv[0],
|
||||
resources.PROCESS_COMMAND_ARGS:
|
||||
sys.argv[1:],
|
||||
}
|
||||
resource.update({
|
||||
f"{PROCESS_ENV}.{k}": env[k]
|
||||
@@ -92,19 +99,21 @@ class MemoryInfo:
|
||||
self._total_physical_ram = 0
|
||||
self._total_virtual_memory = 0
|
||||
self._total_swap_memory = 0
|
||||
try:
|
||||
contents = PROC_MEMINFO_PATH.read_text(encoding="utf-8")
|
||||
except OSError as e:
|
||||
logging.warning("Encountered an issue reading /proc/meminfo: %s", e)
|
||||
return
|
||||
if sys.platform.startswith('linux'):
|
||||
try:
|
||||
contents = PROC_MEMINFO_PATH.read_text(encoding="utf-8")
|
||||
except OSError as e:
|
||||
print(f'Encountered an issue reading /proc/meminfo: {e}',
|
||||
file=sys.stderr)
|
||||
return
|
||||
|
||||
for line in contents.splitlines():
|
||||
if line.startswith(self.MEMINFO_SWAP_MEMORY_TOTAL):
|
||||
self._total_swap_memory = self._get_mem_value(line)
|
||||
elif line.startswith(self.MEMINFO_VIRTUAL_MEMORY_TOTAL):
|
||||
self._total_virtual_memory = self._get_mem_value(line)
|
||||
elif line.startswith(self.MEMINFO_PHYSICAL_RAM_TOTAL):
|
||||
self._total_physical_ram = self._get_mem_value(line)
|
||||
for line in contents.splitlines():
|
||||
if line.startswith(self.MEMINFO_SWAP_MEMORY_TOTAL):
|
||||
self._total_swap_memory = self._get_mem_value(line)
|
||||
elif line.startswith(self.MEMINFO_VIRTUAL_MEMORY_TOTAL):
|
||||
self._total_virtual_memory = self._get_mem_value(line)
|
||||
elif line.startswith(self.MEMINFO_PHYSICAL_RAM_TOTAL):
|
||||
self._total_physical_ram = self._get_mem_value(line)
|
||||
|
||||
@property
|
||||
def total_physical_ram(self) -> int:
|
||||
@@ -136,11 +145,10 @@ class MemoryInfo:
|
||||
"""
|
||||
components = line.split()
|
||||
if len(components) == 1:
|
||||
logging.warning(
|
||||
"Unexpected /proc/meminfo entry with no label:number value was "
|
||||
"provided. Value read: '%s'",
|
||||
line,
|
||||
)
|
||||
print(
|
||||
'Unexpected /proc/meminfo entry with no label:number value was '
|
||||
f'provided. Value read: {line}',
|
||||
file=sys.stderr)
|
||||
return 0
|
||||
size = int(components[1])
|
||||
if len(components) == 2:
|
||||
@@ -149,10 +157,9 @@ class MemoryInfo:
|
||||
# indication that a memory unit besides kB (kibibytes) is expected,
|
||||
# except in the cases of page counts, where no unit is provided.
|
||||
if components[2] != "kB":
|
||||
logging.warning(
|
||||
"Unit for memory consumption in /proc/meminfo does "
|
||||
"not conform to expectations. Please review the "
|
||||
"read value: %s",
|
||||
line,
|
||||
)
|
||||
print(
|
||||
'Unit for memory consumption in /proc/meminfo does '
|
||||
'not conform to expectations. Please review the '
|
||||
'read value: %s',
|
||||
file=sys.stderr)
|
||||
return size * 1024
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
"""The tests for resource detector classes."""
|
||||
|
||||
import getpass
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
@@ -100,7 +99,7 @@ MemTotal: 35 kB
|
||||
|
||||
|
||||
def test_memory_info_class_warns_on_unexpected_unit(monkeypatch,
|
||||
caplog) -> None:
|
||||
capsys) -> None:
|
||||
proc_meminfo_contents = """
|
||||
SwapTotal: 15 mB
|
||||
VmallocTotal: 25 gB
|
||||
@@ -113,10 +112,10 @@ MemTotal: 35 tB
|
||||
"read_text",
|
||||
mock_read_text(detector.PROC_MEMINFO_PATH, proc_meminfo_contents),
|
||||
)
|
||||
caplog.set_level(logging.WARNING)
|
||||
|
||||
m = detector.MemoryInfo()
|
||||
assert "Unit for memory consumption in /proc/meminfo" in caplog.text
|
||||
stderr = capsys.readouterr().err
|
||||
assert "Unit for memory consumption in /proc/meminfo" in stderr
|
||||
# We do not attempt to correct unexpected units
|
||||
assert m.total_swap_memory == 15 * 1024
|
||||
assert m.total_physical_ram == 35 * 1024
|
||||
@@ -139,7 +138,7 @@ SwapTotal: 15
|
||||
assert m.total_swap_memory == 15
|
||||
|
||||
|
||||
def test_memory_info_class_no_provided_value(monkeypatch, caplog) -> None:
|
||||
def test_memory_info_class_no_provided_value(monkeypatch, capsys) -> None:
|
||||
proc_meminfo_contents = """
|
||||
SwapTotal:
|
||||
"""
|
||||
@@ -150,10 +149,10 @@ SwapTotal:
|
||||
"read_text",
|
||||
mock_read_text(detector.PROC_MEMINFO_PATH, proc_meminfo_contents),
|
||||
)
|
||||
caplog.set_level(logging.WARNING)
|
||||
|
||||
detector.MemoryInfo()
|
||||
assert "Unexpected /proc/meminfo entry with no label:number" in caplog.text
|
||||
stderr = capsys.readouterr().err
|
||||
assert "Unexpected /proc/meminfo entry with no label:number" in stderr
|
||||
|
||||
|
||||
def test_system_info_to_capture_memory_resources(monkeypatch) -> None:
|
||||
|
||||
Reference in New Issue
Block a user