Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

# vim: tabstop=4 shiftwidth=4 softtabstop=4 

 

# Copyright 2012 OpenStack LLC 

# 

# 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 

# 

#      http://www.apache.org/licenses/LICENSE-2.0 

# 

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

 

from keystone.common import logging 

from keystone.common import manager 

from keystone.common import wsgi 

from keystone import config 

from keystone import exception 

from keystone import identity 

from keystone import policy 

from keystone import token 

 

 

CONF = config.CONF 

LOG = logging.getLogger(__name__) 

 

 

class Manager(manager.Manager): 

    """Default pivot point for the Stats backend. 

 

    See :mod:`keystone.common.manager.Manager` for more details on how this 

    dynamically calls the backend. 

 

    """ 

 

    def __init__(self): 

        super(Manager, self).__init__(CONF.stats.driver) 

 

 

class Driver(object): 

    """Interface description for a Stats driver.""" 

 

    def get_stats(self, api): 

        """Retrieve all previously-captured statistics for an interface.""" 

        raise exception.NotImplemented() 

 

    def set_stats(self, api, stats_ref): 

        """Update statistics for an interface.""" 

        raise exception.NotImplemented() 

 

    def increment_stat(self, api, category, value): 

        """Increment the counter for an individual statistic.""" 

        raise exception.NotImplemented() 

 

 

class StatsExtension(wsgi.ExtensionRouter): 

    """Reports on previously-collected request/response statistics.""" 

 

    def add_routes(self, mapper): 

        stats_controller = StatsController() 

 

        mapper.connect( 

            '/OS-STATS/stats', 

            controller=stats_controller, 

            action='get_stats', 

            conditions=dict(method=['GET'])) 

        mapper.connect( 

            '/OS-STATS/stats', 

            controller=stats_controller, 

            action='reset_stats', 

            conditions=dict(method=['DELETE'])) 

 

 

class StatsController(wsgi.Application): 

    def __init__(self): 

        self.identity_api = identity.Manager() 

        self.policy_api = policy.Manager() 

        self.stats_api = Manager() 

        self.token_api = token.Manager() 

        super(StatsController, self).__init__() 

 

    def get_stats(self, context): 

        self.assert_admin(context) 

        return { 

            'OS-STATS:stats': [ 

                { 

                    'type': 'identity', 

                    'api': 'admin', 

                    'extra': self.stats_api.get_stats(context, 'admin'), 

                }, 

                { 

                    'type': 'identity', 

                    'api': 'public', 

                    'extra': self.stats_api.get_stats(context, 'public'), 

                }, 

            ] 

        } 

 

    def reset_stats(self, context): 

        self.assert_admin(context) 

        self.stats_api.set_stats(context, 'public', dict()) 

        self.stats_api.set_stats(context, 'admin', dict()) 

 

 

class StatsMiddleware(wsgi.Middleware): 

    """Monitors various request/response attribute statistics.""" 

 

    request_attributes = ['application_url', 

                          'method', 

                          'path', 

                          'path_qs', 

                          'remote_addr'] 

 

    response_attributes = ['status_int'] 

 

    def __init__(self, *args, **kwargs): 

        self.stats_api = Manager() 

        return super(StatsMiddleware, self).__init__(*args, **kwargs) 

 

    def _resolve_api(self, host): 

        if str(CONF.admin_port) in host: 

            return 'admin' 

        elif str(CONF.public_port) in host: 

            return 'public' 

        else: 

            # NOTE(dolph): I don't think this is actually reachable, but hey 

            msg = 'Unable to resolve API as either public or admin: %s' % host 

            LOG.warning(msg) 

            return host 

 

    def capture_stats(self, host, obj, attributes): 

        """Collect each attribute from the given object.""" 

        for attribute in attributes: 

            self.stats_api.increment_stat(None, 

                                          self._resolve_api(host), 

                                          attribute, 

                                          getattr(obj, attribute)) 

 

    def process_request(self, request): 

        """Monitor incoming request attributes.""" 

        self.capture_stats(request.host, request, self.request_attributes) 

 

    def process_response(self, request, response): 

        """Monitor outgoing response attributes.""" 

        self.capture_stats(request.host, response, self.response_attributes) 

        return response