metrics.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # -*- coding: utf-8 -*-
  2. # Copyright 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 twisted.internet import defer
  16. from synapse.util.logcontext import LoggingContext
  17. import synapse.metrics
  18. from functools import wraps
  19. import logging
  20. logger = logging.getLogger(__name__)
  21. metrics = synapse.metrics.get_metrics_for(__name__)
  22. block_timer = metrics.register_distribution(
  23. "block_timer",
  24. labels=["block_name"]
  25. )
  26. block_ru_utime = metrics.register_distribution(
  27. "block_ru_utime", labels=["block_name"]
  28. )
  29. block_ru_stime = metrics.register_distribution(
  30. "block_ru_stime", labels=["block_name"]
  31. )
  32. block_db_txn_count = metrics.register_distribution(
  33. "block_db_txn_count", labels=["block_name"]
  34. )
  35. block_db_txn_duration = metrics.register_distribution(
  36. "block_db_txn_duration", labels=["block_name"]
  37. )
  38. def measure_func(name):
  39. def wrapper(func):
  40. @wraps(func)
  41. @defer.inlineCallbacks
  42. def measured_func(self, *args, **kwargs):
  43. with Measure(self.clock, name):
  44. r = yield func(self, *args, **kwargs)
  45. defer.returnValue(r)
  46. return measured_func
  47. return wrapper
  48. class Measure(object):
  49. __slots__ = [
  50. "clock", "name", "start_context", "start", "new_context", "ru_utime",
  51. "ru_stime", "db_txn_count", "db_txn_duration", "created_context"
  52. ]
  53. def __init__(self, clock, name):
  54. self.clock = clock
  55. self.name = name
  56. self.start_context = None
  57. self.start = None
  58. self.created_context = False
  59. def __enter__(self):
  60. self.start = self.clock.time_msec()
  61. self.start_context = LoggingContext.current_context()
  62. if not self.start_context:
  63. self.start_context = LoggingContext("Measure")
  64. self.start_context.__enter__()
  65. self.created_context = True
  66. self.ru_utime, self.ru_stime = self.start_context.get_resource_usage()
  67. self.db_txn_count = self.start_context.db_txn_count
  68. self.db_txn_duration = self.start_context.db_txn_duration
  69. def __exit__(self, exc_type, exc_val, exc_tb):
  70. if isinstance(exc_type, Exception) or not self.start_context:
  71. return
  72. duration = self.clock.time_msec() - self.start
  73. block_timer.inc_by(duration, self.name)
  74. context = LoggingContext.current_context()
  75. if context != self.start_context:
  76. logger.warn(
  77. "Context has unexpectedly changed from '%s' to '%s'. (%r)",
  78. self.start_context, context, self.name
  79. )
  80. return
  81. if not context:
  82. logger.warn("Expected context. (%r)", self.name)
  83. return
  84. ru_utime, ru_stime = context.get_resource_usage()
  85. block_ru_utime.inc_by(ru_utime - self.ru_utime, self.name)
  86. block_ru_stime.inc_by(ru_stime - self.ru_stime, self.name)
  87. block_db_txn_count.inc_by(
  88. context.db_txn_count - self.db_txn_count, self.name
  89. )
  90. block_db_txn_duration.inc_by(
  91. context.db_txn_duration - self.db_txn_duration, self.name
  92. )
  93. if self.created_context:
  94. self.start_context.__exit__(exc_type, exc_val, exc_tb)