Coverage for nova/profiler.py: 71%

32 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-17 15:08 +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. 

15 

16from oslo_utils import importutils 

17import webob.dec 

18 

19import nova.conf 

20 

21profiler = importutils.try_import('osprofiler.profiler') 

22profiler_web = importutils.try_import('osprofiler.web') 

23 

24CONF = nova.conf.CONF 

25 

26 

27class WsgiMiddleware(object): 

28 

29 def __init__(self, application, **kwargs): 

30 self.application = application 

31 

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) 

37 

38 def filter_(app): 

39 return cls(app, **local_conf) 

40 

41 return filter_ 

42 

43 @webob.dec.wsgify 

44 def __call__(self, request): 

45 return request.get_response(self.application) 

46 

47 

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 

57 

58 

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 

62 

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 """ 

66 

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 

72 

73 return decorator