MCPcopy Create free account
hub / github.com/AgentOps-AI/agentops / _is_installed_package

Function _is_installed_package

agentops/instrumentation/__init__.py:143–178  ·  view source on GitHub ↗

Determines if the given module object corresponds to an installed site-package rather than a local module, especially when names might collide. `package_name_key` is the key from TARGET_PACKAGES (e.g., 'agents', 'google.adk').

(module_obj: ModuleType, package_name_key: str)

Source from the content-addressed store, hash-verified

141
142# New helper function to check module origin
143def _is_installed_package(module_obj: ModuleType, package_name_key: str) -> bool:
144 """
145 Determines if the given module object corresponds to an installed site-package
146 rather than a local module, especially when names might collide.
147 `package_name_key` is the key from TARGET_PACKAGES (e.g., 'agents', 'google.adk').
148 """
149 if not hasattr(module_obj, "__file__") or not module_obj.__file__:
150 logger.debug(
151 f"_is_installed_package: Module '{package_name_key}' has no __file__, assuming it might be an SDK namespace package. Returning True."
152 )
153 return True
154
155 module_path = os.path.normcase(os.path.realpath(os.path.abspath(module_obj.__file__)))
156
157 # Priority 1: Check if it's in any site-packages directory.
158 site_packages_dirs = site.getsitepackages()
159 if isinstance(site_packages_dirs, str):
160 site_packages_dirs = [site_packages_dirs]
161
162 if hasattr(site, "USER_SITE") and site.USER_SITE and os.path.exists(site.USER_SITE):
163 site_packages_dirs.append(site.USER_SITE)
164
165 normalized_site_packages_dirs = [
166 os.path.normcase(os.path.realpath(p)) for p in site_packages_dirs if p and os.path.exists(p)
167 ]
168
169 for sp_dir in normalized_site_packages_dirs:
170 if module_path.startswith(sp_dir):
171 logger.debug(
172 f"_is_installed_package: Module '{package_name_key}' is a library, instrumenting '{package_name_key}'."
173 )
174 return True
175
176 # Priority 2: If not in site-packages, it's highly likely a local module or not an SDK we target.
177 logger.debug(f"_is_installed_package: Module '{package_name_key}' is a local module, skipping instrumentation.")
178 return False
179
180
181def _is_package_instrumented(package_name: str) -> bool:

Callers 2

_import_monitorFunction · 0.85
instrument_allFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…