Skip to content

Commit 91b4de4

Browse files
committed
Updated logic for function code scraping
1 parent 08b5709 commit 91b4de4

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

middleware/distro.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,34 +123,43 @@ def record_exception(exc: Exception, span_name: Optional[str] = None) -> None:
123123
def extract_function_code(tb_frame, lineno):
124124
"""Extracts the full function body where the exception occurred."""
125125
try:
126+
# Get the source lines and the starting line number of the function
126127
source_lines, start_line = inspect.getsourcelines(tb_frame)
127128
end_line = start_line + len(source_lines) - 1
128129

130+
# If the function body is too long, limit the number of lines
129131
if len(source_lines) > 20:
130-
# Get 10 lines above and 10 below the exception line
131-
start_idx = max(0, lineno - start_line - 10)
132-
end_idx = min(len(source_lines), lineno - start_line + 10)
133-
source_lines = source_lines[(start_idx - 1):end_idx]
134-
135-
start_line = start_line + start_idx
136-
end_line = start_line + end_idx
137-
138-
function_code = "".join(source_lines) # Convert to a string
139-
132+
# Define the number of lines to show before and after the exception line
133+
lines_before = 10
134+
lines_after = 10
135+
136+
# Calculate the start and end indices for slicing
137+
start_idx = max(0, lineno - start_line - lines_before)
138+
end_idx = min(len(source_lines), lineno - start_line + lines_after)
139+
140+
# Extract the relevant lines
141+
source_lines = source_lines[start_idx:end_idx]
142+
143+
# Adjust the start and end line numbers
144+
start_line += start_idx
145+
end_line = start_line + len(source_lines) - 1
146+
147+
# Convert the list of lines to a single string
148+
function_code = "".join(source_lines)
149+
140150
return {
141151
"function_code": function_code,
142152
"function_start_line": start_line,
143153
"function_end_line": end_line,
144-
}
145-
154+
}
155+
146156
except Exception as e:
157+
# Handle cases where the source code cannot be extracted
147158
return {
148159
"function_code": f"Error extracting function code: {e}",
149160
"function_start_line": None,
150-
"function_end_line": None
161+
"function_end_line": None,
151162
}
152-
153-
_original_record_exception = Span.record_exception
154163

155164
def custom_record_exception_wrapper(self: Span,
156165
exception: BaseException,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "middleware-io"
7-
version = "2.1.2rc16"
7+
version = "2.1.2rc17"
88
requires-python = ">=3.8"
99
description = "Middleware's APM tool enables Python developers to effortlessly monitor their applications, gathering distributed tracing, metrics, logs, and profiling data for valuable insights and performance optimization."
1010
authors = [{ name = "middleware-dev" }]

0 commit comments

Comments
 (0)