Skip to content

Commit

Permalink
adding metrics updates
Browse files Browse the repository at this point in the history
Signed-off-by: Paige Patton <[email protected]>
  • Loading branch information
paigerube14 committed Feb 12, 2025
1 parent 0413bd8 commit 84d835f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 51 deletions.
28 changes: 13 additions & 15 deletions src/krkn_lib/elastic/krkn_elastic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import datetime
import logging
import math
import time
Expand Down Expand Up @@ -118,19 +117,10 @@ def upload_metrics_to_elasticsearch(
time_start = time.time()
try:
for metric in raw_data:
if (
isinstance(metric["timestamp"], int)
and isinstance(metric["value"], float)
and isinstance(metric["name"], str)
):

result = self.push_metric(
ElasticMetric(
run_uuid=run_uuid,
name=metric["name"],
created_at=datetime.datetime.now(),
timestamp=int(metric["timestamp"]),
value=float(metric["value"]),
**metric
),
index,
)
Expand All @@ -141,7 +131,8 @@ def upload_metrics_to_elasticsearch(
)

return int(time.time() - time_start)
except Exception:
except Exception as e:
self.safe_logger.error(f'Upload metric exception: {e}')
return -1

def push_alert(self, alert: ElasticAlert, index: str) -> int:
Expand All @@ -159,7 +150,8 @@ def push_alert(self, alert: ElasticAlert, index: str) -> int:
time_start = time.time()
alert.save(using=self.es, index=index)
return int(time.time() - time_start)
except Exception:
except Exception as e:
self.safe_logger.error(f"Push alert exception: {e}")
return -1

def push_metric(self, metric: ElasticMetric, index: str) -> int:
Expand All @@ -177,9 +169,12 @@ def push_metric(self, metric: ElasticMetric, index: str) -> int:
time_start = time.time()
metric.save(using=self.es, index=index)
return int(time.time() - time_start)
except Exception:
except Exception as e:
print('error' +str(e))
self.safe_logger.error(f'Exception pushing metric: {e}')
return -1


def push_telemetry(self, telemetry: ChaosRunTelemetry, index: str):
if not index:
raise Exception("index cannot be None or empty")
Expand All @@ -189,7 +184,7 @@ def push_telemetry(self, telemetry: ChaosRunTelemetry, index: str):
elastic_chaos.save(using=self.es, index=index)
return int(time.time() - time_start)
except Exception as e:
self.safe_logger.info("Elastic push telemetry error: " + str(e))
self.safe_logger.info(f"Elastic push telemetry error: {e}")
return -1

def search_telemetry(self, run_uuid: str, index: str):
Expand All @@ -210,6 +205,7 @@ def search_telemetry(self, run_uuid: str, index: str):
ElasticChaosRunTelemetry(**hit.to_dict()) for hit in result
]
except NotFoundError:
self.safe_logger.error("Search telemetry not found")
return []
return documents

Expand All @@ -229,6 +225,7 @@ def search_alert(self, run_uuid: str, index: str) -> list[ElasticAlert]:
result = search.execute()
documents = [ElasticAlert(**hit.to_dict()) for hit in result]
except NotFoundError:
self.safe_logger.error("Search alert not found")
return []
return documents

Expand All @@ -248,5 +245,6 @@ def search_metric(self, run_uuid: str, index: str) -> list[ElasticMetric]:
result = search.execute()
documents = [ElasticMetric(**hit.to_dict()) for hit in result]
except NotFoundError:
self.safe_logger.error("Search metric not found")
return []
return documents
24 changes: 1 addition & 23 deletions src/krkn_lib/models/elastic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
InnerDoc,
Integer,
Keyword,
Long,
Nested,
Text,
)
Expand Down Expand Up @@ -36,38 +35,17 @@ def __init__(
self.created_at = created_at


class ElasticMetricValue(InnerDoc):
timestamp = Long()
value = Float()

def __init__(self, timestamp: int, value: float, **kwargs):
super().__init__(**kwargs)
self.timestamp = timestamp
self.value = value


class ElasticMetric(Document):
run_uuid = Keyword()
name = Text()
created_at = Date()
timestamp = Long()
value = Float()
timestamp = Date()

def __init__(
self,
run_uuid: str,
name: str,
created_at: datetime,
timestamp: int,
value: float,
**kwargs,
):
super().__init__(**kwargs)
self.run_uuid = run_uuid
self.name = name
self.created_at = created_at
self.timestamp = timestamp
self.value = value


# Telemetry models
Expand Down
2 changes: 1 addition & 1 deletion src/krkn_lib/prometheus/krkn_prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def process_prom_query_in_range(
granularity = math.ceil(
(end_time - start_time).total_seconds() / 11000
)
granularity = granularity if granularity > 0 else 1
granularity = granularity if granularity > 10 else 10
if self.prom_cli:
try:
return self.prom_cli.custom_query_range(
Expand Down
23 changes: 11 additions & 12 deletions src/krkn_lib/tests/test_krkn_elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,24 @@ def test_push_search_alert(self):
def test_push_search_metric(self):
run_uuid = str(uuid.uuid4())
index = "test-push-metric"
timestamp = datetime.datetime.now()
metric_1 = ElasticMetric(
run_uuid=run_uuid,
name="metric_1",
timestamp=100,
value=1.0,
created_at=datetime.datetime.now(),
metricName="metric_1",
timestamp=timestamp,
value=1.0
)
result = self.lib_elastic.push_metric(metric_1, index)
self.assertNotEqual(result, -1)
time.sleep(1)
metrics = self.lib_elastic.search_metric(run_uuid, index)
self.assertEqual(len(metrics), 1)
metric = next(
metric for metric in metrics if metric.name == "metric_1"
)
metric = metrics[0]

self.assertIsNotNone(metric)
self.assertEqual(metric.value, 1.0)
self.assertEqual(metric.timestamp, 100)
self.assertEqual(metric.timestamp, timestamp.strftime('%Y-%m-%dT%H:%M:%S.%f'))
self.assertEqual(metric.run_uuid, run_uuid)
self.assertEqual(metric.name, "metric_1")
self.assertEqual(metric["value"], 1.0)

def test_push_search_telemetry(self):
run_uuid = str(uuid.uuid4())
Expand Down Expand Up @@ -100,16 +98,17 @@ def test_upload_metric_to_elasticsearch(self):
len(self.lib_elastic.search_metric(bad_metric_uuid, index)), 0
)

time_now = datetime.datetime.now()
self.lib_elastic.upload_metrics_to_elasticsearch(
run_uuid=good_metric_uuid,
raw_data=[{"name": name, "timestamp": 10, "value": 3.14}],
raw_data=[{"name": name, "timestamp": time_now, "value": 3.14}],
index=index,
)
time.sleep(1)
metric = self.lib_elastic.search_metric(good_metric_uuid, index)
self.assertEqual(len(metric), 1)
self.assertEqual(metric[0].name, name)
self.assertEqual(metric[0].timestamp, 10)
self.assertEqual(metric[0].timestamp, time_now.strftime('%Y-%m-%dT%H:%M:%S.%f'))
self.assertEqual(metric[0].value, 3.14)

def test_search_alert_not_existing(self):
Expand Down

0 comments on commit 84d835f

Please sign in to comment.