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
This commit is contained in:
Marcelo Martins 2013-03-25 11:27:36 -05:00
parent b115356af6
commit 1126e59c12
5 changed files with 31 additions and 5 deletions

View File

@ -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.

View File

@ -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 <verb> 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

View File

@ -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)

View File

@ -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']

View File

@ -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()