Code Repositories xandikos / b381095
Initial prometheus support. Jelmer Vernooń≥ 3 years ago
8 changed file(s) with 129 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 #!/usr/bin/env python3
1 # Xandikos
2 # Copyright (C) 2016 Jelmer Vernooij <jelmer@jelmer.uk>
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; version 2
7 # of the License or (at your option) any later version of
8 # the License.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # MA 02110-1301, USA.
19
20 import sys
21
22 from prometheus_client import multiprocess
23 from prometheus_client import CollectorRegistry, make_wsgi_app
24 from xandikos.prometheus import DEFAULT_PROMETHEUS_DIR
25
26 import optparse
27 import sys
28
29 from xandikos import __version__
30 parser = optparse.OptionParser(version='.'.join(map(str, __version__)))
31 parser.add_option("-l", "--listen_address", dest="listen_address",
32 default="localhost",
33 help="Binding IP address.")
34 parser.add_option("-p", "--port", dest="port", type=int,
35 default=8000,
36 help="Port to listen on.")
37 parser.add_option("-d", "--prometheus-directory",
38 default=DEFAULT_PROMETHEUS_DIR,
39 dest="prometheus_dir",
40 help="Path to prometheus statistics.")
41
42 options, args = parser.parse_args()
43 from wsgiref.simple_server import make_server
44
45 registry = CollectorRegistry()
46 multiprocess.MultiProcessCollector(registry, options.prometheus_dir)
47 app = make_wsgi_app(registry)
48
49 server = make_server(options.listen_address, options.port, app)
50 server.serve_forever()
0 ARGS="--listen_address=localhost --port=9211"
0 [Unit]
1 Description=Prometheus exporter for Xandikos
2 Documentation=https://github.com/jelmer/xandikos
3
4 [Service]
5 Restart=always
6 User=xandikos
7 Group=xandikos
8 EnvironmentFile=/etc/default/prometheus-xandikos-exporter
9 ExecStart=/usr/bin/prometheus-xandikos-exporter $ARGS
10 ExecStartPre=/usr/bin/install -o $USER -g $GID -d /run/xandikos
11
12 [Install]
13 WantedBy=multi-user.target
00 dulwich
11 icalendar
22 defusedxml
3 prometheus_client
11 socket = 127.0.0.1:8001
22 uid = xandikos
33 gid = xandikos
4 exec-as-root = install -u xandikos -g xandikos -d /run/xandikos
45 master = true
56 cheaper = 2
67 processes = 4
0 #!/usr/bin/env python3
1 # Xandikos
2 # Copyright (C) 2016 Jelmer Vernooij <jelmer@jelmer.uk>
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; version 2
7 # of the License or (at your option) any later version of
8 # the License.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # MA 02110-1301, USA.
19
20 from prometheus_client import make_wsgi_app
21
22 DEFAULT_PROMETHEUS_DIR = '/run/xandikos/prometheus'
23
24
25 class PrometheusRedirector(object):
26
27 def __init__(self, inner_app, prometheus_registry):
28 self._inner_app = inner_app
29 self._prometheus_app = make_wsgi_app(prometheus_registry)
30
31 def __call__(self, environ, start_response):
32 if environ['PATH_INFO'] == '/metrics':
33 return self._prometheus_app(environ, start_response)
34 return self._inner_app(environ, start_response)
2828 import os
2929 import posixpath
3030 import uuid
31
32 from prometheus_client import Summary
3133
3234 from xandikos import access, caldav, carddav, sync, webdav, infit, scheduling, timezones
3335 from xandikos.store import (
545547 ])
546548
547549
550 REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
551
552
548553 class WellknownRedirector(object):
549554 """Redirect paths under .well-known/ to the appropriate paths."""
550555
552557 self._inner_app = inner_app
553558 self._dav_root = dav_root
554559
560 @REQUEST_TIME.time()
555561 def __call__(self, environ, start_response):
556562 # See https://tools.ietf.org/html/rfc6764
557563 if ((environ['SCRIPT_NAME'] + environ['PATH_INFO'])
583589 parser.add_option("--dav-root",
584590 default="/",
585591 help="Path to DAV root.")
592 parser.add_option("--prometheus-metrics",
593 action="store_true",
594 dest="prometheus",
595 help="Whether to expose a /metrics target.")
586596 options, args = parser.parse_args(argv)
587597
588598 if options.directory is None:
592602 app = XandikosApp(
593603 options.directory,
594604 current_user_principal=options.current_user_principal)
605
606 if options.prometheus:
607 from .prometheus import PrometheusRedirector
608 from prometheus_client.core import REGISTRY
609 app = PrometheusRedirector(app, REGISTRY)
595610
596611 from wsgiref.simple_server import make_server
597612 app = WellknownRedirector(app, options.dav_root)
2323
2424 from xandikos.web import XandikosApp
2525
26 from prometheus_client import multiprocess
27 from prometheus_client import CollectorRegistry, make_wsgi_app
28
29
2630 app = XandikosApp(
2731 path=os.environ['XANDIKOSPATH'],
2832 current_user_principal=os.environ.get('CURRENT_USER_PRINCIPAL', '/user/'))
33
34 if os.environ.get('ENABLE_PROMETHEUS', '1') == '1':
35 from xandikos.prometheus import PrometheusRedirector, DEFAULT_PROMETHEUS_DIR
36 prometheus_dir = os.environ.get('PROMETHEUS_DIR', DEFAULT_PROMETHEUS_DIR)
37 registry = CollectorRegistry()
38 multiprocess.MultiProcessCollector(registry, os.prometheus_dir)
39 app_with_metrics = PrometheusRedirector(app, registry)