Merge lp:~cprov/adt-cloud-worker/log-cleanup into lp:adt-cloud-worker

Proposed by Celso Providelo
Status: Merged
Approved by: Celso Providelo
Approved revision: 31
Merged at revision: 29
Proposed branch: lp:~cprov/adt-cloud-worker/log-cleanup
Merge into: lp:adt-cloud-worker
Diff against target: 171 lines (+64/-20)
3 files modified
adt-service.conf (+5/-0)
adt_cloud_worker/__init__.py (+58/-20)
requirements.txt (+1/-0)
To merge this branch: bzr merge lp:~cprov/adt-cloud-worker/log-cleanup
Reviewer Review Type Date Requested Status
Thomi Richards (community) Approve
Review via email: mp+253617@code.launchpad.net

Commit message

Suppressing adt-run output from the logs, adding logstash & file logging handlers along with some messaging cleanup.

Description of the change

Suppressing adt-run output from the logs, adding logstash & file logging handlers along with some messaging cleanup.

To post a comment you must log in.
Revision history for this message
Thomi Richards (thomir-deactivatedaccount) wrote :

<thomi> cprov: ok, only thing is, best practise is to do: logger.info("%s", var) not logger.info("%s" % (var)) or logger.info("{}".format(var))
<cprov> thomi: I noticed in your MPs, let me fix mine
<thomi> cprov: also, in my MPs I set 'requests' to WARNING
<thomi> best make them all the same I guess
<thomi> cprov: otherwise, looks good

Looks good - suggest you fix the above two issues.

review: Approve
30. By Celso Providelo

Fixing logging format style.

31. By Celso Providelo

'requests' logging set to WARNING.

Revision history for this message
Celso Providelo (cprov) wrote :

Both fixed, thanks.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'adt-service.conf'
2--- adt-service.conf 2015-03-16 20:50:34 +0000
3+++ adt-service.conf 2015-03-20 01:06:08 +0000
4@@ -10,3 +10,8 @@
5 os_auth_url = http://172.20.161.138:5000/v2.0/
6 os_region_name = bot-prototype
7 extra_args = --net-id=415a0839-eb05-4e7a-907c-413c657f4bf5
8+
9+[logstash]
10+host = localhost
11+port = 5959
12+version = 1
13
14=== modified file 'adt_cloud_worker/__init__.py'
15--- adt_cloud_worker/__init__.py 2015-03-19 03:08:53 +0000
16+++ adt_cloud_worker/__init__.py 2015-03-20 01:06:08 +0000
17@@ -23,9 +23,9 @@
18 import argparse
19 import configparser
20 import kombu
21-from kombu.log import get_logger
22 from kombu.mixins import ConsumerMixin
23-from kombu.utils.debug import setup_logging
24+import logging
25+import logstash
26 import os
27 import socket
28 import subprocess
29@@ -44,7 +44,8 @@
30 )
31 API_VERSION = "v1"
32
33-logger = get_logger(__name__)
34+
35+logger = logging.getLogger(__name__)
36
37
38 class AdtNovaWorker(ConsumerMixin):
39@@ -79,8 +80,8 @@
40 Run requested tests and posts results to the 'adt_results' queue
41 for later checking.
42 """
43- logger.info('Got: {}'.format(body))
44 body['worker'] = self.worker_name
45+ logger.info('Received message request: %s', body, extra=body)
46
47 # Run requested tests safely.
48 tarball_path = None
49@@ -97,8 +98,12 @@
50 'nova', 'extra_args').split()
51 adt_kwargs['nova_flavor'] = get_default_testbed_flavor()
52 arguments = _make_adt_argument_list(adt_kwargs)
53-
54+ logger.info(
55+ 'Running adt-run with: %s', ' '.join(arguments),
56+ extra=body)
57 body['exit_code'] = run_adt(arguments)
58+ logger.info(
59+ 'Exit code: %s', body['exit_code'], extra=body)
60 _create_run_metadata_file(result_dir, body)
61 tarball_path = _create_tarball_from_directory(
62 result_dir,
63@@ -110,18 +115,19 @@
64 upload_tarball_to_swift_container(tarball_path, container_name)
65 except Exception as e:
66 # Unexpected failures ...
67- logger.error(e, exc_info=True)
68 body['exit_code'] = '100'
69+ logger.error(e, exc_info=True, extra=body)
70 finally:
71- # The post results
72- # TODO: send error logging to result-checker in the message
73- queue = self.connection.SimpleQueue('adt.results.{}'.format(API_VERSION))
74+ logger.info('Posting results: %s', body, extra=body)
75+ # TODO: send error logging to result-checker in the message.
76+ queue = self.connection.SimpleQueue(
77+ 'adt.results.{}'.format(API_VERSION))
78 queue.put(body)
79 queue.close()
80 if tarball_path and os.path.exists(tarball_path):
81 os.remove(tarball_path)
82 message.ack()
83- logger.info('Done!')
84+ logger.info('Done!', extra=body)
85
86
87 def _create_run_metadata_file(directory, request):
88@@ -174,14 +180,11 @@
89
90 Returns the exit code from adt-run.
91 """
92- # TODO: We probably want something more clever here:
93- try:
94- subprocess.check_call(['adt-run'] + arguments)
95- except subprocess.CalledProcessError as e:
96- # log?
97- # TODO: filter log content to avoid leaking cloud credentials.
98- return e.returncode
99- return 0
100+ cmd_line = ['adt-run'] + arguments
101+ proc = subprocess.Popen(
102+ cmd_line, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
103+ proc.communicate()
104+ return proc.returncode
105
106
107 def _make_adt_argument_list(request_configuration):
108@@ -250,9 +253,42 @@
109 return tarball_path
110
111
112+def configure_logging(config):
113+ root_logger = logging.getLogger()
114+ root_logger.setLevel(logging.INFO)
115+
116+ # Silence requests logging, which is created by nova, keystone and swift.
117+ requests_logger = logging.getLogger('requests')
118+ requests_logger.setLevel(logging.WARNING)
119+
120+ # If there is no ./logs directory, fallback to stderr.
121+ log_path = os.path.abspath(os.path.join(__file__, '../../logs/app.log'))
122+ log_dir = os.path.dirname(log_path)
123+ if os.path.exists(log_dir):
124+ handler = logging.FileHandler(log_path)
125+ else:
126+ print("'logs' directory '{}' does not exist, using stderr "
127+ "for app log.".format(log_dir))
128+ handler = logging.StreamHandler()
129+
130+ handler.setFormatter(
131+ logging.Formatter(
132+ '%(asctime)s %(name)s %(levelname)s: %(message)s'
133+ )
134+ )
135+ root_logger.addHandler(handler)
136+
137+ if 'logstash' in config:
138+ root_logger.addHandler(
139+ logstash.LogstashHandler(
140+ config['logstash']['host'],
141+ int(config['logstash']['port']),
142+ int(config['logstash']['version'])
143+ )
144+ )
145+
146+
147 def main():
148- setup_logging(loglevel='DEBUG', loggers=[''])
149-
150 parser = argparse.ArgumentParser(
151 description='ADT cloud worker ...')
152 parser.add_argument('-c', '--conf', default='adt-service.conf',
153@@ -263,6 +299,8 @@
154 config = configparser.ConfigParser()
155 config.read(args.conf)
156
157+ configure_logging(config)
158+
159 set_config_dict(config)
160
161 amqp_uris = config.get('amqp', 'uris').split()
162
163=== modified file 'requirements.txt'
164--- requirements.txt 2015-03-08 22:20:15 +0000
165+++ requirements.txt 2015-03-20 01:06:08 +0000
166@@ -1,5 +1,6 @@
167 kombu==3.0.24
168 python-keystoneclient==1.2.0
169+python-logstash==0.4.2
170 python-novaclient==2.22.0
171 python-swiftclient==2.3.1
172

Subscribers

People subscribed via source and target branches