Skip to content

Commit aabc7d7

Browse files
committed
Capture enqueue source via sys._getframe instead of inspect.stack
inspect.stack() walks the entire call stack and builds FrameInfo for every frame just to read filename and lineno from frame 1. At realistic stack depth that was ~500us of pure overhead per run_in_worker call; sys._getframe(1) is ~75ns for the same information. Matches the existing pattern in plain/plain/logs/logger.py.
1 parent 6fc7c4e commit aabc7d7

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

plain-jobs/plain/jobs/jobs.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import datetime
4-
import inspect
4+
import sys
55
import time
66
from abc import ABCMeta, abstractmethod
77
from contextlib import AbstractContextManager, nullcontext
@@ -96,16 +96,17 @@ def run_in_worker(
9696
attributes={**metric_attributes, MESSAGING_OPERATION_NAME: "send"},
9797
) as span:
9898
try:
99-
# Try to automatically annotate the source of the job
100-
caller = inspect.stack()[1]
101-
source = f"{caller.filename}:{caller.lineno}"
99+
frame = sys._getframe(1)
100+
filename = frame.f_code.co_filename
101+
lineno = frame.f_lineno
102+
source = f"{filename}:{lineno}"
102103
span.set_attributes(
103104
{
104-
CODE_FILE_PATH: caller.filename,
105-
CODE_LINE_NUMBER: caller.lineno,
105+
CODE_FILE_PATH: filename,
106+
CODE_LINE_NUMBER: lineno,
106107
}
107108
)
108-
except (IndexError, AttributeError):
109+
except (ValueError, AttributeError):
109110
source = ""
110111

111112
parameters = JobParameters.to_json(self._init_args, self._init_kwargs)

0 commit comments

Comments
 (0)