Skip to content

Commit c455ea1

Browse files
committed
Fixed crash on connection error
Another Python 3 issue: use args instead of message Along with this improved logging
1 parent fa24ec0 commit c455ea1

2 files changed

Lines changed: 12 additions & 25 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ This extension can be easily installed using the weewx extensions installer.
1616

1717
1. Download the extension from github:
1818
```
19-
wget https://github.com/Bert-R/weewx-prometheus/archive/v1.4.0.tar.gz
19+
wget https://github.com/Bert-R/weewx-prometheus/archive/v1.5.0.tar.gz
2020
```
2121

2222
2. Install using the weewx extension utility
2323
```
24-
wee_extension --install v1.4.0.tar.gz
24+
wee_extension --install v1.5.0.tar.gz
2525
```
2626

2727
3. Update **weewx.conf** to appropriately tag weather data for submission into the Prometheus pushgateway and subsequent scraping from Prometheus. Note that **job** and **instance** names may be subject to relabeling depending on your Prometheus environment.

bin/user/prompush.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@
223223

224224
import queue
225225
import sys
226-
import syslog
226+
import logging
227+
228+
log = logging.getLogger(__name__)
227229

228230
class PromPush(weewx.restx.StdRESTful):
229231
"""
@@ -239,7 +241,7 @@ def __init__(self, engine, config_dict):
239241
_prom_dict = weeutil.weeutil.accumulateLeaves(
240242
config_dict['StdRESTful']['PromPush'], max_level=1)
241243
except KeyError as e:
242-
logerr("config error: missing parameter %s" % e)
244+
log.error("config error: missing parameter %s" % e)
243245
return
244246

245247
_manager_dict = weewx.manager.get_manager_dict(
@@ -250,7 +252,7 @@ def __init__(self, engine, config_dict):
250252
**_prom_dict)
251253
self.loop_thread.start()
252254
self.bind(weewx.NEW_LOOP_PACKET, self.new_loop_packet)
253-
loginfo("data will be sent to pushgateway at %s:%s" %
255+
log.info("data will be sent to pushgateway at %s:%s" %
254256
(_prom_dict['host'], _prom_dict['port']))
255257

256258
def new_loop_packet(self, event):
@@ -317,15 +319,15 @@ def post_metrics(self, data):
317319
headers={'Content-Type': 'application/octet-stream'})
318320
if 200 <= _res.status_code <= 299:
319321
# success
320-
# logdbg("pushgw post return code - %s" % _res.status_code)
322+
# log.debug("pushgw post return code - %s" % _res.status_code)
321323
return
322324
else:
323325
# something went awry
324-
logerr("pushgw post error: %s" % _res.text)
326+
log.error("pushgw post error: %s" % _res.text)
325327
return
326328

327329
except requests.ConnectionError as e:
328-
logerr("pushgw post error: %s" % e.message)
330+
log.error("pushgw post error: %s" % e.args)
329331

330332

331333
def process_record(self, record, dbm):
@@ -334,7 +336,7 @@ def process_record(self, record, dbm):
334336
record_data = ''
335337

336338
if self.skip_post:
337-
loginfo("-- prompush: skipping post")
339+
log.info("-- prompush: skipping post")
338340
else:
339341
for key, val in record.items():
340342
if val is None:
@@ -349,21 +351,6 @@ def process_record(self, record, dbm):
349351
record_data += "%s %s\n" % (str(weather_metrics[key]['name']), str(val))
350352
else:
351353
if key != 'type':
352-
loginfo("missing field [%s] in defs" % (key))
354+
log.info("missing field [%s] in defs" % (key))
353355

354356
self.post_metrics(record_data)
355-
356-
357-
#---------------------------------------------------------------------
358-
# misc. logging functions
359-
def logmsg(level, msg):
360-
syslog.syslog(level, 'prom-push: %s' % msg)
361-
362-
def logdbg(msg):
363-
logmsg(syslog.LOG_DEBUG, msg)
364-
365-
def loginfo(msg):
366-
logmsg(syslog.LOG_INFO, msg)
367-
368-
def logerr(msg):
369-
logmsg(syslog.LOG_ERR, msg)

0 commit comments

Comments
 (0)