Skip to content

Commit

Permalink
merging...
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed Jul 21, 2024
2 parents 5c24965 + 86aaa47 commit 1a31d26
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 34 deletions.
2 changes: 1 addition & 1 deletion django_statsd/__about__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__package_name__ = 'django-statsd'
__version__ = '2.5.2'
__version__ = '2.6.0'
__author__ = 'Rick van Hattem'
__author_email__ = '[email protected]'
__description__ = (
Expand Down
57 changes: 36 additions & 21 deletions django_statsd/middleware.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
from __future__ import with_statement
import time
import logging

import collections
import functools
import logging
import re
import threading
import time
import warnings
import collections
import statsd
import re

from django.utils import deprecation
import statsd
from django.core import exceptions

from . import utils
from . import settings
from . import settings, utils

logger = logging.getLogger(__name__)


TAGS_LIKE_SUPPORTED = ['=', '_is_']
try:
MAKE_TAGS_LIKE = settings.STATSD_TAGS_LIKE
Expand Down Expand Up @@ -53,10 +51,10 @@ def __enter__(self):
self.timer.start(self.key)

def __exit__(
self,
type_,
value,
traceback,
self,
type_,
value,
traceback,
):
self.timer.stop(self.key)

Expand Down Expand Up @@ -132,14 +130,22 @@ def __call__(self, key):
return WithTimer(self, key)


class StatsdMiddleware(deprecation.MiddlewareMixin):
class StatsdMiddleware:
scope = threading.local()

def __init__(self, get_response=None):
deprecation.MiddlewareMixin.__init__(self, get_response)
self.get_response = get_response
self.scope.timings = None
self.scope.counter = None

def __call__(self, request):
# store the timings in the request so it can be used everywhere
self.process_request(request)
try:
return self.process_response(request, self.get_response(request))
finally:
self.cleanup(request)

@classmethod
def skip_view(cls, view_name):
for pattern in settings.STATSD_VIEWS_TO_SKIP:
Expand Down Expand Up @@ -200,20 +206,20 @@ def process_response(self, request, response):
return response

self.scope.counter_codes.increment(
str(response.status_code // 100) + 'xx')
f'{str(response.status_code // 100)}xx')
self.scope.counter_codes.submit('http_codes')

if settings.STATSD_TRACK_MIDDLEWARE:
StatsdMiddleware.scope.timings.stop('process_response')
if MAKE_TAGS_LIKE:
method = 'method' + MAKE_TAGS_LIKE
method = f'method{MAKE_TAGS_LIKE}'
method += request.method.lower().replace('.', '_')

ajax_name = 'is_ajax' + MAKE_TAGS_LIKE
ajax_name += str(is_ajax(request)).lower()
is_ajax_ = f'is_ajax_{MAKE_TAGS_LIKE}'
is_ajax_ += str(is_ajax(request)).lower()

if getattr(self, 'view_name', None):
self.stop(method, self.view_name, ajax_name)
self.stop(method, self.view_name, is_ajax_)
else:
method = request.method.lower()
if is_ajax(request):
Expand Down Expand Up @@ -241,7 +247,14 @@ def cleanup(self, request):
request.statsd = None


class StatsdMiddlewareTimer(deprecation.MiddlewareMixin):
class StatsdMiddlewareTimer:

def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
self.process_request(request)
return self.process_response(request, self.get_response(request))

def process_request(self, request):
if settings.STATSD_TRACK_MIDDLEWARE:
Expand Down Expand Up @@ -307,6 +320,7 @@ def wrapper(prefix, f):
def _wrapper(*args, **kwargs):
with with_('%s.%s' % (prefix, f.__name__.lower())):
return f(*args, **kwargs)

return _wrapper


Expand All @@ -315,6 +329,7 @@ def named_wrapper(name, f):
def _wrapper(*args, **kwargs):
with with_(name):
return f(*args, **kwargs)

return _wrapper


Expand Down
23 changes: 11 additions & 12 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
[tox]
envlist =
py{36,37}-django{22,32},
py{38,39,310}-django{22,32,40},
flake8,
docs
envlist =
py3{8,9,10,11,12,13,14}-django{4.2,5.0},
flake8,
docs

skip_missing_interpreters = True
usedevelop = True

[gh-actions]
python =
python =
3.6: py36
3.7: py37
3.8: py38
Expand All @@ -18,13 +17,13 @@ python =

[testenv]
deps =
django22: Django<3
django32: Django<4
django40: Django>=4.0,<4.1
-r{toxinidir}/tests/requirements.txt
setuptools
django42: Django>=4.2,<4.3 # Added currently supported versions
django50: Django>=5.0,<5.1 # Added currently supported versions
-r{toxinidir}/tests/requirements.txt

envlist =
py3{6,7,8,9,10}-django{22,32,40}
envlist =
py3{8,9,10,11,12,13,14}-django{4.2,5.0},

commands =
python setup.py test
Expand Down

0 comments on commit 1a31d26

Please sign in to comment.