diff --git a/.travis.yml b/.travis.yml index 06de4fed..040b8db6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,8 +3,8 @@ language: go services: - docker script: - - make test - - make docker-build-test upload-coverage + - DEV_REGISTRY="docker.io" make test + - DEV_REGISTRY="docker.io" make docker-build-test upload-coverage notifications: slack: rooms: diff --git a/charts/controller/templates/_helpers.tpl b/charts/controller/templates/_helpers.tpl index 13f91020..73c65ef8 100644 --- a/charts/controller/templates/_helpers.tpl +++ b/charts/controller/templates/_helpers.tpl @@ -83,7 +83,7 @@ env: name: database-creds key: password - name: DRYCC_DATABASE_URL - value: "postgres://$(DRYCC_DATABASE_USER):$(DRYCC_DATABASE_PASSPORT)@$(DRYCC_DATABASE_SERVICE_HOST):$(DRYCC_DATABASE_SERVICE_PORT)/$(DRYCC_DATABASE_USER)" + value: "postgres://$(DRYCC_DATABASE_USER):$(DRYCC_DATABASE_PASSWORD)@$(DRYCC_DATABASE_SERVICE_HOST):$(DRYCC_DATABASE_SERVICE_PORT)/$(DRYCC_DATABASE_USER)" {{- end }} - name: WORKFLOW_NAMESPACE valueFrom: diff --git a/rootfs/api/influxdb.py b/rootfs/api/influxdb.py index 6db257b2..38aa295a 100644 --- a/rootfs/api/influxdb.py +++ b/rootfs/api/influxdb.py @@ -1,28 +1,36 @@ import threading +import logging from typing import Iterator from contextlib import closing from django.conf import settings from influxdb_client import InfluxDBClient from influxdb_client.client.flux_table import FluxRecord +from influxdb_client.rest import ApiException local = threading.local() +logger = logging.getLogger(__name__) def _get_influxdb_client() -> InfluxDBClient: if not hasattr(local, "influxdb_client"): local.influxdb_client = InfluxDBClient( - url=settings.INFLUXDB_URL, - token=settings.INFLUXDB_TOKEN, - org=settings.INFLUXDB_ORG + url=settings.DRYCC_INFLUXDB_URL, + token=settings.DRYCC_INFLUXDB_TOKEN, + org=settings.DRYCC_INFLUXDB_ORG ) return local.influxdb_client def _query_stream(flux_script: str) -> Iterator[FluxRecord]: with closing(_get_influxdb_client()) as client: - with closing(client.query_api()) as query_api: - with closing(query_api.query_stream(flux_script)) as records: - yield from records + try: + query_api = client.query_api() + records = query_api.query_stream(flux_script) + except ApiException as e: + logger.exception(e) + yield from [] + else: + yield from records def query_container_count( diff --git a/rootfs/api/serializers.py b/rootfs/api/serializers.py index c5ba4996..710f2343 100644 --- a/rootfs/api/serializers.py +++ b/rootfs/api/serializers.py @@ -716,3 +716,13 @@ def update(self, instance, validated_data): instance.attach_update() instance.save() return instance + + +class MetricSerializer(serializers.Serializer): + import time + now = int(time.time()) + default_start = (now - now % 3600) - 3600 + default_stop = now - now % 3600 + start = serializers.IntegerField(min_value=946656000, max_value=4102416000, required=False, default=default_start) # noqa + stop = serializers.IntegerField(min_value=946656000, max_value=4102416000, required=False, default=default_stop) # noqa + every = serializers.CharField(max_length=50, required=False, default='5m') # noqa diff --git a/rootfs/api/urls.py b/rootfs/api/urls.py index 2bac67b0..5ed8ac65 100644 --- a/rootfs/api/urls.py +++ b/rootfs/api/urls.py @@ -155,4 +155,6 @@ views.UserView.as_view({'patch': 'enable'})), url(r'^users/(?P[\w.@+-]+)/disable/?$', views.UserView.as_view({'patch': 'disable'})), + url(r'^apps/(?P{})/metrics/(?P[a-z0-9]+(\-[a-z0-9]+)*)?$'.format(settings.APP_URL_REGEX), # noqa + views.MetricView.as_view({'get': 'status'})), ] diff --git a/rootfs/api/views.py b/rootfs/api/views.py index b0b40a62..c1a50dd3 100644 --- a/rootfs/api/views.py +++ b/rootfs/api/views.py @@ -914,7 +914,7 @@ class MetricView(BaseDryccViewSet): """Getting monitoring indicators from influxdb""" def _get_app(self): - app = get_object_or_404(models.App, id=self.kwargs['app_id']) + app = get_object_or_404(models.App, id=self.kwargs['id']) self.check_object_permissions(self.request, app) return app @@ -976,13 +976,22 @@ def status(self, request, **kwargs): ], } """ - app_id, container_type = self._get_app().pk, kwargs['container_type'] - start, stop, every = kwargs['start'], kwargs['stop'], kwargs["every"] - return { + app_id, container_type = self._get_app().id, kwargs['container_type'] + + data = serializers.MetricSerializer(data=self.request.query_params) + if not data.is_valid(): + return Response(data.errors, status=422) + start, stop, every = data.validated_data['start'], data.validated_data[ + 'stop'], data.validated_data["every"] + return Response({ "app_id": app_id, "container_type": container_type, - "container_count": self._get_container_count(app_id, container_type, start, stop), - "cpu_usage_list": self._get_cpus(app_id, container_type, start, stop, every), - "memory": self._get_memory(app_id, container_type, start, stop, every), - "networks": self._get_networks(app_id, container_type, start, stop, every) - } + "container_count": self._get_container_count( + app_id, container_type, start, stop), + "cpu_usage_list": self._get_cpus( + app_id, container_type, start, stop, every), + "memory": self._get_memory( + app_id, container_type, start, stop, every), + "networks": self._get_networks( + app_id, container_type, start, stop, every) + })