From dbe9b2571a0e29206491d624b07fbba54f4e39ab Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Mon, 26 Aug 2019 17:41:38 +0200 Subject: [PATCH 1/2] Bump package version to 1.15.3 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 1291534c..183bbe70 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from distutils.version import LooseVersion from setuptools import find_packages, setup -VERSION = '1.15.2' +VERSION = '1.15.3' # Import README.md into long_description pwd = path.abspath(path.dirname(__file__)) From a2ebd3e01a3ddb13c77e18d9b640096eb0b9b001 Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Mon, 2 Sep 2019 12:13:55 +0200 Subject: [PATCH 2/2] Check for and conditionally use gunicorn logger * A few improved logging messages * Greatly improved log module * Determine if we are running in a gunicorn package if so, use the gunicorn logger * Otherwise retrieve and configure a standard logger --- instana/agent.py | 8 +++---- instana/fsm.py | 4 ++-- instana/log.py | 56 +++++++++++++++++++++++++++++++++-------------- instana/meter.py | 2 +- instana/sensor.py | 8 +++---- 5 files changed, 50 insertions(+), 28 deletions(-) diff --git a/instana/agent.py b/instana/agent.py index 29965079..dca1da33 100644 --- a/instana/agent.py +++ b/instana/agent.py @@ -137,7 +137,7 @@ def is_agent_listening(self, host, port): server_header = response.headers["Server"] if server_header == AGENT_HEADER: - logger.debug("Host agent found on %s:%d", host, port) + logger.debug("Instana host agent found on %s:%d", host, port) rv = True else: logger.debug("...something is listening on %s:%d but it's not the Instana Host Agent: %s", @@ -179,7 +179,7 @@ def is_agent_ready(self): return True return False except (requests.ConnectTimeout, requests.ConnectionError): - logger.debug("is_agent_ready: host agent connection error") + logger.debug("is_agent_ready: Instana host agent connection error") def report_data(self, entity_data): """ @@ -197,7 +197,7 @@ def report_data(self, entity_data): if response.status_code is 200: self.last_seen = datetime.now() except (requests.ConnectTimeout, requests.ConnectionError): - logger.debug("report_data: host agent connection error") + logger.debug("report_data: Instana host agent connection error") finally: return response @@ -222,7 +222,7 @@ def report_traces(self, spans): if response.status_code is 200: self.last_seen = datetime.now() except (requests.ConnectTimeout, requests.ConnectionError): - logger.debug("report_traces: host agent connection error") + logger.debug("report_traces: Instana host agent connection error") finally: return response diff --git a/instana/fsm.py b/instana/fsm.py index 093238c9..399c0187 100644 --- a/instana/fsm.py +++ b/instana/fsm.py @@ -117,7 +117,7 @@ def lookup_agent_host(self, e): return True if self.warnedPeriodic is False: - logger.warn("Instana Host Agent couldn't be found. Will retry periodically...") + logger.info("Instana Host Agent couldn't be found. Will retry periodically...") self.warnedPeriodic = True self.schedule_retry(self.lookup_agent_host, e, self.THREAD_NAME + ": agent_lookup") @@ -177,7 +177,7 @@ def schedule_retry(self, fun, e, name): self.timer.start() def on_ready(self, _): - logger.info("Host agent available. We're in business. Announced pid: %s (true pid: %s)", + logger.info("Instana host agent available. We're in business. Announced pid: %s (true pid: %s)", str(os.getpid()), str(self.agent.from_.pid)) def __get_real_pid(self): diff --git a/instana/log.py b/instana/log.py index 6a6dac81..307068d0 100644 --- a/instana/log.py +++ b/instana/log.py @@ -1,31 +1,55 @@ -import logging as log +import logging import os +import sys -logger = log.getLogger('instana') +logger = None -def init(level): - ch = log.StreamHandler() - f = log.Formatter('%(asctime)s: %(process)d %(levelname)s %(name)s: %(message)s') +def get_standard_logger(): + """ + Retrieves and configures a standard logger for the Instana package + + :return: Logger + """ + standard_logger = logging.getLogger("instana") + + ch = logging.StreamHandler() + f = logging.Formatter('%(asctime)s: %(process)d %(levelname)s %(name)s: %(message)s') ch.setFormatter(f) - logger.addHandler(ch) + standard_logger.addHandler(ch) if "INSTANA_DEBUG" in os.environ: - logger.setLevel(log.DEBUG) + standard_logger.setLevel(logging.DEBUG) else: - logger.setLevel(level) + standard_logger.setLevel(logging.WARN) + return standard_logger -def debug(s, *args): - logger.debug("%s %s", s, ' '.join(args)) +def running_in_gunicorn(): + """ + Determines if we are running inside of a gunicorn process and that the gunicorn logging package + is available. -def info(s, *args): - logger.info("%s %s", s, ' '.join(args)) + :return: Boolean + """ + process_check = False + package_check = False + for arg in sys.argv: + if arg.find('gunicorn') >= 0: + process_check = True + + try: + from gunicorn import glogging + except ImportError: + pass + else: + package_check = True -def warn(s, *args): - logger.warn("%s %s", s, ' '.join(args)) + return process_check and package_check -def error(s, *args): - logger.error("%s %s", s, ' '.join(args)) +if running_in_gunicorn(): + logger = logging.getLogger("gunicorn.error") +else: + logger = get_standard_logger() diff --git a/instana/meter.py b/instana/meter.py index 7f241e29..f8e67df0 100644 --- a/instana/meter.py +++ b/instana/meter.py @@ -178,7 +178,7 @@ def metric_work(): self.process() if self.agent.is_timed_out(): - logger.warn("Host agent offline for >1 min. Going to sit in a corner...") + logger.warn("Instana host agent unreachable for >1 min. Going to sit in a corner...") self.agent.reset() return False return True diff --git a/instana/sensor.py b/instana/sensor.py index 59acf375..25db6b18 100644 --- a/instana/sensor.py +++ b/instana/sensor.py @@ -1,6 +1,5 @@ from __future__ import absolute_import -from .log import init as init_logger from .meter import Meter from .options import Options @@ -12,15 +11,14 @@ class Sensor(object): def __init__(self, agent, options=None): self.set_options(options) - init_logger(self.options.log_level) - self.agent = agent self.meter = Meter(agent) def set_options(self, options): - self.options = options - if not self.options: + if options is None: self.options = Options() + else: + self.options = options def start(self): # Nothing to do for the Sensor; Pass onto Meter