grass-metrics
Unified metrics library for Grass projects. Supports DataDog (DogStatsD/UDP) and OpenTelemetry/OpenObserve (OTLP gRPC/HTTP) with automatic backend selection and dual-write mode.
Installation
# Core (no backend dependencies -- bring your own)
pip install grass-metrics
# With DataDog support
pip install "grass-metrics[datadog]"
# With OpenTelemetry support
pip install "grass-metrics[otel]"
# Both backends
pip install "grass-metrics[all]"
Quick Start
from grass_metrics import create_metrics_manager
metrics = create_metrics_manager()
metrics.increment("wl.my_service.items.processed")
metrics.gauge("wl.my_service.queue.size", 42)
metrics.histogram("wl.my_service.latency_seconds", 1.23)
# With extra tags
metrics.increment("wl.my_service.uploads.success", extra_tags=["provider:s3"])
# Send what is recorded so far (e.g. before a long pause), keep running
metrics.flush()
# Shutdown (flushes OTel buffers)
metrics.shutdown()
Metric Types
| Method | DataDog | OTel / OpenObserve |
|---|---|---|
increment(name, value) |
statsd.increment |
Counter, DELTA: each point is the increase in that export interval |
gauge(name, value) |
statsd.gauge |
Gauge: each point is the value itself (float), last value in the interval wins |
histogram(name, value) |
statsd.histogram |
Histogram, DELTA |
set_gauge() is an alias of gauge().
Gauges are for levels and running totals reported repeatedly: queue size,
memory, a crawler's items-scraped-so-far. Query them in OpenObserve with
last_over_time(), or rate() / increase() for running totals. On OTel, a
gauge series that is not set during an export interval sends no point for that
interval, so call gauge() on every report cycle.
Upgrading from 0.2.x
Before 0.3.0, gauge() on OTel sent the change since the previous call,
rounded to an int, as a DELTA UpDownCounter (41200.5 then 41300.5 arrived as
41200 then 100). It now sends the value. The OpenObserve metric type changes
from sum to gauge for existing names, so check dashboards and alerts built on
the old points.
Backend Selection
The factory picks the backend in this priority order:
| Priority | Source | Example |
|---|---|---|
| 1 | backend argument to create_metrics_manager() |
create_metrics_manager("otel") |
| 2 | METRICS_BACKEND env var |
METRICS_BACKEND=dual |
| 3 | Local Redis key metrics_backend |
Set by preloader from cluster config |
| 4 | Default | "datadog" |
Supported values: datadog, otel, dual
- datadog -- Sends metrics via DogStatsD UDP to the DataDog Agent
- otel -- Sends metrics via OTLP (gRPC or HTTP) to an OpenTelemetry Collector / OpenObserve
- dual -- Sends to both simultaneously (DataDog primary, OTel secondary). Useful during migration.
Environment Variables
Common
| Variable | Description | Default |
|---|---|---|
METRICS_BACKEND |
Backend selection | datadog |
ENVIRONMENT |
Environment name for tags | unknown |
CLUSTER_NAME or CLUSTERNAME |
Cluster name for tags | unknown |
HOSTNAME |
Host name for tags | unknown |
SERVICE_NAME |
Service name for tags | - |
POD_NAME |
Pod name for tags (optional) | - |
LOCAL_REDIS_DB_URI |
Redis URI for dynamic backend override | - |
DataDog
| Variable | Description | Default |
|---|---|---|
DD_AGENT_HOST |
DataDog agent host | 172.17.0.1 |
DD_DOGSTATSD_PORT |
DogStatsD port | 8125 |
OpenTelemetry / OpenObserve
| Variable | Description | Default |
|---|---|---|
OTEL_EXPORTER_ENDPOINT |
OTLP endpoint | auto (see below) |
OTEL_EXPORTER_AUTH_TOKEN |
Auth header value | - |
NODE_IP |
K8s node IP for local agent | - |
OTel endpoint resolution: explicit > OTEL_EXPORTER_ENDPOINT > http://{NODE_IP}:4317 > gateway fallback.
Kubernetes Pod Spec
env:
- name: METRICS_BACKEND
valueFrom:
configMapKeyRef:
name: scraper-config
key: metrics_backend
# DataDog
- name: DD_AGENT_HOST
valueFrom:
fieldRef:
fieldPath: status.hostIP
# OTel
- name: NODE_IP
valueFrom:
fieldRef:
fieldPath: status.hostIP
Docker Compose
environment:
METRICS_BACKEND: "otel"
DD_AGENT_HOST: "datadog" # service name
OTEL_EXPORTER_ENDPOINT: "http://otel-collector:4317"
Domain-Specific Metrics (Consumer Pattern)
For projects with many metrics, wrap MetricsManager in a thin domain class:
from grass_metrics import create_metrics_manager
class YtdMetrics:
DOWNLOADS_SUCCESS = "wl.ytd.downloads.healthy_count"
DOWNLOADS_FAILED = "wl.ytd.downloads.failed_count"
UPLOAD_DURATION = "wl.ytd.upload.duration_seconds"
def __init__(self):
self._m = create_metrics_manager()
def increment_downloaded_videos_count(self):
self._m.increment(self.DOWNLOADS_SUCCESS)
def increment_failed_downloads_count(self):
self._m.increment(self.DOWNLOADS_FAILED)
def send_upload_duration(self, seconds: float):
self._m.histogram(self.UPLOAD_DURATION, seconds)
def shutdown(self):
self._m.shutdown()
Direct Publisher Access
For advanced use cases, publishers can be used directly. Import them from their specific modules (requires the corresponding extras):
# Requires grass-metrics[datadog]
from grass_metrics.datadog_publisher import DatadogPublisher
dd = DatadogPublisher(statsd_host="172.17.0.1", statsd_port=8125)
dd.increment_count("my.counter", 1, ["env:prod"])
# Requires grass-metrics[otel]
from grass_metrics.otel_publisher import OtelPublisher
otel = OtelPublisher(service_name="my-svc", use_grpc=True)
otel.increment_count("my.counter", 1, ["env:prod"])
otel.shutdown()
Architecture
┌──────────────────────────────────────────────────┐
│ Consumer Project │
│ (e.g. YtdMetrics with domain-specific methods) │
└──────────────────────┬───────────────────────────┘
│
create_metrics_manager(backend=...)
│
┌──────▼──────┐
│ Factory │ resolves backend from
│ factory.py │ env / Redis / default
└──────┬──────┘
│
┌────────────┼────────────┐
│ │ │
┌─────▼─────┐ ┌───▼────┐ ┌────▼─────┐
│ MetricsMgr│ │MetrMgr │ │ DualMgr │
│ + DD Pub │ │+ OTel │ │ DD + OTel│
└───────────┘ └────────┘ └──────────┘
Metadata
Release files for grass-metrics 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| grass_metrics-0.3.0.tar.gz | 16.4 kB | Without provenance | Details for grass_metrics-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| grass_metrics-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for grass_metrics-0.3.0-py3-none-any.whl |
Total release size: 31.4 kB
Release files / grass_metrics-0.3.0.tar.gz
| Download URL | grass_metrics-0.3.0.tar.gz |
|---|---|
| Size | 16.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d95037ad2cfc04f755c32d947606fbc0582ef3cfc37ead9efc9180fbaad65ebf
|
|
BLAKE2b-256 checksum How to use checksums |
08b3f2a7711a63e497470a4fb627920f80c78f7e3baf93b5114557c887bde3e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / grass_metrics-0.3.0-py3-none-any.whl
| Download URL | grass_metrics-0.3.0-py3-none-any.whl |
|---|---|
| Size | 15.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3449af07aa011be49a8e152138c06bf95c460bf6f21666c8b8e5f4f0d93589d2
|
|
BLAKE2b-256 checksum How to use checksums |
f894d9d60f52a0eeb45d8ca8040746d002d1b7747afa8aa6f3bf8ad148d522d3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for grass-metrics 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.