~ubuntu-branches/ubuntu/trusty/swift/trusty-updates

« back to all changes in this revision

Viewing changes to swift/common/middleware/proxy_logging.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Soren Hansen, Chuck Short
  • Date: 2012-09-07 19:02:36 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20120907190236-fqrmbzm7v6zivs8d
Tags: 1.7.0-0ubuntu1
[ Soren Hansen ]
* Update debian/watch to account for symbolically named tarballs and
  use newer URL.
* Run unit tests at build time.
* Fix Launchpad URLs in debian/watch.

[ Chuck Short ]
* New upstream release
* debian/control: Add pubthon-moc as a build dep
* debian/rules: Dont fail if testsuite fails.

Show diffs side-by-side

added added

removed removed

Lines of Context:
94
94
    def __init__(self, app, conf):
95
95
        self.app = app
96
96
        self.log_hdrs = conf.get('log_headers', 'no').lower() in TRUE_VALUES
 
97
 
 
98
        # The leading access_* check is in case someone assumes that
 
99
        # log_statsd_valid_http_methods behaves like the other log_statsd_*
 
100
        # settings.
 
101
        self.valid_methods = conf.get(
 
102
            'access_log_statsd_valid_http_methods',
 
103
            conf.get('log_statsd_valid_http_methods',
 
104
                     'GET,HEAD,POST,PUT,DELETE,COPY'))
 
105
        self.valid_methods = [m.strip().upper() for m in
 
106
                              self.valid_methods.split(',') if m.strip()]
97
107
        access_log_conf = {}
98
108
        for key in ('log_facility', 'log_name', 'log_level', 'log_udp_host',
99
 
                    'log_udp_port'):
 
109
                    'log_udp_port', 'log_statsd_host', 'log_statsd_port',
 
110
                    'log_statsd_default_sample_rate',
 
111
                    'log_statsd_metric_prefix'):
100
112
            value = conf.get('access_' + key, conf.get(key, None))
101
113
            if value:
102
114
                access_log_conf[key] = value
103
115
        self.access_logger = get_logger(access_log_conf,
104
 
                        log_route='proxy-access')
 
116
                                        log_route='proxy-access')
 
117
        self.access_logger.set_statsd_prefix('proxy-server')
105
118
 
106
119
    def log_request(self, env, status_int, bytes_received, bytes_sent,
107
120
                    request_time, client_disconnect):
124
137
        logged_headers = None
125
138
        if self.log_hdrs:
126
139
            logged_headers = '\n'.join('%s: %s' % (k, v)
127
 
                for k, v in req.headers.items())
128
 
        self.access_logger.info(' '.join(quote(str(x) if x else '-')
 
140
                                       for k, v in req.headers.items())
 
141
        method = req.environ.get('swift.orig_req_method', req.method)
 
142
        self.access_logger.info(' '.join(
 
143
            quote(str(x) if x else '-')
129
144
            for x in (
130
145
                get_remote_client(req),
131
146
                req.remote_addr,
132
147
                time.strftime('%d/%b/%Y/%H/%M/%S', time.gmtime()),
133
 
                req.method,
 
148
                method,
134
149
                the_request,
135
150
                req.environ.get('SERVER_PROTOCOL'),
136
151
                status_int,
145
160
                '%.4f' % request_time,
146
161
                req.environ.get('swift.source'),
147
162
            )))
148
 
        self.access_logger.txn_id = None
 
163
        # Log timing and bytes-transfered data to StatsD
 
164
        if req.path.startswith('/v1/'):
 
165
            try:
 
166
                stat_type = [None, 'account', 'container',
 
167
                             'object'][req.path.strip('/').count('/')]
 
168
            except IndexError:
 
169
                stat_type = 'object'
 
170
        else:
 
171
            stat_type = env.get('swift.source')
 
172
        # Only log data for valid controllers (or SOS) to keep the metric count
 
173
        # down (egregious errors will get logged by the proxy server itself).
 
174
        if stat_type:
 
175
            stat_method = method if method in self.valid_methods \
 
176
                else 'BAD_METHOD'
 
177
            metric_name = '.'.join((stat_type, stat_method, str(status_int)))
 
178
            self.access_logger.timing(metric_name + '.timing',
 
179
                                      request_time * 1000)
 
180
            self.access_logger.update_stats(metric_name + '.xfer',
 
181
                                            bytes_received + bytes_sent)
149
182
 
150
183
    def __call__(self, env, start_response):
151
184
        start_response_args = [None]
175
208
                        ('content-length', str(sum(len(i) for i in iterable))))
176
209
                else:
177
210
                    raise Exception('WSGI [proxy-logging]: No content-length '
178
 
                        'or transfer-encoding header sent and there is '
179
 
                        'content! %r' % chunk)
 
211
                                    'or transfer-encoding header sent and '
 
212
                                    'there is content! %r' % chunk)
180
213
            start_response(*start_response_args[0])
181
214
            bytes_sent = 0
182
215
            client_disconnect = False
190
223
                raise
191
224
            finally:
192
225
                status_int = int(start_response_args[0][0].split(' ', 1)[0])
193
 
                self.log_request(env, status_int,
194
 
                        input_proxy.bytes_received, bytes_sent,
195
 
                        time.time() - start_time,
196
 
                        client_disconnect or input_proxy.client_disconnect)
 
226
                self.log_request(
 
227
                    env, status_int, input_proxy.bytes_received, bytes_sent,
 
228
                    time.time() - start_time,
 
229
                    client_disconnect or input_proxy.client_disconnect)
197
230
 
198
231
        try:
199
232
            iterable = self.app(env, my_start_response)
200
233
        except Exception:
201
 
            self.log_request(env, 500, input_proxy.bytes_received, 0,
202
 
                    time.time() - start_time, input_proxy.client_disconnect)
 
234
            self.log_request(
 
235
                env, 500, input_proxy.bytes_received, 0,
 
236
                time.time() - start_time, input_proxy.client_disconnect)
203
237
            raise
204
238
        else:
205
239
            return iter_response(iterable)