1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2012 OpenStack LLC
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
17
from keystone import config
18
from keystone import exception
19
from keystone import identity
20
from keystone import policy
21
from keystone import token
22
from keystone.common import logging
23
from keystone.common import manager
24
from keystone.common import wsgi
28
LOG = logging.getLogger(__name__)
31
class Manager(manager.Manager):
32
"""Default pivot point for the Stats backend.
34
See :mod:`keystone.common.manager.Manager` for more details on how this
35
dynamically calls the backend.
40
super(Manager, self).__init__(CONF.stats.driver)
44
"""Interface description for a Stats driver."""
46
def get_stats(self, api):
47
"""Retrieve all previously-captured statistics for an interface."""
48
raise exception.NotImplemented()
50
def set_stats(self, api, stats_ref):
51
"""Update statistics for an interface."""
52
raise exception.NotImplemented()
54
def increment_stat(self, api, category, value):
55
"""Increment the counter for an individual statistic."""
56
raise exception.NotImplemented()
59
class StatsExtension(wsgi.ExtensionRouter):
60
"""Reports on previously-collected request/response statistics."""
62
def add_routes(self, mapper):
63
stats_controller = StatsController()
67
controller=stats_controller,
69
conditions=dict(method=['GET']))
72
controller=stats_controller,
74
conditions=dict(method=['DELETE']))
77
class StatsController(wsgi.Application):
79
self.identity_api = identity.Manager()
80
self.policy_api = policy.Manager()
81
self.stats_api = Manager()
82
self.token_api = token.Manager()
83
super(StatsController, self).__init__()
85
def get_stats(self, context):
86
self.assert_admin(context)
92
'extra': self.stats_api.get_stats(context, 'admin'),
97
'extra': self.stats_api.get_stats(context, 'public'),
102
def reset_stats(self, context):
103
self.assert_admin(context)
104
self.stats_api.set_stats(context, 'public', dict())
105
self.stats_api.set_stats(context, 'admin', dict())
108
class StatsMiddleware(wsgi.Middleware):
109
"""Monitors various request/response attribute statistics."""
111
request_attributes = ['application_url',
117
response_attributes = ['status_int']
119
def __init__(self, *args, **kwargs):
120
self.stats_api = Manager()
121
return super(StatsMiddleware, self).__init__(*args, **kwargs)
123
def _resolve_api(self, host):
124
if str(CONF.admin_port) in host:
126
elif str(CONF.public_port) in host:
129
# NOTE(dolph): I don't think this is actually reachable, but hey
130
msg = 'Unable to resolve API as either public or admin: %s' % host
134
def capture_stats(self, host, obj, attributes):
135
"""Collect each attribute from the given object."""
136
for attribute in attributes:
137
self.stats_api.increment_stat(None,
138
self._resolve_api(host),
140
getattr(obj, attribute))
142
def process_request(self, request):
143
"""Monitor incoming request attributes."""
144
self.capture_stats(request.host, request, self.request_attributes)
146
def process_response(self, request, response):
147
"""Monitor outgoing response attributes."""
148
self.capture_stats(request.host, response, self.response_attributes)