matrix-org / synapse

Synapse: Matrix homeserver written in Python/Twisted.
https://matrix-org.github.io/synapse
Apache License 2.0
11.79k stars 2.13k forks source link

Ability to add Sentry environment #14715

Open yasinishyn opened 1 year ago

yasinishyn commented 1 year ago

Currently, it's not possible to define Sentry environments. The initialization code is https://github.com/matrix-org/synapse/blob/9af2be192a759c22d189b72cc0a7580cd9de8a37/synapse/app/_base.py#L620

The SDK used supports environments definition as documented here https://docs.sentry.io/platforms/python/configuration/environments/

If configured, the environment is set to production by default. It's not ideal for teams who maintain several environments for the synapse backend (e.g. production, staging, development, etc...)

An easy fix might be to:

  1. Add environment to the homeserver.yaml file

    sentry:
    dsn: "..."
    environment: "..."
  2. change synapse/config/metrics.py here https://github.com/matrix-org/synapse/blob/9af2be192a759c22d189b72cc0a7580cd9de8a37/synapse/config/metrics.py#L59

    
    ...
        self.sentry_enabled = "sentry" in config
        if self.sentry_enabled:
            check_requirements("sentry")
    
            self.sentry_dsn = config["sentry"].get("dsn")
            self.sentry_environment = config["sentry"].get("environment", "production")

3. change `/synapse/app/_base.py` here https://github.com/matrix-org/synapse/blob/9af2be192a759c22d189b72cc0a7580cd9de8a37/synapse/app/_base.py#L620
sentry_sdk.init(
    dsn=hs.config.metrics.sentry_dsn,
    environment=hs.config.metrics.sentry_environment,
    release=SYNAPSE_VERSION,
)


I can submit a PR for this if that helps fixing the issue.
clokep commented 1 year ago

@yasinishyn I think your solution is reasonable, although I wonder if it would make more sense to add an extra_config into the homserver config file which then just gets passed to sentry_sdk.init(...)?

Something like:

sentry:
  dsn: ...
  extra_config:
    environment: ...
    debug: true

Then update the config code to pull that out and initialize the SDK:

sentry_sdk.init(
    dsn=hs.config.metrics.sentry_dsn,
    release=SYNAPSE_VERSION,
    **hs.config.metrics.sentry_extra_config,
)

This would avoid Synapse needing to add / track the upstream Sentry features.