Skip to content

Commit

Permalink
Implement stopwatch metrics reporting with unit test (#485)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel McKnight <[email protected]>
  • Loading branch information
NeonDaniel and NeonDaniel authored Nov 17, 2023
1 parent ae8ba38 commit 4428c31
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
37 changes: 18 additions & 19 deletions neon_utils/metrics_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,28 @@

from socket import gethostname
from time import time, strftime
from ovos_bus_client import Message, MessageBusClient
from ovos_utils.log import LOG

from neon_utils.message_utils import dig_for_message


# TODO: Enable metric reporting DM
class Stopwatch:
"""
Provides a stopwatch object compatible with mycroft.metrics Stopwatch.
"""

def __init__(self, metric_name=None, allow_reporting=False):
def __init__(self, metric_name=None, allow_reporting=False, bus=None):
"""
Create a stopwatch object with an optional metric_name
Args:
metric_name: name of the metric this stopwatch is measuring
allow_reporting: boolean flag to allow this stopwatch to report measured metrics
@param metric_name: name of the metric this stopwatch is measuring
@param allow_reporting: boolean flag to allow this stopwatch to report measured metrics
@param bus: MessageBusClient object to report metrics with
"""
self._metric = metric_name
self._report = allow_reporting
self._context = dict()
self._bus = bus
self.start_time = None
self.time = None

Expand All @@ -55,16 +58,7 @@ def __enter__(self):

def __exit__(self, typ, val, traceback):
self.stop()
# self.report()

# def add_context(self, context: dict):
# """
# Add context to the measured metric.
# Args:
# context: dict of arbitrary data to add to this metric reporting
# """
# if self._metric:
# self._context = context
self.report()

def start(self):
self.start_time = time()
Expand All @@ -73,10 +67,14 @@ def stop(self):
self.time = time() - self.start_time
return self.time

# def report(self):
# if self._metric and self._report:
# report_metric(self._metric, self._context)
# self._context = dict()
def report(self):
if self._metric and self._report:
self._bus = self._bus or MessageBusClient()
message = dig_for_message() or Message("")
message.context['timestamp'] = time()
self._bus.emit(message.forward("neon.metric",
{"name": self._metric,
"duration": self.time}))


def report_metric(name: str, **kwargs):
Expand All @@ -85,6 +83,7 @@ def report_metric(name: str, **kwargs):
:param name: Name of the metric to report
:param kwargs: Arbitrary data to include with metric report
"""
# TODO: Deprecate and move to PHAL plugin
try:
from neon_utils.mq_utils import send_mq_request
send_mq_request("/neon_metrics", {**{"name": name}, **kwargs},
Expand Down
15 changes: 15 additions & 0 deletions tests/metric_util_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import sys
import os
import unittest
from unittest.mock import Mock

sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
from neon_utils.metrics_utils import *
Expand Down Expand Up @@ -76,6 +77,20 @@ def test_stopwatch_init_params(self):
self.assertIsNone(stopwatch._metric)
self.assertFalse(stopwatch._report)

def test_stopwatch_report_metric(self):
from ovos_utils.messagebus import FakeBus
bus = FakeBus()
on_metric = Mock()
bus.on("neon.metric", on_metric)
stopwatch = Stopwatch("test", True, bus)
with stopwatch:
sleep(0.05)
on_metric.assert_called_once()
msg = on_metric.call_args[0][0]
self.assertEqual(msg.data['name'], "test")
self.assertIsInstance(msg.data['duration'], float)
self.assertIsInstance(msg.context['timestamp'], float)

def test_report_metric(self):
self.assertTrue(report_metric("test", data="this is only a test"))

Expand Down

0 comments on commit 4428c31

Please sign in to comment.