def test_when_args_have_metric(self): # arrange, act metric = Provider.get_metric(Metric(host="custom_metric")) # assert self.assertIsInstance(metric, IMetric) self.assertIsInstance(metric, Metric) self.assertEqual(metric.host, "custom_metric")
def test_when_args_are_falsy(self): metric = Provider.get_metric(None, object, {}, []) # assert self.assertIsInstance(metric, IMetric) self.assertIsInstance(metric, Metric) self.assertEqual(metric.host, "localhost")
def test_when_args_is_class_with_dogstatsd(self): # arrange class A: def __init__(self): self.statsd = DogStatsd(host="custom_dog_in_class") # act metric = Provider.get_metric(A()) # assert self.assertIsInstance(metric, DogStatsd) self.assertEqual(metric.host, "custom_dog_in_class")
def test_when_args_is_class_with_metric(self): # arrange class A: def __init__(self): self.metric = Metric(host="custom_metric_in_class") # act metric = Provider.get_metric(A()) # assert self.assertIsInstance(metric, IMetric) self.assertIsInstance(metric, Metric) self.assertEqual(metric.host, "custom_metric_in_class")
def test_when_args_have_dogstatsd(self): # arrange, act metric = Provider.get_metric(DogStatsd(host="custom_dog")) # assert self.assertIsInstance(metric, DogStatsd) self.assertEqual(metric.host, "custom_dog")
def inner(*args: Any, **kwargs: Any) -> Any: # setup tracing and tags trace_id = Resolver.resolve_trace_id(trace_id_from=trace_id_from, **kwargs) identity = Resolver.resolve_identity(*args, func=func, trace_id=trace_id) additional_tags = Resolver.resolve_tags_from(tags_from=tags_from, **kwargs) all_tags = additional_tags + static_tags imetric = Provider.get_metric(*args) # start timing time_start: float = time.monotonic() try: # actual function execution response: Any = func(*args, **kwargs) # calculate process time process_time = int(time.monotonic() - time_start) * 1000 # append extra tags all_tags.append( Resolver.resolve_observed_sli_tag( process_time=process_time)) # send metrics, finished successfully imetric.timing("%s.time.finished" % metric, process_time, all_tags) imetric.gauge("%s.time_gauge.finished" % metric, process_time, all_tags) imetric.increment("%s.finished" % metric, 1, all_tags) except Exception as ex: # calculate process time process_time = int(time.monotonic() - time_start) * 1000 # append extra tags all_tags.append('exception:%s' % type(ex).__name__) all_tags.append( Resolver.resolve_observed_sli_tag( process_time=process_time)) # accept on, returns True if type(ex) in accept_on: # log warning Provider.get_logger(*args).warning( "%s: %s(%s) during '%s' accepted.\n%s" % (identity, type(ex).__name__, ex, func.__name__, traceback.format_exc())) # send metrics, raised but accepted imetric.timing("%s.time.accepted" % metric, process_time, all_tags) imetric.gauge("%s.time_gauge.accepted" % metric, process_time, all_tags) imetric.increment('%s.exception.accepted' % metric, 1, all_tags) # return truthy, to be acknowledged return True # decline on, returns False if type(ex) in decline_on: # log error Provider.get_logger(*args).error( "%s: %s(%s) during '%s' declined.\n%s" % (identity, type(ex).__name__, ex, func.__name__, traceback.format_exc())) # send metrics, raised but declined imetric.timing("%s.time.declined" % metric, process_time, all_tags) imetric.gauge("%s.time_gauge.declined" % metric, process_time, all_tags) imetric.increment('%s.exception.declined' % metric, 1, all_tags) # return falsy, not to be acknowledged return False # unhandled exception, log error Provider.get_logger(*args).error( "%s: %s(%s) during '%s' raised.\n%s" % (identity, type(ex).__name__, ex, func.__name__, traceback.format_exc())) # send metrics, raised and unhandled imetric.timing("%s.time.raised" % metric, process_time, all_tags) imetric.gauge("%s.time_gauge.raised" % metric, process_time, all_tags) imetric.increment('%s.exception.raised' % metric, 1, all_tags) # check if notification client available slack = Provider.get_slack(*args) if slack: # notify slack.error(header=identity, title=type(ex).__name__, text=f"{ex}\n{traceback.format_exc()}") # re-raise raise ex finally: # send metric, start imetric.increment("%s.start" % metric, 1, all_tags) # return actual response of the function return response