-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvespa-exporter.py
212 lines (169 loc) · 7.83 KB
/
vespa-exporter.py
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
#!/usr/bin/env python
import json
import time
import os
from prometheus_client import start_http_server, Gauge
import requests
import logging
import re
from threading import Lock, Thread
http_port = 9426
config_server = os.getenv('VESPA_CONFIGSERVER', 'localhost:19071')
config_url = 'http://' + config_server + '/config/v2/tenant/default/application/default/cloud.config.model/client'
prom_metrics = {}
prom_metrics_lock = Lock()
endpoints = {}
first_cap_re = re.compile('(.)([A-Z][a-z]+)')
all_cap_re = re.compile('([a-z0-9])([A-Z])')
def ensure_metric_exists(name, description, labels=()):
prom_metrics_lock.acquire()
if name not in prom_metrics:
prom_metrics[name] = Gauge(name, description, labels)
prom_metrics_lock.release()
def camelcase_convert(name):
s1 = first_cap_re.sub(r'\1_\2', name)
return all_cap_re.sub(r'\1_\2', s1).lower()
def expose_status_code(parsed_json, service_name, host_port):
status_code = parsed_json['status']['code']
name = service_name + '_' + 'status_code_up'
ensure_metric_exists(name, 'Status code up?', ['host'])
if status_code == 'up':
value = 1
else:
value = 0
prom_metrics[name].labels(host=host_port).set(value)
def expose_snapshot(parsed_json, service_name, host_port):
snapshot_to = parsed_json['metrics']['snapshot']['to']
name = service_name + '_' + 'snapshot_to'
ensure_metric_exists(name, 'Snapshot to timestamp', ['host'])
prom_metrics[name].labels(host=host_port).set(snapshot_to)
snapshot_from = parsed_json['metrics']['snapshot']['from']
name = service_name + '_' + 'snapshot_from'
ensure_metric_exists(name, 'Snapshot from timestamp', ['host'])
prom_metrics[name].labels(host=host_port).set(snapshot_from)
def get_metrics():
global endpoints
get_application_generation()
try:
response = requests.get(config_url, timeout=10)
try:
model = json.loads(response.text)
except ValueError:
logger.error('JSON parse failed.')
raise ValueError
endpoints = {}
for host in model['hosts']:
for service in host['services']:
for port in service['ports']:
if 'http' in port['tags'].split(' ') and 'state' in port['tags'].split(' '):
if service['type'] in endpoints:
endpoints[service['type']].append(host['name']+':'+str(port['number']))
else:
endpoints[service['type']] = [host['name']+':'+str(port['number'])]
except requests.exceptions.RequestException as e:
logger.error('Request failed (could not update infos from cluster controller %s): %s', config_server, e)
if not endpoints:
raise ValueError
for service_type in ['searchnode', 'distributor']:
for service_hostport in endpoints[service_type]:
t = Thread(target=get_standardservice_metrics, args=(service_type, service_hostport))
t.daemon = True
t.start()
for c in endpoints['container']:
# The comma after "c" is necessary to specify it's a tuple
t = Thread(target=get_container_metrics, args=(c,))
t.daemon = True
t.start()
def get_application_generation():
application_generation_url = 'http://' + config_server + '/application/v2/tenant/default/application/default'
try:
response = requests.get(application_generation_url, timeout=10)
try:
model = json.loads(response.text)
except ValueError as e:
logger.error('JSON parse of application generation failed from %s: %s', config_server, e)
raise ValueError
ensure_metric_exists('vespa_application_generation', 'The generation of the deployed application.')
prom_metrics['vespa_application_generation'].set(model["generation"])
except requests.exceptions.RequestException as e:
logger.error('Request failed (could not get application generation from config server %s): %s', config_server, e)
def get_standardservice_metrics(service_type, hostport):
service = 'vespa_' + service_type
url = 'http://' + hostport + '/state/v1/metrics'
try:
response = requests.get(url, timeout=10)
ensure_metric_exists(service + '_exporter_http_fetch_failed', 'The exporter HTTP request to fetch the metrics failed.', ['host'])
prom_metrics[service + '_exporter_http_fetch_failed'].labels(host=hostport).set(0)
try:
m = json.loads(response.text)
except ValueError:
logger.error('JSON parse failed.')
raise ValueError
expose_status_code(m, service, hostport)
expose_snapshot(m, service, hostport)
for v in m['metrics']['values']:
name = service + '_' + v['name']
name = name.replace('.', '_').replace('-', '_')
name = name.replace('[', '').replace(']', '')
desc = v['description']
labels = ['aggregation', 'host']
labelvalues = {}
labelvalues['host'] = hostport
for d in ['documenttype', 'field', 'disk', 'operantiontype']:
if d in v['dimensions']:
labels.append(d)
labelvalues[d] = v['dimensions'][d]
ensure_metric_exists(name, desc, labels)
for agg, value in v['values'].items():
labelvalues['aggregation'] = agg
prom_metrics[name].labels(**labelvalues).set(value)
except requests.exceptions.RequestException as e:
ensure_metric_exists(service + '_exporter_http_fetch_failed', 'The exporter HTTP request to fetch the metrics failed.', ['host'])
prom_metrics[service + '_exporter_http_fetch_failed'].labels(host=hostport).set(1)
logger.error('Request failed (could not update metrics from endpoint %s): %s', hostport, e)
def get_container_metrics(hostport):
service = 'vespa_container'
url = 'http://' + hostport + '/state/v1/metrics'
try:
response = requests.get(url, timeout=10)
try:
m = json.loads(response.text)
except ValueError:
logger.error('JSON parse failed.')
raise ValueError
expose_status_code(m, service, hostport)
expose_snapshot(m, service, hostport)
for v in m['metrics']['values']:
name = service + '_' + camelcase_convert(v['name'])
name = name.replace('.', '_').replace('-', '_')
name = name.replace('[', '').replace(']', '')
desc = name
labels = ['aggregation', 'host']
labelvalues = {}
labelvalues['host'] = hostport
if 'dimensions' in v:
for d in ['chain', 'handler', 'api', 'operation', 'status', 'serverName', 'serverPort',
'httpMethod', 'gcName', 'documenttype', 'field', 'rankProfile', 'docidPartition']:
if d in v['dimensions']:
labels.append(d.lower())
labelvalues[d.lower()] = v['dimensions'][d]
ensure_metric_exists(name, desc, labels)
for agg, value in v['values'].items():
labelvalues['aggregation'] = agg
prom_metrics[name].labels(**labelvalues).set(value)
except requests.exceptions.RequestException as e:
logger.error('Request failed (could not update metrics from endpoint %s): %s', hostport, e)
def main():
try:
start_http_server(http_port)
while True:
get_metrics()
time.sleep(30)
except KeyboardInterrupt:
exit(0)
if __name__ == '__main__':
LOG_LEVEL = logging.getLevelName(os.getenv('LOG_LEVEL', 'DEBUG'))
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
level=LOG_LEVEL)
logger = logging.getLogger('vespa-exporter')
main()