Skip to content

Commit

Permalink
refactor kqueen metrics mechanism.
Browse files Browse the repository at this point in the history
previously i cant get users/orgs metrics with:
'''localhost:5000/metrics/'''

- kqueen metrics updating
- logging for above operation (info level)

Notes:
We export all application metrics on /metrics and this metric can scraped by any external Prometheus instance. There two ways to authenticate to get this endpoint:

1. Provided valid token in header

TOKEN=$(curl -s -H "Content-Type: application/json" --data '{"username":"admin","password":"default"}' -X POST localhost:5000/api/v1/auth | jq -r '.access_token'); echo $TOKEN; curl -H "Authorization: Bearer $TOKEN"
And then go to localhost:5000/metrics

2. Add scraper IP address to PROMETHEUS_WHITELIST configuration and then

Go to {prometheus_ip}:{port}/metrics

review-docs: https://docs.google.com/document/d/1Bnor6D78fVzOndie52wFHVVbgV0daTOHrVP1IgKZvic/edit#

test: travis checks, demo
  • Loading branch information
naumvd95 committed Mar 12, 2018
1 parent 5f97a3f commit 533dde3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -28,13 +28,16 @@ jobs:
- pip install -e ".[dev]"
script:
- export prometheus_multiproc_dir=$(mktemp -d)
- ls -al $prometheus_multiproc_dir
- python3 -m pytest --cov=. --cov-report=term-missing:skip-covered
- ls -al $prometheus_multiproc_dir
- coverage xml
- coveralls
- |
[ ! -z "${CODACY_PROJECT_TOKEN}" ] && python-codacy-coverage -r coverage.xml || echo "Codacy coverage NOT exported"
- pip3 install --editable .
- python3 -m kqueen &
- ls -al $prometheus_multiproc_dir
- sleep 2
- wget -O - http://localhost:5000/api/v1/health
- stage: build
Expand Down
8 changes: 7 additions & 1 deletion kqueen/blueprints/metrics/views.py
Expand Up @@ -5,12 +5,15 @@
from flask_jwt import _jwt_required
from ipaddress import ip_address
from ipaddress import ip_network
from prometheus_client import CollectorRegistry
from kqueen.blueprints.metrics.helpers import MetricUpdater
from prometheus_client import CONTENT_TYPE_LATEST
from prometheus_client import CollectorRegistry
from prometheus_client import generate_latest
from prometheus_client import multiprocess
import logging

metrics = Blueprint('metrics', __name__)
logger = logging.getLogger('kqueen_api')


@metrics.route('/')
Expand All @@ -20,6 +23,7 @@ def root():
if ip_address(request.remote_addr) not in ip_whitelist:
_jwt_required(current_app.config['JWT_DEFAULT_REALM'])

MU = MetricUpdater()
registry = CollectorRegistry()
multiprocess.MultiProcessCollector(registry)

Expand All @@ -28,5 +32,7 @@ def root():
response = make_response(data)
response.headers['Content-Type'] = CONTENT_TYPE_LATEST
response.headers['Content-Length'] = str(len(data))
logger.info('Kqueen metrics updating')
MU.update_metrics()

return response

0 comments on commit 533dde3

Please sign in to comment.