Skip to content

[PLT-46] Add method / heurisrtics to extract original sdk method name from python trace #1845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion libs/lbox-clients/src/lbox/request_client.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import inspect
import json
import logging
import os
import re
import sys
from datetime import datetime, timezone
from types import MappingProxyType
from typing import Callable, Dict, Optional
from typing import Callable, Dict, Optional, TypedDict

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


LABELBOX_CALL_PATTERN = re.compile(r"/labelbox/")
TEST_FILE_PATTERN = re.compile(r".*test.*\.py$")


class _RequestInfo(TypedDict):
prefix: str
class_name: str
method_name: str


def call_info():
method_name = "Unknown"
prefix = ""
class_name = ""
skip_methods = ["wrapper", "__init__"]
skip_classes = ["PaginatedCollection", "_CursorPagination", "_OffsetPagination"]

try:
call_info = None
for stack in reversed(inspect.stack()):
if LABELBOX_CALL_PATTERN.search(stack.filename):
call_info = stack
method_name = call_info.function
class_name = call_info.frame.f_locals.get(
"self", None
).__class__.__name__

if method_name not in skip_methods and class_name not in skip_classes:
if TEST_FILE_PATTERN.search(call_info.filename):
prefix = "test:"
else:
if class_name == "NoneType":
class_name = ""
break

except Exception:
pass
return _RequestInfo(prefix=prefix, class_name=class_name, method_name=method_name)


def call_info_as_str():
info = call_info()
return f"{info['prefix']}{info['class_name']}:{info['method_name']}"


class RequestClient:
"""A Labelbox request client.

Expand Down Expand Up @@ -186,6 +233,8 @@ def convert_value(value):
if files:
del headers["Content-Type"]
del headers["Accept"]
headers["X-SDK-Method"] = call_info_as_str()

request = requests.Request(
"POST",
endpoint,
Expand Down
Loading