Skip to content

Commit 17a4dce

Browse files
committed
export histogram metrics
Signed-off-by: hwassman <[email protected]>
1 parent 5abda59 commit 17a4dce

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

source/collector.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ def parse_tags(self, filtersMap):
8989

9090
class MetricTimeSeries(object):
9191

92-
def __init__(self, name: str, desc: str):
93-
self.mtype = 'gauge'
92+
def __init__(self, name: str, desc: str, type: Optional[str] = None):
93+
self.mtype = type or 'gauge'
9494
self.mname = name
9595
self.desc = desc
9696
self.timeseries: list[TimeSeries] = []
@@ -154,8 +154,9 @@ def _setup_static_metrics_data(self, metric_names: List[str]):
154154
mDict = {}
155155
md = MetadataHandler()
156156
spec = md.metricsDesc
157+
type = 'histogram' if self.sensor == 'GPFSWaiters' else 'gauge'
157158
for name in metric_names:
158-
ts = MetricTimeSeries(name, spec.get(name, "Desc not found"))
159+
ts = MetricTimeSeries(name, spec.get(name, "Desc not found"), type)
159160
mDict[name] = ts
160161
self.metrics = mDict
161162

source/prometheus.py

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def format_response(self, data) -> [str]:
6161
resp.extend(header)
6262
for sts in metric.timeseries:
6363
for _key, _value in sts.dps.items():
64-
sts_resp = SingleTimeSeriesResponse(name, _key, _value, sts.tags)
64+
sts_resp = SingleTimeSeriesResponse(name, _key, _value, sts.tags, metric.mtype)
6565
self.logger.trace(f'sts_resp.str_expfmt output: {sts_resp.str_expfmt()}')
6666
resp.extend(sts_resp.str_expfmt())
6767
return resp
@@ -201,6 +201,13 @@ def GET(self, **params):
201201
resString = '\n'.join(resp) + '\n'
202202
return resString
203203

204+
# /metrics_gpfs_waiters
205+
elif 'metrics_gpfs_waiters' in cherrypy.request.script_name:
206+
resp = self.metrics(['GPFSWaiters'])
207+
cherrypy.response.headers['Content-Type'] = 'text/plain'
208+
resString = '\n'.join(resp) + '\n'
209+
return resString
210+
204211
# /metrics
205212
elif 'metrics' in cherrypy.request.script_name:
206213
resp = self.metrics()
@@ -238,14 +245,26 @@ def OPTIONS(self):
238245

239246
class SingleTimeSeriesResponse():
240247

241-
def __init__(self, metricname, timestamp, value, tags):
248+
def __init__(self, metricname, timestamp, value, tags, type):
242249
self.metric = metricname
243250
self.timestamp = timestamp * 1000
244251
self.value = value if value is not None else 0 # TODO check if we should return None or null
245252
self.tags = tags
253+
self.type = type
246254

247-
def str_expfmt(self) -> str:
255+
def str_expfmt(self) -> [str]:
248256
myset = []
257+
mstring = ''
258+
259+
if self.type == 'histogram':
260+
mstring = self._str_expfmt_histogram()
261+
else:
262+
mstring = self._str_expfmt_gauge()
263+
264+
myset.append(mstring)
265+
return myset
266+
267+
def _str_expfmt_gauge(self) -> str:
249268

250269
if self.tags:
251270
labels = ','.join('%s="%s"' % (k, v) for k, v in self.tags.items())
@@ -261,5 +280,30 @@ def str_expfmt(self) -> str:
261280
value=repr(float(self.value)),
262281
timestamp=int(self.timestamp)
263282
)
264-
myset.append(mstring)
265-
return myset
283+
return mstring
284+
285+
def _str_expfmt_histogram(self) -> str:
286+
287+
labels = ''
288+
289+
if self.tags:
290+
for k, v in self.tags.items():
291+
if k == 'waiters_time_threshold':
292+
k = 'le'
293+
elif v == 'all':
294+
v = '+Inf'
295+
if labels == '':
296+
labels = labels + '%s="%s"' % (k, v)
297+
else:
298+
labels = labels + ',%s="%s"' % (k, v)
299+
300+
if labels:
301+
fmtstr = '{name}{{{labels}}} {value} {timestamp}'
302+
else:
303+
fmtstr = '{name} {value} {timestamp}'
304+
mstring = fmtstr.format(name=self.metric + '_bucket',
305+
labels=labels,
306+
value=repr(float(self.value)),
307+
timestamp=int(self.timestamp)
308+
)
309+
return mstring

source/zimonGrafanaIntf.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ def main(argv):
321321
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
322322
}
323323
)
324+
# query for GPFSWaiters sensor metrics (PrometheusExporter)
325+
cherrypy.tree.mount(exporter, '/metrics_gpfs_waiters',
326+
{'/':
327+
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
328+
}
329+
)
324330

325331
logger.info("%s", MSG['sysStart'].format(sys.version, cherrypy.__version__))
326332

0 commit comments

Comments
 (0)