Skip to content

Commit

Permalink
feat: add websocket logging (pinterest#1110)
Browse files Browse the repository at this point in the history
* feat: add websocket logging

* update package.json

* add uid comment
  • Loading branch information
jczhong84 authored and aidenprice committed Jan 3, 2024
1 parent 15a102c commit a4be9dd
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "querybook",
"version": "3.15.3",
"version": "3.15.4",
"description": "A Big Data Webapp",
"private": true,
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions querybook/server/const/event_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
class EventType(Enum):
# an api request
API = "api"
# websocket event
WEBSOCKET = "websocket"
# a UI element gets viewed
VIEW = "view"
# a UI element gets clicked
Expand Down
9 changes: 8 additions & 1 deletion querybook/server/datasources_socketio/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
from flask_socketio import disconnect

from app.flask_app import socketio
from lib.event_logger import event_logger
from lib.logger import get_logger

LOG = get_logger(__file__)


def register_socket(url, namespace=None):
def register_socket(url, namespace=None, websocket_logging=True):
def wrapper(fn):
@socketio.on(url, namespace=namespace)
@functools.wraps(fn)
Expand All @@ -19,6 +20,12 @@ def handler(*args, **kwargs):
disconnect()
else:
try:
if websocket_logging:
event_logger.log_websocket_event(
route=namespace + "/" + url,
args=args,
kwargs=kwargs,
)
fn(*args, **kwargs)
except Exception as e:
LOG.error(e, exc_info=True)
Expand Down
8 changes: 8 additions & 0 deletions querybook/server/lib/event_logger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,13 @@ def log_api_request(self, route: str, method: str, params: dict):
except Exception as e:
LOG.error(e, exc_info=True)

def log_websocket_event(self, route: str, args: list, kwargs: dict):
try:
self.logger.log_websocket_event(
uid=current_user.id, route=route, args=args, kwargs=kwargs
)
except Exception as e:
LOG.error(e, exc_info=True)


event_logger = EventLogger()
15 changes: 15 additions & 0 deletions querybook/server/lib/event_logger/base_event_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def log_api_request(self, uid: int, route: str, method: str, params: dict) -> No
"""Log an API request.
Args:
uid (int): id of the user who performed the action
method (str): request method, e.g. GET, POST
route (str): route of the api endpoint which serves the request
params (dict): params of the request, includes path params,
Expand All @@ -101,6 +102,20 @@ def log_api_request(self, uid: int, route: str, method: str, params: dict) -> No
event_data = {"method": method, "route": route, "params": params}
self.log(uid=uid, event_type=EventType.API, event_data=event_data)

def log_websocket_event(
self, uid: int, route: str, args: list, kwargs: dict
) -> None:
"""Log websocket event.
Args:
uid (int): id of the user who performed the action
route (str): route is namespace joined with event name.
args (list): non-keyworded arguments passed over from client.
params (dict): keyworded arguments passed over from client.
"""
event_data = {"route": route, "args": args, "kwargs": kwargs}
self.log(uid=uid, event_type=EventType.WEBSOCKET, event_data=event_data)

def __match_filter_rule(self, rule: ApiFilterRule, route: str, method: str) -> bool:
route_matched = (
route.startswith(rule["route"])
Expand Down

0 comments on commit a4be9dd

Please sign in to comment.