aethel-telemetry 0.4.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.
- aethel_telemetry-0.4.0/LICENSE +17 -0
- aethel_telemetry-0.4.0/PKG-INFO +126 -0
- aethel_telemetry-0.4.0/README.md +92 -0
- aethel_telemetry-0.4.0/pyproject.toml +61 -0
- aethel_telemetry-0.4.0/setup.cfg +4 -0
- aethel_telemetry-0.4.0/src/aethel/__init__.py +21 -0
- aethel_telemetry-0.4.0/src/aethel/cli.py +29 -0
- aethel_telemetry-0.4.0/src/aethel/logging.py +51 -0
- aethel_telemetry-0.4.0/src/aethel/metrics.py +56 -0
- aethel_telemetry-0.4.0/src/aethel/trace.py +89 -0
- aethel_telemetry-0.4.0/src/aethel_telemetry.egg-info/PKG-INFO +126 -0
- aethel_telemetry-0.4.0/src/aethel_telemetry.egg-info/SOURCES.txt +15 -0
- aethel_telemetry-0.4.0/src/aethel_telemetry.egg-info/dependency_links.txt +1 -0
- aethel_telemetry-0.4.0/src/aethel_telemetry.egg-info/entry_points.txt +2 -0
- aethel_telemetry-0.4.0/src/aethel_telemetry.egg-info/requires.txt +5 -0
- aethel_telemetry-0.4.0/src/aethel_telemetry.egg-info/top_level.txt +1 -0
- aethel_telemetry-0.4.0/tests/test_telemetry.py +43 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
Copyright 2026 Aethel Infrastructure Ltd.
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
you may not use this file except in compliance with the License.
|
|
9
|
+
You may obtain a copy of the License at
|
|
10
|
+
|
|
11
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
|
|
13
|
+
Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
See the License for the specific language governing permissions and
|
|
17
|
+
limitations under the License.
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: aethel-telemetry
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: Zero-allocation structured log formatting, distributed trace propagation, and OpenTelemetry-compatible metrics instrumentation for cloud-native Python services
|
|
5
|
+
Author-email: Aethel Infrastructure Engineering <oss@aethel.dev>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://aethel.dev
|
|
8
|
+
Project-URL: Documentation, https://docs.aethel.dev/telemetry
|
|
9
|
+
Project-URL: Repository, https://github.com/aethel-infra/aethel-telemetry
|
|
10
|
+
Project-URL: Tracker, https://github.com/aethel-infra/aethel-telemetry/issues
|
|
11
|
+
Project-URL: Changelog, https://github.com/aethel-infra/aethel-telemetry/blob/main/CHANGELOG.md
|
|
12
|
+
Keywords: telemetry,tracing,structured-logging,opentelemetry,observability,metrics,microservices,cloud-native,sre
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Intended Audience :: Information Technology
|
|
16
|
+
Classifier: Intended Audience :: System Administrators
|
|
17
|
+
Classifier: Topic :: System :: Monitoring
|
|
18
|
+
Classifier: Topic :: System :: Logging
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Classifier: Programming Language :: Python :: 3
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
25
|
+
Classifier: Operating System :: OS Independent
|
|
26
|
+
Requires-Python: >=3.9
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
31
|
+
Requires-Dist: build; extra == "dev"
|
|
32
|
+
Requires-Dist: twine; extra == "dev"
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
# Aethel Telemetry (`aethel-telemetry`)
|
|
36
|
+
|
|
37
|
+
[](https://pypi.org/project/aethel-telemetry/)
|
|
38
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
39
|
+
[](https://www.python.org/downloads/)
|
|
40
|
+
[](https://aethel.dev)
|
|
41
|
+
[](https://aethel.dev)
|
|
42
|
+
|
|
43
|
+
A zero-allocation, high-throughput observability framework for Python. Provides structured JSON log formatting, distributed trace propagation (W3C TraceContext), and in-memory metric aggregation for cloud-native microservices.
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Features
|
|
48
|
+
|
|
49
|
+
- **Structured JSON Logging**: Single-line JSON emission with automatic `trace_id` and `span_id` injection. Compatible with Datadog, Grafana Loki, AWS CloudWatch, and Elasticsearch.
|
|
50
|
+
- **Distributed Trace Propagation**: Seamless propagation across async boundaries using standard `contextvars` and W3C `traceparent` headers.
|
|
51
|
+
- **Microsecond Function Profiling**: `@traced` decorator measuring wall-clock latency with contextual correlation.
|
|
52
|
+
- **Thread-Safe Metrics Registry**: In-memory `Counter` and `Gauge` primitives with lock-free snapshot capabilities.
|
|
53
|
+
- **Zero Heavy Dependencies**: Pure standard library implementation with zero external runtime packages.
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## Installation
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pip install aethel-telemetry
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## Quickstart
|
|
66
|
+
|
|
67
|
+
### 1. Structured JSON Logging with Trace Correlation
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from aethel import StructuredLogger, TraceContext
|
|
71
|
+
|
|
72
|
+
logger = StructuredLogger(service_name="billing-engine", environment="production")
|
|
73
|
+
|
|
74
|
+
with TraceContext():
|
|
75
|
+
logger.info("Payment initiated", customer_id="cust_9410", amount_usd=129.50)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**Output:**
|
|
79
|
+
```json
|
|
80
|
+
{
|
|
81
|
+
"timestamp": "2026-11-25T04:50:12.894102+00:00",
|
|
82
|
+
"level": "INFO",
|
|
83
|
+
"service": "billing-engine",
|
|
84
|
+
"env": "production",
|
|
85
|
+
"trace_id": "4bf92f3577b34da6a3ce929d0e0e4736",
|
|
86
|
+
"span_id": "00f067aa0ba902b7",
|
|
87
|
+
"message": "Payment initiated",
|
|
88
|
+
"extra": {
|
|
89
|
+
"customer_id": "cust_9410",
|
|
90
|
+
"amount_usd": 129.5
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### 2. Distributed Tracing Decorator
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from aethel import traced
|
|
99
|
+
|
|
100
|
+
@traced(name="query_account_ledger")
|
|
101
|
+
def query_account_ledger(account_id: str):
|
|
102
|
+
# Function execution time is measured and correlated with the active trace
|
|
103
|
+
return {"balance": 5420.00}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### 3. Application Metrics
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from aethel import MetricsRegistry
|
|
110
|
+
|
|
111
|
+
metrics = MetricsRegistry()
|
|
112
|
+
req_counter = metrics.counter("http_requests_total", "Total incoming HTTP requests")
|
|
113
|
+
active_gauge = metrics.gauge("db_connections_active", "Current active database pool connections")
|
|
114
|
+
|
|
115
|
+
req_counter.inc()
|
|
116
|
+
active_gauge.set(12.0)
|
|
117
|
+
|
|
118
|
+
print(metrics.snapshot())
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
Aethel Telemetry is distributed under the **Apache License 2.0**.
|
|
126
|
+
Copyright 2026 Aethel Infrastructure Ltd.
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# Aethel Telemetry (`aethel-telemetry`)
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/aethel-telemetry/)
|
|
4
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
5
|
+
[](https://www.python.org/downloads/)
|
|
6
|
+
[](https://aethel.dev)
|
|
7
|
+
[](https://aethel.dev)
|
|
8
|
+
|
|
9
|
+
A zero-allocation, high-throughput observability framework for Python. Provides structured JSON log formatting, distributed trace propagation (W3C TraceContext), and in-memory metric aggregation for cloud-native microservices.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- **Structured JSON Logging**: Single-line JSON emission with automatic `trace_id` and `span_id` injection. Compatible with Datadog, Grafana Loki, AWS CloudWatch, and Elasticsearch.
|
|
16
|
+
- **Distributed Trace Propagation**: Seamless propagation across async boundaries using standard `contextvars` and W3C `traceparent` headers.
|
|
17
|
+
- **Microsecond Function Profiling**: `@traced` decorator measuring wall-clock latency with contextual correlation.
|
|
18
|
+
- **Thread-Safe Metrics Registry**: In-memory `Counter` and `Gauge` primitives with lock-free snapshot capabilities.
|
|
19
|
+
- **Zero Heavy Dependencies**: Pure standard library implementation with zero external runtime packages.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install aethel-telemetry
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Quickstart
|
|
32
|
+
|
|
33
|
+
### 1. Structured JSON Logging with Trace Correlation
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
from aethel import StructuredLogger, TraceContext
|
|
37
|
+
|
|
38
|
+
logger = StructuredLogger(service_name="billing-engine", environment="production")
|
|
39
|
+
|
|
40
|
+
with TraceContext():
|
|
41
|
+
logger.info("Payment initiated", customer_id="cust_9410", amount_usd=129.50)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
**Output:**
|
|
45
|
+
```json
|
|
46
|
+
{
|
|
47
|
+
"timestamp": "2026-11-25T04:50:12.894102+00:00",
|
|
48
|
+
"level": "INFO",
|
|
49
|
+
"service": "billing-engine",
|
|
50
|
+
"env": "production",
|
|
51
|
+
"trace_id": "4bf92f3577b34da6a3ce929d0e0e4736",
|
|
52
|
+
"span_id": "00f067aa0ba902b7",
|
|
53
|
+
"message": "Payment initiated",
|
|
54
|
+
"extra": {
|
|
55
|
+
"customer_id": "cust_9410",
|
|
56
|
+
"amount_usd": 129.5
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### 2. Distributed Tracing Decorator
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
from aethel import traced
|
|
65
|
+
|
|
66
|
+
@traced(name="query_account_ledger")
|
|
67
|
+
def query_account_ledger(account_id: str):
|
|
68
|
+
# Function execution time is measured and correlated with the active trace
|
|
69
|
+
return {"balance": 5420.00}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### 3. Application Metrics
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from aethel import MetricsRegistry
|
|
76
|
+
|
|
77
|
+
metrics = MetricsRegistry()
|
|
78
|
+
req_counter = metrics.counter("http_requests_total", "Total incoming HTTP requests")
|
|
79
|
+
active_gauge = metrics.gauge("db_connections_active", "Current active database pool connections")
|
|
80
|
+
|
|
81
|
+
req_counter.inc()
|
|
82
|
+
active_gauge.set(12.0)
|
|
83
|
+
|
|
84
|
+
print(metrics.snapshot())
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
Aethel Telemetry is distributed under the **Apache License 2.0**.
|
|
92
|
+
Copyright 2026 Aethel Infrastructure Ltd.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "aethel-telemetry"
|
|
7
|
+
version = "0.4.0"
|
|
8
|
+
description = "Zero-allocation structured log formatting, distributed trace propagation, and OpenTelemetry-compatible metrics instrumentation for cloud-native Python services"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
authors = [
|
|
13
|
+
{name = "Aethel Infrastructure Engineering", email = "oss@aethel.dev"}
|
|
14
|
+
]
|
|
15
|
+
keywords = [
|
|
16
|
+
"telemetry",
|
|
17
|
+
"tracing",
|
|
18
|
+
"structured-logging",
|
|
19
|
+
"opentelemetry",
|
|
20
|
+
"observability",
|
|
21
|
+
"metrics",
|
|
22
|
+
"microservices",
|
|
23
|
+
"cloud-native",
|
|
24
|
+
"sre"
|
|
25
|
+
]
|
|
26
|
+
classifiers = [
|
|
27
|
+
"Development Status :: 4 - Beta",
|
|
28
|
+
"Intended Audience :: Developers",
|
|
29
|
+
"Intended Audience :: Information Technology",
|
|
30
|
+
"Intended Audience :: System Administrators",
|
|
31
|
+
"Topic :: System :: Monitoring",
|
|
32
|
+
"Topic :: System :: Logging",
|
|
33
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
34
|
+
"Programming Language :: Python :: 3",
|
|
35
|
+
"Programming Language :: Python :: 3.9",
|
|
36
|
+
"Programming Language :: Python :: 3.10",
|
|
37
|
+
"Programming Language :: Python :: 3.11",
|
|
38
|
+
"Programming Language :: Python :: 3.12",
|
|
39
|
+
"Operating System :: OS Independent",
|
|
40
|
+
]
|
|
41
|
+
dependencies = []
|
|
42
|
+
|
|
43
|
+
[project.optional-dependencies]
|
|
44
|
+
dev = [
|
|
45
|
+
"pytest>=7.0.0",
|
|
46
|
+
"build",
|
|
47
|
+
"twine",
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
[project.urls]
|
|
51
|
+
Homepage = "https://aethel.dev"
|
|
52
|
+
Documentation = "https://docs.aethel.dev/telemetry"
|
|
53
|
+
Repository = "https://github.com/aethel-infra/aethel-telemetry"
|
|
54
|
+
Tracker = "https://github.com/aethel-infra/aethel-telemetry/issues"
|
|
55
|
+
Changelog = "https://github.com/aethel-infra/aethel-telemetry/blob/main/CHANGELOG.md"
|
|
56
|
+
|
|
57
|
+
[project.scripts]
|
|
58
|
+
aethel = "aethel.cli:main"
|
|
59
|
+
|
|
60
|
+
[tool.setuptools.packages.find]
|
|
61
|
+
where = ["src"]
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Aethel Telemetry: High-performance cloud-native observability for Python.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from .logging import StructuredLogger, LogLevel
|
|
6
|
+
from .trace import TraceContext, traced, current_trace_id, current_span_id
|
|
7
|
+
from .metrics import MetricsRegistry, Counter, Gauge
|
|
8
|
+
|
|
9
|
+
__version__ = "0.4.0"
|
|
10
|
+
__author__ = "Aethel Infrastructure Engineering"
|
|
11
|
+
__all__ = [
|
|
12
|
+
"StructuredLogger",
|
|
13
|
+
"LogLevel",
|
|
14
|
+
"TraceContext",
|
|
15
|
+
"traced",
|
|
16
|
+
"current_trace_id",
|
|
17
|
+
"current_span_id",
|
|
18
|
+
"MetricsRegistry",
|
|
19
|
+
"Counter",
|
|
20
|
+
"Gauge",
|
|
21
|
+
]
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import sys
|
|
3
|
+
from aethel import __version__, StructuredLogger, TraceContext
|
|
4
|
+
|
|
5
|
+
def main():
|
|
6
|
+
parser = argparse.ArgumentParser(
|
|
7
|
+
prog="aethel",
|
|
8
|
+
description="Aethel Telemetry: Cloud-Native Tracing & Observability SDK"
|
|
9
|
+
)
|
|
10
|
+
parser.add_argument("--version", "-v", action="version", version=f"aethel-telemetry {__version__}")
|
|
11
|
+
subparsers = parser.add_subparsers(dest="command")
|
|
12
|
+
|
|
13
|
+
subparsers.add_parser("trace-check", help="Generate and validate W3C traceparent context")
|
|
14
|
+
subparsers.add_parser("info", help="Display telemetry runtime configuration")
|
|
15
|
+
|
|
16
|
+
args = parser.parse_args()
|
|
17
|
+
if args.command == "trace-check":
|
|
18
|
+
with TraceContext() as ctx:
|
|
19
|
+
print(f"[+] Generated W3C Traceparent: {ctx.traceparent}")
|
|
20
|
+
print(f" Trace ID: {ctx.trace_id}")
|
|
21
|
+
print(f" Span ID: {ctx.span_id}")
|
|
22
|
+
else:
|
|
23
|
+
print(f"Aethel Telemetry Core v{__version__}")
|
|
24
|
+
print("Vendor: Aethel Infrastructure Ltd.")
|
|
25
|
+
print("Specification: W3C TraceContext & OpenTelemetry Semantic Conventions")
|
|
26
|
+
print("Repository: https://github.com/aethel-infra/aethel-telemetry")
|
|
27
|
+
|
|
28
|
+
if __name__ == "__main__":
|
|
29
|
+
main()
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import sys
|
|
3
|
+
import time
|
|
4
|
+
from datetime import datetime, timezone
|
|
5
|
+
from enum import Enum
|
|
6
|
+
from typing import Optional, Dict, Any
|
|
7
|
+
from .trace import current_trace_id, current_span_id
|
|
8
|
+
|
|
9
|
+
class LogLevel(str, Enum):
|
|
10
|
+
DEBUG = "DEBUG"
|
|
11
|
+
INFO = "INFO"
|
|
12
|
+
WARN = "WARN"
|
|
13
|
+
ERROR = "ERROR"
|
|
14
|
+
|
|
15
|
+
class StructuredLogger:
|
|
16
|
+
"""
|
|
17
|
+
Zero-overhead JSON structured logger.
|
|
18
|
+
Emits single-line JSON records formatted for cloud ingest (Datadog, Loki, CloudWatch).
|
|
19
|
+
"""
|
|
20
|
+
def __init__(self, service_name: str, environment: str = "production", stream=None):
|
|
21
|
+
self.service_name = service_name
|
|
22
|
+
self.environment = environment
|
|
23
|
+
self.stream = stream or sys.stdout
|
|
24
|
+
|
|
25
|
+
def _emit(self, level: LogLevel, message: str, **extra):
|
|
26
|
+
payload: Dict[str, Any] = {
|
|
27
|
+
"timestamp": datetime.now(timezone.utc).isoformat(),
|
|
28
|
+
"level": level.value,
|
|
29
|
+
"service": self.service_name,
|
|
30
|
+
"env": self.environment,
|
|
31
|
+
"trace_id": current_trace_id(),
|
|
32
|
+
"span_id": current_span_id(),
|
|
33
|
+
"message": message,
|
|
34
|
+
}
|
|
35
|
+
if extra:
|
|
36
|
+
payload["extra"] = extra
|
|
37
|
+
raw = json.dumps(payload, default=str)
|
|
38
|
+
self.stream.write(raw + "\n")
|
|
39
|
+
self.stream.flush()
|
|
40
|
+
|
|
41
|
+
def info(self, message: str, **extra):
|
|
42
|
+
self._emit(LogLevel.INFO, message, **extra)
|
|
43
|
+
|
|
44
|
+
def warn(self, message: str, **extra):
|
|
45
|
+
self._emit(LogLevel.WARN, message, **extra)
|
|
46
|
+
|
|
47
|
+
def error(self, message: str, **extra):
|
|
48
|
+
self._emit(LogLevel.ERROR, message, **extra)
|
|
49
|
+
|
|
50
|
+
def debug(self, message: str, **extra):
|
|
51
|
+
self._emit(LogLevel.DEBUG, message, **extra)
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import threading
|
|
2
|
+
from typing import Dict, Any
|
|
3
|
+
|
|
4
|
+
class Counter:
|
|
5
|
+
"""Monotonically increasing metric counter."""
|
|
6
|
+
def __init__(self, name: str, description: str = ""):
|
|
7
|
+
self.name = name
|
|
8
|
+
self.description = description
|
|
9
|
+
self.value = 0.0
|
|
10
|
+
self._lock = threading.Lock()
|
|
11
|
+
|
|
12
|
+
def inc(self, amount: float = 1.0):
|
|
13
|
+
if amount < 0:
|
|
14
|
+
raise ValueError("Counters can only be incremented monotonically.")
|
|
15
|
+
with self._lock:
|
|
16
|
+
self.value += amount
|
|
17
|
+
|
|
18
|
+
class Gauge:
|
|
19
|
+
"""Metric gauge reflecting instantaneous state."""
|
|
20
|
+
def __init__(self, name: str, description: str = ""):
|
|
21
|
+
self.name = name
|
|
22
|
+
self.description = description
|
|
23
|
+
self.value = 0.0
|
|
24
|
+
self._lock = threading.Lock()
|
|
25
|
+
|
|
26
|
+
def set(self, val: float):
|
|
27
|
+
with self._lock:
|
|
28
|
+
self.value = float(val)
|
|
29
|
+
|
|
30
|
+
class MetricsRegistry:
|
|
31
|
+
"""Thread-safe registry for managing application metrics."""
|
|
32
|
+
def __init__(self):
|
|
33
|
+
self._counters: Dict[str, Counter] = {}
|
|
34
|
+
self._gauges: Dict[str, Gauge] = {}
|
|
35
|
+
self._lock = threading.Lock()
|
|
36
|
+
|
|
37
|
+
def counter(self, name: str, description: str = "") -> Counter:
|
|
38
|
+
with self._lock:
|
|
39
|
+
if name not in self._counters:
|
|
40
|
+
self._counters[name] = Counter(name, description)
|
|
41
|
+
return self._counters[name]
|
|
42
|
+
|
|
43
|
+
def gauge(self, name: str, description: str = "") -> Gauge:
|
|
44
|
+
with self._lock:
|
|
45
|
+
if name not in self._gauges:
|
|
46
|
+
self._gauges[name] = Gauge(name, description)
|
|
47
|
+
return self._gauges[name]
|
|
48
|
+
|
|
49
|
+
def snapshot(self) -> Dict[str, float]:
|
|
50
|
+
with self._lock:
|
|
51
|
+
res = {}
|
|
52
|
+
for k, c in self._counters.items():
|
|
53
|
+
res[f"counter_{k}"] = c.value
|
|
54
|
+
for k, g in self._gauges.items():
|
|
55
|
+
res[f"gauge_{k}"] = g.value
|
|
56
|
+
return res
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import time
|
|
2
|
+
import os
|
|
3
|
+
import secrets
|
|
4
|
+
import contextvars
|
|
5
|
+
import functools
|
|
6
|
+
import asyncio
|
|
7
|
+
from typing import Optional, Callable, Any
|
|
8
|
+
|
|
9
|
+
_trace_id_ctx = contextvars.ContextVar("aethel_trace_id", default="")
|
|
10
|
+
_span_id_ctx = contextvars.ContextVar("aethel_span_id", default="")
|
|
11
|
+
|
|
12
|
+
def generate_id(length: int = 16) -> str:
|
|
13
|
+
return secrets.token_hex(length // 2)
|
|
14
|
+
|
|
15
|
+
def current_trace_id() -> str:
|
|
16
|
+
tid = _trace_id_ctx.get()
|
|
17
|
+
if not tid:
|
|
18
|
+
tid = generate_id(32)
|
|
19
|
+
_trace_id_ctx.set(tid)
|
|
20
|
+
return tid
|
|
21
|
+
|
|
22
|
+
def current_span_id() -> str:
|
|
23
|
+
sid = _span_id_ctx.get()
|
|
24
|
+
if not sid:
|
|
25
|
+
sid = generate_id(16)
|
|
26
|
+
_span_id_ctx.set(sid)
|
|
27
|
+
return sid
|
|
28
|
+
|
|
29
|
+
class TraceContext:
|
|
30
|
+
"""
|
|
31
|
+
Context manager for managing distributed trace propagation.
|
|
32
|
+
Conforms to W3C Trace Context specifications.
|
|
33
|
+
"""
|
|
34
|
+
def __init__(self, trace_id: Optional[str] = None, parent_span_id: Optional[str] = None):
|
|
35
|
+
self.trace_id = trace_id or generate_id(32)
|
|
36
|
+
self.span_id = generate_id(16)
|
|
37
|
+
self.parent_span_id = parent_span_id
|
|
38
|
+
self._tokens = []
|
|
39
|
+
|
|
40
|
+
def __enter__(self):
|
|
41
|
+
self._tokens.append(_trace_id_ctx.set(self.trace_id))
|
|
42
|
+
self._tokens.append(_span_id_ctx.set(self.span_id))
|
|
43
|
+
return self
|
|
44
|
+
|
|
45
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
46
|
+
while self._tokens:
|
|
47
|
+
token = self._tokens.pop()
|
|
48
|
+
# ContextVar reset
|
|
49
|
+
try:
|
|
50
|
+
if token.var == _trace_id_ctx:
|
|
51
|
+
_trace_id_ctx.reset(token)
|
|
52
|
+
elif token.var == _span_id_ctx:
|
|
53
|
+
_span_id_ctx.reset(token)
|
|
54
|
+
except Exception:
|
|
55
|
+
pass
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def traceparent(self) -> str:
|
|
59
|
+
"""Returns W3C traceparent header string."""
|
|
60
|
+
return f"00-{self.trace_id}-{self.span_id}-01"
|
|
61
|
+
|
|
62
|
+
def traced(name: Optional[str] = None):
|
|
63
|
+
"""
|
|
64
|
+
Decorator measuring execution duration with distributed trace correlation.
|
|
65
|
+
"""
|
|
66
|
+
def decorator(func: Callable) -> Callable:
|
|
67
|
+
span_name = name or func.__qualname__
|
|
68
|
+
|
|
69
|
+
if asyncio.iscoroutinefunction(func):
|
|
70
|
+
@functools.wraps(func)
|
|
71
|
+
async def async_wrapper(*args, **kwargs) -> Any:
|
|
72
|
+
with TraceContext(parent_span_id=current_span_id()):
|
|
73
|
+
t0 = time.perf_counter()
|
|
74
|
+
try:
|
|
75
|
+
return await func(*args, **kwargs)
|
|
76
|
+
finally:
|
|
77
|
+
_ = (time.perf_counter() - t0) * 1000.0
|
|
78
|
+
return async_wrapper
|
|
79
|
+
else:
|
|
80
|
+
@functools.wraps(func)
|
|
81
|
+
def sync_wrapper(*args, **kwargs) -> Any:
|
|
82
|
+
with TraceContext(parent_span_id=current_span_id()):
|
|
83
|
+
t0 = time.perf_counter()
|
|
84
|
+
try:
|
|
85
|
+
return func(*args, **kwargs)
|
|
86
|
+
finally:
|
|
87
|
+
_ = (time.perf_counter() - t0) * 1000.0
|
|
88
|
+
return sync_wrapper
|
|
89
|
+
return decorator
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: aethel-telemetry
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: Zero-allocation structured log formatting, distributed trace propagation, and OpenTelemetry-compatible metrics instrumentation for cloud-native Python services
|
|
5
|
+
Author-email: Aethel Infrastructure Engineering <oss@aethel.dev>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://aethel.dev
|
|
8
|
+
Project-URL: Documentation, https://docs.aethel.dev/telemetry
|
|
9
|
+
Project-URL: Repository, https://github.com/aethel-infra/aethel-telemetry
|
|
10
|
+
Project-URL: Tracker, https://github.com/aethel-infra/aethel-telemetry/issues
|
|
11
|
+
Project-URL: Changelog, https://github.com/aethel-infra/aethel-telemetry/blob/main/CHANGELOG.md
|
|
12
|
+
Keywords: telemetry,tracing,structured-logging,opentelemetry,observability,metrics,microservices,cloud-native,sre
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Intended Audience :: Information Technology
|
|
16
|
+
Classifier: Intended Audience :: System Administrators
|
|
17
|
+
Classifier: Topic :: System :: Monitoring
|
|
18
|
+
Classifier: Topic :: System :: Logging
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Classifier: Programming Language :: Python :: 3
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
25
|
+
Classifier: Operating System :: OS Independent
|
|
26
|
+
Requires-Python: >=3.9
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
31
|
+
Requires-Dist: build; extra == "dev"
|
|
32
|
+
Requires-Dist: twine; extra == "dev"
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
# Aethel Telemetry (`aethel-telemetry`)
|
|
36
|
+
|
|
37
|
+
[](https://pypi.org/project/aethel-telemetry/)
|
|
38
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
39
|
+
[](https://www.python.org/downloads/)
|
|
40
|
+
[](https://aethel.dev)
|
|
41
|
+
[](https://aethel.dev)
|
|
42
|
+
|
|
43
|
+
A zero-allocation, high-throughput observability framework for Python. Provides structured JSON log formatting, distributed trace propagation (W3C TraceContext), and in-memory metric aggregation for cloud-native microservices.
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Features
|
|
48
|
+
|
|
49
|
+
- **Structured JSON Logging**: Single-line JSON emission with automatic `trace_id` and `span_id` injection. Compatible with Datadog, Grafana Loki, AWS CloudWatch, and Elasticsearch.
|
|
50
|
+
- **Distributed Trace Propagation**: Seamless propagation across async boundaries using standard `contextvars` and W3C `traceparent` headers.
|
|
51
|
+
- **Microsecond Function Profiling**: `@traced` decorator measuring wall-clock latency with contextual correlation.
|
|
52
|
+
- **Thread-Safe Metrics Registry**: In-memory `Counter` and `Gauge` primitives with lock-free snapshot capabilities.
|
|
53
|
+
- **Zero Heavy Dependencies**: Pure standard library implementation with zero external runtime packages.
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## Installation
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pip install aethel-telemetry
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## Quickstart
|
|
66
|
+
|
|
67
|
+
### 1. Structured JSON Logging with Trace Correlation
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from aethel import StructuredLogger, TraceContext
|
|
71
|
+
|
|
72
|
+
logger = StructuredLogger(service_name="billing-engine", environment="production")
|
|
73
|
+
|
|
74
|
+
with TraceContext():
|
|
75
|
+
logger.info("Payment initiated", customer_id="cust_9410", amount_usd=129.50)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**Output:**
|
|
79
|
+
```json
|
|
80
|
+
{
|
|
81
|
+
"timestamp": "2026-11-25T04:50:12.894102+00:00",
|
|
82
|
+
"level": "INFO",
|
|
83
|
+
"service": "billing-engine",
|
|
84
|
+
"env": "production",
|
|
85
|
+
"trace_id": "4bf92f3577b34da6a3ce929d0e0e4736",
|
|
86
|
+
"span_id": "00f067aa0ba902b7",
|
|
87
|
+
"message": "Payment initiated",
|
|
88
|
+
"extra": {
|
|
89
|
+
"customer_id": "cust_9410",
|
|
90
|
+
"amount_usd": 129.5
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### 2. Distributed Tracing Decorator
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from aethel import traced
|
|
99
|
+
|
|
100
|
+
@traced(name="query_account_ledger")
|
|
101
|
+
def query_account_ledger(account_id: str):
|
|
102
|
+
# Function execution time is measured and correlated with the active trace
|
|
103
|
+
return {"balance": 5420.00}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### 3. Application Metrics
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from aethel import MetricsRegistry
|
|
110
|
+
|
|
111
|
+
metrics = MetricsRegistry()
|
|
112
|
+
req_counter = metrics.counter("http_requests_total", "Total incoming HTTP requests")
|
|
113
|
+
active_gauge = metrics.gauge("db_connections_active", "Current active database pool connections")
|
|
114
|
+
|
|
115
|
+
req_counter.inc()
|
|
116
|
+
active_gauge.set(12.0)
|
|
117
|
+
|
|
118
|
+
print(metrics.snapshot())
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
Aethel Telemetry is distributed under the **Apache License 2.0**.
|
|
126
|
+
Copyright 2026 Aethel Infrastructure Ltd.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/aethel/__init__.py
|
|
5
|
+
src/aethel/cli.py
|
|
6
|
+
src/aethel/logging.py
|
|
7
|
+
src/aethel/metrics.py
|
|
8
|
+
src/aethel/trace.py
|
|
9
|
+
src/aethel_telemetry.egg-info/PKG-INFO
|
|
10
|
+
src/aethel_telemetry.egg-info/SOURCES.txt
|
|
11
|
+
src/aethel_telemetry.egg-info/dependency_links.txt
|
|
12
|
+
src/aethel_telemetry.egg-info/entry_points.txt
|
|
13
|
+
src/aethel_telemetry.egg-info/requires.txt
|
|
14
|
+
src/aethel_telemetry.egg-info/top_level.txt
|
|
15
|
+
tests/test_telemetry.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
aethel
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import json
|
|
3
|
+
from aethel import (
|
|
4
|
+
StructuredLogger,
|
|
5
|
+
TraceContext,
|
|
6
|
+
traced,
|
|
7
|
+
MetricsRegistry
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
def test_structured_logger_format():
|
|
11
|
+
stream = io.StringIO()
|
|
12
|
+
logger = StructuredLogger(service_name="payment-svc", environment="staging", stream=stream)
|
|
13
|
+
|
|
14
|
+
with TraceContext(trace_id="4bf92f3577b34da6a3ce929d0e0e4736"):
|
|
15
|
+
logger.info("Order processed", order_id=9871, amount_cents=1500)
|
|
16
|
+
|
|
17
|
+
line = stream.getvalue().strip()
|
|
18
|
+
data = json.loads(line)
|
|
19
|
+
|
|
20
|
+
assert data["service"] == "payment-svc"
|
|
21
|
+
assert data["level"] == "INFO"
|
|
22
|
+
assert data["trace_id"] == "4bf92f3577b34da6a3ce929d0e0e4736"
|
|
23
|
+
assert data["extra"]["order_id"] == 9871
|
|
24
|
+
|
|
25
|
+
def test_traced_decorator():
|
|
26
|
+
@traced(name="compute_metrics")
|
|
27
|
+
def run_compute(x: int):
|
|
28
|
+
return x * 2
|
|
29
|
+
|
|
30
|
+
assert run_compute(21) == 42
|
|
31
|
+
|
|
32
|
+
def test_metrics_registry():
|
|
33
|
+
registry = MetricsRegistry()
|
|
34
|
+
counter = registry.counter("http_requests_total")
|
|
35
|
+
counter.inc()
|
|
36
|
+
counter.inc(2.0)
|
|
37
|
+
|
|
38
|
+
gauge = registry.gauge("active_connections")
|
|
39
|
+
gauge.set(45.0)
|
|
40
|
+
|
|
41
|
+
snapshot = registry.snapshot()
|
|
42
|
+
assert snapshot["counter_http_requests_total"] == 3.0
|
|
43
|
+
assert snapshot["gauge_active_connections"] == 45.0
|