Skip to content

Commit 3528458

Browse files
authored
feat: unpack payload into log function (apache#28521)
1 parent 1573c10 commit 3528458

File tree

3 files changed

+78
-22
lines changed

3 files changed

+78
-22
lines changed

superset/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979

8080
# By default will log events to the metadata database with `DBEventLogger`
8181
# Note that you can use `StdOutEventLogger` for debugging
82+
# Note that you can write your own event logger by extending `AbstractEventLogger`
83+
# https://github.com/apache/superset/blob/master/superset/utils/log.py
8284
EVENT_LOGGER = DBEventLogger()
8385

8486
SUPERSET_LOG_VIEW = True

superset/utils/log.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,31 @@ def get_logger_from_status(
9191

9292

9393
class AbstractEventLogger(ABC):
94+
# Parameters that are passed under the `curated_payload` arg to the log method
95+
curated_payload_params = {
96+
"force",
97+
"standalone",
98+
"runAsync",
99+
"json",
100+
"csv",
101+
"queryLimit",
102+
"select_as_cta",
103+
}
104+
# Similarly, parameters that are passed under the `curated_form_data` arg
105+
curated_form_data_params = {
106+
"dashboardId",
107+
"sliceId",
108+
"viz_type",
109+
"force",
110+
"compare_lag",
111+
"forecastPeriods",
112+
"granularity_sqla",
113+
"legendType",
114+
"legendOrientation",
115+
"show_legend",
116+
"time_grain_sqla",
117+
}
118+
94119
def __call__(
95120
self,
96121
action: str,
@@ -120,6 +145,16 @@ def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
120145
**self.payload_override,
121146
)
122147

148+
@classmethod
149+
def curate_payload(cls, payload: dict[str, Any]) -> dict[str, Any]:
150+
"""Curate payload to only include relevant keys/safe keys"""
151+
return {k: v for k, v in payload.items() if k in cls.curated_payload_params}
152+
153+
@classmethod
154+
def curate_form_data(cls, payload: dict[str, Any]) -> dict[str, Any]:
155+
"""Curate form_data to only include relevant keys/safe keys"""
156+
return {k: v for k, v in payload.items() if k in cls.curated_form_data_params}
157+
123158
@abstractmethod
124159
def log( # pylint: disable=too-many-arguments
125160
self,
@@ -129,6 +164,8 @@ def log( # pylint: disable=too-many-arguments
129164
duration_ms: int | None,
130165
slice_id: int | None,
131166
referrer: str | None,
167+
curated_payload: dict[str, Any] | None,
168+
curated_form_data: dict[str, Any] | None,
132169
*args: Any,
133170
**kwargs: Any,
134171
) -> None:
@@ -180,6 +217,7 @@ def log_with_context( # pylint: disable=too-many-locals,too-many-arguments
180217
"database_driver": database.driver,
181218
}
182219

220+
form_data: dict[str, Any] = {}
183221
if "form_data" in payload:
184222
form_data, _ = get_form_data()
185223
payload["form_data"] = form_data
@@ -207,6 +245,8 @@ def log_with_context( # pylint: disable=too-many-locals,too-many-arguments
207245
slice_id=slice_id,
208246
duration_ms=duration_ms,
209247
referrer=referrer,
248+
curated_payload=self.curate_payload(payload),
249+
curated_form_data=self.curate_form_data(form_data),
210250
**database_params,
211251
)
212252

@@ -380,6 +420,8 @@ def log( # pylint: disable=too-many-arguments
380420
duration_ms: int | None,
381421
slice_id: int | None,
382422
referrer: str | None,
423+
curated_payload: dict[str, Any] | None,
424+
curated_form_data: dict[str, Any] | None,
383425
*args: Any,
384426
**kwargs: Any,
385427
) -> None:
@@ -390,6 +432,8 @@ def log( # pylint: disable=too-many-arguments
390432
duration_ms=duration_ms,
391433
slice_id=slice_id,
392434
referrer=referrer,
435+
curated_payload=curated_payload,
436+
curated_form_data=curated_form_data,
393437
**kwargs,
394438
)
395439
print("StdOutEventLogger: ", data)

tests/integration_tests/event_logger_tests.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,19 @@ def log(
141141
with logger(action="foo", engine="bar"):
142142
pass
143143

144-
assert logger.records == [
145-
{
146-
"records": [{"path": "/", "engine": "bar"}],
147-
"database_id": None,
148-
"user_id": 2,
149-
"duration": 15000,
150-
}
151-
]
144+
self.assertEquals(
145+
logger.records,
146+
[
147+
{
148+
"records": [{"path": "/", "engine": "bar"}],
149+
"database_id": None,
150+
"user_id": 2,
151+
"duration": 15000,
152+
"curated_payload": {},
153+
"curated_form_data": {},
154+
}
155+
],
156+
)
152157

153158
@patch("superset.utils.core.g", spec={})
154159
def test_context_manager_log_with_context(self, mock_g):
@@ -183,20 +188,25 @@ def log(
183188
payload_override={"engine": "sqlite"},
184189
)
185190

186-
assert logger.records == [
187-
{
188-
"records": [
189-
{
190-
"path": "/",
191-
"object_ref": {"baz": "food"},
192-
"payload_override": {"engine": "sqlite"},
193-
}
194-
],
195-
"database_id": None,
196-
"user_id": 2,
197-
"duration": 5558756000,
198-
}
199-
]
191+
self.assertEquals(
192+
logger.records,
193+
[
194+
{
195+
"records": [
196+
{
197+
"path": "/",
198+
"object_ref": {"baz": "food"},
199+
"payload_override": {"engine": "sqlite"},
200+
}
201+
],
202+
"database_id": None,
203+
"user_id": 2,
204+
"duration": 5558756000,
205+
"curated_payload": {},
206+
"curated_form_data": {},
207+
}
208+
],
209+
)
200210

201211
@patch("superset.utils.core.g", spec={})
202212
def test_log_with_context_user_null(self, mock_g):

0 commit comments

Comments
 (0)