Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ RUN chmod +x /start.sh
# Template config
ENV APP_ENTRYPOINT web
ENV LOG_LEVEL info
ENV LOG_SPARQL_ALL True
ENV MU_SPARQL_ENDPOINT 'http://database:8890/sparql'
ENV MU_SPARQL_UPDATEPOINT 'http://database:8890/sparql'
ENV MU_APPLICATION_GRAPH 'http://mu.semte.ch/application'
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ my-python:
```

### Environment variables
#### General

- `LOG_LEVEL` takes the same options as defined in the Python [logging](https://docs.python.org/3/library/logging.html#logging-levels) module.

Expand All @@ -317,7 +318,16 @@ my-python:

- `MU_SPARQL_TIMEOUT` is used to configure the timeout (in seconds) for SPARQL queries.

#### SPARQL Query Logging
- `LOG_SPARQL_ALL`: Log *all* executed queries, read as well as update (default `True`)

- `LOG_SPARQL_QUERIES`: Log *read* queries (default: `undefined`). Overrules `LOG_SPARQL_ALL`

- `LOG_SPARQL_UPDATES`: Log *update* queries (default `undefined`). Overrules `LOG_SPARQL_ALL`.

The string "true", ignoring casing, is considered `True`. All other values are considered `False`.

#### Meinheld Gunicorn Docker Variables
Since this template is based on the meinheld-gunicorn-docker image, all possible environment config for that image is also available for the template. See [meinheld-gunicorn-docker#environment-variables](https://github.com/tiangolo/meinheld-gunicorn-docker#environment-variables) for more info. The template configures `WEB_CONCURRENCY` in particular to `1` by default.

### Production
Expand Down
15 changes: 14 additions & 1 deletion helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
consoleHandler = logging.StreamHandler(stream=sys.stdout)# or stderr?
logger.addHandler(consoleHandler)

LOG_SPARQL_ALL_VAR = os.environ.get('LOG_SPARQL_ALL')
LOG_SPARQL_QUERIES = os.environ.get(
'LOG_SPARQL_QUERIES',
default=LOG_SPARQL_ALL_VAR
).lower() == 'true'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the Python boolean syntax is True (capitalized) I suggest adding that one too.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm. I didn't notice the lowercasing method.

LOG_SPARQL_UPDATES = os.environ.get(
'LOG_SPARQL_UPDATES',
default=LOG_SPARQL_ALL_VAR
).lower() == 'true'

def generate_uuid():
"""Generates a random unique user id (UUID) based on the host ID and current time"""
return str(uuid.uuid1())
Expand Down Expand Up @@ -119,14 +129,15 @@ def validate_resource_type(expected_type, data):

def query(the_query):
"""Execute the given SPARQL query (select/ask/construct) on the triplestore and returns the results in the given return Format (JSON by default)."""
log("execute query: \n" + the_query)
for header in MU_HEADERS:
if header in request.headers:
sparqlQuery.customHttpHeaders[header] = request.headers[header]
else: # Make sure headers used for a previous query are cleared
if header in sparqlQuery.customHttpHeaders:
del sparqlQuery.customHttpHeaders[header]
sparqlQuery.setQuery(the_query)
if LOG_SPARQL_QUERIES:
log("Execute query: \n" + the_query)
return sparqlQuery.query().convert()


Expand All @@ -140,6 +151,8 @@ def update(the_query):
del sparqlUpdate.customHttpHeaders[header]
sparqlUpdate.setQuery(the_query)
if sparqlUpdate.isSparqlUpdateRequest():
if LOG_SPARQL_UPDATES:
log("Execute query: \n" + the_query)
sparqlUpdate.query()


Expand Down