# Custom configuration

TOC:

* [Flask App Configuration Hooks](#flask-app-configuration-hooks)
* [Secret\_key Rotation](#secret_key-rotation)
* [Event Logging](#event-logging)
* [StatsD Logging](#statsd-logging)

## 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:

```python
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 <a href="#secret_key-rotation" id="secret_key-rotation"></a>

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`:

```python
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:

```python
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:

```python
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`.

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

{% hint style="info" %}
Note that it’s also possible to implement your own logger by deriving `spotrix.stats_logger.BaseStatsLogger`.
{% endhint %}
