Skip to content

Commit 3761e58

Browse files
author
Val Brodsky
committed
Add method / heurisrtics to extract original sdk method name from python trace
1 parent b6675f3 commit 3761e58

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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
@@ -22,6 +24,39 @@ 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+
29+
30+
def call_info():
31+
method_name = "Unknown"
32+
prefix = ""
33+
class_name = ""
34+
skip_methods = ["wrapper", "__init__"]
35+
skip_classes = ["PaginatedCollection", "_CursorPagination", "_OffsetPagination"]
36+
37+
try:
38+
call_info = None
39+
for stack in reversed(inspect.stack()):
40+
if LABELBOX_CALL_PATTERN.search(stack.filename):
41+
call_info = stack
42+
method_name = call_info.function
43+
class_name = call_info.frame.f_locals.get(
44+
"self", None
45+
).__class__.__name__
46+
47+
if method_name not in skip_methods and class_name not in skip_classes:
48+
if "test" in call_info.filename:
49+
prefix = "test:"
50+
else:
51+
if class_name == "NoneType":
52+
class_name = ""
53+
break
54+
55+
except Exception:
56+
pass
57+
return (prefix, class_name, method_name)
58+
59+
2560
class RequestClient:
2661
"""A Labelbox request client.
2762
@@ -186,6 +221,9 @@ def convert_value(value):
186221
if files:
187222
del headers["Content-Type"]
188223
del headers["Accept"]
224+
headers["X-SDK-Method"] = (
225+
f"{call_info()[0]}{call_info()[1]}:{call_info()[2]}"
226+
)
189227
request = requests.Request(
190228
"POST",
191229
endpoint,

0 commit comments

Comments
 (0)