mirror of https://github.com/apache/superset.git
feat: unpack payload into log function (#28521)
This commit is contained in:
parent
1573c101a7
commit
35284589cb
|
@ -79,6 +79,8 @@ STATS_LOGGER = DummyStatsLogger()
|
|||
|
||||
# By default will log events to the metadata database with `DBEventLogger`
|
||||
# Note that you can use `StdOutEventLogger` for debugging
|
||||
# Note that you can write your own event logger by extending `AbstractEventLogger`
|
||||
# https://github.com/apache/superset/blob/master/superset/utils/log.py
|
||||
EVENT_LOGGER = DBEventLogger()
|
||||
|
||||
SUPERSET_LOG_VIEW = True
|
||||
|
|
|
@ -91,6 +91,31 @@ def get_logger_from_status(
|
|||
|
||||
|
||||
class AbstractEventLogger(ABC):
|
||||
# Parameters that are passed under the `curated_payload` arg to the log method
|
||||
curated_payload_params = {
|
||||
"force",
|
||||
"standalone",
|
||||
"runAsync",
|
||||
"json",
|
||||
"csv",
|
||||
"queryLimit",
|
||||
"select_as_cta",
|
||||
}
|
||||
# Similarly, parameters that are passed under the `curated_form_data` arg
|
||||
curated_form_data_params = {
|
||||
"dashboardId",
|
||||
"sliceId",
|
||||
"viz_type",
|
||||
"force",
|
||||
"compare_lag",
|
||||
"forecastPeriods",
|
||||
"granularity_sqla",
|
||||
"legendType",
|
||||
"legendOrientation",
|
||||
"show_legend",
|
||||
"time_grain_sqla",
|
||||
}
|
||||
|
||||
def __call__(
|
||||
self,
|
||||
action: str,
|
||||
|
@ -120,6 +145,16 @@ class AbstractEventLogger(ABC):
|
|||
**self.payload_override,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def curate_payload(cls, payload: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Curate payload to only include relevant keys/safe keys"""
|
||||
return {k: v for k, v in payload.items() if k in cls.curated_payload_params}
|
||||
|
||||
@classmethod
|
||||
def curate_form_data(cls, payload: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Curate form_data to only include relevant keys/safe keys"""
|
||||
return {k: v for k, v in payload.items() if k in cls.curated_form_data_params}
|
||||
|
||||
@abstractmethod
|
||||
def log( # pylint: disable=too-many-arguments
|
||||
self,
|
||||
|
@ -129,6 +164,8 @@ class AbstractEventLogger(ABC):
|
|||
duration_ms: int | None,
|
||||
slice_id: int | None,
|
||||
referrer: str | None,
|
||||
curated_payload: dict[str, Any] | None,
|
||||
curated_form_data: dict[str, Any] | None,
|
||||
*args: Any,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
|
@ -180,6 +217,7 @@ class AbstractEventLogger(ABC):
|
|||
"database_driver": database.driver,
|
||||
}
|
||||
|
||||
form_data: dict[str, Any] = {}
|
||||
if "form_data" in payload:
|
||||
form_data, _ = get_form_data()
|
||||
payload["form_data"] = form_data
|
||||
|
@ -207,6 +245,8 @@ class AbstractEventLogger(ABC):
|
|||
slice_id=slice_id,
|
||||
duration_ms=duration_ms,
|
||||
referrer=referrer,
|
||||
curated_payload=self.curate_payload(payload),
|
||||
curated_form_data=self.curate_form_data(form_data),
|
||||
**database_params,
|
||||
)
|
||||
|
||||
|
@ -380,6 +420,8 @@ class StdOutEventLogger(AbstractEventLogger):
|
|||
duration_ms: int | None,
|
||||
slice_id: int | None,
|
||||
referrer: str | None,
|
||||
curated_payload: dict[str, Any] | None,
|
||||
curated_form_data: dict[str, Any] | None,
|
||||
*args: Any,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
|
@ -390,6 +432,8 @@ class StdOutEventLogger(AbstractEventLogger):
|
|||
duration_ms=duration_ms,
|
||||
slice_id=slice_id,
|
||||
referrer=referrer,
|
||||
curated_payload=curated_payload,
|
||||
curated_form_data=curated_form_data,
|
||||
**kwargs,
|
||||
)
|
||||
print("StdOutEventLogger: ", data)
|
||||
|
|
|
@ -141,14 +141,19 @@ class TestEventLogger(unittest.TestCase):
|
|||
with logger(action="foo", engine="bar"):
|
||||
pass
|
||||
|
||||
assert logger.records == [
|
||||
self.assertEquals(
|
||||
logger.records,
|
||||
[
|
||||
{
|
||||
"records": [{"path": "/", "engine": "bar"}],
|
||||
"database_id": None,
|
||||
"user_id": 2,
|
||||
"duration": 15000,
|
||||
"curated_payload": {},
|
||||
"curated_form_data": {},
|
||||
}
|
||||
]
|
||||
],
|
||||
)
|
||||
|
||||
@patch("superset.utils.core.g", spec={})
|
||||
def test_context_manager_log_with_context(self, mock_g):
|
||||
|
@ -183,7 +188,9 @@ class TestEventLogger(unittest.TestCase):
|
|||
payload_override={"engine": "sqlite"},
|
||||
)
|
||||
|
||||
assert logger.records == [
|
||||
self.assertEquals(
|
||||
logger.records,
|
||||
[
|
||||
{
|
||||
"records": [
|
||||
{
|
||||
|
@ -195,8 +202,11 @@ class TestEventLogger(unittest.TestCase):
|
|||
"database_id": None,
|
||||
"user_id": 2,
|
||||
"duration": 5558756000,
|
||||
"curated_payload": {},
|
||||
"curated_form_data": {},
|
||||
}
|
||||
]
|
||||
],
|
||||
)
|
||||
|
||||
@patch("superset.utils.core.g", spec={})
|
||||
def test_log_with_context_user_null(self, mock_g):
|
||||
|
|
Loading…
Reference in New Issue