aboutsummaryrefslogtreecommitdiff
path: root/prometheus_haproxy_log_exporter/cli.py
blob: c29b5a462b367ebc16378550937d6c0e40135a4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/python3

# Copyright (C) 2016  Christopher Baines <mail@cbaines.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import logging
import configargparse

from os.path import join, dirname, normpath
from http.server import HTTPServer

from . import __version__
from . import metrics
from .exposition import create_request_handler


def get_argument_parser():
    p = configargparse.ArgParser(
        prog="prometheus-haproxy-log-exporter",
    )

    p.add(
        '--version',
        action='version',
        version=__version__,
        help="Show the version",
    )

    p.add(
        '-c',
        '--config',
        is_config_file=True,
        help="config file path",
        env_var='CONFIG',
    )

    p.add(
        '--port',
        default='9129',
        help="Port on which to expose metrics",
        type=int,
        env_var='PORT',
    )
    p.add(
        '--host',
        default='0.0.0.0',
        help="Host on which to expose metrics",
        env_var='HOST',
    )

    p.add(
        '--licence-location',
        default=join(dirname(dirname(normpath(__file__))), 'LICENSE'),
        help="The location of the licence, linked to through the web interface",
        env_var='LICENCE_LOCATION',
    )

    # Processor arguments
    processor = p.add_mutually_exclusive_group(required=True)
    processor.add_argument(
        '-f',
        '--file',
        help="read logs from a log file",
        type=configargparse.FileType('r'),
        action='store',
        dest='file',
        env_var='LOG_FILE',
    )
    processor.add_argument(
        '-j',
        '--journal',
        help="read logs from systemd journal",
        dest='journal',
        const="haproxy.service",
        nargs='?',
        action='store',
        env_var='JOURNAL_UNIT',
    )
    processor.add_argument(
        '-s',
        '--stdin',
        help="read logs from stdin",
        dest='stdin',
        action='store_true',
        env_var='STDIN',
    )

    p.add(
        '--enabled-metrics',
        nargs='+',
        default=(
            [
                'requests_total',
                'bytes_read_total',
                'backend_queue_length',
                'server_queue_length',
            ] +
            list(metrics.TIMERS.keys())
        ),
        choices=(
            [
                'requests_total',
                'bytes_read_total',
                'backend_queue_length',
                'server_queue_length',
            ] +
            list(metrics.TIMERS.keys())
        ),
        help="Comma separated list of timers to export",
        env_var='ENABLED_TIMERS',
    )

    for counter in (
        metrics.bytes_read_total,
        metrics.requests_total,
    ):
        name_with_hyphens = counter.__name__.replace('_', '-')

        p.add(
            '--%s-labels' % name_with_hyphens,
            nargs='+',
            default=['status_code', 'backend_name', 'server_name'],
            choices=metrics.REQUEST_LABELS,
            help="Labels to use for %s" % counter.__name__,
            env_var='%s_LABELS' % counter.__name__.upper(),
        )

    for timer_name, (_, documentation) in metrics.TIMERS.items():
        p.add_argument(
            '--%s-labels' % timer_name.replace('_', '-'),
            nargs='+',
            default=[],
            choices=metrics.REQUEST_LABELS,
            help="Labels for the %s timer" % timer_name,
            env_var='%s_LABELS' % timer_name.upper(),
        )

        p.add_argument(
            '--%s-buckets' % timer_name.replace('_', '-'),
            nargs='+',
            default=metrics.DEFAULT_TIMER_BUCKETS,
            help="Labels for the %s metric" % timer_name,
            env_var='%s_BUCKETS' % timer_name.upper(),
        )

    for queue_histogram in (
        metrics.backend_queue_length,
        metrics.server_queue_length,
    ):
        name_with_hyphens = queue_histogram.__name__.replace('_', '-')

        p.add_argument(
            '--%s-labels' % name_with_hyphens,
            nargs='+',
            default=[],
            choices=metrics.REQUEST_LABELS,
            help="Labels for the %s metric" % queue_histogram.__name__,
            env_var='%s_LABELS' % queue_histogram.__name__.upper(),
        )

        p.add_argument(
            '--%s-buckets' % name_with_hyphens,
            nargs='+',
            default=metrics.DEFAULT_QUEUE_LENGTH_BUCKETS,
            help="Labels for the %s metric" % queue_histogram.__name__,
            env_var='%s_BUCKETS' % queue_histogram.__name__.upper(),
        )

    return p


def create_log_processor(options, error):
    metric_updaters = []

    for timer_name in metrics.TIMERS.keys():
        if timer_name not in options.enabled_metrics:
            continue

        labelnames = getattr(options, '%s_labels' % timer_name)
        buckets = getattr(options, '%s_buckets' % timer_name)

        metric_updaters.append(
            metrics.timer(timer_name, labelnames, buckets),
        )

    for counter in (
        metrics.bytes_read_total,
        metrics.requests_total,
    ):
        if counter.__name__ not in options.enabled_metrics:
            continue

        labelnames = getattr(options, '%s_labels' % counter.__name__)

        metric_updaters.append(counter(labelnames))

    for queue_histogram in (
        metrics.backend_queue_length,
        metrics.server_queue_length,
    ):
        if queue_histogram.__name__ not in options.enabled_metrics:
            continue

        labelnames = getattr(options, '%s_labels' % queue_histogram.__name__)
        buckets = getattr(options, '%s_buckets' % queue_histogram.__name__)

        metric_updaters.append(queue_histogram(labelnames, buckets))

    if options.stdin:
        from .stdin import StdinProcessor

        log_processor = StdinProcessor(metric_updaters)
    elif options.journal:
        from .journal import JournalProcessor

        log_processor = JournalProcessor(
            metric_updaters=metric_updaters,
            unit=options.journal,
        )
    elif options.file:
        from .file import LogFileProcessor

        log_processor = LogFileProcessor(
            metric_updaters=metric_updaters,
            path=options.file,
        )

    return log_processor


def main():
    logging.basicConfig(level=logging.DEBUG)

    p = get_argument_parser()
    options = p.parse_args()

    logging.info(p.format_values())

    log_processor = create_log_processor(options, p.error)
    log_processor.start()

    host = options.host
    port = options.port

    httpd = HTTPServer(
        (host, port),
        create_request_handler(options.licence_location),
    )

    logging.info("Listing on port %s:%d" % (host, port))

    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass