grass-metrics 0.1.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.
- grass_metrics-0.1.0/LICENSE +21 -0
- grass_metrics-0.1.0/PKG-INFO +214 -0
- grass_metrics-0.1.0/README.md +181 -0
- grass_metrics-0.1.0/pyproject.toml +54 -0
- grass_metrics-0.1.0/setup.cfg +4 -0
- grass_metrics-0.1.0/src/grass_metrics/__init__.py +34 -0
- grass_metrics-0.1.0/src/grass_metrics/datadog_publisher.py +89 -0
- grass_metrics-0.1.0/src/grass_metrics/dual.py +57 -0
- grass_metrics-0.1.0/src/grass_metrics/factory.py +177 -0
- grass_metrics-0.1.0/src/grass_metrics/manager.py +103 -0
- grass_metrics-0.1.0/src/grass_metrics/otel_publisher.py +223 -0
- grass_metrics-0.1.0/src/grass_metrics/publisher.py +39 -0
- grass_metrics-0.1.0/src/grass_metrics.egg-info/PKG-INFO +214 -0
- grass_metrics-0.1.0/src/grass_metrics.egg-info/SOURCES.txt +15 -0
- grass_metrics-0.1.0/src/grass_metrics.egg-info/dependency_links.txt +1 -0
- grass_metrics-0.1.0/src/grass_metrics.egg-info/requires.txt +16 -0
- grass_metrics-0.1.0/src/grass_metrics.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Alex
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: grass-metrics
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Unified metrics library supporting DataDog (DogStatsD) and OpenTelemetry/OpenObserve with automatic backend selection and dual-write mode.
|
|
5
|
+
Author: Alex
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Classifier: Development Status :: 4 - Beta
|
|
8
|
+
Classifier: Intended Audience :: Developers
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Topic :: System :: Monitoring
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
Requires-Python: >=3.9
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Provides-Extra: datadog
|
|
21
|
+
Requires-Dist: datadog>=0.44.0; extra == "datadog"
|
|
22
|
+
Provides-Extra: otel
|
|
23
|
+
Requires-Dist: opentelemetry-api>=1.20.0; extra == "otel"
|
|
24
|
+
Requires-Dist: opentelemetry-sdk>=1.20.0; extra == "otel"
|
|
25
|
+
Requires-Dist: opentelemetry-exporter-otlp-proto-grpc>=1.20.0; extra == "otel"
|
|
26
|
+
Requires-Dist: opentelemetry-exporter-otlp-proto-http>=1.20.0; extra == "otel"
|
|
27
|
+
Provides-Extra: all
|
|
28
|
+
Requires-Dist: grass-metrics[datadog,otel]; extra == "all"
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: grass-metrics[all]; extra == "dev"
|
|
31
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# grass-metrics
|
|
35
|
+
|
|
36
|
+
Unified metrics library for Grass projects. Supports **DataDog** (DogStatsD/UDP) and **OpenTelemetry/OpenObserve** (OTLP gRPC/HTTP) with automatic backend selection and dual-write mode.
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
# Core (no backend dependencies -- bring your own)
|
|
42
|
+
pip install grass-metrics
|
|
43
|
+
|
|
44
|
+
# With DataDog support
|
|
45
|
+
pip install "grass-metrics[datadog]"
|
|
46
|
+
|
|
47
|
+
# With OpenTelemetry support
|
|
48
|
+
pip install "grass-metrics[otel]"
|
|
49
|
+
|
|
50
|
+
# Both backends
|
|
51
|
+
pip install "grass-metrics[all]"
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Quick Start
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from grass_metrics import create_metrics_manager
|
|
58
|
+
|
|
59
|
+
metrics = create_metrics_manager()
|
|
60
|
+
|
|
61
|
+
metrics.increment("wl.my_service.items.processed")
|
|
62
|
+
metrics.gauge("wl.my_service.queue.size", 42)
|
|
63
|
+
metrics.histogram("wl.my_service.latency_seconds", 1.23)
|
|
64
|
+
|
|
65
|
+
# With extra tags
|
|
66
|
+
metrics.increment("wl.my_service.uploads.success", extra_tags=["provider:s3"])
|
|
67
|
+
|
|
68
|
+
# Shutdown (flushes OTel buffers)
|
|
69
|
+
metrics.shutdown()
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Backend Selection
|
|
73
|
+
|
|
74
|
+
The factory picks the backend in this priority order:
|
|
75
|
+
|
|
76
|
+
| Priority | Source | Example |
|
|
77
|
+
|----------|--------|---------|
|
|
78
|
+
| 1 | `backend` argument to `create_metrics_manager()` | `create_metrics_manager("otel")` |
|
|
79
|
+
| 2 | `METRICS_BACKEND` env var | `METRICS_BACKEND=dual` |
|
|
80
|
+
| 3 | Local Redis key `metrics_backend` | Set by preloader from cluster config |
|
|
81
|
+
| 4 | Default | `"datadog"` |
|
|
82
|
+
|
|
83
|
+
Supported values: `datadog`, `otel`, `dual`
|
|
84
|
+
|
|
85
|
+
- **datadog** -- Sends metrics via DogStatsD UDP to the DataDog Agent
|
|
86
|
+
- **otel** -- Sends metrics via OTLP (gRPC or HTTP) to an OpenTelemetry Collector / OpenObserve
|
|
87
|
+
- **dual** -- Sends to both simultaneously (DataDog primary, OTel secondary). Useful during migration.
|
|
88
|
+
|
|
89
|
+
## Environment Variables
|
|
90
|
+
|
|
91
|
+
### Common
|
|
92
|
+
|
|
93
|
+
| Variable | Description | Default |
|
|
94
|
+
|----------|-------------|---------|
|
|
95
|
+
| `METRICS_BACKEND` | Backend selection | `datadog` |
|
|
96
|
+
| `ENVIRONMENT` | Environment name for tags | `unknown` |
|
|
97
|
+
| `CLUSTER_NAME` or `CLUSTERNAME` | Cluster name for tags | `unknown` |
|
|
98
|
+
| `HOSTNAME` | Host name for tags | `unknown` |
|
|
99
|
+
| `SERVICE_NAME` | Service name for tags | - |
|
|
100
|
+
| `POD_NAME` | Pod name for tags (optional) | - |
|
|
101
|
+
| `LOCAL_REDIS_DB_URI` | Redis URI for dynamic backend override | - |
|
|
102
|
+
|
|
103
|
+
### DataDog
|
|
104
|
+
|
|
105
|
+
| Variable | Description | Default |
|
|
106
|
+
|----------|-------------|---------|
|
|
107
|
+
| `DD_AGENT_HOST` | DataDog agent host | `172.17.0.1` |
|
|
108
|
+
| `DD_DOGSTATSD_PORT` | DogStatsD port | `8125` |
|
|
109
|
+
|
|
110
|
+
### OpenTelemetry / OpenObserve
|
|
111
|
+
|
|
112
|
+
| Variable | Description | Default |
|
|
113
|
+
|----------|-------------|---------|
|
|
114
|
+
| `OTEL_EXPORTER_ENDPOINT` | OTLP endpoint | auto (see below) |
|
|
115
|
+
| `OTEL_EXPORTER_AUTH_TOKEN` | Auth header value | - |
|
|
116
|
+
| `NODE_IP` | K8s node IP for local agent | - |
|
|
117
|
+
|
|
118
|
+
OTel endpoint resolution: explicit > `OTEL_EXPORTER_ENDPOINT` > `http://{NODE_IP}:4317` > gateway fallback.
|
|
119
|
+
|
|
120
|
+
## Kubernetes Pod Spec
|
|
121
|
+
|
|
122
|
+
```yaml
|
|
123
|
+
env:
|
|
124
|
+
- name: METRICS_BACKEND
|
|
125
|
+
valueFrom:
|
|
126
|
+
configMapKeyRef:
|
|
127
|
+
name: scraper-config
|
|
128
|
+
key: metrics_backend
|
|
129
|
+
# DataDog
|
|
130
|
+
- name: DD_AGENT_HOST
|
|
131
|
+
valueFrom:
|
|
132
|
+
fieldRef:
|
|
133
|
+
fieldPath: status.hostIP
|
|
134
|
+
# OTel
|
|
135
|
+
- name: NODE_IP
|
|
136
|
+
valueFrom:
|
|
137
|
+
fieldRef:
|
|
138
|
+
fieldPath: status.hostIP
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Docker Compose
|
|
142
|
+
|
|
143
|
+
```yaml
|
|
144
|
+
environment:
|
|
145
|
+
METRICS_BACKEND: "otel"
|
|
146
|
+
DD_AGENT_HOST: "datadog" # service name
|
|
147
|
+
OTEL_EXPORTER_ENDPOINT: "http://otel-collector:4317"
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Domain-Specific Metrics (Consumer Pattern)
|
|
151
|
+
|
|
152
|
+
For projects with many metrics, wrap `MetricsManager` in a thin domain class:
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
from grass_metrics import create_metrics_manager
|
|
156
|
+
|
|
157
|
+
class YtdMetrics:
|
|
158
|
+
DOWNLOADS_SUCCESS = "wl.ytd.downloads.healthy_count"
|
|
159
|
+
DOWNLOADS_FAILED = "wl.ytd.downloads.failed_count"
|
|
160
|
+
UPLOAD_DURATION = "wl.ytd.upload.duration_seconds"
|
|
161
|
+
|
|
162
|
+
def __init__(self):
|
|
163
|
+
self._m = create_metrics_manager()
|
|
164
|
+
|
|
165
|
+
def increment_downloaded_videos_count(self):
|
|
166
|
+
self._m.increment(self.DOWNLOADS_SUCCESS)
|
|
167
|
+
|
|
168
|
+
def increment_failed_downloads_count(self):
|
|
169
|
+
self._m.increment(self.DOWNLOADS_FAILED)
|
|
170
|
+
|
|
171
|
+
def send_upload_duration(self, seconds: float):
|
|
172
|
+
self._m.histogram(self.UPLOAD_DURATION, seconds)
|
|
173
|
+
|
|
174
|
+
def shutdown(self):
|
|
175
|
+
self._m.shutdown()
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Direct Publisher Access
|
|
179
|
+
|
|
180
|
+
For advanced use cases, publishers can be used directly:
|
|
181
|
+
|
|
182
|
+
```python
|
|
183
|
+
from grass_metrics import DatadogPublisher, OtelPublisher
|
|
184
|
+
|
|
185
|
+
dd = DatadogPublisher(statsd_host="172.17.0.1", statsd_port=8125)
|
|
186
|
+
dd.increment_count("my.counter", 1, ["env:prod"])
|
|
187
|
+
|
|
188
|
+
otel = OtelPublisher(service_name="my-svc", use_grpc=True)
|
|
189
|
+
otel.increment_count("my.counter", 1, ["env:prod"])
|
|
190
|
+
otel.shutdown()
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Architecture
|
|
194
|
+
|
|
195
|
+
```
|
|
196
|
+
┌──────────────────────────────────────────────────┐
|
|
197
|
+
│ Consumer Project │
|
|
198
|
+
│ (e.g. YtdMetrics with domain-specific methods) │
|
|
199
|
+
└──────────────────────┬───────────────────────────┘
|
|
200
|
+
│
|
|
201
|
+
create_metrics_manager(backend=...)
|
|
202
|
+
│
|
|
203
|
+
┌──────▼──────┐
|
|
204
|
+
│ Factory │ resolves backend from
|
|
205
|
+
│ factory.py │ env / Redis / default
|
|
206
|
+
└──────┬──────┘
|
|
207
|
+
│
|
|
208
|
+
┌────────────┼────────────┐
|
|
209
|
+
│ │ │
|
|
210
|
+
┌─────▼─────┐ ┌───▼────┐ ┌────▼─────┐
|
|
211
|
+
│ MetricsMgr│ │MetrMgr │ │ DualMgr │
|
|
212
|
+
│ + DD Pub │ │+ OTel │ │ DD + OTel│
|
|
213
|
+
└───────────┘ └────────┘ └──────────┘
|
|
214
|
+
```
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# grass-metrics
|
|
2
|
+
|
|
3
|
+
Unified metrics library for Grass projects. Supports **DataDog** (DogStatsD/UDP) and **OpenTelemetry/OpenObserve** (OTLP gRPC/HTTP) with automatic backend selection and dual-write mode.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# Core (no backend dependencies -- bring your own)
|
|
9
|
+
pip install grass-metrics
|
|
10
|
+
|
|
11
|
+
# With DataDog support
|
|
12
|
+
pip install "grass-metrics[datadog]"
|
|
13
|
+
|
|
14
|
+
# With OpenTelemetry support
|
|
15
|
+
pip install "grass-metrics[otel]"
|
|
16
|
+
|
|
17
|
+
# Both backends
|
|
18
|
+
pip install "grass-metrics[all]"
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from grass_metrics import create_metrics_manager
|
|
25
|
+
|
|
26
|
+
metrics = create_metrics_manager()
|
|
27
|
+
|
|
28
|
+
metrics.increment("wl.my_service.items.processed")
|
|
29
|
+
metrics.gauge("wl.my_service.queue.size", 42)
|
|
30
|
+
metrics.histogram("wl.my_service.latency_seconds", 1.23)
|
|
31
|
+
|
|
32
|
+
# With extra tags
|
|
33
|
+
metrics.increment("wl.my_service.uploads.success", extra_tags=["provider:s3"])
|
|
34
|
+
|
|
35
|
+
# Shutdown (flushes OTel buffers)
|
|
36
|
+
metrics.shutdown()
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Backend Selection
|
|
40
|
+
|
|
41
|
+
The factory picks the backend in this priority order:
|
|
42
|
+
|
|
43
|
+
| Priority | Source | Example |
|
|
44
|
+
|----------|--------|---------|
|
|
45
|
+
| 1 | `backend` argument to `create_metrics_manager()` | `create_metrics_manager("otel")` |
|
|
46
|
+
| 2 | `METRICS_BACKEND` env var | `METRICS_BACKEND=dual` |
|
|
47
|
+
| 3 | Local Redis key `metrics_backend` | Set by preloader from cluster config |
|
|
48
|
+
| 4 | Default | `"datadog"` |
|
|
49
|
+
|
|
50
|
+
Supported values: `datadog`, `otel`, `dual`
|
|
51
|
+
|
|
52
|
+
- **datadog** -- Sends metrics via DogStatsD UDP to the DataDog Agent
|
|
53
|
+
- **otel** -- Sends metrics via OTLP (gRPC or HTTP) to an OpenTelemetry Collector / OpenObserve
|
|
54
|
+
- **dual** -- Sends to both simultaneously (DataDog primary, OTel secondary). Useful during migration.
|
|
55
|
+
|
|
56
|
+
## Environment Variables
|
|
57
|
+
|
|
58
|
+
### Common
|
|
59
|
+
|
|
60
|
+
| Variable | Description | Default |
|
|
61
|
+
|----------|-------------|---------|
|
|
62
|
+
| `METRICS_BACKEND` | Backend selection | `datadog` |
|
|
63
|
+
| `ENVIRONMENT` | Environment name for tags | `unknown` |
|
|
64
|
+
| `CLUSTER_NAME` or `CLUSTERNAME` | Cluster name for tags | `unknown` |
|
|
65
|
+
| `HOSTNAME` | Host name for tags | `unknown` |
|
|
66
|
+
| `SERVICE_NAME` | Service name for tags | - |
|
|
67
|
+
| `POD_NAME` | Pod name for tags (optional) | - |
|
|
68
|
+
| `LOCAL_REDIS_DB_URI` | Redis URI for dynamic backend override | - |
|
|
69
|
+
|
|
70
|
+
### DataDog
|
|
71
|
+
|
|
72
|
+
| Variable | Description | Default |
|
|
73
|
+
|----------|-------------|---------|
|
|
74
|
+
| `DD_AGENT_HOST` | DataDog agent host | `172.17.0.1` |
|
|
75
|
+
| `DD_DOGSTATSD_PORT` | DogStatsD port | `8125` |
|
|
76
|
+
|
|
77
|
+
### OpenTelemetry / OpenObserve
|
|
78
|
+
|
|
79
|
+
| Variable | Description | Default |
|
|
80
|
+
|----------|-------------|---------|
|
|
81
|
+
| `OTEL_EXPORTER_ENDPOINT` | OTLP endpoint | auto (see below) |
|
|
82
|
+
| `OTEL_EXPORTER_AUTH_TOKEN` | Auth header value | - |
|
|
83
|
+
| `NODE_IP` | K8s node IP for local agent | - |
|
|
84
|
+
|
|
85
|
+
OTel endpoint resolution: explicit > `OTEL_EXPORTER_ENDPOINT` > `http://{NODE_IP}:4317` > gateway fallback.
|
|
86
|
+
|
|
87
|
+
## Kubernetes Pod Spec
|
|
88
|
+
|
|
89
|
+
```yaml
|
|
90
|
+
env:
|
|
91
|
+
- name: METRICS_BACKEND
|
|
92
|
+
valueFrom:
|
|
93
|
+
configMapKeyRef:
|
|
94
|
+
name: scraper-config
|
|
95
|
+
key: metrics_backend
|
|
96
|
+
# DataDog
|
|
97
|
+
- name: DD_AGENT_HOST
|
|
98
|
+
valueFrom:
|
|
99
|
+
fieldRef:
|
|
100
|
+
fieldPath: status.hostIP
|
|
101
|
+
# OTel
|
|
102
|
+
- name: NODE_IP
|
|
103
|
+
valueFrom:
|
|
104
|
+
fieldRef:
|
|
105
|
+
fieldPath: status.hostIP
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Docker Compose
|
|
109
|
+
|
|
110
|
+
```yaml
|
|
111
|
+
environment:
|
|
112
|
+
METRICS_BACKEND: "otel"
|
|
113
|
+
DD_AGENT_HOST: "datadog" # service name
|
|
114
|
+
OTEL_EXPORTER_ENDPOINT: "http://otel-collector:4317"
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Domain-Specific Metrics (Consumer Pattern)
|
|
118
|
+
|
|
119
|
+
For projects with many metrics, wrap `MetricsManager` in a thin domain class:
|
|
120
|
+
|
|
121
|
+
```python
|
|
122
|
+
from grass_metrics import create_metrics_manager
|
|
123
|
+
|
|
124
|
+
class YtdMetrics:
|
|
125
|
+
DOWNLOADS_SUCCESS = "wl.ytd.downloads.healthy_count"
|
|
126
|
+
DOWNLOADS_FAILED = "wl.ytd.downloads.failed_count"
|
|
127
|
+
UPLOAD_DURATION = "wl.ytd.upload.duration_seconds"
|
|
128
|
+
|
|
129
|
+
def __init__(self):
|
|
130
|
+
self._m = create_metrics_manager()
|
|
131
|
+
|
|
132
|
+
def increment_downloaded_videos_count(self):
|
|
133
|
+
self._m.increment(self.DOWNLOADS_SUCCESS)
|
|
134
|
+
|
|
135
|
+
def increment_failed_downloads_count(self):
|
|
136
|
+
self._m.increment(self.DOWNLOADS_FAILED)
|
|
137
|
+
|
|
138
|
+
def send_upload_duration(self, seconds: float):
|
|
139
|
+
self._m.histogram(self.UPLOAD_DURATION, seconds)
|
|
140
|
+
|
|
141
|
+
def shutdown(self):
|
|
142
|
+
self._m.shutdown()
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Direct Publisher Access
|
|
146
|
+
|
|
147
|
+
For advanced use cases, publishers can be used directly:
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
from grass_metrics import DatadogPublisher, OtelPublisher
|
|
151
|
+
|
|
152
|
+
dd = DatadogPublisher(statsd_host="172.17.0.1", statsd_port=8125)
|
|
153
|
+
dd.increment_count("my.counter", 1, ["env:prod"])
|
|
154
|
+
|
|
155
|
+
otel = OtelPublisher(service_name="my-svc", use_grpc=True)
|
|
156
|
+
otel.increment_count("my.counter", 1, ["env:prod"])
|
|
157
|
+
otel.shutdown()
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Architecture
|
|
161
|
+
|
|
162
|
+
```
|
|
163
|
+
┌──────────────────────────────────────────────────┐
|
|
164
|
+
│ Consumer Project │
|
|
165
|
+
│ (e.g. YtdMetrics with domain-specific methods) │
|
|
166
|
+
└──────────────────────┬───────────────────────────┘
|
|
167
|
+
│
|
|
168
|
+
create_metrics_manager(backend=...)
|
|
169
|
+
│
|
|
170
|
+
┌──────▼──────┐
|
|
171
|
+
│ Factory │ resolves backend from
|
|
172
|
+
│ factory.py │ env / Redis / default
|
|
173
|
+
└──────┬──────┘
|
|
174
|
+
│
|
|
175
|
+
┌────────────┼────────────┐
|
|
176
|
+
│ │ │
|
|
177
|
+
┌─────▼─────┐ ┌───▼────┐ ┌────▼─────┐
|
|
178
|
+
│ MetricsMgr│ │MetrMgr │ │ DualMgr │
|
|
179
|
+
│ + DD Pub │ │+ OTel │ │ DD + OTel│
|
|
180
|
+
└───────────┘ └────────┘ └──────────┘
|
|
181
|
+
```
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "grass-metrics"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Unified metrics library supporting DataDog (DogStatsD) and OpenTelemetry/OpenObserve with automatic backend selection and dual-write mode."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Alex" },
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 4 - Beta",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.9",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Topic :: System :: Monitoring",
|
|
25
|
+
"Typing :: Typed",
|
|
26
|
+
]
|
|
27
|
+
dependencies = []
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
datadog = [
|
|
31
|
+
"datadog>=0.44.0",
|
|
32
|
+
]
|
|
33
|
+
otel = [
|
|
34
|
+
"opentelemetry-api>=1.20.0",
|
|
35
|
+
"opentelemetry-sdk>=1.20.0",
|
|
36
|
+
"opentelemetry-exporter-otlp-proto-grpc>=1.20.0",
|
|
37
|
+
"opentelemetry-exporter-otlp-proto-http>=1.20.0",
|
|
38
|
+
]
|
|
39
|
+
all = [
|
|
40
|
+
"grass-metrics[datadog,otel]",
|
|
41
|
+
]
|
|
42
|
+
dev = [
|
|
43
|
+
"grass-metrics[all]",
|
|
44
|
+
"pytest>=7.0",
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
# Update these URLs when publishing
|
|
48
|
+
[project.urls]
|
|
49
|
+
# Homepage = "https://github.com/your-org/grass-platform/tree/main/libs/python/grass-metrics"
|
|
50
|
+
# Repository = "https://github.com/your-org/grass-platform"
|
|
51
|
+
# Issues = "https://github.com/your-org/grass-platform/issues"
|
|
52
|
+
|
|
53
|
+
[tool.setuptools.packages.find]
|
|
54
|
+
where = ["src"]
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""
|
|
2
|
+
grass-metrics -- Unified metrics for Grass projects.
|
|
3
|
+
|
|
4
|
+
Supports DataDog (DogStatsD) and OpenTelemetry/OpenObserve backends
|
|
5
|
+
with automatic environment tagging, dual-write mode, and a factory
|
|
6
|
+
that picks the right backend from env vars or Redis config.
|
|
7
|
+
|
|
8
|
+
Quick start::
|
|
9
|
+
|
|
10
|
+
from grass_metrics import create_metrics_manager
|
|
11
|
+
|
|
12
|
+
metrics = create_metrics_manager()
|
|
13
|
+
metrics.increment("wl.my_service.items.processed")
|
|
14
|
+
metrics.gauge("wl.my_service.queue.size", 42)
|
|
15
|
+
metrics.histogram("wl.my_service.latency_seconds", 1.23)
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from grass_metrics.factory import create_metrics_manager, reset_cache
|
|
19
|
+
from grass_metrics.manager import MetricsManager
|
|
20
|
+
from grass_metrics.dual import DualMetricsManager
|
|
21
|
+
from grass_metrics.publisher import MetricsPublisher
|
|
22
|
+
from grass_metrics.datadog_publisher import DatadogPublisher
|
|
23
|
+
from grass_metrics.otel_publisher import OtelPublisher
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"create_metrics_manager",
|
|
27
|
+
"reset_cache",
|
|
28
|
+
"MetricsManager",
|
|
29
|
+
"DualMetricsManager",
|
|
30
|
+
"MetricsPublisher",
|
|
31
|
+
"DatadogPublisher",
|
|
32
|
+
"OtelPublisher",
|
|
33
|
+
]
|
|
34
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""
|
|
2
|
+
DataDog StatsD publisher.
|
|
3
|
+
|
|
4
|
+
Communicates with the DataDog Agent via UDP/DogStatsD.
|
|
5
|
+
|
|
6
|
+
Host resolution order:
|
|
7
|
+
1. Explicit ``statsd_host`` parameter
|
|
8
|
+
2. ``DD_AGENT_HOST`` env var (K8s: set from status.hostIP via downward API)
|
|
9
|
+
3. ``172.17.0.1`` fallback (Docker bridge network)
|
|
10
|
+
|
|
11
|
+
Kubernetes pod spec example::
|
|
12
|
+
|
|
13
|
+
env:
|
|
14
|
+
- name: DD_AGENT_HOST
|
|
15
|
+
valueFrom:
|
|
16
|
+
fieldRef:
|
|
17
|
+
fieldPath: status.hostIP
|
|
18
|
+
- name: DD_DOGSTATSD_PORT
|
|
19
|
+
value: "8125"
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import logging
|
|
25
|
+
import os
|
|
26
|
+
|
|
27
|
+
from datadog import DogStatsd
|
|
28
|
+
|
|
29
|
+
logger = logging.getLogger(__name__)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class DatadogPublisher:
|
|
33
|
+
"""UDP-based DogStatsD publisher."""
|
|
34
|
+
|
|
35
|
+
def __init__(
|
|
36
|
+
self,
|
|
37
|
+
statsd_host: str | None = None,
|
|
38
|
+
statsd_port: int | None = None,
|
|
39
|
+
):
|
|
40
|
+
if statsd_host is None:
|
|
41
|
+
statsd_host = os.environ.get("DD_AGENT_HOST", "172.17.0.1")
|
|
42
|
+
if statsd_port is None:
|
|
43
|
+
statsd_port = int(os.environ.get("DD_DOGSTATSD_PORT", "8125"))
|
|
44
|
+
|
|
45
|
+
self.statsd = DogStatsd(host=statsd_host, port=statsd_port)
|
|
46
|
+
self.host = statsd_host
|
|
47
|
+
self.port = statsd_port
|
|
48
|
+
|
|
49
|
+
logger.info(
|
|
50
|
+
"DatadogPublisher initialized: %s:%s", statsd_host, statsd_port,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
# -- MetricsPublisher interface ----------------------------------------
|
|
54
|
+
|
|
55
|
+
def increment_count(
|
|
56
|
+
self,
|
|
57
|
+
metric_name: str,
|
|
58
|
+
count: int = 1,
|
|
59
|
+
count_tags: list[str] | None = None,
|
|
60
|
+
) -> None:
|
|
61
|
+
try:
|
|
62
|
+
self.statsd.increment(metric_name, value=count, tags=count_tags or [])
|
|
63
|
+
except Exception as exc:
|
|
64
|
+
logger.debug("DD increment error (%s): %s", metric_name, exc)
|
|
65
|
+
|
|
66
|
+
def send_gauge(
|
|
67
|
+
self,
|
|
68
|
+
metric_name: str,
|
|
69
|
+
value: float,
|
|
70
|
+
tags: list[str] | None = None,
|
|
71
|
+
) -> None:
|
|
72
|
+
try:
|
|
73
|
+
self.statsd.gauge(metric_name, value, tags=tags or [])
|
|
74
|
+
except Exception as exc:
|
|
75
|
+
logger.debug("DD gauge error (%s): %s", metric_name, exc)
|
|
76
|
+
|
|
77
|
+
def send_histogram(
|
|
78
|
+
self,
|
|
79
|
+
metric_name: str,
|
|
80
|
+
value: float,
|
|
81
|
+
tags: list[str] | None = None,
|
|
82
|
+
) -> None:
|
|
83
|
+
try:
|
|
84
|
+
self.statsd.histogram(metric_name, value, tags=tags or [])
|
|
85
|
+
except Exception as exc:
|
|
86
|
+
logger.debug("DD histogram error (%s): %s", metric_name, exc)
|
|
87
|
+
|
|
88
|
+
def shutdown(self) -> None:
|
|
89
|
+
"""No-op -- DogStatsD is fire-and-forget UDP."""
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Dual-write metrics manager.
|
|
3
|
+
|
|
4
|
+
Delegates every call to two managers simultaneously so both backends
|
|
5
|
+
receive identical data. Useful during migration to validate that the
|
|
6
|
+
secondary (e.g. OTel) output matches the primary (e.g. DataDog).
|
|
7
|
+
|
|
8
|
+
The primary manager's return values are authoritative; errors in the
|
|
9
|
+
secondary are logged but never propagated.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import logging
|
|
15
|
+
|
|
16
|
+
from grass_metrics.manager import MetricsManager
|
|
17
|
+
|
|
18
|
+
logger = logging.getLogger(__name__)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class DualMetricsManager:
|
|
22
|
+
"""Transparent proxy that fans out to two MetricsManager instances."""
|
|
23
|
+
|
|
24
|
+
def __init__(self, primary: MetricsManager, secondary: MetricsManager):
|
|
25
|
+
self._primary = primary
|
|
26
|
+
self._secondary = secondary
|
|
27
|
+
|
|
28
|
+
# Expose the publisher from the primary for introspection if needed
|
|
29
|
+
@property
|
|
30
|
+
def publisher(self):
|
|
31
|
+
return self._primary.publisher
|
|
32
|
+
|
|
33
|
+
def __getattr__(self, name: str):
|
|
34
|
+
primary_attr = getattr(self._primary, name)
|
|
35
|
+
if not callable(primary_attr):
|
|
36
|
+
return primary_attr
|
|
37
|
+
|
|
38
|
+
def _dual_call(*args, **kwargs):
|
|
39
|
+
result = primary_attr(*args, **kwargs)
|
|
40
|
+
try:
|
|
41
|
+
getattr(self._secondary, name)(*args, **kwargs)
|
|
42
|
+
except Exception as exc:
|
|
43
|
+
logger.debug("Dual-write secondary (%s) error: %s", name, exc)
|
|
44
|
+
return result
|
|
45
|
+
|
|
46
|
+
return _dual_call
|
|
47
|
+
|
|
48
|
+
def shutdown(self) -> None:
|
|
49
|
+
"""Shut down secondary first (flush OTel), then primary."""
|
|
50
|
+
try:
|
|
51
|
+
self._secondary.shutdown()
|
|
52
|
+
except Exception as exc:
|
|
53
|
+
logger.debug("Dual secondary shutdown error: %s", exc)
|
|
54
|
+
try:
|
|
55
|
+
self._primary.shutdown()
|
|
56
|
+
except Exception as exc:
|
|
57
|
+
logger.debug("Dual primary shutdown error: %s", exc)
|