25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

82 satır
2.6 KiB

  1. # Copyright 2022 The Matrix.org Foundation C.I.C
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import TYPE_CHECKING
  15. import attr
  16. from synapse.metrics.background_process_metrics import run_as_background_process
  17. if TYPE_CHECKING:
  18. from synapse.server import HomeServer
  19. from prometheus_client import Gauge
  20. # Gauge to expose daily active users metrics
  21. current_dau_gauge = Gauge(
  22. "synapse_admin_daily_active_users",
  23. "Current daily active users count",
  24. )
  25. @attr.s(auto_attribs=True)
  26. class CommonUsageMetrics:
  27. """Usage metrics shared between the phone home stats and the prometheus exporter."""
  28. daily_active_users: int
  29. class CommonUsageMetricsManager:
  30. """Collects common usage metrics."""
  31. def __init__(self, hs: "HomeServer") -> None:
  32. self._store = hs.get_datastores().main
  33. self._clock = hs.get_clock()
  34. async def get_metrics(self) -> CommonUsageMetrics:
  35. """Get the CommonUsageMetrics object. If no collection has happened yet, do it
  36. before returning the metrics.
  37. Returns:
  38. The CommonUsageMetrics object to read common metrics from.
  39. """
  40. return await self._collect()
  41. async def setup(self) -> None:
  42. """Keep the gauges for common usage metrics up to date."""
  43. run_as_background_process(
  44. desc="common_usage_metrics_update_gauges", func=self._update_gauges
  45. )
  46. self._clock.looping_call(
  47. run_as_background_process,
  48. 5 * 60 * 1000,
  49. desc="common_usage_metrics_update_gauges",
  50. func=self._update_gauges,
  51. )
  52. async def _collect(self) -> CommonUsageMetrics:
  53. """Collect the common metrics and either create the CommonUsageMetrics object to
  54. use if it doesn't exist yet, or update it.
  55. """
  56. dau_count = await self._store.count_daily_users()
  57. return CommonUsageMetrics(
  58. daily_active_users=dau_count,
  59. )
  60. async def _update_gauges(self) -> None:
  61. """Update the Prometheus gauges."""
  62. metrics = await self._collect()
  63. current_dau_gauge.set(float(metrics.daily_active_users))