metrics.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.util.check_dependencies import DependencyException, check_requirements
  17. from ._base import Config, ConfigError
  18. @attr.s
  19. class MetricsFlags:
  20. known_servers: bool = attr.ib(
  21. default=False, validator=attr.validators.instance_of(bool)
  22. )
  23. @classmethod
  24. def all_off(cls) -> "MetricsFlags":
  25. """
  26. Instantiate the flags with all options set to off.
  27. """
  28. return cls(**{x.name: False for x in attr.fields(cls)})
  29. class MetricsConfig(Config):
  30. section = "metrics"
  31. def read_config(self, config, **kwargs):
  32. self.enable_metrics = config.get("enable_metrics", False)
  33. self.report_stats = config.get("report_stats", None)
  34. self.report_stats_endpoint = config.get(
  35. "report_stats_endpoint", "https://matrix.org/report-usage-stats/push"
  36. )
  37. self.metrics_port = config.get("metrics_port")
  38. self.metrics_bind_host = config.get("metrics_bind_host", "127.0.0.1")
  39. if self.enable_metrics:
  40. _metrics_config = config.get("metrics_flags") or {}
  41. self.metrics_flags = MetricsFlags(**_metrics_config)
  42. else:
  43. self.metrics_flags = MetricsFlags.all_off()
  44. self.sentry_enabled = "sentry" in config
  45. if self.sentry_enabled:
  46. try:
  47. check_requirements("sentry")
  48. except DependencyException as e:
  49. raise ConfigError(
  50. e.message # noqa: B306, DependencyException.message is a property
  51. )
  52. self.sentry_dsn = config["sentry"].get("dsn")
  53. if not self.sentry_dsn:
  54. raise ConfigError(
  55. "sentry.dsn field is required when sentry integration is enabled"
  56. )
  57. def generate_config_section(self, report_stats=None, **kwargs):
  58. res = """\
  59. ## Metrics ###
  60. # Enable collection and rendering of performance metrics
  61. #
  62. #enable_metrics: false
  63. # Enable sentry integration
  64. # NOTE: While attempts are made to ensure that the logs don't contain
  65. # any sensitive information, this cannot be guaranteed. By enabling
  66. # this option the sentry server may therefore receive sensitive
  67. # information, and it in turn may then diseminate sensitive information
  68. # through insecure notification channels if so configured.
  69. #
  70. #sentry:
  71. # dsn: "..."
  72. # Flags to enable Prometheus metrics which are not suitable to be
  73. # enabled by default, either for performance reasons or limited use.
  74. #
  75. metrics_flags:
  76. # Publish synapse_federation_known_servers, a gauge of the number of
  77. # servers this homeserver knows about, including itself. May cause
  78. # performance problems on large homeservers.
  79. #
  80. #known_servers: true
  81. # Whether or not to report anonymized homeserver usage statistics.
  82. #
  83. """
  84. if report_stats is None:
  85. res += "#report_stats: true|false\n"
  86. else:
  87. res += "report_stats: %s\n" % ("true" if report_stats else "false")
  88. res += """
  89. # The endpoint to report the anonymized homeserver usage statistics to.
  90. # Defaults to https://matrix.org/report-usage-stats/push
  91. #
  92. #report_stats_endpoint: https://example.com/report-usage-stats/push
  93. """
  94. return res