Coverage for nova/profiler.py: 71%
32 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-24 11:16 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-24 11:16 +0000
1# Copyright 2016 IBM Corporation.
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
16from oslo_utils import importutils
17import webob.dec
19import nova.conf
21profiler = importutils.try_import('osprofiler.profiler')
22profiler_web = importutils.try_import('osprofiler.web')
24CONF = nova.conf.CONF
27class WsgiMiddleware(object):
29 def __init__(self, application, **kwargs):
30 self.application = application
32 @classmethod
33 def factory(cls, global_conf, **local_conf):
34 if profiler_web: 34 ↛ 38line 34 didn't jump to line 38 because the condition on line 34 was always true
35 return profiler_web.WsgiMiddleware.factory(global_conf,
36 **local_conf)
38 def filter_(app):
39 return cls(app, **local_conf)
41 return filter_
43 @webob.dec.wsgify
44 def __call__(self, request):
45 return request.get_response(self.application)
48def get_traced_meta():
49 if profiler and 'profiler' in CONF and CONF.profiler.enabled: 49 ↛ 50line 49 didn't jump to line 50 because the condition on line 49 was never true
50 return profiler.TracedMeta
51 else:
52 # NOTE(rpodolyaka): if we do not return a child of type, then Python
53 # fails to build a correct MRO when osprofiler is not installed
54 class NoopMeta(type):
55 pass
56 return NoopMeta
59def trace_cls(name, **kwargs):
60 """Wrap the OSProfiler trace_cls decorator so that it will not try to
61 patch the class unless OSProfiler is present and enabled in the config
63 :param name: The name of action. E.g. wsgi, rpc, db, etc..
64 :param kwargs: Any other keyword args used by profiler.trace_cls
65 """
67 def decorator(cls):
68 if profiler and 'profiler' in CONF and CONF.profiler.enabled: 68 ↛ 69line 68 didn't jump to line 69 because the condition on line 68 was never true
69 trace_decorator = profiler.trace_cls(name, kwargs)
70 return trace_decorator(cls)
71 return cls
73 return decorator