Skip to content
Merged
Show file tree
Hide file tree
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
79 changes: 79 additions & 0 deletions agentops/sdk/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import atexit
import threading
import platform
import sys
import os
import psutil
from typing import List, Optional

from opentelemetry import metrics, trace
Expand All @@ -24,6 +28,73 @@
# No need to create shortcuts since we're using our own ResourceAttributes class now


def get_imported_libraries():
"""
Get the top-level imported libraries in the current script.

Returns:
list: List of imported libraries
"""
user_libs = []

Check warning on line 38 in agentops/sdk/core.py

View check run for this annotation

Codecov / codecov/patch

agentops/sdk/core.py#L38

Added line #L38 was not covered by tests

builtin_modules = {

Check warning on line 40 in agentops/sdk/core.py

View check run for this annotation

Codecov / codecov/patch

agentops/sdk/core.py#L40

Added line #L40 was not covered by tests
'builtins', 'sys', 'os', '_thread', 'abc', 'io', 're', 'types',
'collections', 'enum', 'math', 'datetime', 'time', 'warnings'
}

try:
main_module = sys.modules.get('__main__')
if main_module and hasattr(main_module, '__dict__'):
for name, obj in main_module.__dict__.items():
if isinstance(obj, type(sys)) and hasattr(obj, '__name__'):
mod_name = obj.__name__.split('.')[0]
if (mod_name and

Check warning on line 51 in agentops/sdk/core.py

View check run for this annotation

Codecov / codecov/patch

agentops/sdk/core.py#L45-L51

Added lines #L45 - L51 were not covered by tests
not mod_name.startswith('_') and
mod_name not in builtin_modules):
user_libs.append(mod_name)
except Exception as e:
logger.debug(f"Error getting imports: {e}")

Check warning on line 56 in agentops/sdk/core.py

View check run for this annotation

Codecov / codecov/patch

agentops/sdk/core.py#L54-L56

Added lines #L54 - L56 were not covered by tests

return user_libs

Check warning on line 58 in agentops/sdk/core.py

View check run for this annotation

Codecov / codecov/patch

agentops/sdk/core.py#L58

Added line #L58 was not covered by tests


def get_system_stats():
"""
Get basic system stats including CPU and memory information.

Returns:
dict: Dictionary with system information
"""
system_info = {

Check warning on line 68 in agentops/sdk/core.py

View check run for this annotation

Codecov / codecov/patch

agentops/sdk/core.py#L68

Added line #L68 was not covered by tests
ResourceAttributes.HOST_MACHINE: platform.machine(),
ResourceAttributes.HOST_NAME: platform.node(),
ResourceAttributes.HOST_NODE: platform.node(),
ResourceAttributes.HOST_PROCESSOR: platform.processor(),
ResourceAttributes.HOST_SYSTEM: platform.system(),
ResourceAttributes.HOST_VERSION: platform.version(),
ResourceAttributes.HOST_OS_RELEASE: platform.release(),
}

# Add CPU stats
try:
system_info[ResourceAttributes.CPU_COUNT] = os.cpu_count() or 0
system_info[ResourceAttributes.CPU_PERCENT] = psutil.cpu_percent(interval=0.1)
except Exception as e:
logger.debug(f"Error getting CPU stats: {e}")

Check warning on line 83 in agentops/sdk/core.py

View check run for this annotation

Codecov / codecov/patch

agentops/sdk/core.py#L79-L83

Added lines #L79 - L83 were not covered by tests

# Add memory stats
try:
memory = psutil.virtual_memory()
system_info[ResourceAttributes.MEMORY_TOTAL] = memory.total
system_info[ResourceAttributes.MEMORY_AVAILABLE] = memory.available
system_info[ResourceAttributes.MEMORY_USED] = memory.used
system_info[ResourceAttributes.MEMORY_PERCENT] = memory.percent
except Exception as e:
logger.debug(f"Error getting memory stats: {e}")

Check warning on line 93 in agentops/sdk/core.py

View check run for this annotation

Codecov / codecov/patch

agentops/sdk/core.py#L86-L93

Added lines #L86 - L93 were not covered by tests

return system_info

Check warning on line 95 in agentops/sdk/core.py

View check run for this annotation

Codecov / codecov/patch

agentops/sdk/core.py#L95

Added line #L95 was not covered by tests


def setup_telemetry(
service_name: str = "agentops",
project_id: Optional[str] = None,
Expand Down Expand Up @@ -58,6 +129,14 @@
# Add project_id as a custom resource attribute
resource_attrs[ResourceAttributes.PROJECT_ID] = project_id
logger.debug(f"Including project_id in resource attributes: {project_id}")

# Add system information
system_stats = get_system_stats()
resource_attrs.update(system_stats)

Check warning on line 135 in agentops/sdk/core.py

View check run for this annotation

Codecov / codecov/patch

agentops/sdk/core.py#L134-L135

Added lines #L134 - L135 were not covered by tests

# Add imported libraries
imported_libraries = get_imported_libraries()
resource_attrs[ResourceAttributes.IMPORTED_LIBRARIES] = imported_libraries

Check warning on line 139 in agentops/sdk/core.py

View check run for this annotation

Codecov / codecov/patch

agentops/sdk/core.py#L138-L139

Added lines #L138 - L139 were not covered by tests

resource = Resource(resource_attrs)
provider = TracerProvider(resource=resource)
Expand Down
22 changes: 22 additions & 0 deletions agentops/semconv/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,25 @@ class ResourceAttributes:
# SDK attributes
SDK_NAME = "agentops.sdk.name"
SDK_VERSION = "agentops.sdk.version"

# Host machine attributes
HOST_MACHINE = "host.machine"
HOST_NAME = "host.name"
HOST_NODE = "host.node"
HOST_OS_RELEASE = "host.os_release"
HOST_PROCESSOR = "host.processor"
HOST_SYSTEM = "host.system"
HOST_VERSION = "host.version"

# CPU attributes
CPU_COUNT = "cpu.count"
CPU_PERCENT = "cpu.percent"

# Memory attributes
MEMORY_TOTAL = "memory.total"
MEMORY_AVAILABLE = "memory.available"
MEMORY_USED = "memory.used"
MEMORY_PERCENT = "memory.percent"

# Libraries
IMPORTED_LIBRARIES = "imported_libraries"
Loading