Source code for ferrosoft.apps.emiflow.metrics
# Copyright (c) 2025 Ferrosoft GmbH. All rights reserved.
"""Prometheus metrics for emiflow tonnage tracking.
Exposes per-tenant tonnage usage and per-organisation tonnage limits so
subscription overages can be alerted on and dashboarded.
"""
from prometheus_client import Counter, Gauge
#: Cumulative tonnage processed per ``(tenant, organization)`` pair, in
#: metric tons. Seeded at startup from the database and incremented as new
#: booking records are committed.
TonnageCounter = Counter(
"emiflow_tonnage", "Count of tonnage in metric ton", ["tenant", "organization"]
)
#: Per-organisation maximum tonnage allowed by the active subscription
#: plan, in metric tons. Zero when no plan is attached.
TonnageLimit = Gauge(
"emiflow_tonnage_limit", "Limit of tonnage in metric ton", ["organization"]
)
[docs]
def setup():
"""Seed metrics from the current database state.
Initialises :data:`TonnageCounter` from the tonnage service's summary
so the counter does not start at zero after a process restart, and
publishes :data:`TonnageLimit` for every organisation that has a
subscription plan attached.
"""
from ferrosoft.apps.emiflow.services.tonnage import TonnageService
from ferrosoft.apps.ferrobase.models import Organization
summary = TonnageService.get_instance().get_summary()
for entry in summary:
TonnageCounter.labels(
tenant=entry.tenant.name, organization=entry.organization.name
).inc(float(entry.tonnage))
for org in Organization.objects.all():
subscription = org.subscription
if subscription is not None:
TonnageLimit.labels(organization=org.name).set(
float(subscription.max_tonnage or 0)
)