You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

112 lines
4.0 KiB

  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. # Copyright 2019 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import attr
  16. from synapse.python_dependencies import DependencyException, check_requirements
  17. from ._base import Config, ConfigError
  18. @attr.s
  19. class MetricsFlags:
  20. known_servers = attr.ib(default=False, validator=attr.validators.instance_of(bool))
  21. @classmethod
  22. def all_off(cls):
  23. """
  24. Instantiate the flags with all options set to off.
  25. """
  26. return cls(**{x.name: False for x in attr.fields(cls)})
  27. class MetricsConfig(Config):
  28. section = "metrics"
  29. def read_config(self, config, **kwargs):
  30. self.enable_metrics = config.get("enable_metrics", False)
  31. self.report_stats = config.get("report_stats", None)
  32. self.report_stats_endpoint = config.get(
  33. "report_stats_endpoint", "https://matrix.org/report-usage-stats/push"
  34. )
  35. self.metrics_port = config.get("metrics_port")
  36. self.metrics_bind_host = config.get("metrics_bind_host", "127.0.0.1")
  37. if self.enable_metrics:
  38. _metrics_config = config.get("metrics_flags") or {}
  39. self.metrics_flags = MetricsFlags(**_metrics_config)
  40. else:
  41. self.metrics_flags = MetricsFlags.all_off()
  42. self.sentry_enabled = "sentry" in config
  43. if self.sentry_enabled:
  44. try:
  45. check_requirements("sentry")
  46. except DependencyException as e:
  47. raise ConfigError(
  48. e.message # noqa: B306, DependencyException.message is a property
  49. )
  50. self.sentry_dsn = config["sentry"].get("dsn")
  51. if not self.sentry_dsn:
  52. raise ConfigError(
  53. "sentry.dsn field is required when sentry integration is enabled"
  54. )
  55. def generate_config_section(self, report_stats=None, **kwargs):
  56. res = """\
  57. ## Metrics ###
  58. # Enable collection and rendering of performance metrics
  59. #
  60. #enable_metrics: false
  61. # Enable sentry integration
  62. # NOTE: While attempts are made to ensure that the logs don't contain
  63. # any sensitive information, this cannot be guaranteed. By enabling
  64. # this option the sentry server may therefore receive sensitive
  65. # information, and it in turn may then diseminate sensitive information
  66. # through insecure notification channels if so configured.
  67. #
  68. #sentry:
  69. # dsn: "..."
  70. # Flags to enable Prometheus metrics which are not suitable to be
  71. # enabled by default, either for performance reasons or limited use.
  72. #
  73. metrics_flags:
  74. # Publish synapse_federation_known_servers, a gauge of the number of
  75. # servers this homeserver knows about, including itself. May cause
  76. # performance problems on large homeservers.
  77. #
  78. #known_servers: true
  79. # Whether or not to report anonymized homeserver usage statistics.
  80. #
  81. """
  82. if report_stats is None:
  83. res += "#report_stats: true|false\n"
  84. else:
  85. res += "report_stats: %s\n" % ("true" if report_stats else "false")
  86. res += """
  87. # The endpoint to report the anonymized homeserver usage statistics to.
  88. # Defaults to https://matrix.org/report-usage-stats/push
  89. #
  90. #report_stats_endpoint: https://example.com/report-usage-stats/push
  91. """
  92. return res