diff --git a/src/orchestrator/api/views.py b/src/orchestrator/api/views.py index e8f6c158..7e176a29 100644 --- a/src/orchestrator/api/views.py +++ b/src/orchestrator/api/views.py @@ -131,6 +131,7 @@ class Stats(object): "incomingTransactionResponseSize": 0, "incomingTransactionErrors": 0, "serviceTime": 0, + "serviceTimeTotal": 0, "outgoingTransactions": 0, "outgoingTransactionRequestSize": 0, "outgoingTransactionResponseSize": 0, @@ -146,12 +147,13 @@ def collectMetrics(self, service_start, service_name, subservice_name, flow_metrics = flow.getFlowMetrics() else: flow_metrics = { - "serviceTime": 0, - "outgoingTransactions": 0, - "outgoingTransactionRequestSize": 0, - "outgoingTransactionResponseSize": 0, - "outgoingTransactionErrors": 0, - } + "serviceTime": 0, + "serviceTimeTotal": 0, + "outgoingTransactions": 0, + "outgoingTransactionRequestSize": 0, + "outgoingTransactionResponseSize": 0, + "outgoingTransactionErrors": 0, + } if service_name and not service_name in self.service: self.service[service_name] = { @@ -161,6 +163,7 @@ def collectMetrics(self, service_start, service_name, subservice_name, "incomingTransactionResponseSize": 0, "incomingTransactionErrors": 0, "serviceTime": 0, + "serviceTimeTotal": 0, "outgoingTransactions": 0, "outgoingTransactionRequestSize": 0, "outgoingTransactionResponseSize": 0, @@ -177,13 +180,13 @@ def collectMetrics(self, service_start, service_name, subservice_name, "incomingTransactionResponseSize": 0, "incomingTransactionErrors": 0, "serviceTime": 0, + "serviceTimeTotal": 0, "outgoingTransactions": 0, "outgoingTransactionRequestSize": 0, "outgoingTransactionResponseSize": 0, "outgoingTransactionErrors": 0, } - # TODO: # Analize "response"" to know if is a Response about an error or not if response.status_code not in [200, 201, 204]: # API error @@ -198,13 +201,13 @@ def collectMetrics(self, service_start, service_name, subservice_name, self.service[service_name]["subservs"][subservice_name]["incomingTransactionErrors"] += 1 self.service[service_name]["subservs"][subservice_name]["incomingTransactionRequestSize"] += len(json.dumps(request.data)) self.service[service_name]["subservs"][subservice_name]["incomingTransactionResponseSize"] += len(json.dumps(response.data)) - self.service[service_name]["subservs"][subservice_name]["serviceTime"] += (service_stop - service_start) + self.service[service_name]["subservs"][subservice_name]["serviceTimeTotal"] += (service_stop - service_start) self.service[service_name]["subservs"][subservice_name]["outgoingTransactions"] += flow_metrics["outgoingTransactions"] self.service[service_name]["subservs"][subservice_name]["outgoingTransactionRequestSize"] += flow_metrics["outgoingTransactionRequestSize"] self.service[service_name]["subservs"][subservice_name]["outgoingTransactionResponseSize"] += flow_metrics["outgoingTransactionResponseSize"] self.service[service_name]["subservs"][subservice_name]["outgoingTransactionErrors"] += flow_metrics["outgoingTransactionErrors"] - self.service[service_name]["subservs"][subservice_name]["serviceTime"] += flow_metrics["serviceTime"] + self.service[service_name]["subservs"][subservice_name]["serviceTimeTotal"] += flow_metrics["serviceTimeTotal"] @@ -215,12 +218,12 @@ def collectMetrics(self, service_start, service_name, subservice_name, self.service[service_name]["sum"]["incomingTransactionErrors"] += 1 self.service[service_name]["sum"]["incomingTransactionRequestSize"] += len(json.dumps(request.data)) self.service[service_name]["sum"]["incomingTransactionResponseSize"] += len(json.dumps(response.data)) - self.service[service_name]["sum"]["serviceTime"] += (service_stop - service_start) + self.service[service_name]["sum"]["serviceTimeTotal"] += (service_stop - service_start) self.service[service_name]["sum"]["outgoingTransactions"] += flow_metrics["outgoingTransactions"] self.service[service_name]["sum"]["outgoingTransactionRequestSize"] += flow_metrics["outgoingTransactionRequestSize"] self.service[service_name]["sum"]["outgoingTransactionResponseSize"] += flow_metrics["outgoingTransactionResponseSize"] self.service[service_name]["sum"]["outgoingTransactionErrors"] += flow_metrics["outgoingTransactionErrors"] - self.service[service_name]["sum"]["serviceTime"] += flow_metrics["serviceTime"] + self.service[service_name]["sum"]["serviceTimeTotal"] += flow_metrics["serviceTimeTotal"] # Sum if not transactionError: @@ -229,12 +232,12 @@ def collectMetrics(self, service_start, service_name, subservice_name, self.sum["incomingTransactionErrors"] += 1 self.sum["incomingTransactionRequestSize"] += len(json.dumps(request.data)) self.sum["incomingTransactionResponseSize"] += len(json.dumps(response.data)) - self.sum["serviceTime"] += (service_stop - service_start) + self.sum["serviceTimeTotal"] += (service_stop - service_start) self.sum["outgoingTransactions"] += flow_metrics["outgoingTransactions"] self.sum["outgoingTransactionRequestSize"] += flow_metrics["outgoingTransactionRequestSize"] self.sum["outgoingTransactionResponseSize"] += flow_metrics["outgoingTransactionResponseSize"] self.sum["outgoingTransactionErrors"] += flow_metrics["outgoingTransactionErrors"] - self.sum["serviceTime"] += flow_metrics["serviceTime"] + self.sum["serviceTimeTotal"] += flow_metrics["serviceTimeTotal"] def resetMetrics(self): @@ -245,6 +248,7 @@ def resetMetrics(self): "incomingTransactionResponseSize": 0, "incomingTransactionErrors": 0, "serviceTime": 0, + "serviceTimeTotal": 0, "outgoingTransactions": 0, "outgoingTransactionRequestSize": 0, "outgoingTransactionResponseSize": 0, @@ -252,6 +256,40 @@ def resetMetrics(self): } + def composeMetrics(self): + + result = { + "service": self.service, + "sum": self.sum + } + + for serv in result["service"]: + if result["service"][serv]["sum"]["serviceTimeTotal"] > 0: + result["service"][serv]["sum"]["serviceTime"] = result["service"][serv]["sum"]["serviceTimeTotal"] / ( + result["service"][serv]["sum"]["incomingTransactions"] + + result["service"][serv]["sum"]["incomingTransactionErrors"] + + result["service"][serv]["sum"]["outgoingTransactions"] + + result["service"][serv]["sum"]["outgoingTransactionErrors"] + ) + + for subserv in result["service"][serv]["subservs"]: + if result["service"][serv]["subservs"][subserv]["serviceTimeTotal"] > 0: + result["service"][serv]["subservs"][subserv]["serviceTime"] = result["service"][serv]["subservs"][subserv]["serviceTimeTotal"] / ( + result["service"][serv]["subservs"][subserv]["incomingTransactions"] + + result["service"][serv]["subservs"][subserv]["incomingTransactionErrors"] + + result["service"][serv]["subservs"][subserv]["outgoingTransactions"] + + result["service"][serv]["subservs"][subserv]["outgoingTransactionErrors"] + ) + + if result["sum"]["serviceTimeTotal"] > 0: + result["sum"]["serviceTime"] = result["sum"]["serviceTimeTotal"] / ( + result["sum"]["incomingTransactions"] + + result["sum"]["incomingTransactionErrors"] + + result["sum"]["outgoingTransactions"] + + result["sum"]["outgoingTransactionErrors"] + ) + return result + class IoTConf(Stats): throttle_classes = (AnonRateThrottle,) @@ -3033,10 +3071,8 @@ def get(self, request): reset = request.GET.get('reset', False) == "true" try: - result = { - "service": self.service, - "sum": self.sum - } + result = self.composeMetrics() + if reset: self.resetMetrics() @@ -3061,10 +3097,7 @@ def delete(self, request): CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: - result = { - "service" : self.service, - "sum" : self.sum - } + result = self.composeMetrics() self.resetMetrics() response = Response(result, status=status.HTTP_204_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) diff --git a/src/orchestrator/common/util.py b/src/orchestrator/common/util.py index 9fe3ca7f..7f63d8b9 100644 --- a/src/orchestrator/common/util.py +++ b/src/orchestrator/common/util.py @@ -70,6 +70,7 @@ def __init__(self, self.service = {} self.sum = { "serviceTime": 0, + "serviceTimeTotal": 0, "outgoingTransactions": 0, "outgoingTransactionRequestSize": 0, "outgoingTransactionResponseSize": 0, @@ -271,7 +272,7 @@ def collectOutgoingMetrics(self, service_start, data_request, response): self.sum["outgoingTransactionErrors"] += 1 self.sum["outgoingTransactionRequestSize"] += len(json.dumps(data_request)) self.sum["outgoingTransactionResponseSize"] += len(json.dumps(data_response)) - self.sum["serviceTime"] += (service_stop - service_start) + self.sum["serviceTimeTotal"] += (service_stop - service_start) except Exception, ex: self.logger.error("ERROR collecting outgoing metrics %s", ex) diff --git a/src/orchestrator/core/flow/base.py b/src/orchestrator/core/flow/base.py index a555fa08..20bf858d 100644 --- a/src/orchestrator/core/flow/base.py +++ b/src/orchestrator/core/flow/base.py @@ -122,6 +122,7 @@ def __init__(self, self.sum = { "serviceTime": 0, + "serviceTimeTotal": 0, "outgoingTransactions": 0, "outgoingTransactionRequestSize": 0, "outgoingTransactionResponseSize": 0,