Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Commit 11ceace

Browse files
committed
add generic prometheus plugin
1 parent 5eb39df commit 11ceace

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

README.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ NewRelic platform. Currently supported backend systems are:
1515
- pgBouncer
1616
- PHP FPM
1717
- PostgreSQL
18+
- Prometheus
1819
- RabbitMQ
1920
- Redis
2021
- Riak
@@ -313,6 +314,44 @@ E.g.:
313314
superuser: False
314315
relation_stats: False
315316

317+
Prometheus Installation Notes
318+
------------------------
319+
You can monitor any prometheus exporter endpoint via http/https.
320+
Prometheus samples are named by their name and their labels.
321+
E.g. ``http_requests_total{job="apiserver", status=200}`` becomes ``http_requests_total/job/apiserver/status/200``
322+
323+
By default all samples are added as derive values. To treat a sample as gauge value you can use the ``gauges`` configuration value.
324+
E.g.:
325+
326+
::
327+
prometheus:
328+
- name: my-go-app
329+
scheme: http
330+
host: localhost
331+
port: 8080
332+
gauges:
333+
- go_threads
334+
335+
Furthermore the plugin allows you to include and exclude certain samples by their name.
336+
If the ``include`` configuration parameter is not set, all samples are included (except for excluded ones).
337+
If the ``include`` configuration parameter is set, only those samples are included.
338+
If the ``exclude`` configuration parameter is set, those samples are excluded (even if they are listed in the ``include`` configuration parameter).
339+
E.g.:
340+
341+
::
342+
prometheus:
343+
- name: my-go-app
344+
scheme: http
345+
host: localhost
346+
port: 8080
347+
include:
348+
- go_threads
349+
- go_info
350+
exclude:
351+
- go_info
352+
353+
If you are monitoring a prometheus exporter via a HTTPS connection you can use the ``verify_ssl_cert`` configuration value to disable SSL certificate verification.
354+
316355
RabbitMQ Installation Notes
317356
---------------------------
318357
The user specified must have access to all virtual hosts you wish to monitor and should have either the Administrator tag or the Monitoring tag.
@@ -462,6 +501,14 @@ Configuration Example
462501
dbname: postgres
463502
superuser: True
464503

504+
prometheus:
505+
- name: my-go-app
506+
scheme: http
507+
host: localhost
508+
port: 8080
509+
gauges:
510+
- go_threads
511+
465512
rabbitmq:
466513
- name: rabbitmq@localhost
467514
host: localhost

newrelic_python_agent/plugins/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'php_apc': 'newrelic_python_agent.plugins.php_apc.APC',
2020
'php_fpm': 'newrelic_python_agent.plugins.php_fpm.FPM',
2121
'postgresql': 'newrelic_python_agent.plugins.postgresql.PostgreSQL',
22+
'prometheus': 'newrelic_python_agent.plugins.prometheus.Prometheus',
2223
'rabbitmq': 'newrelic_python_agent.plugins.rabbitmq.RabbitMQ',
2324
'redis': 'newrelic_python_agent.plugins.redis.Redis',
2425
'riak': 'newrelic_python_agent.plugins.riak.Riak',
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
Generic prometheus Support
3+
4+
"""
5+
from prometheus_client.parser import text_string_to_metric_families
6+
from functools import reduce
7+
import logging
8+
import re
9+
10+
from newrelic_python_agent.plugins import base
11+
12+
LOGGER = logging.getLogger(__name__)
13+
14+
class Prometheus(base.HTTPStatsPlugin):
15+
16+
DEFAULT_PATH = 'metrics'
17+
GUID = 'com.meetme.newrelic_prometheus_agent'
18+
INCLUDE_CONFIG_KEY = 'include'
19+
EXCLUDE_CONFIG_KEY = 'exclude'
20+
GAUGES_CONFIG_KEY = 'gauges'
21+
22+
def __init__(self, config, poll_interval, last_interval_values=None):
23+
super(Prometheus, self).__init__(config, poll_interval, last_interval_values)
24+
25+
26+
def add_datapoints(self, raw_metrics):
27+
"""Add all of the data points for a node
28+
29+
:param str metrics: The metrics content
30+
31+
"""
32+
hasMetrics = False
33+
if not raw_metrics:
34+
return
35+
for family in text_string_to_metric_families(raw_metrics):
36+
for sample in family.samples:
37+
hasMetrics = True
38+
if (
39+
not self.INCLUDE_CONFIG_KEY in self.config or
40+
sample.name in self.config[self.INCLUDE_CONFIG_KEY]
41+
):
42+
if (
43+
self.EXCLUDE_CONFIG_KEY in self.config and
44+
sample.name in self.config[self.EXCLUDE_CONFIG_KEY]
45+
):
46+
LOGGER.debug('Ignoring sample: %r', sample)
47+
else:
48+
name = reduce(
49+
(lambda k, i: k + '/' + i[0] + '/' + i[1]),
50+
sample.labels.iteritems(),
51+
sample.name
52+
)
53+
if (
54+
self.GAUGES_CONFIG_KEY in self.config and
55+
sample.name in self.config[self.GAUGES_CONFIG_KEY]
56+
):
57+
self.add_gauge_value(name,
58+
sample.name,
59+
sample.value)
60+
else:
61+
self.add_derive_value(name,
62+
sample.name,
63+
sample.value)
64+
if not hasMetrics:
65+
LOGGER.debug('Metrics output: %r', raw_metrics)

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
tests_require = []
1717
extras_require = {'mongodb': ['pymongo'],
1818
'pgbouncer': ['psycopg2'],
19-
'postgresql': ['psycopg2']}
19+
'postgresql': ['psycopg2'],
20+
'prometheus': ['prometheus_client']}
2021

2122
if sys.version_info < (2, 7, 0):
2223
install_requires.append('importlib')

0 commit comments

Comments
 (0)