Skip to content

Commit 44d2cd6

Browse files
authored
Merge pull request #106 from maykinmedia/issue/88-sentry-config
🐛 [#88] remove function to init Sentry
2 parents 8cd42fc + ed56965 commit 44d2cd6

File tree

7 files changed

+129
-41
lines changed

7 files changed

+129
-41
lines changed

docs/quickstart.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ Add the following imports to the top of the project's ``base.py`` file:
2828
from open_api_framework.conf.base import * # noqa
2929
from open_api_framework.conf.utils import config
3030
31-
Then you can initialize sentry by adding the following line to ``base.py``:
32-
33-
.. code:: python
34-
35-
init_sentry()
36-
3731
3832
After that, compare the settings from `open_api_framework.conf.base`_ to the settings
3933
defined in the project's ``base.py`` and remove settings from the latter to make use of the generic settings (if this is desired).

open_api_framework/conf/base.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import os
33
import warnings
44
from pathlib import Path
5-
from typing import Callable
65

76
from django.urls import reverse_lazy
87

@@ -809,35 +808,40 @@
809808
#
810809
# SENTRY - error monitoring
811810
#
811+
SENTRY_DSN = config(
812+
"SENTRY_DSN",
813+
None,
814+
help_text=(
815+
"URL of the sentry project to send error reports to. Default empty, "
816+
"i.e. -> no monitoring set up. Highly recommended to configure this."
817+
),
818+
auto_display_default=False,
819+
)
812820

821+
SENTRY_BEFORE_SEND = config(
822+
"SENTRY_BEFORE_SEND",
823+
None,
824+
help_text=(
825+
"Callback, which allows projects to define their own before_send filters"
826+
),
827+
auto_display_default=False,
828+
)
813829

814-
def init_sentry(before_send: Callable | None = None):
815-
SENTRY_DSN = config(
816-
"SENTRY_DSN",
817-
None,
818-
help_text=(
819-
"URL of the sentry project to send error reports to. Default empty, "
820-
"i.e. -> no monitoring set up. Highly recommended to configure this."
821-
),
822-
auto_display_default=False,
823-
)
830+
if SENTRY_DSN:
831+
SENTRY_CONFIG = {
832+
"dsn": SENTRY_DSN,
833+
"release": RELEASE or "RELEASE not set",
834+
"environment": ENVIRONMENT,
835+
}
824836

825-
if SENTRY_DSN:
826-
SENTRY_CONFIG = {
827-
"dsn": SENTRY_DSN,
828-
"release": RELEASE or "RELEASE not set",
829-
"environment": ENVIRONMENT,
830-
}
831-
832-
# Allow projects to define their own before_send filters
833-
if before_send:
834-
SENTRY_CONFIG["before_send"] = before_send
835-
836-
sentry_sdk.init(
837-
**SENTRY_CONFIG,
838-
integrations=get_sentry_integrations(),
839-
send_default_pii=True,
840-
)
837+
if SENTRY_BEFORE_SEND:
838+
SENTRY_CONFIG["before_send"] = SENTRY_BEFORE_SEND
839+
840+
sentry_sdk.init(
841+
**SENTRY_CONFIG,
842+
integrations=get_sentry_integrations(),
843+
send_default_pii=True,
844+
)
841845

842846

843847
#

pyproject.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Documentation = "http://open_api_framework.readthedocs.io/en/latest/"
6868
tests = [
6969
"pytest",
7070
"pytest-django",
71+
"pytest-env",
7172
"tox",
7273
"isort",
7374
"black",
@@ -99,7 +100,12 @@ sections=["FUTURE", "STDLIB", "DJANGO", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER
99100

100101
[tool.pytest.ini_options]
101102
testpaths = ["tests"]
102-
DJANGO_SETTINGS_MODULE = "testapp.settings"
103+
pythonpath = "."
104+
env = [
105+
"DJANGO_SETTINGS_MODULE=testapp.settings",
106+
"SECRET_KEY=not-so-secret",
107+
"SENTRY_DSN=https://[email protected]/1"
108+
]
103109

104110
[tool.bumpversion]
105111
current_version = "0.9.2"

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
[flake8]
44
max-line-length = 119
5-
exclude=env,.tox,doc
5+
exclude=env,.tox,doc,tests/test_generate_envvar_docs.py

testapp/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from pathlib import Path
22

3+
from open_api_framework.conf.base import SENTRY_CONFIG, SENTRY_DSN # noqa
34
from open_api_framework.conf.utils import config
45

56
BASE_DIR = Path(__file__).resolve(strict=True).parent

tests/test_generate_envvar_docs.py

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,92 @@
1818
Required
1919
--------
2020
21-
* ``SECRET_KEY``: Secret key that's used for certain cryptographic utilities. Defaults to: \
22-
``so-secret-i-cant-believe-you-are-looking-at-this``.
21+
* ``SECRET_KEY``: Secret key that's used for certain cryptographic utilities. Defaults to: ``so-secret-i-cant-believe-you-are-looking-at-this``.
22+
* ``ALLOWED_HOSTS``: a comma separated (without spaces!) list of domains that serve the installation. Used to protect against Host header attacks. Defaults to: ``(empty string)``.
23+
* ``CACHE_DEFAULT``: redis cache address for the default cache (this **MUST** be set when using Docker). Defaults to: ``localhost:6379/0``.
24+
* ``CACHE_AXES``: redis cache address for the brute force login protection cache (this **MUST** be set when using Docker). Defaults to: ``localhost:6379/0``.
25+
* ``EMAIL_HOST``: hostname for the outgoing e-mail server (this **MUST** be set when using Docker). Defaults to: ``localhost``.
26+
27+
28+
Database
29+
--------
30+
31+
* ``DB_NAME``: name of the PostgreSQL database. Defaults to: ``testapp``.
32+
* ``DB_USER``: username of the database user. Defaults to: ``testapp``.
33+
* ``DB_PASSWORD``: password of the database user. Defaults to: ``testapp``.
34+
* ``DB_HOST``: hostname of the PostgreSQL database. Defaults to ``db`` for the docker environment, otherwise defaults to ``localhost``.
35+
* ``DB_PORT``: port number of the database. Defaults to: ``5432``.
36+
37+
38+
Cross-Origin-Resource-Sharing
39+
-----------------------------
40+
41+
* ``CORS_ALLOW_ALL_ORIGINS``: allow cross-domain access from any client. Defaults to: ``False``.
42+
* ``CORS_ALLOWED_ORIGINS``: explicitly list the allowed origins for cross-domain requests. Example: http://localhost:3000,https://some-app.gemeente.nl. Defaults to: ``[]``.
43+
* ``CORS_ALLOWED_ORIGIN_REGEXES``: same as ``CORS_ALLOWED_ORIGINS``, but supports regular expressions. Defaults to: ``[]``.
44+
* ``CORS_EXTRA_ALLOW_HEADERS``: headers that are allowed to be sent as part of the cross-domain request. By default, Authorization, Accept-Crs and Content-Crs are already included. The value of this variable is added to these already included headers. Defaults to: ``[]``.
45+
46+
47+
Celery
48+
------
49+
50+
* ``CELERY_RESULT_BACKEND``: the URL of the backend/broker that will be used by Celery to send the notifications. Defaults to: ``redis://localhost:6379/1``.
51+
52+
53+
Elastic APM
54+
-----------
55+
56+
* ``ELASTIC_APM_SERVER_URL``: URL where Elastic APM is hosted. Defaults to: ``None``.
57+
* ``ELASTIC_APM_SERVICE_NAME``: Name of the service for this application in Elastic APM. Defaults to ``testapp - <environment>``.
58+
* ``ELASTIC_APM_SECRET_TOKEN``: Token used to communicate with Elastic APM. Defaults to: ``default``.
59+
* ``ELASTIC_APM_TRANSACTION_SAMPLE_RATE``: By default, the agent will sample every transaction (e.g. request to your service). To reduce overhead and storage requirements, set the sample rate to a value between 0.0 and 1.0. Defaults to: ``0.1``.
60+
61+
62+
Content Security Policy
63+
-----------------------
64+
65+
* ``CSP_EXTRA_DEFAULT_SRC``: Extra default source URLs for CSP other than ``self``. Used for ``img-src``, ``style-src`` and ``script-src``. Defaults to: ``[]``.
66+
* ``CSP_REPORT_URI``: URI of the``report-uri`` directive. Defaults to: ``None``.
67+
* ``CSP_REPORT_PERCENTAGE``: Percentage of requests that get the ``report-uri`` directive. Defaults to: ``0``.
68+
* ``CSP_EXTRA_FORM_ACTION``: Add additional ``form-action`` source to the default . Defaults to: ``[]``.
69+
* ``CSP_FORM_ACTION``: Override the default ``form-action`` source. Defaults to: ``['"\\'self\\'"']``.
70+
* ``CSP_EXTRA_IMG_SRC``: Extra ``img-src`` sources for CSP other than ``CSP_DEFAULT_SRC``. Defaults to: ``[]``.
71+
* ``CSP_OBJECT_SRC``: ``object-src`` urls. Defaults to: ``['"\\'none\\'"']``.
2372
2473
2574
Optional
2675
--------
2776
28-
* ``DEBUG``: Only set this to ``True`` on a local development environment. Various other \
29-
security settings are derived from this setting!. Defaults to: ``False``.
30-
* ``IS_HTTPS``: Used to construct absolute URLs and controls a variety of security settings. \
31-
Defaults to the inverse of ``DEBUG``.
77+
* ``SITE_ID``: The database ID of the site object. You usually won't have to touch this. Defaults to: ``1``.
78+
* ``DEBUG``: Only set this to ``True`` on a local development environment. Various other security settings are derived from this setting!. Defaults to: ``False``.
79+
* ``USE_X_FORWARDED_HOST``: whether to grab the domain/host from the X-Forwarded-Host header or not. This header is typically set by reverse proxies (such as nginx, traefik, Apache...). Note: this is a header that can be spoofed and you need to ensure you control it before enabling this. Defaults to: ``False``.
80+
* ``IS_HTTPS``: Used to construct absolute URLs and controls a variety of security settings. Defaults to the inverse of ``DEBUG``.
81+
* ``EMAIL_PORT``: port number of the outgoing e-mail server. Note that if you're on Google Cloud, sending e-mail via port 25 is completely blocked and you should use 487 for TLS. Defaults to: ``25``.
82+
* ``EMAIL_HOST_USER``: username to connect to the mail server. Defaults to: ``(empty string)``.
83+
* ``EMAIL_HOST_PASSWORD``: password to connect to the mail server. Defaults to: ``(empty string)``.
84+
* ``EMAIL_USE_TLS``: whether to use TLS or not to connect to the mail server. Should be True if you're changing the ``EMAIL_PORT`` to 487. Defaults to: ``False``.
85+
* ``DEFAULT_FROM_EMAIL``: The default email address from which emails are sent. Defaults to: ``[email protected]``.
86+
* ``LOG_STDOUT``: whether to log to stdout or not. Defaults to: ``True``.
87+
* ``LOG_LEVEL``: control the verbosity of logging output. Available values are ``CRITICAL``, ``ERROR``, ``WARNING``, ``INFO`` and ``DEBUG``. Defaults to: ``WARNING``.
88+
* ``LOG_QUERIES``: enable (query) logging at the database backend level. Note that you must also set ``DEBUG=1``, which should be done very sparingly!. Defaults to: ``False``.
89+
* ``LOG_REQUESTS``: enable logging of the outgoing requests. Defaults to: ``False``.
90+
* ``CELERY_LOGLEVEL``: control the verbosity of logging output for celery, independent of ``LOG_LEVEL``. Available values are ``CRITICAL``, ``ERROR``, ``WARNING``, ``INFO`` and ``DEBUG``. Defaults to: ``INFO``.
91+
* ``SESSION_COOKIE_AGE``: For how long, in seconds, the session cookie will be valid. Defaults to: ``1209600``.
92+
* ``SESSION_COOKIE_SAMESITE``: The value of the SameSite flag on the session cookie. This flag prevents the cookie from being sent in cross-site requests thus preventing CSRF attacks and making some methods of stealing session cookie impossible.Currently interferes with OIDC. Keep the value set at Lax if used. Defaults to: ``Lax``.
93+
* ``CSRF_COOKIE_SAMESITE``: The value of the SameSite flag on the CSRF cookie. This flag prevents the cookie from being sent in cross-site requests. Defaults to: ``Strict``.
94+
* ``ENVIRONMENT``: An identifier for the environment, displayed in the admin depending on the settings module used and included in the error monitoring (see ``SENTRY_DSN``). The default is set according to ``DJANGO_SETTINGS_MODULE``.
95+
* ``SUBPATH``: If hosted on a subpath, provide the value here. If you provide ``/gateway``, the component assumes its running at the base URL: ``https://somedomain/gateway/``. Defaults to an empty string. Defaults to: ``None``.
96+
* ``RELEASE``: The version number or commit hash of the application (this is also sent to Sentry).
97+
* ``NUM_PROXIES``: the number of reverse proxies in front of the application, as an integer. This is used to determine the actual client IP adres. On Kubernetes with an ingress you typically want to set this to 2. Defaults to: ``1``.
98+
* ``CSRF_TRUSTED_ORIGINS``: A list of trusted origins for unsafe requests (e.g. POST). Defaults to: ``[]``.
99+
* ``NOTIFICATIONS_DISABLED``: indicates whether or not notifications should be sent to the Notificaties API for operations on the API endpoints. Defaults to ``True`` for the ``dev`` environment, otherwise defaults to ``False``.
100+
* ``SENTRY_DSN``: URL of the sentry project to send error reports to. Default empty, i.e. -> no monitoring set up. Highly recommended to configure this.
101+
* ``SENTRY_BEFORE_SEND``: Callback, which allows projects to define their own before_send filters.
102+
* ``DISABLE_2FA``: Whether or not two factor authentication should be disabled. Defaults to: ``False``.
103+
* ``LOG_OUTGOING_REQUESTS_EMIT_BODY``: Whether or not outgoing request bodies should be logged. Defaults to: ``True``.
104+
* ``LOG_OUTGOING_REQUESTS_DB_SAVE``: Whether or not outgoing request logs should be saved to the database. Defaults to: ``False``.
105+
* ``LOG_OUTGOING_REQUESTS_DB_SAVE_BODY``: Whether or not outgoing request bodies should be saved to the database. Defaults to: ``True``.
106+
* ``LOG_OUTGOING_REQUESTS_MAX_AGE``: The amount of time after which request logs should be deleted from the database. Defaults to: ``7``.
32107
33108
34109

tests/test_settings.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def test_sentry_settings():
2+
"""
3+
test that sentry settings are initialized
4+
"""
5+
from django.conf import settings
6+
7+
assert hasattr(settings, "SENTRY_CONFIG") is True
8+
assert hasattr(settings, "SENTRY_DSN") is True

0 commit comments

Comments
 (0)