create metric dynamically

This commit is contained in:
Jakob Ketterl 2020-10-02 17:16:16 +02:00
parent 5d4f3b8d90
commit 4a8e9472ab

View File

@ -154,23 +154,24 @@ class AprsParser(Parser):
super().__init__(handler) super().__init__(handler)
self.ax25parser = Ax25Parser() self.ax25parser = Ax25Parser()
self.deframer = KissDeframer() self.deframer = KissDeframer()
self.metric = self.getMetric() self.metric = None
def setDialFrequency(self, freq): def setDialFrequency(self, freq):
super().setDialFrequency(freq) super().setDialFrequency(freq)
self.metric = self.getMetric() self.metric = None
def getMetric(self): def getMetric(self):
if self.metric is None:
band = "unknown" band = "unknown"
if self.band is not None: if self.band is not None:
band = self.band.getName() band = self.band.getName()
name = "aprs.decodes.{band}.aprs".format(band=band) name = "aprs.decodes.{band}.aprs".format(band=band)
metrics = Metrics.getSharedInstance() metrics = Metrics.getSharedInstance()
metric = metrics.getMetric(name) self.metric = metrics.getMetric(name)
if metric is None: if self.metric is None:
metric = CounterMetric() self.metric = CounterMetric()
metrics.addMetric(name, metric) metrics.addMetric(name, self.metric)
return metric return self.metric
def parse(self, raw): def parse(self, raw):
for frame in self.deframer.parse(raw): for frame in self.deframer.parse(raw):
@ -182,7 +183,7 @@ class AprsParser(Parser):
logger.debug("decoded APRS data: %s", aprsData) logger.debug("decoded APRS data: %s", aprsData)
self.updateMap(aprsData) self.updateMap(aprsData)
self.metric.inc() self.getMetric().inc()
self.handler.write_aprs_data(aprsData) self.handler.write_aprs_data(aprsData)
except Exception: except Exception:
logger.exception("exception while parsing aprs data") logger.exception("exception while parsing aprs data")