Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more stats (rendering time). #2138

Merged
merged 2 commits into from
Apr 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ c2cgeoportal/scaffolds/update/package.json_tmpl: ngeo/package.json .build/requir

$(MAKO_FILES:.mako=): .build/venv/bin/c2c-template ${VARS_FILES}

%: %.mako
%: %.mako .build/requirements.timestamp
$(C2C_TEMPLATE_CMD) --engine mako --files $<

c2cgeoportal/locale/c2cgeoportal.pot: lingua.cfg $(SRC_FILES) .build/requirements.timestamp
Expand Down
34 changes: 25 additions & 9 deletions c2cgeoportal/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ def get_stats(self, request):
return {"timers": timers}


INVALID_KEY_CHARS = re.compile(r"[:|\.]")


class _StatsDBackend(object): # pragma: nocover
def __init__(self, address, prefix):
self._prefix = prefix
Expand All @@ -132,7 +135,7 @@ def __init__(self, address, prefix):
self._socket.connect(sa)

def _key(self, key):
return self._prefix + ".".join([i.replace(".", r"_") for i in key])
return self._prefix + ".".join([INVALID_KEY_CHARS.sub("_", i) for i in key])

def timer(self, key, duration):
the_key = self._key(key)
Expand All @@ -143,12 +146,7 @@ def timer(self, key, duration):
pass # Ignore errors (must survive if stats cannot be sent)


def _request_callback(event): # pragma: nocover
"""
Callback called when a new HTTP request is incoming.
"""
measure = timer()

def _create_finished_cb(kind, measure): # pragma: nocover
def finished_cb(request):
if request.exception is not None:
if isinstance(request.exception, HTTPException):
Expand All @@ -161,10 +159,27 @@ def finished_cb(request):
name = "_not_found"
else:
name = request.matched_route.name
key = ["route", request.method, name, str(status)]
key = [kind, request.method, name, str(status)]
measure.stop(key)
return finished_cb


event.request.add_finished_callback(finished_cb)
def _request_callback(event): # pragma: nocover
"""
Callback called when a new HTTP request is incoming.
"""
measure = timer()
event.request.add_finished_callback(_create_finished_cb("route", measure))


def _before_rendered_callback(event): # pragma: nocover
"""
Callback called when the rendering is starting.
"""
request = event.get("request", None)
if request:
measure = timer()
request.add_finished_callback(_create_finished_cb("render", measure))


def _simplify_sql(sql):
Expand Down Expand Up @@ -215,6 +230,7 @@ def init_pyramid_spy(config): # pragma: nocover
:param config: The Pyramid config
"""
config.add_subscriber(_request_callback, pyramid.events.NewRequest)
config.add_subscriber(_before_rendered_callback, pyramid.events.BeforeRender)


def _get_env_or_settings(config, what_env, what_vars, default):
Expand Down