Custom configuration

TOC:

Flask App Configuration Hooks

FLASK_APP_MUTATOR is a configuration function that can be provided in your environment, receives the app object and can alter it in any way. For example, add FLASK_APP_MUTATOR into your spotrix_config.py to setup session cookie expiration time to 24 hours:

from flask import session
from flask import Flask


def make_session_permanent():
    '''
    Enable maxAge for the cookie 'session'
    '''
    session.permanent = True

# Set up max age of session to 24 hours
PERMANENT_SESSION_LIFETIME = timedelta(hours=24)
def FLASK_APP_MUTATOR(app: Flask) -> None:
    app.before_request_funcs.setdefault(None, []).append(make_session_permanent)

SECRET_KEY Rotation

If you want to rotate the SECRET_KEY(change the existing secret key), follow the below steps.

Add the new SECRET_KEY and PREVIOUS_SECRET_KEY to spotrix_config.py:

PREVIOUS_SECRET_KEY = 'CURRENT_SECRET_KEY'
# To find out 'CURRENT_SECRET_KEY' follow these steps
# 1. Got to spotrix shell  : $ spotrix shell
# 2. Run the command       : >>> from flask import current_app; print(current_app.config["SECRET_KEY"])

SECRET_KEY = 'YOUR_OWN_RANDOM_GENERATED_SECRET_KEY' # Generate a secure SECRET_KEY usng "openssl rand -base64 42"

Then run spotrix re-encrypt-secrets.

Event Logging

Spotrix by default logs special action events in its internal database (DBEventLogger). These logs can be accessed on the UI by navigating to Security > Action Log. You can freely customize these logs by implementing your own event log class. When custom log class is enabled DBEventLogger is disabled and logs stop being populated in UI logs view. To achieve both, custom log class should extend built-in DBEventLogger log class.

Here's an example of a simple JSON-to-stdout class:

def log(self, user_id, action, *args, **kwargs):
    records = kwargs.get('records', list())
    dashboard_id = kwargs.get('dashboard_id')
    slice_id = kwargs.get('slice_id')
    duration_ms = kwargs.get('duration_ms')
    referrer = kwargs.get('referrer')

    for record in records:
        log = dict(
            action=action,
            json=record,on
            dashboard_id=dashboard_id,
            slice_id=slice_id,
            duration_ms=duration_ms,
            referrer=referrer,
            user_id=user_id
        )
        print(json.dumps(log))

End by updating your config to pass in an instance of the logger you want to use:

EVENT_LOGGER = JSONStdOutEventLogger()

StatsD Logging

Spotrix can be instrumented to log events to StatsD if desired. Most endpoints hit are logged as well as key events like query start and end in SQL Lab.

To setup StatsD logging, it’s a matter of configuring the logger in your spotrix_config.py.

from spotrix.stats_logger import StatsdStatsLogger
STATS_LOGGER = StatsdStatsLogger(host='localhost', port=8125, prefix='spotrix')

Note that it’s also possible to implement your own logger by deriving spotrix.stats_logger.BaseStatsLogger.

Last updated