Added basic counter for message processor
This commit is contained in:
parent
07ddcdf5d1
commit
4bcdf383ba
|
@ -1,16 +1,41 @@
|
||||||
|
import redis
|
||||||
from meshtastic.mesh_pb2 import MeshPacket
|
from meshtastic.mesh_pb2 import MeshPacket
|
||||||
from prometheus_client import CollectorRegistry
|
from prometheus_client import CollectorRegistry, Counter
|
||||||
|
|
||||||
from exporter.registry import ProcessorRegistry
|
from exporter.registry import ProcessorRegistry
|
||||||
|
|
||||||
|
|
||||||
class MessageProcessor:
|
class MessageProcessor:
|
||||||
def __init__(self, registry: CollectorRegistry):
|
def __init__(self, registry: CollectorRegistry, redis_client: redis.Redis):
|
||||||
self.registry = registry
|
self.registry = registry
|
||||||
|
self.redis_client = redis_client
|
||||||
|
self.counter = Counter('mesh_packets', 'Number of mesh packets processed',
|
||||||
|
['source_id', 'source_short_name', 'source_long_name', 'portnum'],
|
||||||
|
registry=self.registry)
|
||||||
|
|
||||||
def process(self, mesh_packet: MeshPacket):
|
def process(self, mesh_packet: MeshPacket):
|
||||||
port_num = mesh_packet.decoded.portnum
|
port_num = mesh_packet.decoded.portnum
|
||||||
payload = mesh_packet.decoded.payload
|
payload = mesh_packet.decoded.payload
|
||||||
processor = ProcessorRegistry.get_processor(port_num)(self.registry)
|
processor = ProcessorRegistry.get_processor(port_num)(self.registry, self.redis_client)
|
||||||
|
|
||||||
processor.process(payload)
|
client_details = self._get_client_details(mesh_packet)
|
||||||
|
self.counter.labels(
|
||||||
|
source_id=client_details['id'],
|
||||||
|
source_short_name=client_details['short_name'],
|
||||||
|
source_long_name=client_details['long_name'],
|
||||||
|
portnum=port_num
|
||||||
|
).inc()
|
||||||
|
processor.process_packet(payload)
|
||||||
|
|
||||||
|
def _get_client_details(self, mesh_packet: MeshPacket):
|
||||||
|
from_id = mesh_packet['from']
|
||||||
|
|
||||||
|
details = self.redis_client.hgetall(f"node:{from_id}")
|
||||||
|
if details:
|
||||||
|
return details
|
||||||
|
|
||||||
|
return {
|
||||||
|
'id': from_id,
|
||||||
|
'short_name': 'Unknown',
|
||||||
|
'long_name': 'Unknown',
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from venv import logger
|
from venv import logger
|
||||||
|
|
||||||
|
import redis
|
||||||
import unishox2
|
import unishox2
|
||||||
from meshtastic.admin_pb2 import AdminMessage
|
from meshtastic.admin_pb2 import AdminMessage
|
||||||
from meshtastic.mesh_pb2 import Position, User, Routing, Waypoint, RouteDiscovery, NeighborInfo
|
from meshtastic.mesh_pb2 import Position, User, Routing, Waypoint, RouteDiscovery, NeighborInfo
|
||||||
|
@ -14,8 +15,9 @@ from prometheus_client import CollectorRegistry
|
||||||
|
|
||||||
|
|
||||||
class Processor(ABC):
|
class Processor(ABC):
|
||||||
def __init__(self, registry: CollectorRegistry):
|
def __init__(self, registry: CollectorRegistry, redis_client: redis.Redis):
|
||||||
self.registry = registry
|
self.registry = registry
|
||||||
|
self.redis_client = redis_client
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def process(self, payload):
|
def process(self, payload):
|
||||||
|
|
2
main.py
2
main.py
|
@ -67,6 +67,6 @@ if __name__ == "__main__":
|
||||||
keepalive=int(os.getenv('mqtt_keepalive', 60)),
|
keepalive=int(os.getenv('mqtt_keepalive', 60)),
|
||||||
)
|
)
|
||||||
# Configure the Processor and the Exporter
|
# Configure the Processor and the Exporter
|
||||||
processor = MessageProcessor(registry)
|
processor = MessageProcessor(registry, redis_client)
|
||||||
|
|
||||||
mqtt_client.loop_forever()
|
mqtt_client.loop_forever()
|
||||||
|
|
Loading…
Reference in a new issue