Skip to content

Commit 2b02805

Browse files
committed
feat:remove thread pool wrap
Change-Id: Idf5110cfde8789737ab0f984ba7c36ac2eae151c Co-developed-by: Cursor <noreply@cursor.com>
1 parent 01daa83 commit 2b02805

File tree

7 files changed

+42
-386
lines changed

7 files changed

+42
-386
lines changed

instrumentation-loongsuite/loongsuite-instrumentation-mem0/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ conventions** and should be treated as experimental.
114114

115115
## Compatibility
116116

117-
- Python: `>= 3.8, < 3.13`
117+
- Python: `>= 3.8, <= 3.13`
118118
- Mem0 / `mem0ai`: `>= 1.0.0`
119119
- OpenTelemetry API: `>= 1.20.0`
120120

instrumentation-loongsuite/loongsuite-instrumentation-mem0/pyproject.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ classifiers = [
2525
"Programming Language :: Python :: 3.13",
2626
]
2727
dependencies = [
28-
"wrapt",
29-
"opentelemetry-api",
30-
"opentelemetry-instrumentation",
31-
"opentelemetry-semantic-conventions",
28+
"wrapt >=1.17.3,<2",
29+
"opentelemetry-api ~=1.37",
30+
"opentelemetry-instrumentation ~=0.58b0",
31+
"opentelemetry-semantic-conventions ~=0.58b0",
3232
]
3333

3434
[project.optional-dependencies]

instrumentation-loongsuite/loongsuite-instrumentation-mem0/src/opentelemetry/instrumentation/mem0/__init__.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
from opentelemetry.instrumentation.mem0.internal._extractors import (
1717
METHOD_EXTRACTION_RULES,
1818
)
19-
from opentelemetry.instrumentation.mem0.internal._thread_pool_handler import (
20-
ThreadPoolContextPropagationHandler,
21-
)
2219
from opentelemetry.instrumentation.mem0.internal._wrapper import (
2320
GraphStoreWrapper,
2421
MemoryOperationWrapper,
@@ -133,17 +130,6 @@ def _instrument(self, **kwargs: Any) -> None:
133130
)
134131

135132
# Wrap ThreadPoolExecutor.submit to support context propagation
136-
# Some mem0 methods (add, get_all, search) use ThreadPoolExecutor,
137-
# causing OpenTelemetry context to not auto-propagate to child threads.
138-
# We use wrapt's standard wrapping mechanism for proper cleanup support.
139-
140-
self._threadpool_handler = ThreadPoolContextPropagationHandler()
141-
wrap_function_wrapper(
142-
module="concurrent.futures",
143-
name="ThreadPoolExecutor.submit",
144-
wrapper=self._threadpool_handler,
145-
)
146-
147133
# Execute instrumentation (traces only, metrics removed)
148134
self._instrument_memory_operations(tracer)
149135
self._instrument_memory_client_operations(tracer)
@@ -159,9 +145,6 @@ def _uninstrument(self, **kwargs: Any) -> None:
159145
if not self._is_instrumented:
160146
return
161147

162-
# Unwrap ThreadPoolExecutor.submit
163-
self._uninstrument_threadpool()
164-
165148
# Uninstrument Memory and MemoryClient operations (symmetric to _instrument)
166149
self._uninstrument_memory_operations()
167150
self._uninstrument_memory_client_operations()
@@ -174,16 +157,6 @@ def _uninstrument(self, **kwargs: Any) -> None:
174157
# Reset instrumentation state
175158
self._is_instrumented = False
176159

177-
def _uninstrument_threadpool(self) -> None:
178-
"""Unwrap ThreadPoolExecutor.submit."""
179-
try:
180-
import concurrent.futures
181-
182-
unwrap(concurrent.futures.ThreadPoolExecutor, "submit")
183-
logger.debug("Successfully unwrapped ThreadPoolExecutor.submit")
184-
except Exception as e:
185-
logger.debug(f"Failed to unwrap ThreadPoolExecutor.submit: {e}")
186-
187160
def _uninstrument_memory_operations(self) -> None:
188161
"""Uninstrument Memory and AsyncMemory operations."""
189162
if not _MEM0_CORE_AVAILABLE or Memory is None or AsyncMemory is None:

instrumentation-loongsuite/loongsuite-instrumentation-mem0/src/opentelemetry/instrumentation/mem0/internal/_extractors.py

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,41 @@ class MethodExtractionRule:
8888
}
8989

9090

91+
GENERIC_MEMORY_ID_PAGINATION_SEARCH_SPEC: List[Tuple[str, str, Any]] = [
92+
("infer", SemanticAttributes.GEN_AI_MEMORY_INFER, bool),
93+
(
94+
"memory_type",
95+
SemanticAttributes.GEN_AI_MEMORY_MEMORY_TYPE,
96+
safe_str,
97+
),
98+
("memory_id", SemanticAttributes.GEN_AI_MEMORY_ID, safe_str),
99+
("limit", SemanticAttributes.GEN_AI_MEMORY_LIMIT, safe_int),
100+
("page", SemanticAttributes.GEN_AI_MEMORY_PAGE, safe_int),
101+
(
102+
"page_size",
103+
SemanticAttributes.GEN_AI_MEMORY_PAGE_SIZE,
104+
safe_int,
105+
),
106+
("top_k", SemanticAttributes.GEN_AI_MEMORY_TOP_K, safe_int),
107+
(
108+
"threshold",
109+
SemanticAttributes.GEN_AI_MEMORY_THRESHOLD,
110+
safe_float,
111+
),
112+
("rerank", SemanticAttributes.GEN_AI_MEMORY_RERANK, bool),
113+
(
114+
"only_metadata_based_search",
115+
SemanticAttributes.GEN_AI_MEMORY_ONLY_METADATA_BASED_SEARCH,
116+
bool,
117+
),
118+
(
119+
"keyword_search",
120+
SemanticAttributes.GEN_AI_MEMORY_KEYWORD_SEARCH,
121+
bool,
122+
),
123+
]
124+
125+
91126
def _extract_input_content(
92127
operation_name: str, kwargs: Dict[str, Any]
93128
) -> Optional[str]:
@@ -583,39 +618,7 @@ def extract_generic_attributes(
583618
attributes,
584619
instance=None,
585620
kwargs=kwargs,
586-
spec=[
587-
("infer", SemanticAttributes.GEN_AI_MEMORY_INFER, bool),
588-
(
589-
"memory_type",
590-
SemanticAttributes.GEN_AI_MEMORY_MEMORY_TYPE,
591-
safe_str,
592-
),
593-
("memory_id", SemanticAttributes.GEN_AI_MEMORY_ID, safe_str),
594-
("limit", SemanticAttributes.GEN_AI_MEMORY_LIMIT, safe_int),
595-
("page", SemanticAttributes.GEN_AI_MEMORY_PAGE, safe_int),
596-
(
597-
"page_size",
598-
SemanticAttributes.GEN_AI_MEMORY_PAGE_SIZE,
599-
safe_int,
600-
),
601-
("top_k", SemanticAttributes.GEN_AI_MEMORY_TOP_K, safe_int),
602-
(
603-
"threshold",
604-
SemanticAttributes.GEN_AI_MEMORY_THRESHOLD,
605-
safe_float,
606-
),
607-
("rerank", SemanticAttributes.GEN_AI_MEMORY_RERANK, bool),
608-
(
609-
"only_metadata_based_search",
610-
SemanticAttributes.GEN_AI_MEMORY_ONLY_METADATA_BASED_SEARCH,
611-
bool,
612-
),
613-
(
614-
"keyword_search",
615-
SemanticAttributes.GEN_AI_MEMORY_KEYWORD_SEARCH,
616-
bool,
617-
),
618-
],
621+
spec=GENERIC_MEMORY_ID_PAGINATION_SEARCH_SPEC,
619622
)
620623

621624
# List parameters: fields / categories

instrumentation-loongsuite/loongsuite-instrumentation-mem0/src/opentelemetry/instrumentation/mem0/internal/_thread_pool_handler.py

Lines changed: 0 additions & 92 deletions
This file was deleted.

instrumentation-loongsuite/loongsuite-instrumentation-mem0/test-requirements-oldest.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ wrapt==1.17.3
1212
opentelemetry-api==1.37
1313
opentelemetry-sdk==1.37
1414
opentelemetry-semantic-conventions==0.58b0
15+
opentelemetry-instrumentation-threading==0.58b0
1516

1617
# Mock and utilities
1718
mock>=4.0.0

0 commit comments

Comments
 (0)