AETHER-369 Export Prometheus metrics

Change-Id: I1e06310b17360f99b6aef969e8ecb19bdc96925c
diff --git a/edge-monitoring/Dockerfile.server b/edge-monitoring/Dockerfile.server
index 3648f2b..7b44045 100644
--- a/edge-monitoring/Dockerfile.server
+++ b/edge-monitoring/Dockerfile.server
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License
 
-FROM python:3.7-slim
+FROM python:3.7
 
 WORKDIR /usr/src/app
 COPY requirements.txt ./
diff --git a/edge-monitoring/edge_monitoring_server.py b/edge-monitoring/edge_monitoring_server.py
index ed2088c..41ed675 100755
--- a/edge-monitoring/edge_monitoring_server.py
+++ b/edge-monitoring/edge_monitoring_server.py
@@ -15,7 +15,10 @@
 # limitations under the License.
 
 import time
-from flask import Flask, jsonify, abort, request
+from flask import Flask, jsonify, abort, request, Response
+import prometheus_client as prom
+
+NO_RESULT_THRESHOLD = 720
 
 app = Flask(__name__)
 edges = [
@@ -29,6 +32,43 @@
     }
 ]
 
+status_codes = {
+    "no result": -2,
+    "error": -1,
+    "disconnected": 0,
+    "connecting": 1,
+    "connected": 2
+}
+
+cp_status = prom.Gauge("aetheredge_status_control_plane", "Control plane status code", ["name"])
+up_status = prom.Gauge("aetheredge_status_user_plane", "User plane status code", ["name"])
+last_update = prom.Gauge("aetheredge_last_update", "Last reported test result", ["name"])
+
+def time_out_stale_results():
+    for edge in edges:
+        time_elapsed = time.time() - edge["last_update"]
+        if time_elapsed > NO_RESULT_THRESHOLD:
+            edge['status']['control_plane'] = "no result"
+            edge['status']['user_plane'] = "no result"
+
+
+@app.route('/edges/metrics', methods=['GET'])
+def get_prometheus_metrics():
+    res = []
+    time_out_stale_results()
+    for edge in edges:
+        if edge['name'] == "production-edge-example":
+            continue
+
+        cp_status.labels(edge['name']).set(status_codes[edge['status']['control_plane']])
+        up_status.labels(edge['name']).set(status_codes[edge['status']['user_plane']])
+        last_update.labels(edge['name']).set(edge['last_update'])
+
+    res.append(prom.generate_latest(cp_status))
+    res.append(prom.generate_latest(up_status))
+    res.append(prom.generate_latest(last_update))
+    return Response(res, mimetype="text/plain")
+
 
 @app.route('/edges/healthz', methods=['GET'])
 def get_health():
@@ -37,11 +77,13 @@
 
 @app.route('/edges', methods=['GET'])
 def get_edges():
+    time_out_stale_results()
     return jsonify({'edges': edges})
 
 
 @app.route('/edges/<string:name>', methods=['GET'])
 def get_edge(name):
+    time_out_stale_results()
     edge = [edge for edge in edges if edge['name'] == name]
     if len(edge) == 0:
         abort(404)
diff --git a/edge-monitoring/edge_monitoring_server_k8s.yaml b/edge-monitoring/edge_monitoring_server_k8s.yaml
index 49a21e2..7fb384e 100644
--- a/edge-monitoring/edge_monitoring_server_k8s.yaml
+++ b/edge-monitoring/edge_monitoring_server_k8s.yaml
@@ -24,10 +24,12 @@
     metadata:
       labels:
         app: edge-monitoring-server
+      annotations:
+        field.cattle.io/workloadMetrics: '[{"path":"/edges/metrics","port":80,"schema":"HTTP"}]'
     spec:
       containers:
       - name: server
-        image: docker.io/omecproject/edge-monitoring-server:0.1.0
+        image: docker.io/omecproject/edge-monitoring-server:0.2.0
         imagePullPolicy: Always
         command: ["python", "edge_monitoring_server.py"]
         livenessProbe:
@@ -64,4 +66,4 @@
       - backend:
           serviceName: edge-monitoring-server
           servicePort: 80
-        path: /edges
\ No newline at end of file
+        path: /edges
diff --git a/edge-monitoring/requirements.txt b/edge-monitoring/requirements.txt
index 0410a74..d87fc2e 100644
--- a/edge-monitoring/requirements.txt
+++ b/edge-monitoring/requirements.txt
@@ -2,3 +2,4 @@
 requests
 git+git://github.com/sch3m4/pyadb@master#egg=pyadb
 python-daemon
+prometheus-client