From 1126e59c1242a8ea01ead5fe4b05330e47559fa5 Mon Sep 17 00:00:00 2001 From: Marcelo Martins Date: Mon, 25 Mar 2013 11:27:36 -0500 Subject: [PATCH] Adding a new optional variable called trans_id_suffix The trans_id_suffix (default is empty) would be appended to the swift transaction id allowing one to easily figure out from which cluster that X-Trans-Id belongs to. This is very useful when one is managing more than one swift cluster. Also updated sample and manpage to reflect the changes. Change-Id: Icdf63643e9c1bde36a9ef5e3f41ee9fb20e55f5d --- doc/manpages/proxy-server.conf.5 | 6 +++++- etc/proxy-server.conf-sample | 6 ++++++ swift/common/middleware/catch_errors.py | 10 +++++++--- swift/proxy/server.py | 3 ++- test/unit/common/middleware/test_except.py | 11 +++++++++++ 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/doc/manpages/proxy-server.conf.5 b/doc/manpages/proxy-server.conf.5 index 67899f69b9..43682979a5 100644 --- a/doc/manpages/proxy-server.conf.5 +++ b/doc/manpages/proxy-server.conf.5 @@ -78,6 +78,10 @@ Syslog log facility. The default is LOG_LOCAL0. Logging level. The default is INFO. .IP \fBlog_address\fR Logging address. The default is /dev/log. +.IP \fBtrans_id_suffix\fR +This optional suffix (default is empty) that would be appended to the swift transaction +id allows one to easily figure out from which cluster that X-Trans-Id belongs to. +This is very useful when one is managing more than one swift cluster. .RE .PD @@ -431,7 +435,7 @@ Python regular expressions of substrings that will not be allowed in a name. .RS 0 -.IP "\fB[filter:proxy_logging]\fR" +.IP "\fB[filter:proxy-logging]\fR" .RE Logging for the proxy server now lives in this middleware. diff --git a/etc/proxy-server.conf-sample b/etc/proxy-server.conf-sample index c05a345c88..ad90173e18 100644 --- a/etc/proxy-server.conf-sample +++ b/etc/proxy-server.conf-sample @@ -16,6 +16,11 @@ # log_level = INFO # log_headers = False # log_address = /dev/log +# This optional suffix (default is empty) that would be appended to the swift transaction +# id allows one to easily figure out from which cluster that X-Trans-Id belongs to. +# This is very useful when one is managing more than one swift cluster. +# trans_id_suffix = +# # comma separated list of functions to call to setup custom log handlers. # functions get passed: conf, name, log_to_console, log_route, fmt, logger, # adapted_logger @@ -340,6 +345,7 @@ use = egg:swift#proxy_logging # What HTTP methods are allowed for StatsD logging (comma-sep); request methods # not in this list will have "BAD_METHOD" for the portion of the metric. # log_statsd_valid_http_methods = GET,HEAD,POST,PUT,DELETE,COPY,OPTIONS +# # Note: The double proxy-logging in the pipeline is not a mistake. The # left-most proxy-logging is there to log requests that were handled in # middleware and never made it through to the right-most middleware (and diff --git a/swift/common/middleware/catch_errors.py b/swift/common/middleware/catch_errors.py index 4053825376..8159c47043 100644 --- a/swift/common/middleware/catch_errors.py +++ b/swift/common/middleware/catch_errors.py @@ -23,12 +23,13 @@ from swift.common.wsgi import WSGIContext class CatchErrorsContext(WSGIContext): - def __init__(self, app, logger): + def __init__(self, app, logger, trans_id_suffix=''): super(CatchErrorsContext, self).__init__(app) self.logger = logger + self.trans_id_suffix = trans_id_suffix def handle_request(self, env, start_response): - trans_id = 'tx' + uuid.uuid4().hex + trans_id = 'tx' + uuid.uuid4().hex + self.trans_id_suffix env['swift.trans_id'] = trans_id self.logger.txn_id = trans_id try: @@ -60,12 +61,15 @@ class CatchErrorMiddleware(object): def __init__(self, app, conf): self.app = app self.logger = get_logger(conf, log_route='catch-errors') + self.trans_id_suffix = conf.get('trans_id_suffix', '') def __call__(self, env, start_response): """ If used, this should be the first middleware in pipeline. """ - context = CatchErrorsContext(self.app, self.logger) + context = CatchErrorsContext(self.app, + self.logger, + self.trans_id_suffix) return context.handle_request(env, start_response) diff --git a/swift/proxy/server.py b/swift/proxy/server.py index 607236ca47..2fbf55002f 100644 --- a/swift/proxy/server.py +++ b/swift/proxy/server.py @@ -63,6 +63,7 @@ class Application(object): self.put_queue_depth = int(conf.get('put_queue_depth', 10)) self.object_chunk_size = int(conf.get('object_chunk_size', 65536)) self.client_chunk_size = int(conf.get('client_chunk_size', 65536)) + self.trans_id_suffix = conf.get('trans_id_suffix', '') self.error_suppression_interval = \ int(conf.get('error_suppression_interval', 60)) self.error_suppression_limit = \ @@ -210,7 +211,7 @@ class Application(object): controller = controller(self, **path_parts) if 'swift.trans_id' not in req.environ: # if this wasn't set by an earlier middleware, set it now - trans_id = 'tx' + uuid.uuid4().hex + trans_id = 'tx' + uuid.uuid4().hex + self.trans_id_suffix req.environ['swift.trans_id'] = trans_id self.logger.txn_id = trans_id req.headers['x-trans-id'] = req.environ['swift.trans_id'] diff --git a/test/unit/common/middleware/test_except.py b/test/unit/common/middleware/test_except.py index 05c243777b..60e7d44e5c 100644 --- a/test/unit/common/middleware/test_except.py +++ b/test/unit/common/middleware/test_except.py @@ -82,5 +82,16 @@ class TestCatchErrors(unittest.TestCase): resp = app(req.environ, start_response) self.assertEquals(list(resp), ['An error occurred']) + def test_trans_id_header_suffix(self): + self.assertEquals(self.logger.txn_id, None) + + def start_response(status, headers, exc_info=None): + self.assert_('x-trans-id' in (x[0] for x in headers)) + app = catch_errors.CatchErrorMiddleware( + FakeApp(), {'trans_id_suffix': '-stuff'}) + req = Request.blank('/v1/a/c/o') + app(req.environ, start_response) + self.assertTrue(self.logger.txn_id.endswith('-stuff')) + if __name__ == '__main__': unittest.main()