Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add basic optional sentry.io integration #4632

Merged
merged 7 commits into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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 changelog.d/4632.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add basic optional sentry.io integration
30 changes: 30 additions & 0 deletions synapse/app/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
from twisted.internet import error, reactor
from twisted.protocols.tls import TLSMemoryBIOFactory

import synapse
from synapse.app import check_bind_error
from synapse.crypto import context_factory
from synapse.util import PreserveLoggingContext
from synapse.util.rlimit import change_resource_limit
from synapse.util.versionstring import get_version_string

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -266,9 +268,37 @@ def handle_sighup(*args, **kwargs):
# It is now safe to start your Synapse.
hs.start_listening(listeners)
hs.get_datastore().start_profiling()

setup_sentry_io(hs)
except Exception:
traceback.print_exc(file=sys.stderr)
reactor = hs.get_reactor()
if reactor.running:
reactor.stop()
sys.exit(1)


def setup_sentry_io(hs):
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
"""Enable sentry.io integration, if enabled in configuration

Args:
hs (synapse.server.HomeServer)
"""

if not hs.config.sentry_enabled:
return

import sentry_sdk
sentry_sdk.init(
dsn=hs.config.sentry_dsn,
release=get_version_string(synapse),
)

# We set some default tags that give some context to this instance
with sentry_sdk.configure_scope() as scope:
scope.set_tag("matrix_server_name", hs.config.server_name)

app = hs.config.worker_app if hs.config.worker_app else "synapse.app.homeserver"
name = hs.config.worker_name if hs.config.worker_name else "master"
scope.set_tag("worker_app", app)
scope.set_tag("worker_name", name)
24 changes: 23 additions & 1 deletion synapse/config/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from ._base import Config
from ._base import Config, ConfigError

MISSING_SENTRY = (
"""Missing sentry_sdk library. This is required for enable sentry.io
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
integration.

Install by running:
pip install sentry_sdk
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
"""
)


class MetricsConfig(Config):
Expand All @@ -23,12 +32,25 @@ def read_config(self, config):
self.metrics_port = config.get("metrics_port")
self.metrics_bind_host = config.get("metrics_bind_host", "127.0.0.1")

self.sentry_enabled = "sentry" in config
if self.sentry_enabled:
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
try:
import sentry_sdk # noqa F401
except ImportError:
raise ConfigError(MISSING_SENTRY)

self.sentry_dsn = config["sentry"]["dsn"]

def default_config(self, report_stats=None, **kwargs):
res = """\
## Metrics ###

# Enable collection and rendering of performance metrics
enable_metrics: False

# Enable sentry.io integration
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
#sentry:
# dsn: "..."
"""

if report_stats is None:
Expand Down
1 change: 1 addition & 0 deletions synapse/python_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"saml2": ["pysaml2>=4.5.0"],
"url_preview": ["lxml>=3.5.0"],
"test": ["mock>=2.0", "parameterized"],
"sentry": ["sentry-sdk>=0.7.2"],
Copy link
Member

Choose a reason for hiding this comment

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

I'm a bit concerned that we tell people to install with synapse[all], and it's starting to pull in the entire internet. Perhaps we should consider revising that advice.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I somewhat agree, though I think that is a change to consider outside of this PR

Copy link
Member

Choose a reason for hiding this comment

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

yup

}


Expand Down