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

feat: add websocket logging #1110

Merged
merged 3 commits into from
Dec 22, 2022
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 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}
czgu marked this conversation as resolved.
Show resolved Hide resolved
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