Skip to content

Commit

Permalink
Refactor MetricsClient logging and variable assignments for improved …
Browse files Browse the repository at this point in the history
…clarity
  • Loading branch information
StTysh committed Nov 18, 2024
1 parent 6651a57 commit 92567bd
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 106 deletions.
44 changes: 25 additions & 19 deletions jasmin_metrics_client/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, token: str) -> None:
retry_on_timeout=True,
)
self.search = Search(using=self.es)
logging.info("Elasticsearch client initialized successfully.")
logging.debug("Elasticsearch client initialized successfully.")
except ApiError as e:
raise ApiError(
message=f"Unexpected error initializing Elasticsearch client: {str(e)}",
Expand All @@ -47,22 +47,22 @@ def get_all_metrics(self) -> Optional[List[str]]:
"""
response: Response[Hit]
try:
s = self.search.index("jasmin-metrics-production")
s.aggs.bucket(
search = self.search.index("jasmin-metrics-production")
search.aggs.bucket(
"unique_metrics",
"terms",
field="prometheus.labels.metric_name.keyword",
size=1000,
)
response = s.execute()
response = search.execute()
except ApiError as e:
raise ApiError(
message=f"Unexpected error fetching all metrics: {str(e)}",
meta=e.meta,
body=e.body,
)
if not response.aggregations.unique_metrics.buckets:
logging.info("No matrics found")
logging.debug("No matrics found")
return []

res = [buckets.key for buckets in response.aggregations.unique_metrics.buckets]
Expand All @@ -81,19 +81,19 @@ def get_metric_labels(self, metric_name: str) -> Optional[List[str]]:
response: Response[Hit]

try:
s = self.search.index("jasmin-metrics-production")
s = s.filter(
search = self.search.index("jasmin-metrics-production")
search = search.filter(
"match", **{"prometheus.labels.metric_name.keyword": metric_name}
)
response = s[0:1].execute()
response = search[0:1].execute()
except ApiError as e:
raise ApiError(
message=f"Unexpected error fetching metric {metric_name}: {str(e)}",
meta=e.meta,
body=e.body,
)
if not response.hits:
logging.info(f"No labels found for metric: {metric_name}")
logging.debug(f"No labels found for metric: {metric_name}")
return []
labels = set()
for hit in response:
Expand All @@ -120,18 +120,18 @@ def get_metric(

response: Response[Hit]
try:
s = self.search.index("jasmin-metrics-production")
s = self._build_query(s, metric_name, filters)
s = s[:size]
response = s.execute()
search = self.search.index("jasmin-metrics-production")
search = self._build_query(search, metric_name, filters)
search = search[:size]
response = search.execute()
except ApiError as e:
raise ApiError(
message=f"Unexpected error fetching metric {metric_name}: {str(e)}",
meta=e.meta,
body=e.body,
)
if not response.hits:
logging.info(f"No data found for metric: {metric_name}")
logging.debug(f"No data found for metric: {metric_name}")
return pd.DataFrame()

data: List[Dict[str, float]] = []
Expand All @@ -150,14 +150,20 @@ def get_metric(

@staticmethod
def _build_query(
s: Search, metric_name: str, filters: Optional[dict[str, dict[str, str]]] = None
search: Search,
metric_name: str,
filters: Optional[dict[str, dict[str, str]]] = None,
) -> Search:
"""Helper function to build Elasticsearch query."""
s = s.filter("term", **{"prometheus.labels.metric_name.keyword": metric_name})
search = search.filter(
"term", **{"prometheus.labels.metric_name.keyword": metric_name}
)
if filters:
if "labels" in filters:
for key, value in filters["labels"].items():
s = s.query("term", **{f"prometheus.labels.{key}.keyword": value})
search = search.query(
"term", **{f"prometheus.labels.{key}.keyword": value}
)

if "time" in filters:
time_range = filters["time"]
Expand All @@ -180,7 +186,7 @@ def _build_query(
if end_date > datetime.now():
raise ValueError("The 'end' date cannot be in the future")

s = s.filter(
search = search.filter(
"range",
**{
"@timestamp": {
Expand All @@ -190,4 +196,4 @@ def _build_query(
},
)

return s
return search
Loading

0 comments on commit 92567bd

Please sign in to comment.