Commit 27b22d9c authored by Daniele Venzano's avatar Daniele Venzano

Some more code for sending metrics to influxdb

parent d8fec85f
.. _monitoring:
Monitoring interface
====================
REST API
--------
service_time
^^^^^^^^^^^^
Time in milliseconds taken to service a request. The tags associated with the request will add more details:
* action: get, post, delete, ...
* object: user, application, container, ...
* user: user name of the authenticated user that performed the request
#!/usr/bin/env bash
\ No newline at end of file
# Copyright (c) 2016, Daniele Venzano
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
class ZoeMetricsBase:
def point(self, name, value, **kwargs):
raise NotImplementedError
......@@ -28,15 +28,16 @@ def init(influxdb_dbname, influxdb_url):
_influxdb_endpoint = influxdb_url + '/write?precision=ms&db=' + influxdb_dbname
def _send_buffer(self):
payload = '\n'.join(self.buffer)
r = requests.post(self.influxdb_endpoint, data=payload)
if r.status_code != 204:
log.error('error writing metrics to influxdb')
self.buffer = []
def _send_buffer():
if _influxdb_endpoint is not None:
payload = '\n'.join(_buffer)
r = requests.post(_influxdb_endpoint, data=payload)
if r.status_code != 204:
log.error('error writing metrics to influxdb')
_buffer.clear()
def point(conf, measurement_name, value, **kwargs):
def point(measurement_name: str, value: int, **kwargs):
ts = time.time()
point_str = measurement_name
for k, v in kwargs.items():
......@@ -46,4 +47,8 @@ def point(conf, measurement_name, value, **kwargs):
_buffer.append(point_str)
if len(_buffer) > 5:
_send_buffer(conf)
_send_buffer()
def time_diff_ms(start: float, end: float) -> int:
return int((end - start) * 1000)
......@@ -13,10 +13,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import time
from werkzeug.exceptions import BadRequest
from flask_restful import Resource, request
from zoe_lib.exceptions import ZoeException, ZoeRestAPIException
from zoe_lib.metrics.influxdb import point, time_diff_ms
from zoe_scheduler.state.manager import StateManager
from zoe_scheduler.platform_manager import PlatformManager
from zoe_scheduler.rest_api.utils import catch_exceptions
......@@ -35,7 +38,8 @@ class ApplicationAPI(Resource):
self.platform = kwargs['platform']
@catch_exceptions
def get(self, application_id):
def get(self, application_id: int):
start = time.time()
calling_user = authenticate(request, self.state)
app = self.state.get_one('application', id=application_id)
......@@ -43,10 +47,15 @@ class ApplicationAPI(Resource):
raise ZoeRestAPIException('No such application', 404)
is_authorized(calling_user, app, 'get')
return app.to_dict(checkpoint=False)
ret = app.to_dict(checkpoint=False)
end = time.time()
point('service_time', time_diff_ms(start, end), action='get', object='application', user=calling_user.name)
return ret
@catch_exceptions
def delete(self, application_id):
def delete(self, application_id: int):
start = time.time()
calling_user = authenticate(request, self.state)
app = self.state.get_one('application', id=application_id)
......@@ -65,6 +74,9 @@ class ApplicationAPI(Resource):
self.state.delete('application', app.id)
self.state.state_updated()
end = time.time()
point('service_time', time_diff_ms(start, end), action='delete', object='application', user=calling_user.name)
return '', 204
......@@ -79,6 +91,7 @@ class ApplicationCollectionAPI(Resource):
@catch_exceptions
def post(self):
start = time.time()
calling_user = authenticate(request, self.state)
try:
......@@ -98,4 +111,7 @@ class ApplicationCollectionAPI(Resource):
app.id = self.state.gen_id()
self.state.new('application', app)
self.state.state_updated()
end = time.time()
point('service_time', time_diff_ms(start, end), action='post', object='application', user=calling_user.name)
return {'application_id': app.id}, 201
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment