-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added redis metrics decorator * Added metrics db model & migration * Added metrics indexing redis to db * Added metrics routes * Updated comments and formats * Use scan in place of keys redis search * Clean up comments
- Loading branch information
Showing
21 changed files
with
957 additions
and
12 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
discovery-provider/alembic/versions/f54b539b0527_create_metrics_routes_table.py
This file contains 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,41 @@ | ||
"""Create Metrics Routes Table | ||
Revision ID: f54b539b0527 | ||
Revises: 776ca72b16db | ||
Create Date: 2020-07-31 09:41:14.132668 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'f54b539b0527' | ||
down_revision = '776ca72b16db' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.create_table('route_metrics', | ||
sa.Column('route_path', sa.String(), nullable=False), | ||
sa.Column('version', sa.String(), nullable=False), | ||
sa.Column('query_string', sa.String(), nullable=False, default=''), | ||
sa.Column('count', sa.Integer(), nullable=False), | ||
sa.Column('timestamp', sa.DateTime, nullable=False, default=sa.func.now()), | ||
sa.Column('created_at', sa.DateTime, nullable=False, default=sa.func.now()), | ||
sa.Column('updated_at', sa.DateTime, nullable=False, onupdate=sa.func.now()), | ||
sa.PrimaryKeyConstraint('route_path', 'query_string', 'timestamp') | ||
) | ||
op.create_table('app_name_metrics', | ||
sa.Column('application_name', sa.String(), nullable=False), | ||
sa.Column('count', sa.Integer(), nullable=False), | ||
sa.Column('timestamp', sa.DateTime, nullable=False, default=sa.func.now()), | ||
sa.Column('created_at', sa.DateTime, nullable=False, default=sa.func.now()), | ||
sa.Column('updated_at', sa.DateTime, nullable=False, onupdate=sa.func.now()), | ||
sa.PrimaryKeyConstraint('application_name', 'timestamp') | ||
) | ||
|
||
def downgrade(): | ||
op.drop_table('route_metrics') | ||
op.drop_table('app_name_metrics') |
This file contains 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 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 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 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,93 @@ | ||
import logging # pylint: disable=C0302 | ||
from datetime import datetime | ||
from flask import Flask, Blueprint | ||
from flask_restx import Resource, Namespace, fields, reqparse | ||
from src import api_helpers | ||
from src.api.v1.helpers import make_response, success_response, to_dict | ||
from .models.metrics import route_metric, app_name_metric, app_name | ||
from src.queries.get_route_metrics import get_route_metrics | ||
from src.queries.get_app_name_metrics import get_app_name_metrics | ||
from src.queries.get_app_names import get_app_names | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
ns = Namespace('metrics', description='Metrics related operations') | ||
|
||
route_metrics_response = make_response("metrics_reponse", ns, fields.List(fields.Nested(route_metric))) | ||
app_name_response = make_response("app_name_response", ns, fields.List(fields.Nested(app_name))) | ||
app_name_metrics_response = make_response("app_name_metrics_response", ns, fields.List(fields.Nested(app_name_metric))) | ||
|
||
metrics_route_parser = reqparse.RequestParser() | ||
metrics_route_parser.add_argument('path', required=True) | ||
metrics_route_parser.add_argument('query_string', required=False) | ||
metrics_route_parser.add_argument('start_time', required=True, type=int) | ||
metrics_route_parser.add_argument('limit', required=False, type=int) | ||
metrics_route_parser.add_argument('version', required=False, action='append') | ||
|
||
@ns.route("/routes") | ||
class RouteMetrics(Resource): | ||
@ns.expect(metrics_route_parser) | ||
@ns.marshal_with(route_metrics_response) | ||
def get(self): | ||
"""Get the route metrics""" | ||
args = metrics_route_parser.parse_args() | ||
if args.get('limit') is None: | ||
args['limit'] = 48 | ||
else: | ||
args['limit'] = min(args.get('limit'), 48) | ||
try: | ||
args['start_time'] = datetime.utcfromtimestamp(args['start_time']) | ||
except: | ||
return api_helpers.error_response('Poorly formated start_time parameter', 400) | ||
|
||
route_metrics = get_route_metrics(args) | ||
response = success_response(route_metrics) | ||
return response | ||
|
||
|
||
metrics_app_name_list_parser = reqparse.RequestParser() | ||
metrics_app_name_list_parser.add_argument('limit', required=False, type=int) | ||
metrics_app_name_list_parser.add_argument('offset', required=False, type=int) | ||
|
||
@ns.route("/app_name") | ||
class AppNameListMetrics(Resource): | ||
@ns.expect(metrics_app_name_list_parser) | ||
@ns.marshal_with(app_name_response) | ||
def get(self): | ||
"""List all the app names""" | ||
args = metrics_app_name_list_parser.parse_args() | ||
if args.get('limit') is None: | ||
args['limit'] = 100 | ||
else: | ||
args['limit'] = min(args.get('limit'), 100) | ||
if args.get('offset') is None: | ||
args['offset'] = 0 | ||
|
||
app_names = get_app_names(args) | ||
response = success_response(app_names) | ||
return response | ||
|
||
|
||
metrics_app_name_parser = reqparse.RequestParser() | ||
metrics_app_name_parser.add_argument('start_time', required=True, type=int) | ||
metrics_app_name_parser.add_argument('limit', required=False, type=int) | ||
|
||
@ns.route("/app_name/<string:app_name>") | ||
class AppNameMetrics(Resource): | ||
@ns.expect(metrics_app_name_parser) | ||
@ns.marshal_with(app_name_metrics_response) | ||
def get(self, app_name): | ||
"""Get the app name metrics""" | ||
args = metrics_app_name_parser.parse_args() | ||
if args.get('limit') is None: | ||
args['limit'] = 48 | ||
else: | ||
args['limit'] = min(args.get('limit'), 48) | ||
try: | ||
args['start_time'] = datetime.utcfromtimestamp(args['start_time']) | ||
except: | ||
return api_helpers.error_response('Poorly formated start_time parameter', 400) | ||
app_name_metrics = get_app_name_metrics(app_name, args) | ||
response = success_response(app_name_metrics) | ||
return response | ||
|
This file contains 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,17 @@ | ||
from flask_restx import fields | ||
from .common import ns | ||
|
||
route_metric = ns.model('route_metric', { | ||
"route": fields.String, | ||
"timestamp": fields.String, | ||
"count": fields.Integer | ||
}) | ||
|
||
app_name_metric = ns.model('app_name_metric', { | ||
"timestamp": fields.String, | ||
"count": fields.Integer | ||
}) | ||
|
||
app_name = ns.model('app_name', { | ||
"name": fields.String | ||
}) |
This file contains 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 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 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
Oops, something went wrong.