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

Commit 3b5b9f1

Browse files
authored
Merge pull request #45 from CandoImage/prometheus-plugin
add generic prometheus plugin
2 parents 5eb39df + 6fa83e8 commit 3b5b9f1

File tree

4 files changed

+122
-2
lines changed

4 files changed

+122
-2
lines changed

README.rst

Lines changed: 54 additions & 1 deletion
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
@@ -80,7 +81,7 @@ On Ubuntu systems 16.04 and newer, you can try running ``systemctl enable newrel
8081
Installing Additional Requirements
8182
----------------------------------
8283

83-
To use the MongoDB the ``mongodb`` library is required. For the pgBouncer or PostgreSQL plugin you must install the ``psycopg2`` library. To easily do
84+
To use the MongoDB the ``mongodb`` library is required. For the pgBouncer or PostgreSQL plugin you must install the ``psycopg2`` library. For the prometheus plugin you must install the ``prometheus_client`` library. To easily do
8485
this, make sure you have the latest version of ``pip`` installed (https://pip.pypa.io/). This should be done after installing the agent itself:
8586

8687
::
@@ -95,6 +96,10 @@ or::
9596

9697
$ pip install newrelic-python-agent[postgresql]
9798

99+
or::
100+
101+
$ pip install newrelic-python-agent[prometheus]
102+
98103
If this does not work for you, make sure you are running a recent copy of ``pip`` (>= 1.3).
99104

100105
Plugin Configuration Stanzas
@@ -313,6 +318,46 @@ E.g.:
313318
superuser: False
314319
relation_stats: False
315320

321+
Prometheus Installation Notes
322+
------------------------
323+
You can monitor any prometheus exporter endpoint via http/https.
324+
Prometheus samples are named by their name and their labels.
325+
E.g. ``http_requests_total{job="apiserver", status=200}`` becomes ``http_requests_total/job/apiserver/status/200``
326+
327+
By default all samples are added as derive values. To treat a sample as gauge value you can use the ``gauges`` configuration value.
328+
E.g.:
329+
330+
::
331+
332+
prometheus:
333+
- name: my-go-app
334+
scheme: http
335+
host: localhost
336+
port: 8080
337+
gauges:
338+
- go_threads
339+
340+
Furthermore the plugin allows you to include and exclude certain samples by their name.
341+
If the ``include`` configuration parameter is not set, all samples are included (except for excluded ones).
342+
If the ``include`` configuration parameter is set, only those samples are included.
343+
If the ``exclude`` configuration parameter is set, those samples are excluded (even if they are listed in the ``include`` configuration parameter).
344+
E.g.:
345+
346+
::
347+
348+
prometheus:
349+
- name: my-go-app
350+
scheme: http
351+
host: localhost
352+
port: 8080
353+
include:
354+
- go_threads
355+
- go_info
356+
exclude:
357+
- go_info
358+
359+
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.
360+
316361
RabbitMQ Installation Notes
317362
---------------------------
318363
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 +507,14 @@ Configuration Example
462507
dbname: postgres
463508
superuser: True
464509

510+
prometheus:
511+
- name: my-go-app
512+
scheme: http
513+
host: localhost
514+
port: 8080
515+
gauges:
516+
- go_threads
517+
465518
rabbitmq:
466519
- name: rabbitmq@localhost
467520
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)