Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

78 rindas
2.6 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. from typing import Any, Optional
  16. import attr
  17. from synapse.types import JsonDict
  18. from synapse.util.check_dependencies import check_requirements
  19. from ._base import Config, ConfigError
  20. @attr.s
  21. class MetricsFlags:
  22. known_servers: bool = attr.ib(
  23. default=False, validator=attr.validators.instance_of(bool)
  24. )
  25. @classmethod
  26. def all_off(cls) -> "MetricsFlags":
  27. """
  28. Instantiate the flags with all options set to off.
  29. """
  30. return cls(**{x.name: False for x in attr.fields(cls)})
  31. class MetricsConfig(Config):
  32. section = "metrics"
  33. def read_config(self, config: JsonDict, **kwargs: Any) -> None:
  34. self.enable_metrics = config.get("enable_metrics", False)
  35. self.report_stats = config.get("report_stats", None)
  36. self.report_stats_endpoint = config.get(
  37. "report_stats_endpoint", "https://matrix.org/report-usage-stats/push"
  38. )
  39. self.metrics_port = config.get("metrics_port")
  40. self.metrics_bind_host = config.get("metrics_bind_host", "127.0.0.1")
  41. if self.enable_metrics:
  42. _metrics_config = config.get("metrics_flags") or {}
  43. self.metrics_flags = MetricsFlags(**_metrics_config)
  44. else:
  45. self.metrics_flags = MetricsFlags.all_off()
  46. self.sentry_enabled = "sentry" in config
  47. if self.sentry_enabled:
  48. check_requirements("sentry")
  49. self.sentry_dsn = config["sentry"].get("dsn")
  50. self.sentry_environment = config["sentry"].get("environment")
  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(
  56. self, report_stats: Optional[bool] = None, **kwargs: Any
  57. ) -> str:
  58. if report_stats is not None:
  59. res = "report_stats: %s\n" % ("true" if report_stats else "false")
  60. else:
  61. res = "\n"
  62. return res