-
Notifications
You must be signed in to change notification settings - Fork 45
Añadir soporte para métricas #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
avara1986
merged 20 commits into
python-microservices:master
from
alexppg:feature/add_metrics_support
Dec 1, 2019
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e7dac80
Add metrics support out of the box
alexppg 0613b32
Split to ease modularization
alexppg cb08921
Add logger metrics
alexppg 8f76186
Add requirements
alexppg b306496
Update pipfile lock
alexppg 4dc4dc6
Delete unnecesary line
alexppg 5b36a90
Deacoplate class of logger function
alexppg 96ccc09
Deacoplate logger metrics from metrics
alexppg 7d771d9
Delete unused imported functions
alexppg 4864eee
Create metrics test
alexppg b18a316
Fix vulnerable dependencies
alexppg b5f235c
Test metrics endpoint
alexppg d47f494
Restore deleted line
alexppg 4b1c507
Migrate metrics implementation to service
alexppg e3f7093
Add metrics documentation
alexppg 421892c
Memoize config per service (#63)
miguelgrubin a0efe72
Merge branch 'master' into feature/add_metrics_support
alexppg 1fb2a02
Add jaeger metrics
alexppg 4948c14
Don't use namespace for metrics
alexppg 2213921
Add jaeger metrics test
alexppg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import time | ||
| import logging | ||
|
|
||
| from flask import Blueprint, Response, request | ||
| from prometheus_client import Counter, Histogram, generate_latest | ||
| from pyms.flask.services.driver import DriverService | ||
|
|
||
| # Based on https://github.com/sbarratt/flask-prometheus | ||
| # and https://github.com/korfuri/python-logging-prometheus/ | ||
|
|
||
| FLASK_REQUEST_LATENCY = Histogram( | ||
| "flask_request_latency_seconds", "Flask Request Latency", ["method", "endpoint"] | ||
| ) | ||
| FLASK_REQUEST_COUNT = Counter( | ||
| "flask_request_count", "Flask Request Count", ["method", "endpoint", "http_status"] | ||
| ) | ||
|
|
||
| LOGGER_TOTAL_MESSAGES = Counter( | ||
| "python_logging_messages_total", | ||
| "Count of log entries by service and level.", | ||
| ["service", "level"], | ||
| ) | ||
|
|
||
|
|
||
| def before_request(): | ||
| request.start_time = time.time() | ||
|
|
||
|
|
||
| def after_request(response): | ||
| request_latency = time.time() - request.start_time | ||
| FLASK_REQUEST_LATENCY.labels(request.method, request.path).observe(request_latency) | ||
| FLASK_REQUEST_COUNT.labels(request.method, request.path, response.status_code).inc() | ||
|
|
||
| return response | ||
|
|
||
|
|
||
| class Service(DriverService): | ||
| service = "metrics" | ||
|
|
||
| def __init__(self, service, *args, **kwargs): | ||
| super().__init__(service, *args, **kwargs) | ||
| self.metrics_blueprint = Blueprint("metrics", __name__) | ||
| self.serve_metrics() | ||
|
|
||
| def monitor(self, app): | ||
| app.before_request(before_request) | ||
| app.after_request(after_request) | ||
|
|
||
| def serve_metrics(self): | ||
| @self.metrics_blueprint.route("/metrics", methods=["GET"]) | ||
| def metrics(): | ||
| return Response( | ||
| generate_latest(), | ||
| mimetype="text/print()lain", | ||
| content_type="text/plain; charset=utf-8", | ||
| ) | ||
|
|
||
| def add_logger_handler(self, logger, service_name): | ||
| logger.addHandler(MetricsLogHandler(service_name)) | ||
| return logger | ||
|
|
||
|
|
||
| class MetricsLogHandler(logging.Handler): | ||
| """A LogHandler that exports logging metrics for Prometheus.io.""" | ||
|
|
||
| def __init__(self, app_name): | ||
| super(MetricsLogHandler, self).__init__() | ||
| self.app_name = app_name | ||
|
|
||
| def emit(self, record): | ||
| LOGGER_TOTAL_MESSAGES.labels(self.app_name, record.levelname).inc() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| pyms: | ||
| metrics: true | ||
| tracer: | ||
| client: "jaeger" | ||
| component_name: "Python Microservice" | ||
| my-ms: | ||
| DEBUG: true | ||
| TESTING: true | ||
| APP_NAME: "Python Microservice" | ||
| APPLICATION_ROOT: / |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| import os | ||
| import unittest.mock | ||
|
|
||
| from flask import current_app | ||
| from prometheus_client import generate_latest | ||
|
|
||
| from pyms.constants import CONFIGMAP_FILE_ENVIRONMENT | ||
| from pyms.flask.app import Microservice | ||
|
|
||
| class TestMetricsFlask(unittest.TestCase): | ||
|
|
||
| BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
|
|
||
| def setUp(self): | ||
| os.environ[CONFIGMAP_FILE_ENVIRONMENT] = os.path.join(self.BASE_DIR, "config-tests-metrics.yml") | ||
| ms = Microservice(service="my-ms", path=__file__) | ||
| self.app = ms.create_app() | ||
| self.client = self.app.test_client() | ||
|
|
||
| def test_metrics_latency(self): | ||
| self.client.get("/") | ||
| self.client.get("/metrics") | ||
| generated_latency_root = b'flask_request_latency_seconds_bucket{endpoint="/",le="0.005",method="GET"}' | ||
| generated_latency_metrics = b'flask_request_latency_seconds_bucket{endpoint="/metrics",le="0.005",method="GET"}' | ||
| assert generated_latency_root in generate_latest() | ||
| assert generated_latency_metrics in generate_latest() | ||
|
|
||
| def test_metrics_count(self): | ||
| self.client.get("/") | ||
| self.client.get("/metrics") | ||
| generated_count_root = b'flask_request_count_total{endpoint="/",http_status="200",method="GET"}' | ||
| generated_count_metrics = b'flask_request_count_total{endpoint="/metrics",http_status="200",method="GET"}' | ||
| assert generated_count_root in generate_latest() | ||
| assert generated_count_metrics in generate_latest() | ||
|
|
||
| def test_metrics_logger(self): | ||
| self.client.get("/") | ||
| self.client.get("/metrics") | ||
| generated_logger = b'python_logging_messages_total{level="INFO",service="Python Microservice With Flask and Lightstep"}' | ||
| assert generated_logger in generate_latest() | ||
|
|
||
| def test_metrics_jaeger(self): | ||
| self.client.get("/") | ||
| self.client.get("/metrics") | ||
| generated_logger = b'jaeger:reporter_spans_total' | ||
| assert generated_logger in generate_latest() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.