[SEBA-284] Pusblishing prometheus counter for gRPC endpoints

Change-Id: I7143b301227b9961363754e83d7ff9826aae6920
diff --git a/lib/xos-genx/xosgenx/targets/grpc_api.xtarget b/lib/xos-genx/xosgenx/targets/grpc_api.xtarget
index dffc347..4abf3dc 100644
--- a/lib/xos-genx/xosgenx/targets/grpc_api.xtarget
+++ b/lib/xos-genx/xosgenx/targets/grpc_api.xtarget
@@ -2,10 +2,12 @@
 import time
 from protos import xos_pb2, xos_pb2_grpc
 from google.protobuf.empty_pb2 import Empty
+from apistats import track_request_time, REQUEST_COUNT
 
 from django.contrib.auth import authenticate as django_authenticate
 from xos.exceptions import *
 from apihelper import XOSAPIHelperMixin, translate_exceptions
+import grpc
 
 class XosService(xos_pb2_grpc.xosServicer, XOSAPIHelperMixin):
     def __init__(self, thread_pool):
@@ -17,41 +19,59 @@
 
 {% for object in proto.messages | sort(attribute='name') %}
 {%- if object.name!='XOSBase' %}
-    @translate_exceptions
+    @translate_exceptions("{{ object.name }}", "List{{ object.name }}")
+    @track_request_time("{{ object.name }}", "List{{ object.name }}")
     def List{{ object.name }}(self, request, context):
       user=self.authenticate(context)
       model=self.get_model("{{ object.name }}")
-      return self.list(model, user)
+      res = self.list(model, user)
+      REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc()
+      return res
 
-    @translate_exceptions
+    @translate_exceptions("{{ object.name }}", "Filter{{ object.name }}")
+    @track_request_time("{{ object.name }}", "Filter{{ object.name }}")
     def Filter{{ object.name }}(self, request, context):
       user=self.authenticate(context)
       model=self.get_model("{{ object.name }}")
-      return self.filter(model, user, request)
+      res = self.filter(model, user, request)
+      REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc()
+      return res
 
-    @translate_exceptions
+    @translate_exceptions("{{ object.name }}", "Get{{ object.name }}")
+    @track_request_time("{{ object.name }}", "Get{{ object.name }}")
     def Get{{ object.name }}(self, request, context):
       user=self.authenticate(context)
       model=self.get_model("{{ object.name }}")
-      return self.get(model, user, request.id)
+      res = self.get(model, user, request.id)
+      REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc()
+      return res
 
-    @translate_exceptions
+    @translate_exceptions("{{ object.name }}", "Create{{ object.name }}")
+    @track_request_time("{{ object.name }}", "Create{{ object.name }}")
     def Create{{ object.name }}(self, request, context):
       user=self.authenticate(context)
       model=self.get_model("{{ object.name }}")
-      return self.create(model, user, request)
+      res = self.create(model, user, request)
+      REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc()
+      return res
 
-    @translate_exceptions
+    @translate_exceptions("{{ object.name }}", "Delete{{ object.name }}")
+    @track_request_time("{{ object.name }}", "Delete{{ object.name }}")
     def Delete{{ object.name }}(self, request, context):
       user=self.authenticate(context)
       model=self.get_model("{{ object.name }}")
-      return self.delete(model, user, request.id)
+      res = self.delete(model, user, request.id)
+      REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc()
+      return res
 
-    @translate_exceptions
+    @translate_exceptions("{{ object.name }}", "Update{{ object.name }}")
+    @track_request_time("{{ object.name }}", "Update{{ object.name }}")
     def Update{{ object.name }}(self, request, context):
       user=self.authenticate(context)
       model=self.get_model("{{ object.name }}")
-      return self.update(model, user, request.id, request, context)
+      res = self.update(model, user, request.id, request, context)
+      REQUEST_COUNT.labels('xos-core', "{{ object.name }}", "List{{ object.name }}", grpc.StatusCode.OK).inc()
+      return res
 {%- endif %}
 {% endfor %}