Skip to content

Commit 8f1bd30

Browse files
authored
Detect and conditionally use the Gunicorn logger (#186)
* Bump package version to 1.15.3 * 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
1 parent 787b19b commit 8f1bd30

File tree

6 files changed

+51
-29
lines changed

6 files changed

+51
-29
lines changed

instana/agent.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def is_agent_listening(self, host, port):
137137

138138
server_header = response.headers["Server"]
139139
if server_header == AGENT_HEADER:
140-
logger.debug("Host agent found on %s:%d", host, port)
140+
logger.debug("Instana host agent found on %s:%d", host, port)
141141
rv = True
142142
else:
143143
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):
179179
return True
180180
return False
181181
except (requests.ConnectTimeout, requests.ConnectionError):
182-
logger.debug("is_agent_ready: host agent connection error")
182+
logger.debug("is_agent_ready: Instana host agent connection error")
183183

184184
def report_data(self, entity_data):
185185
"""
@@ -197,7 +197,7 @@ def report_data(self, entity_data):
197197
if response.status_code is 200:
198198
self.last_seen = datetime.now()
199199
except (requests.ConnectTimeout, requests.ConnectionError):
200-
logger.debug("report_data: host agent connection error")
200+
logger.debug("report_data: Instana host agent connection error")
201201
finally:
202202
return response
203203

@@ -222,7 +222,7 @@ def report_traces(self, spans):
222222
if response.status_code is 200:
223223
self.last_seen = datetime.now()
224224
except (requests.ConnectTimeout, requests.ConnectionError):
225-
logger.debug("report_traces: host agent connection error")
225+
logger.debug("report_traces: Instana host agent connection error")
226226
finally:
227227
return response
228228

instana/fsm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def lookup_agent_host(self, e):
117117
return True
118118

119119
if self.warnedPeriodic is False:
120-
logger.warn("Instana Host Agent couldn't be found. Will retry periodically...")
120+
logger.info("Instana Host Agent couldn't be found. Will retry periodically...")
121121
self.warnedPeriodic = True
122122

123123
self.schedule_retry(self.lookup_agent_host, e, self.THREAD_NAME + ": agent_lookup")
@@ -177,7 +177,7 @@ def schedule_retry(self, fun, e, name):
177177
self.timer.start()
178178

179179
def on_ready(self, _):
180-
logger.info("Host agent available. We're in business. Announced pid: %s (true pid: %s)",
180+
logger.info("Instana host agent available. We're in business. Announced pid: %s (true pid: %s)",
181181
str(os.getpid()), str(self.agent.from_.pid))
182182

183183
def __get_real_pid(self):

instana/log.py

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,55 @@
1-
import logging as log
1+
import logging
22
import os
3+
import sys
34

4-
logger = log.getLogger('instana')
5+
logger = None
56

67

7-
def init(level):
8-
ch = log.StreamHandler()
9-
f = log.Formatter('%(asctime)s: %(process)d %(levelname)s %(name)s: %(message)s')
8+
def get_standard_logger():
9+
"""
10+
Retrieves and configures a standard logger for the Instana package
11+
12+
:return: Logger
13+
"""
14+
standard_logger = logging.getLogger("instana")
15+
16+
ch = logging.StreamHandler()
17+
f = logging.Formatter('%(asctime)s: %(process)d %(levelname)s %(name)s: %(message)s')
1018
ch.setFormatter(f)
11-
logger.addHandler(ch)
19+
standard_logger.addHandler(ch)
1220
if "INSTANA_DEBUG" in os.environ:
13-
logger.setLevel(log.DEBUG)
21+
standard_logger.setLevel(logging.DEBUG)
1422
else:
15-
logger.setLevel(level)
23+
standard_logger.setLevel(logging.WARN)
1624

25+
return standard_logger
1726

18-
def debug(s, *args):
19-
logger.debug("%s %s", s, ' '.join(args))
2027

28+
def running_in_gunicorn():
29+
"""
30+
Determines if we are running inside of a gunicorn process and that the gunicorn logging package
31+
is available.
2132
22-
def info(s, *args):
23-
logger.info("%s %s", s, ' '.join(args))
33+
:return: Boolean
34+
"""
35+
process_check = False
36+
package_check = False
2437

38+
for arg in sys.argv:
39+
if arg.find('gunicorn') >= 0:
40+
process_check = True
41+
42+
try:
43+
from gunicorn import glogging
44+
except ImportError:
45+
pass
46+
else:
47+
package_check = True
2548

26-
def warn(s, *args):
27-
logger.warn("%s %s", s, ' '.join(args))
49+
return process_check and package_check
2850

2951

30-
def error(s, *args):
31-
logger.error("%s %s", s, ' '.join(args))
52+
if running_in_gunicorn():
53+
logger = logging.getLogger("gunicorn.error")
54+
else:
55+
logger = get_standard_logger()

instana/meter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def metric_work():
178178
self.process()
179179

180180
if self.agent.is_timed_out():
181-
logger.warn("Host agent offline for >1 min. Going to sit in a corner...")
181+
logger.warn("Instana host agent unreachable for >1 min. Going to sit in a corner...")
182182
self.agent.reset()
183183
return False
184184
return True

instana/sensor.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import absolute_import
22

3-
from .log import init as init_logger
43
from .meter import Meter
54
from .options import Options
65

@@ -12,15 +11,14 @@ class Sensor(object):
1211

1312
def __init__(self, agent, options=None):
1413
self.set_options(options)
15-
init_logger(self.options.log_level)
16-
1714
self.agent = agent
1815
self.meter = Meter(agent)
1916

2017
def set_options(self, options):
21-
self.options = options
22-
if not self.options:
18+
if options is None:
2319
self.options = Options()
20+
else:
21+
self.options = options
2422

2523
def start(self):
2624
# Nothing to do for the Sensor; Pass onto Meter

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from distutils.version import LooseVersion
55
from setuptools import find_packages, setup
66

7-
VERSION = '1.15.2'
7+
VERSION = '1.15.3'
88

99
# Import README.md into long_description
1010
pwd = path.abspath(path.dirname(__file__))

0 commit comments

Comments
 (0)