metrics.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  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 ._base import Config, ConfigError
  16. MISSING_SENTRY = (
  17. """Missing sentry-sdk library. This is required to enable sentry
  18. integration.
  19. """
  20. )
  21. class MetricsConfig(Config):
  22. def read_config(self, config):
  23. self.enable_metrics = config["enable_metrics"]
  24. self.report_stats = config.get("report_stats", None)
  25. self.metrics_port = config.get("metrics_port")
  26. self.metrics_bind_host = config.get("metrics_bind_host", "127.0.0.1")
  27. self.sentry_enabled = "sentry" in config
  28. if self.sentry_enabled:
  29. try:
  30. import sentry_sdk # noqa F401
  31. except ImportError:
  32. raise ConfigError(MISSING_SENTRY)
  33. self.sentry_dsn = config["sentry"].get("dsn")
  34. if not self.sentry_dsn:
  35. raise ConfigError(
  36. "sentry.dsn field is required when sentry integration is enabled",
  37. )
  38. def default_config(self, report_stats=None, **kwargs):
  39. res = """\
  40. ## Metrics ###
  41. # Enable collection and rendering of performance metrics
  42. #
  43. enable_metrics: False
  44. # Enable sentry integration
  45. # NOTE: While attempts are made to ensure that the logs don't contain
  46. # any sensitive information, this cannot be guaranteed. By enabling
  47. # this option the sentry server may therefore receive sensitive
  48. # information, and it in turn may then diseminate sensitive information
  49. # through insecure notification channels if so configured.
  50. #
  51. #sentry:
  52. # dsn: "..."
  53. """
  54. if report_stats is None:
  55. res += "# report_stats: true|false\n"
  56. else:
  57. res += "report_stats: %s\n" % ('true' if report_stats else 'false')
  58. return res