Skip to content

Commit 9a166d3

Browse files
author
Jakob Mellberg
committed
optional interval
1 parent 429cc05 commit 9a166d3

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

kea_exporter/cli.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import time
22

33
import click
4-
from prometheus_client import start_http_server
4+
from prometheus_client import REGISTRY, make_wsgi_app, start_http_server
55

66
from . import __PROJECT__, __VERSION__
77

@@ -35,7 +35,7 @@
3535
"--interval",
3636
envvar="INTERVAL",
3737
type=int,
38-
default=7.5,
38+
default=0,
3939
help="Specify the metrics update interval in seconds.",
4040
)
4141
@click.option(
@@ -70,13 +70,27 @@ def cli(mode, port, address, interval, **kwargs):
7070
exporter = KeaExporter(**kwargs)
7171
exporter.update()
7272

73-
start_http_server(port, address)
73+
httpd, _ = start_http_server(port, address)
74+
75+
76+
def local_wsgi_app(registry):
77+
func = make_wsgi_app(registry, False)
78+
def app(environ, start_response):
79+
if interval == 0:
80+
exporter.update()
81+
output_array = func(environ, start_response)
82+
return output_array
83+
return app
84+
85+
httpd.set_app(local_wsgi_app(REGISTRY))
86+
7487
click.echo(f"Listening on http://{address}:{port}")
7588

7689
while True:
77-
time.sleep(interval)
78-
exporter.update()
79-
90+
if interval:
91+
time.sleep(interval)
92+
else:
93+
time.sleep(1)
8094

8195
if __name__ == "__main__":
8296
cli()

0 commit comments

Comments
 (0)