Skip to content

Commit

Permalink
Add flag to daemonize prometheus_speedtest
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanralphaviles committed Nov 11, 2018
1 parent 4af2002 commit 7458642
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 11 deletions.
3 changes: 3 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ license. For details, see https://github.com/testing-cabal/mock.
This product bundles glog 0.3.1 which is available under a "2-clause BSD"
license. For details, see https://github.com/benley/python-glog.

This product bundles python-daemon 2.2.0 which is available under an "Apache"
license. For details see https://pagure.io/python-daemon.

This product uses dumb-init which is available under an "MIT" license. For
details, see https://github.com/Yelp/dumb-init.
Copyright (c) 2015 Yelp, Inc.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ pip3 install prometheus_speedtest
### Usage

```
usage: prometheus_speedtest.py [-h] [-p port]
usage: prometheus_speedtest [-h] [-d] [-p port]
Instrument speedtest.net speedtests from Prometheus.
optional arguments:
-h, --help show this help message and exit
-p port, --port port port to listen on.
-d, --daemon Run prometheus_speedtest in the background. (default:
False)
-p port, --port port port to listen on. (default: 8080)
```

### Integrating with Prometheus
Expand Down Expand Up @@ -169,3 +171,4 @@ file for details.
* The Prometheus team <https://prometheus.io>
* Testing in Python team <http://lists.idyll.org/listinfo/testing-in-python>
* Benjamin Staffin [python-glog](https://github.com/benley/python-glog)
* Ben Finney [python-daemon](https://pypi.org/project/python-daemon)
24 changes: 18 additions & 6 deletions prometheus_speedtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
import http
import socketserver

import daemon
import glog as logging
import prometheus_client
from prometheus_client import core
import speedtest

PARSER = argparse.ArgumentParser(
description='Instrument speedtest.net speedtests from Prometheus.')
description='Instrument speedtest.net speedtests from Prometheus.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
PARSER.add_argument(
'-d',
'--daemon',
action='store_true',
default=False,
help='Run prometheus_speedtest in the background.')
PARSER.add_argument(
'-p',
'--port',
Expand All @@ -20,6 +28,8 @@
type=int,
help='port to listen on.')

FLAGS = PARSER.parse_args()


class PrometheusSpeedtest:
"""Enapsulates behavior performing and reporting results of speedtests."""
Expand Down Expand Up @@ -95,8 +105,6 @@ def collect(self):

def main():
"""Entry point for prometheus_speedtest.py."""
flags = PARSER.parse_args()

registry = core.CollectorRegistry(auto_describe=False)
registry.register(SpeedtestCollector())
metrics_handler = prometheus_client.MetricsHandler.factory(registry)
Expand All @@ -106,11 +114,15 @@ def main():
threading_http_server = type(
'ThreadingHTTPServer',
(socketserver.ThreadingMixIn, http.server.HTTPServer), {})
server = threading_http_server(('', flags.port), metrics_handler)
server = threading_http_server(('', FLAGS.port), metrics_handler)

logging.info('Starting HTTP server on port %s', flags.port)
logging.info('Starting HTTP server on port %s', FLAGS.port)
server.serve_forever()


if __name__ == '__main__':
main()
if FLAGS.daemon:
with daemon.DaemonContext():
main()
else:
main()
3 changes: 1 addition & 2 deletions prometheus_speedtest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ def test_collect(mock_metric):
collections.deque(collector.collect())

mock_metric.assert_has_calls(
[mock.call(
labels=[], value=value) for value in speedtest_results])
[mock.call(labels=[], value=value) for value in speedtest_results])


if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ glog>=0.3.1
mock>=2.0.0
prometheus_client>=0.4.2
speedtest-cli>=2.0.2
python-daemon>=2.2.0
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def read_file(rel_path):
'glog>=0.3.1',
'mock>=2.0.0',
'prometheus_client>=0.3.1',
'python-daemon>=2.2.0',
'speedtest-cli>=2.0.2',
],
keywords=['prometheus', 'monitoring', 'speedtest', 'speedtest.net'],
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.0
0.4.0

0 comments on commit 7458642

Please sign in to comment.