Skip to content

Commit cd79739

Browse files
Merge pull request #1621 from roboflow/feat/include-errors-in-usage
Feat/include errors in usage
2 parents ab1cb9b + 8d3cfca commit cd79739

2 files changed

Lines changed: 46 additions & 34 deletions

File tree

inference/usage_tracking/collector.py

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -728,25 +728,31 @@ def sync_wrapper(
728728
)
729729
)
730730
except Exception as exc:
731+
t2 = time.time()
731732
if GCP_SERVERLESS is True:
732-
t2 = time.time()
733733
execution_duration = max(t2 - t1, 0.1)
734-
self.record_usage(
735-
**self._extract_usage_params_from_func_kwargs(
736-
usage_fps=usage_fps,
737-
usage_api_key=usage_api_key,
738-
usage_workflow_id=usage_workflow_id,
739-
usage_workflow_preview=usage_workflow_preview,
740-
usage_inference_test_run=usage_inference_test_run,
741-
usage_billable=usage_billable,
742-
execution_duration=execution_duration,
743-
func=func,
744-
category=category,
745-
exc=str(exc),
746-
args=args,
747-
kwargs=kwargs,
748-
)
734+
else:
735+
execution_duration = t2 - t1
736+
exc_type = type(exc).__name__
737+
if hasattr(exc, "inner_error_type"):
738+
exc_type = exc.inner_error_type
739+
exc_str = f"{exc_type}: {str(exc)}"
740+
self.record_usage(
741+
**self._extract_usage_params_from_func_kwargs(
742+
usage_fps=usage_fps,
743+
usage_api_key=usage_api_key,
744+
usage_workflow_id=usage_workflow_id,
745+
usage_workflow_preview=usage_workflow_preview,
746+
usage_inference_test_run=usage_inference_test_run,
747+
usage_billable=usage_billable,
748+
execution_duration=execution_duration,
749+
func=func,
750+
category=category,
751+
exc=exc_str,
752+
args=args,
753+
kwargs=kwargs,
749754
)
755+
)
750756
raise
751757
return res
752758

@@ -786,25 +792,31 @@ async def async_wrapper(
786792
)
787793
)
788794
except Exception as exc:
795+
t2 = time.time()
789796
if GCP_SERVERLESS is True:
790-
t2 = time.time()
791797
execution_duration = max(t2 - t1, 0.1)
792-
await self.async_record_usage(
793-
**self._extract_usage_params_from_func_kwargs(
794-
usage_fps=usage_fps,
795-
usage_api_key=usage_api_key,
796-
usage_workflow_id=usage_workflow_id,
797-
usage_workflow_preview=usage_workflow_preview,
798-
usage_inference_test_run=usage_inference_test_run,
799-
usage_billable=usage_billable,
800-
execution_duration=execution_duration,
801-
func=func,
802-
category=category,
803-
exc=str(exc),
804-
args=args,
805-
kwargs=kwargs,
806-
)
798+
else:
799+
execution_duration = t2 - t1
800+
exc_type = type(exc).__name__
801+
if hasattr(exc, "inner_error_type"):
802+
exc_type = exc.inner_error_type
803+
exc_str = f"{exc_type}: {str(exc)}"
804+
await self.async_record_usage(
805+
**self._extract_usage_params_from_func_kwargs(
806+
usage_fps=usage_fps,
807+
usage_api_key=usage_api_key,
808+
usage_workflow_id=usage_workflow_id,
809+
usage_workflow_preview=usage_workflow_preview,
810+
usage_inference_test_run=usage_inference_test_run,
811+
usage_billable=usage_billable,
812+
execution_duration=execution_duration,
813+
func=func,
814+
category=category,
815+
exc=exc_str,
816+
args=args,
817+
kwargs=kwargs,
807818
)
819+
)
808820
raise
809821
return res
810822

tests/inference/unit_tests/usage_tracking/test_collector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ def test_func(api_key="test_key"):
955955
test_func()
956956

957957
# then
958-
assert len(usage_collector._usage) == 0
958+
assert len(usage_collector._usage) == 1
959959

960960

961961
def test_record_usage_with_exception_on_GCP(usage_collector_with_mocked_threads):
@@ -979,5 +979,5 @@ def test_func(api_key="test_key"):
979979
json.loads(
980980
usage_collector._usage["test_key"]["model:unknown"]["resource_details"]
981981
).get("error")
982-
== "test exception"
982+
== "Exception: test exception"
983983
)

0 commit comments

Comments
 (0)