OpenStack library for messaging
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.0 KiB

# Copyright (c) 2013-2014 eNovance
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
Send notifications on request
import logging
import os.path
import sys
import traceback as tb
from oslo_config import cfg
from oslo_middleware import base
import six
import webob.dec
import oslo_messaging
from oslo_messaging._i18n import _LE
from oslo_messaging import notify
from oslo_messaging.openstack.common import context
LOG = logging.getLogger(__name__)
def log_and_ignore_error(fn):
def wrapped(*args, **kwargs):
return fn(*args, **kwargs)
except Exception as e:
LOG.exception(_LE('An exception occurred processing '
'the API call: %s ') % e)
return wrapped
class RequestNotifier(base.Middleware):
"""Send notification on request."""
def factory(cls, global_conf, **local_conf):
"""Factory method for paste.deploy."""
conf = global_conf.copy()
def _factory(app):
return cls(app, **conf)
return _factory
def __init__(self, app, **conf):
self.notifier = notify.Notifier(
oslo_messaging.get_transport(cfg.CONF, conf.get('url')),
self.service_name = conf.get('service_name')
self.ignore_req_list = [x.upper().strip() for x in
conf.get('ignore_req_list', '').split(',')]
super(RequestNotifier, self).__init__(app)
def environ_to_dict(environ):
"""Following PEP 333, server variables are lower case, so don't
include them.
return dict((k, v) for k, v in six.iteritems(environ)
if k.isupper() and k != 'HTTP_X_AUTH_TOKEN')
def process_request(self, request):
request.environ['HTTP_X_SERVICE_NAME'] = \
self.service_name or
payload = {
'request': self.environ_to_dict(request.environ),
def process_response(self, request, response,
exception=None, traceback=None):
payload = {
'request': self.environ_to_dict(request.environ),
if response:
payload['response'] = {
'status': response.status,
'headers': response.headers,
if exception:
payload['exception'] = {
'value': repr(exception),
'traceback': tb.format_tb(traceback)
def __call__(self, req):
if req.method in self.ignore_req_list:
return req.get_response(self.application)
response = req.get_response(self.application)
except Exception:
exc_type, value, traceback = sys.exc_info()
self.process_response(req, None, value, traceback)
self.process_response(req, response)
return response