anosys-sdk-core 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- anosys_sdk_core-1.0.0/PKG-INFO +69 -0
- anosys_sdk_core-1.0.0/README.md +46 -0
- anosys_sdk_core-1.0.0/pyproject.toml +36 -0
- anosys_sdk_core-1.0.0/setup.cfg +4 -0
- anosys_sdk_core-1.0.0/src/anosys_sdk_core/__init__.py +35 -0
- anosys_sdk_core-1.0.0/src/anosys_sdk_core/client.py +80 -0
- anosys_sdk_core-1.0.0/src/anosys_sdk_core/config.py +90 -0
- anosys_sdk_core-1.0.0/src/anosys_sdk_core/context.py +109 -0
- anosys_sdk_core-1.0.0/src/anosys_sdk_core/decorators.py +258 -0
- anosys_sdk_core-1.0.0/src/anosys_sdk_core/models.py +118 -0
- anosys_sdk_core-1.0.0/src/anosys_sdk_core/raw_logger.py +9 -0
- anosys_sdk_core-1.0.0/src/anosys_sdk_core/redaction.py +91 -0
- anosys_sdk_core-1.0.0/src/anosys_sdk_core/util/__init__.py +23 -0
- anosys_sdk_core-1.0.0/src/anosys_sdk_core/util/batching.py +167 -0
- anosys_sdk_core-1.0.0/src/anosys_sdk_core/util/json.py +81 -0
- anosys_sdk_core-1.0.0/src/anosys_sdk_core.egg-info/PKG-INFO +69 -0
- anosys_sdk_core-1.0.0/src/anosys_sdk_core.egg-info/SOURCES.txt +19 -0
- anosys_sdk_core-1.0.0/src/anosys_sdk_core.egg-info/dependency_links.txt +1 -0
- anosys_sdk_core-1.0.0/src/anosys_sdk_core.egg-info/requires.txt +2 -0
- anosys_sdk_core-1.0.0/src/anosys_sdk_core.egg-info/top_level.txt +1 -0
- anosys_sdk_core-1.0.0/tests/test_core.py +75 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: anosys-sdk-core
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Core utilities for AnoSys SDK - AI observability and monitoring
|
|
5
|
+
Author-email: AnoSys <support@anosys.ai>
|
|
6
|
+
Project-URL: Homepage, https://anosys.ai
|
|
7
|
+
Project-URL: Documentation, https://docs.anosys.ai
|
|
8
|
+
Project-URL: Repository, https://github.com/anosys-ai/anosys-sdk
|
|
9
|
+
Keywords: anosys,observability,monitoring,logging,ai,llm
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
18
|
+
Classifier: Topic :: System :: Monitoring
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
Requires-Dist: requests>=2.25.1
|
|
22
|
+
Requires-Dist: python-dotenv>=0.21.0
|
|
23
|
+
|
|
24
|
+
# AnoSys SDK Core
|
|
25
|
+
|
|
26
|
+
Core utilities for the AnoSys SDK - shared functionality for all AnoSys integrations.
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pip install anosys-sdk-core
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Usage
|
|
35
|
+
|
|
36
|
+
### Function Decorators
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from anosys_sdk_core import anosys_logger, anosys_raw_logger, setup_api
|
|
40
|
+
import os
|
|
41
|
+
|
|
42
|
+
os.environ["ANOSYS_API_KEY"] = "your-api-key"
|
|
43
|
+
|
|
44
|
+
# Decorator for automatic logging
|
|
45
|
+
@anosys_logger(source="my_app")
|
|
46
|
+
def my_function(data):
|
|
47
|
+
return process(data)
|
|
48
|
+
|
|
49
|
+
# Async functions work too
|
|
50
|
+
@anosys_logger(source="my_app.async")
|
|
51
|
+
async def my_async_function(data):
|
|
52
|
+
return await async_process(data)
|
|
53
|
+
|
|
54
|
+
# Raw logging
|
|
55
|
+
anosys_raw_logger({
|
|
56
|
+
"event": "custom_event",
|
|
57
|
+
"data": {"key": "value"}
|
|
58
|
+
})
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## API Reference
|
|
62
|
+
|
|
63
|
+
- `anosys_logger(source=None)` - Decorator to log function calls
|
|
64
|
+
- `anosys_raw_logger(data)` - Log arbitrary data directly
|
|
65
|
+
- `setup_api(path=None)` - Configure API endpoint
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
MIT
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# AnoSys SDK Core
|
|
2
|
+
|
|
3
|
+
Core utilities for the AnoSys SDK - shared functionality for all AnoSys integrations.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install anosys-sdk-core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Function Decorators
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from anosys_sdk_core import anosys_logger, anosys_raw_logger, setup_api
|
|
17
|
+
import os
|
|
18
|
+
|
|
19
|
+
os.environ["ANOSYS_API_KEY"] = "your-api-key"
|
|
20
|
+
|
|
21
|
+
# Decorator for automatic logging
|
|
22
|
+
@anosys_logger(source="my_app")
|
|
23
|
+
def my_function(data):
|
|
24
|
+
return process(data)
|
|
25
|
+
|
|
26
|
+
# Async functions work too
|
|
27
|
+
@anosys_logger(source="my_app.async")
|
|
28
|
+
async def my_async_function(data):
|
|
29
|
+
return await async_process(data)
|
|
30
|
+
|
|
31
|
+
# Raw logging
|
|
32
|
+
anosys_raw_logger({
|
|
33
|
+
"event": "custom_event",
|
|
34
|
+
"data": {"key": "value"}
|
|
35
|
+
})
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## API Reference
|
|
39
|
+
|
|
40
|
+
- `anosys_logger(source=None)` - Decorator to log function calls
|
|
41
|
+
- `anosys_raw_logger(data)` - Log arbitrary data directly
|
|
42
|
+
- `setup_api(path=None)` - Configure API endpoint
|
|
43
|
+
|
|
44
|
+
## License
|
|
45
|
+
|
|
46
|
+
MIT
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "anosys-sdk-core"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Core utilities for AnoSys SDK - AI observability and monitoring"
|
|
9
|
+
authors = [{name = "AnoSys", email = "support@anosys.ai"}]
|
|
10
|
+
readme = "README.md"
|
|
11
|
+
license = {file = "LICENSE"}
|
|
12
|
+
requires-python = ">=3.10"
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 4 - Beta",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.10",
|
|
19
|
+
"Programming Language :: Python :: 3.11",
|
|
20
|
+
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
22
|
+
"Topic :: System :: Monitoring",
|
|
23
|
+
]
|
|
24
|
+
keywords = ["anosys", "observability", "monitoring", "logging", "ai", "llm"]
|
|
25
|
+
dependencies = [
|
|
26
|
+
"requests>=2.25.1",
|
|
27
|
+
"python-dotenv>=0.21.0",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://anosys.ai"
|
|
32
|
+
Documentation = "https://docs.anosys.ai"
|
|
33
|
+
Repository = "https://github.com/anosys-ai/anosys-sdk"
|
|
34
|
+
|
|
35
|
+
[tool.setuptools.packages.find]
|
|
36
|
+
where = ["src"]
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""
|
|
2
|
+
AnoSys SDK Core - Shared utilities for AnoSys integrations.
|
|
3
|
+
|
|
4
|
+
This package provides core functionality shared across all AnoSys SDK packages:
|
|
5
|
+
- Configuration and API key management
|
|
6
|
+
- HTTP client for Anosys API
|
|
7
|
+
- Function decorators for logging
|
|
8
|
+
- Data transformation utilities
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from anosys_sdk_core.config import get_api_url, get_env_bool, resolve_api_key
|
|
12
|
+
from anosys_sdk_core.client import AnosysClient
|
|
13
|
+
from anosys_sdk_core.decorators import anosys_logger, anosys_raw_logger, setup_api
|
|
14
|
+
from anosys_sdk_core.context import get_user_context, set_user_context
|
|
15
|
+
from anosys_sdk_core.models import BASE_KEY_MAPPING
|
|
16
|
+
|
|
17
|
+
__version__ = "1.0.0"
|
|
18
|
+
|
|
19
|
+
__all__ = [
|
|
20
|
+
# Config
|
|
21
|
+
"get_api_url",
|
|
22
|
+
"get_env_bool",
|
|
23
|
+
"resolve_api_key",
|
|
24
|
+
# Client
|
|
25
|
+
"AnosysClient",
|
|
26
|
+
# Decorators
|
|
27
|
+
"anosys_logger",
|
|
28
|
+
"anosys_raw_logger",
|
|
29
|
+
"setup_api",
|
|
30
|
+
# Context
|
|
31
|
+
"get_user_context",
|
|
32
|
+
"set_user_context",
|
|
33
|
+
# Models
|
|
34
|
+
"BASE_KEY_MAPPING",
|
|
35
|
+
]
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"""
|
|
2
|
+
HTTP client for AnoSys API.
|
|
3
|
+
|
|
4
|
+
Provides a centralized client for sending data to the AnoSys logging endpoint.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import json
|
|
8
|
+
from typing import Any, Dict, Optional
|
|
9
|
+
|
|
10
|
+
import requests
|
|
11
|
+
|
|
12
|
+
from anosys_sdk_core.config import resolve_api_key
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class AnosysClient:
|
|
16
|
+
"""
|
|
17
|
+
HTTP client for sending data to AnoSys API.
|
|
18
|
+
|
|
19
|
+
Attributes:
|
|
20
|
+
api_url: The resolved API endpoint URL
|
|
21
|
+
timeout: Request timeout in seconds
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(
|
|
25
|
+
self,
|
|
26
|
+
api_url: Optional[str] = None,
|
|
27
|
+
api_key: Optional[str] = None,
|
|
28
|
+
timeout: int = 5
|
|
29
|
+
):
|
|
30
|
+
"""
|
|
31
|
+
Initialize the AnoSys client.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
api_url: Override URL (skips API key resolution if provided)
|
|
35
|
+
api_key: AnoSys API key (defaults to ANOSYS_API_KEY env var)
|
|
36
|
+
timeout: Request timeout in seconds
|
|
37
|
+
"""
|
|
38
|
+
if api_url:
|
|
39
|
+
self.api_url = api_url
|
|
40
|
+
else:
|
|
41
|
+
self.api_url = resolve_api_key(api_key)
|
|
42
|
+
self.timeout = timeout
|
|
43
|
+
|
|
44
|
+
def send(self, data: Dict[str, Any]) -> Optional[requests.Response]:
|
|
45
|
+
"""
|
|
46
|
+
Send data to the AnoSys API.
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
data: Dictionary of data to send
|
|
50
|
+
|
|
51
|
+
Returns:
|
|
52
|
+
Response object on success, None on failure
|
|
53
|
+
"""
|
|
54
|
+
try:
|
|
55
|
+
response = requests.post(
|
|
56
|
+
self.api_url,
|
|
57
|
+
json=data,
|
|
58
|
+
timeout=self.timeout
|
|
59
|
+
)
|
|
60
|
+
response.raise_for_status()
|
|
61
|
+
return response
|
|
62
|
+
except requests.exceptions.RequestException as e:
|
|
63
|
+
print(f"[ANOSYS]❌ POST failed: {e}")
|
|
64
|
+
try:
|
|
65
|
+
print(f"[ANOSYS]❌ Data: {json.dumps(data, indent=2)}")
|
|
66
|
+
except Exception:
|
|
67
|
+
pass
|
|
68
|
+
return None
|
|
69
|
+
|
|
70
|
+
def send_batch(self, items: list) -> list:
|
|
71
|
+
"""
|
|
72
|
+
Send multiple items to the AnoSys API.
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
items: List of data dictionaries to send
|
|
76
|
+
|
|
77
|
+
Returns:
|
|
78
|
+
List of response objects (None for failures)
|
|
79
|
+
"""
|
|
80
|
+
return [self.send(item) for item in items]
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Configuration management for AnoSys SDK.
|
|
3
|
+
|
|
4
|
+
Handles API key resolution, URL configuration, and environment variable parsing.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
from typing import Optional
|
|
9
|
+
|
|
10
|
+
import requests
|
|
11
|
+
from dotenv import load_dotenv
|
|
12
|
+
|
|
13
|
+
# Load environment variables from .env file
|
|
14
|
+
load_dotenv()
|
|
15
|
+
|
|
16
|
+
# Default API endpoints
|
|
17
|
+
DEFAULT_API_URL = "https://www.anosys.ai"
|
|
18
|
+
API_KEY_RESOLVER_URL = "https://api.anosys.ai/api/resolveapikeys"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def get_env_bool(var_name: str, default: bool = True) -> bool:
|
|
22
|
+
"""
|
|
23
|
+
Get a boolean value from an environment variable.
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
var_name: Name of the environment variable
|
|
27
|
+
default: Default value if variable is not set
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
Boolean value from the environment variable
|
|
31
|
+
"""
|
|
32
|
+
value = os.getenv(var_name)
|
|
33
|
+
|
|
34
|
+
if value is None:
|
|
35
|
+
return default
|
|
36
|
+
|
|
37
|
+
value_str = value.strip().lower()
|
|
38
|
+
if value_str in ('1', 'true', 'yes', 'on'):
|
|
39
|
+
return True
|
|
40
|
+
elif value_str in ('0', 'false', 'no', 'off'):
|
|
41
|
+
return False
|
|
42
|
+
else:
|
|
43
|
+
return bool(value_str)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def resolve_api_key(api_key: Optional[str] = None, timeout: int = 30) -> str:
|
|
47
|
+
"""
|
|
48
|
+
Resolve the API key to get the logging endpoint URL.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
api_key: AnoSys API key (defaults to ANOSYS_API_KEY env var)
|
|
52
|
+
timeout: Request timeout in seconds
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
The resolved API URL for logging
|
|
56
|
+
"""
|
|
57
|
+
if api_key is None:
|
|
58
|
+
api_key = os.getenv('ANOSYS_API_KEY')
|
|
59
|
+
|
|
60
|
+
if not api_key:
|
|
61
|
+
print("[ERROR]‼️ ANOSYS_API_KEY not found. Please obtain your API key from "
|
|
62
|
+
"https://console.anosys.ai/collect/integrationoptions")
|
|
63
|
+
return DEFAULT_API_URL
|
|
64
|
+
|
|
65
|
+
try:
|
|
66
|
+
response = requests.get(
|
|
67
|
+
f"{API_KEY_RESOLVER_URL}?apikey={api_key}",
|
|
68
|
+
timeout=timeout
|
|
69
|
+
)
|
|
70
|
+
response.raise_for_status()
|
|
71
|
+
data = response.json()
|
|
72
|
+
return data.get("url", DEFAULT_API_URL)
|
|
73
|
+
except requests.exceptions.RequestException as e:
|
|
74
|
+
print(f"[ERROR]❌ Failed to resolve API key: {e}")
|
|
75
|
+
return DEFAULT_API_URL
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def get_api_url(override_url: Optional[str] = None) -> str:
|
|
79
|
+
"""
|
|
80
|
+
Get the API URL for logging.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
override_url: Optional URL to use instead of resolving
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
The API URL to use for logging
|
|
87
|
+
"""
|
|
88
|
+
if override_url:
|
|
89
|
+
return override_url
|
|
90
|
+
return resolve_api_key()
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""
|
|
2
|
+
User context management for AnoSys SDK.
|
|
3
|
+
|
|
4
|
+
Provides utilities for managing user context (session ID, token, etc.) across requests.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import contextvars
|
|
8
|
+
from typing import Any, Dict, Optional
|
|
9
|
+
|
|
10
|
+
# Context variable for storing user context
|
|
11
|
+
_user_context: contextvars.ContextVar[Optional[Dict[str, Any]]] = contextvars.ContextVar(
|
|
12
|
+
'anosys_user_context',
|
|
13
|
+
default=None
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def set_user_context(context: Dict[str, Any]) -> None:
|
|
18
|
+
"""
|
|
19
|
+
Set the current user context.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
context: Dictionary containing user context (session_id, token, etc.)
|
|
23
|
+
"""
|
|
24
|
+
_user_context.set(context)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def get_user_context() -> Optional[Dict[str, Any]]:
|
|
28
|
+
"""
|
|
29
|
+
Get the current user context.
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
The current user context dictionary, or None if not set
|
|
33
|
+
"""
|
|
34
|
+
try:
|
|
35
|
+
return _user_context.get()
|
|
36
|
+
except LookupError:
|
|
37
|
+
return None
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def clear_user_context() -> None:
|
|
41
|
+
"""Clear the current user context."""
|
|
42
|
+
_user_context.set(None)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def extract_session_id(context: Optional[Dict[str, Any]] = None) -> str:
|
|
46
|
+
"""
|
|
47
|
+
Extract session ID from context.
|
|
48
|
+
|
|
49
|
+
Args:
|
|
50
|
+
context: User context dict (uses current context if None)
|
|
51
|
+
|
|
52
|
+
Returns:
|
|
53
|
+
Session ID string or "unknown_session"
|
|
54
|
+
"""
|
|
55
|
+
if context is None:
|
|
56
|
+
context = get_user_context()
|
|
57
|
+
|
|
58
|
+
if context is None:
|
|
59
|
+
return "unknown_session"
|
|
60
|
+
|
|
61
|
+
if isinstance(context, dict):
|
|
62
|
+
return context.get("session_id", "unknown_session")
|
|
63
|
+
|
|
64
|
+
return getattr(context, "session_id", "unknown_session")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def extract_token(context: Optional[Dict[str, Any]] = None) -> Optional[str]:
|
|
68
|
+
"""
|
|
69
|
+
Extract token from context.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
context: User context dict (uses current context if None)
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
Token string or None
|
|
76
|
+
"""
|
|
77
|
+
if context is None:
|
|
78
|
+
context = get_user_context()
|
|
79
|
+
|
|
80
|
+
if context is None:
|
|
81
|
+
return None
|
|
82
|
+
|
|
83
|
+
if isinstance(context, dict):
|
|
84
|
+
return context.get("token")
|
|
85
|
+
|
|
86
|
+
return getattr(context, "token", None)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def clean_contextvars(obj: Any) -> Any:
|
|
90
|
+
"""
|
|
91
|
+
Recursively replace ContextVar objects with their values.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
obj: Object to clean
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
Cleaned object with ContextVars resolved
|
|
98
|
+
"""
|
|
99
|
+
if isinstance(obj, contextvars.ContextVar):
|
|
100
|
+
try:
|
|
101
|
+
return obj.get()
|
|
102
|
+
except LookupError:
|
|
103
|
+
return None
|
|
104
|
+
elif isinstance(obj, dict):
|
|
105
|
+
return {key: clean_contextvars(value) for key, value in obj.items()}
|
|
106
|
+
elif isinstance(obj, (list, tuple)):
|
|
107
|
+
return type(obj)(clean_contextvars(item) for item in obj)
|
|
108
|
+
else:
|
|
109
|
+
return obj
|