Skip to content

Commit

Permalink
Add prometheus metrics (#527)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston authored Nov 18, 2022
1 parent 37bd539 commit 3c05e11
Show file tree
Hide file tree
Showing 28 changed files with 216 additions and 128 deletions.
1 change: 1 addition & 0 deletions changelog.d/527.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add some prometheus metrics and make `prometheus_client` dependency mandatory.
127 changes: 76 additions & 51 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ mypy_path = "stubs"
module = [
"idna",
"netaddr",
"prometheus_client",
"signedjson.*",
"sortedcontainers",
]
Expand Down Expand Up @@ -101,7 +100,7 @@ netaddr = ">=0.7.0"
matrix-common = "^1.1.0"
phonenumbers = ">=8.12.32"
# prometheus-client's lower bound is copied from Synapse.
prometheus-client = { version = ">=0.4.0", optional = true }
prometheus-client = ">=0.4.0"
pynacl = ">=1.2.1"
pyOpenSSL = ">=16.0.0"
pyyaml = ">=3.11"
Expand Down
4 changes: 3 additions & 1 deletion stubs/twisted/web/resource.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import ClassVar
from typing import Any, ClassVar

from twisted.web.server import Request
from zope.interface import Interface, implementer

class IResource(Interface):
Expand All @@ -11,3 +12,4 @@ class IResource(Interface):
class Resource:
isLeaf: ClassVar[bool]
def putChild(self, path: bytes, child: IResource) -> None: ...
def render(self, request: Request) -> Any: ...
20 changes: 20 additions & 0 deletions sydent/http/servlets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import logging
from typing import Any, Awaitable, Callable, Dict, Iterable, TypeVar

from prometheus_client import Counter
from twisted.internet import defer
from twisted.web import server
from twisted.web.resource import Resource
Expand All @@ -29,6 +30,25 @@
logger = logging.getLogger(__name__)


request_counter = Counter(
"sydent_http_received_requests",
"Received requests",
labelnames=("servlet", "method"),
)


class SydentResource(Resource):
"""A subclass of resource that tracks request metrics"""

def __init__(self) -> None:
self._name = self.__class__.__name__
super().__init__()

def render(self, request: Request) -> Any:
request_counter.labels(self._name, request.method).inc()
return super().render(request)


class MatrixRestError(Exception):
"""
Handled by the jsonwrap wrapper. Any servlets that don't use this
Expand Down
7 changes: 3 additions & 4 deletions sydent/http/servlets/accountservlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,21 @@

from typing import TYPE_CHECKING

from twisted.web.resource import Resource
from twisted.web.server import Request

from sydent.http.auth import authV2
from sydent.http.servlets import jsonwrap, send_cors
from sydent.http.servlets import SydentResource, jsonwrap, send_cors
from sydent.types import JsonDict

if TYPE_CHECKING:
from sydent.sydent import Sydent


class AccountServlet(Resource):
class AccountServlet(SydentResource):
isLeaf = False

def __init__(self, syd: "Sydent") -> None:
Resource.__init__(self)
super().__init__()
self.sydent = syd

@jsonwrap
Expand Down
7 changes: 3 additions & 4 deletions sydent/http/servlets/authenticated_bind_threepid_servlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,23 @@

from typing import TYPE_CHECKING

from twisted.web.resource import Resource
from twisted.web.server import Request

from sydent.http.servlets import get_args, jsonwrap, send_cors
from sydent.http.servlets import SydentResource, get_args, jsonwrap, send_cors
from sydent.types import JsonDict

if TYPE_CHECKING:
from sydent.sydent import Sydent


class AuthenticatedBindThreePidServlet(Resource):
class AuthenticatedBindThreePidServlet(SydentResource):
"""A servlet which allows a caller to bind any 3pid they want to an mxid
It is assumed that authentication happens out of band
"""

def __init__(self, sydent: "Sydent") -> None:
Resource.__init__(self)
super().__init__()
self.sydent = sydent

@jsonwrap
Expand Down
7 changes: 3 additions & 4 deletions sydent/http/servlets/authenticated_unbind_threepid_servlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,23 @@

from typing import TYPE_CHECKING

from twisted.web.resource import Resource
from twisted.web.server import Request

from sydent.http.servlets import get_args, jsonwrap, send_cors
from sydent.http.servlets import SydentResource, get_args, jsonwrap, send_cors
from sydent.types import JsonDict

if TYPE_CHECKING:
from sydent.sydent import Sydent


class AuthenticatedUnbindThreePidServlet(Resource):
class AuthenticatedUnbindThreePidServlet(SydentResource):
"""A servlet which allows a caller to unbind any 3pid they want from an mxid
It is assumed that authentication happens out of band
"""

def __init__(self, sydent: "Sydent") -> None:
Resource.__init__(self)
super().__init__()
self.sydent = sydent

@jsonwrap
Expand Down
12 changes: 9 additions & 3 deletions sydent/http/servlets/blindlysignstuffservlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@

import signedjson.key
import signedjson.sign
from twisted.web.resource import Resource
from twisted.web.server import Request

from sydent.db.invite_tokens import JoinTokenStore
from sydent.http.auth import authV2
from sydent.http.servlets import MatrixRestError, get_args, jsonwrap, send_cors
from sydent.http.servlets import (
MatrixRestError,
SydentResource,
get_args,
jsonwrap,
send_cors,
)
from sydent.types import JsonDict

if TYPE_CHECKING:
Expand All @@ -31,10 +36,11 @@
logger = logging.getLogger(__name__)


class BlindlySignStuffServlet(Resource):
class BlindlySignStuffServlet(SydentResource):
isLeaf = True

def __init__(self, syd: "Sydent", require_auth: bool = False) -> None:
super().__init__()
self.sydent = syd
self.server_name = syd.config.general.server_name
self.tokenStore = JoinTokenStore(syd)
Expand Down
12 changes: 9 additions & 3 deletions sydent/http/servlets/bulklookupservlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
import logging
from typing import TYPE_CHECKING

from twisted.web.resource import Resource
from twisted.web.server import Request

from sydent.db.threepid_associations import GlobalAssociationStore
from sydent.http.servlets import MatrixRestError, get_args, jsonwrap, send_cors
from sydent.http.servlets import (
MatrixRestError,
SydentResource,
get_args,
jsonwrap,
send_cors,
)
from sydent.types import JsonDict

if TYPE_CHECKING:
Expand All @@ -28,10 +33,11 @@
logger = logging.getLogger(__name__)


class BulkLookupServlet(Resource):
class BulkLookupServlet(SydentResource):
isLeaf = True

def __init__(self, syd: "Sydent") -> None:
super().__init__()
self.sydent = syd

@jsonwrap
Expand Down
9 changes: 5 additions & 4 deletions sydent/http/servlets/emailservlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@

from typing import TYPE_CHECKING, Optional

from twisted.web.resource import Resource
from twisted.web.server import Request

from sydent.http.auth import authV2
from sydent.http.servlets import get_args, jsonwrap, send_cors
from sydent.http.servlets import SydentResource, get_args, jsonwrap, send_cors
from sydent.types import JsonDict
from sydent.util.emailutils import EmailAddressException, EmailSendException
from sydent.util.stringutils import MAX_EMAIL_ADDRESS_LENGTH, is_valid_client_secret
Expand All @@ -33,10 +32,11 @@
from sydent.sydent import Sydent


class EmailRequestCodeServlet(Resource):
class EmailRequestCodeServlet(SydentResource):
isLeaf = True

def __init__(self, syd: "Sydent", require_auth: bool = False) -> None:
super().__init__()
self.sydent = syd
self.require_auth = require_auth

Expand Down Expand Up @@ -122,10 +122,11 @@ def render_OPTIONS(self, request: Request) -> bytes:
return b""


class EmailValidateCodeServlet(Resource):
class EmailValidateCodeServlet(SydentResource):
isLeaf = True

def __init__(self, syd: "Sydent", require_auth: bool = False) -> None:
super().__init__()
self.sydent = syd
self.require_auth = require_auth

Expand Down
6 changes: 3 additions & 3 deletions sydent/http/servlets/getvalidated3pidservlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@

from typing import TYPE_CHECKING

from twisted.web.resource import Resource
from twisted.web.server import Request

from sydent.db.valsession import ThreePidValSessionStore
from sydent.http.auth import authV2
from sydent.http.servlets import get_args, jsonwrap, send_cors
from sydent.http.servlets import SydentResource, get_args, jsonwrap, send_cors
from sydent.types import JsonDict
from sydent.util.stringutils import is_valid_client_secret
from sydent.validators import (
Expand All @@ -33,10 +32,11 @@
from sydent.sydent import Sydent


class GetValidated3pidServlet(Resource):
class GetValidated3pidServlet(SydentResource):
isLeaf = True

def __init__(self, syd: "Sydent", require_auth: bool = False) -> None:
super().__init__()
self.sydent = syd
self.require_auth = require_auth

Expand Down
6 changes: 3 additions & 3 deletions sydent/http/servlets/hashdetailsservlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
import logging
from typing import TYPE_CHECKING

from twisted.web.resource import Resource
from twisted.web.server import Request

from sydent.http.auth import authV2
from sydent.http.servlets import jsonwrap, send_cors
from sydent.http.servlets import SydentResource, jsonwrap, send_cors
from sydent.types import JsonDict

if TYPE_CHECKING:
Expand All @@ -28,11 +27,12 @@
logger = logging.getLogger(__name__)


class HashDetailsServlet(Resource):
class HashDetailsServlet(SydentResource):
isLeaf = True
known_algorithms = ["sha256", "none"]

def __init__(self, syd: "Sydent", lookup_pepper: str) -> None:
super().__init__()
self.sydent = syd
self.lookup_pepper = lookup_pepper

Expand Down
6 changes: 3 additions & 3 deletions sydent/http/servlets/logoutservlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
import logging
from typing import TYPE_CHECKING

from twisted.web.resource import Resource
from twisted.web.server import Request

from sydent.db.accounts import AccountStore
from sydent.http.auth import authV2, tokenFromRequest
from sydent.http.servlets import MatrixRestError, jsonwrap, send_cors
from sydent.http.servlets import MatrixRestError, SydentResource, jsonwrap, send_cors
from sydent.types import JsonDict

if TYPE_CHECKING:
Expand All @@ -29,10 +28,11 @@
logger = logging.getLogger(__name__)


class LogoutServlet(Resource):
class LogoutServlet(SydentResource):
isLeaf = True

def __init__(self, syd: "Sydent") -> None:
super().__init__()
self.sydent = syd

@jsonwrap
Expand Down
6 changes: 3 additions & 3 deletions sydent/http/servlets/lookupservlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
from typing import TYPE_CHECKING

import signedjson.sign
from twisted.web.resource import Resource
from twisted.web.server import Request

from sydent.db.threepid_associations import GlobalAssociationStore
from sydent.http.servlets import get_args, jsonwrap, send_cors
from sydent.http.servlets import SydentResource, get_args, jsonwrap, send_cors
from sydent.types import JsonDict
from sydent.util import json_decoder

Expand All @@ -31,10 +30,11 @@
logger = logging.getLogger(__name__)


class LookupServlet(Resource):
class LookupServlet(SydentResource):
isLeaf = True

def __init__(self, syd: "Sydent") -> None:
super().__init__()
self.sydent = syd

@jsonwrap
Expand Down
6 changes: 3 additions & 3 deletions sydent/http/servlets/lookupv2servlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
import logging
from typing import TYPE_CHECKING

from twisted.web.resource import Resource
from twisted.web.server import Request

from sydent.db.threepid_associations import GlobalAssociationStore
from sydent.http.auth import authV2
from sydent.http.servlets import get_args, jsonwrap, send_cors
from sydent.http.servlets import SydentResource, get_args, jsonwrap, send_cors
from sydent.http.servlets.hashdetailsservlet import HashDetailsServlet
from sydent.types import JsonDict

Expand All @@ -30,10 +29,11 @@
logger = logging.getLogger(__name__)


class LookupV2Servlet(Resource):
class LookupV2Servlet(SydentResource):
isLeaf = True

def __init__(self, syd: "Sydent", lookup_pepper: str) -> None:
super().__init__()
self.sydent = syd
self.globalAssociationStore = GlobalAssociationStore(self.sydent)
self.lookup_pepper = lookup_pepper
Expand Down
Loading

0 comments on commit 3c05e11

Please sign in to comment.