metrics.py 3.9 KB

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