Skip to content

Commit

Permalink
Provide more information about the sql_json and csv actions and send …
Browse files Browse the repository at this point in the history
…it to App Insights
  • Loading branch information
whelan9453 committed Dec 18, 2019
1 parent d797a89 commit 2565d13
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
17 changes: 16 additions & 1 deletion superset/utils/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def wrapper(*args, **kwargs):
self.stats_logger.incr(f.__name__)
start_dttm = datetime.now()
value = f(*args, **kwargs)
extra_info = {}
if isinstance(value, tuple):
extra_info = value[1]
value = value[0]
duration_ms = (datetime.now() - start_dttm).total_seconds() * 1000

# bulk insert
Expand All @@ -80,6 +84,9 @@ def wrapper(*args, **kwargs):
slice_id=slice_id,
duration_ms=duration_ms,
referrer=referrer,
database=extra_info.get("database"),
schema=extra_info.get("schema"),
sql=extra_info.get("sql"),
)
return value

Expand Down Expand Up @@ -144,6 +151,9 @@ def log(self, user_id, action, *args, **kwargs):
slice_id = kwargs.get("slice_id")
duration_ms = kwargs.get("duration_ms")
referrer = kwargs.get("referrer")
database = kwargs.get("database")
schema = kwargs.get("schema")
sql = kwargs.get("sql")

logs = list()
for record in records:
Expand All @@ -161,7 +171,12 @@ def log(self, user_id, action, *args, **kwargs):
user_id=user_id,
)
logs.append(log)
self.appinsights({'level': 'info', 'success': 'true', 'state':'finish', 'function': action, 'json': json_string, 'duration': duration_ms, 'referrer': referrer, 'user_id': user_id})
self.appinsights(
{'level': 'info', 'success': 'true', 'state':'finish',
'function': action, 'json': json_string, 'duration': duration_ms,
'referrer': referrer, 'user_id': user_id,
'database': database, 'schema': schema, 'sql': sql
})

sesh = current_app.appbuilder.get_session
sesh.bulk_save_objects(logs)
Expand Down
11 changes: 9 additions & 2 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2724,8 +2724,11 @@ def sql_json(self):
# Async request.
if async_flag:
return self._sql_json_async(session, rendered_query, query)

# Extra log info for App Insights
extra_info = {'database': query.database.name, 'schema': query.schema, 'sql': query.sql}
# Sync request.
return self._sql_json_sync(session, rendered_query, query)
return self._sql_json_sync(session, rendered_query, query), extra_info

@has_access
@expose("/csv/<client_id>")
Expand Down Expand Up @@ -2779,6 +2782,9 @@ def csv(self, client_id):
f"CSV exported: {repr(event_info)}", extra={"superset_event": event_info}
)

# Extra log info for App Insights
extra_info = {'database': query.database.name, 'schema': query.schema, 'sql': query.sql}

# Fetch Clickhouse secrets from ENVs
CLICKHOUSE_HOST = os.environ.get('CLICKHOUSE_HOST')
CLICKHOUSE_UNAME = os.environ.get('CLICKHOUSE_UNAME')
Expand All @@ -2797,7 +2803,8 @@ def generate():
s += item + ','
s = s[:-1] + '\n'
yield s
return Response(generate(), mimetype='text/csv')

return Response(generate(), mimetype='text/csv'), extra_info

@api
@handle_api_exception
Expand Down

0 comments on commit 2565d13

Please sign in to comment.