-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New feature in api server to send every api requests statistics to an…
…alytics including response_time, object_type, response status. Also enabling the contrail log query of vnc api stats through webui Closes-Bug: 1481116 Raise user exception when the UUID is not of the standard format. Closes-Bug: 1486290 Change-Id: Ic1ab3e3fb9dc4620b4610456d6f10db4aa52e51f (cherry picked from commit ebe1ecf) (cherry picked from commit a81b02e) (cherry picked from commit d5bc2e3)
- Loading branch information
Showing
7 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
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
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,70 @@ | ||
# | ||
# Copyright (c) 2015 Juniper Networks, Inc. All rights reserved. | ||
# | ||
|
||
import bottle | ||
from datetime import datetime | ||
|
||
from uve.vnc_api.ttypes import VncApiStats, VncApiStatsLog | ||
from pysandesh.gen_py.sandesh.ttypes import SandeshLevel | ||
|
||
|
||
def log_api_stats(func): | ||
def wrapper(api_server_obj, resource_type, *args, **kwargs): | ||
try: | ||
statistics = VncApiStatistics( | ||
obj_type=resource_type.replace('-', '_')) | ||
response = func(api_server_obj, resource_type, *args, **kwargs) | ||
statistics.response_size = len(str(response)) | ||
statistics.response_code = bottle.response.status_code | ||
return response | ||
except Exception as err_response: | ||
if isinstance(err_response, bottle.HTTPError): | ||
statistics.response_size = len(err_response.body) | ||
statistics.response_code = err_response.status_code | ||
else: | ||
statistics.response_size = 0 | ||
# 520 Unknown Error | ||
statistics.response_code = 520 | ||
raise | ||
finally: | ||
# Collect api stats and send to analytics | ||
statistics.collect() | ||
statistics.sendwith(api_server_obj._sandesh) | ||
return wrapper | ||
|
||
|
||
class VncApiStatistics(object): | ||
def __init__(self, obj_type): | ||
self.obj_type = obj_type | ||
self.response_size = 0 | ||
self.time_start = datetime.now() | ||
|
||
def collect(self): | ||
self.time_finish = datetime.now() | ||
domain_name = bottle.request.headers.get('X-Domain-Name', 'None') | ||
if domain_name.lower() == 'none': | ||
domain_name = 'default-domain' | ||
project_name = bottle.request.headers.get('X-Project-Name', 'None') | ||
if project_name.lower() == 'none': | ||
project_name = 'default-project' | ||
|
||
# Create api stats object | ||
self.api_stats = VncApiStats( | ||
object_type=self.obj_type, | ||
operation_type=bottle.request.method, | ||
user=bottle.request.headers.get('X-User-Name'), | ||
useragent=bottle.request.headers.get('X-Contrail-Useragent', | ||
bottle.request.headers.get('User-Agent')), | ||
remote_ip=bottle.request.headers.get('Host'), | ||
domain_name=domain_name, | ||
project_name=project_name, | ||
response_time_in_usec=(self.time_finish - | ||
self.time_start).total_seconds() * 1000000, | ||
response_size=self.response_size, | ||
response_code=self.response_code, | ||
) | ||
|
||
def sendwith(self, sandesh): | ||
stats_log = VncApiStatsLog(api_stats=self.api_stats, sandesh=sandesh) | ||
stats_log.send(sandesh=sandesh) |
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