Skip to content

Commit 1cd8df5

Browse files
vbrodskyVal Brodsky
authored andcommitted
[PLT-46] Add method / heurisrtics to extract original sdk method name from python trace (#1845)
1 parent 8b905b1 commit 1cd8df5

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

libs/lbox-clients/src/lbox/request_client.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import inspect
12
import json
23
import logging
34
import os
5+
import re
46
import sys
57
from datetime import datetime, timezone
68
from types import MappingProxyType
7-
from typing import Callable, Dict, Optional
9+
from typing import Callable, Dict, Optional, TypedDict
810

911
import requests
1012
import requests.exceptions
@@ -22,6 +24,51 @@ def python_version_info():
2224
return f"{version_info.major}.{version_info.minor}.{version_info.micro}-{version_info.releaselevel}"
2325

2426

27+
LABELBOX_CALL_PATTERN = re.compile(r"/labelbox/")
28+
TEST_FILE_PATTERN = re.compile(r".*test.*\.py$")
29+
30+
31+
class _RequestInfo(TypedDict):
32+
prefix: str
33+
class_name: str
34+
method_name: str
35+
36+
37+
def call_info():
38+
method_name = "Unknown"
39+
prefix = ""
40+
class_name = ""
41+
skip_methods = ["wrapper", "__init__"]
42+
skip_classes = ["PaginatedCollection", "_CursorPagination", "_OffsetPagination"]
43+
44+
try:
45+
call_info = None
46+
for stack in reversed(inspect.stack()):
47+
if LABELBOX_CALL_PATTERN.search(stack.filename):
48+
call_info = stack
49+
method_name = call_info.function
50+
class_name = call_info.frame.f_locals.get(
51+
"self", None
52+
).__class__.__name__
53+
54+
if method_name not in skip_methods and class_name not in skip_classes:
55+
if TEST_FILE_PATTERN.search(call_info.filename):
56+
prefix = "test:"
57+
else:
58+
if class_name == "NoneType":
59+
class_name = ""
60+
break
61+
62+
except Exception:
63+
pass
64+
return _RequestInfo(prefix=prefix, class_name=class_name, method_name=method_name)
65+
66+
67+
def call_info_as_str():
68+
info = call_info()
69+
return f"{info['prefix']}{info['class_name']}:{info['method_name']}"
70+
71+
2572
class RequestClient:
2673
"""A Labelbox request client.
2774
@@ -186,6 +233,8 @@ def convert_value(value):
186233
if files:
187234
del headers["Content-Type"]
188235
del headers["Accept"]
236+
headers["X-SDK-Method"] = call_info_as_str()
237+
189238
request = requests.Request(
190239
"POST",
191240
endpoint,

0 commit comments

Comments
 (0)