Customizing monitoring

There are several ways to create custom monitoring scripts that will supply time series data to your Prometheus server. As noted earlier in the Setting up Prometheus section, there are many client libraries available, such as https://github.com/prometheus/client_python.

In the following screenshot, you can see that the preceding project is not very big but does have stars on GitHub.

To use this library, install it using pip (a Python package manager for modules) with the following code:

 $ pip install prometheus_client
Collecting prometheus_client
matplotlib 1.3.1 requires nose, which is not installed.
matplotlib 1.3.1 requires tornado, which is not installed.
Installing collected packages: prometheus-client
Successfully installed prometheus-client-0.6.0

You can also create a simple exporter by running the following code from a Python interpreter or via a file:

 from prometheus_client import start_http_server, Summary
import random
import time
# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
# Decorate function with metric.
@REQUEST_TIME.time()
def process_request(t):
"""A dummy function that takes some time."""
time.sleep(t)
if __name__ == '__main__':
# Start up the server to expose the metrics.
start_http_server(8000)
# Generate some requests.
while True:
process_request(random.random())

The exporter will start on localhost, on port 8000, and the following page will appear when called:

You can add this exporter to your Prometheus server by adding the following code to prometheus.yml and restarting Prometheus with gitlab-ctl restart prometheus on an omnibus-installed GitLab application server, or you can use service prometheus restart on an externally-installed Prometheus:

job_name: 'python_gitlab'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:-
targets: ['localhost:
8000']

You now have the option to modify your own Python application to report metrics, or you can create Python code that gathers metrics from your system. For instance, you may want to parse a log file for certain patterns and accumulate the relevant metrics.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.255.127