Skip to content

Commit

Permalink
fix bug when adding labels to metrics without label filters (#1524)
Browse files Browse the repository at this point in the history
* fix bug when adding labels to metrics without label filters

* fix bug when adding labels to metrics without label filters
  • Loading branch information
arikalon1 committed Aug 9, 2024
1 parent 853dcdf commit 621a000
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
14 changes: 10 additions & 4 deletions src/robusta/core/playbooks/prometheus_enrichment_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
import math
import re
from collections import defaultdict, namedtuple
from datetime import datetime, timedelta
from string import Template
Expand All @@ -25,6 +25,7 @@
ResourceKey = Tuple[ResourceChartResourceType, ResourceChartItemType]
ChartLabelFactory = Callable[[int], str]
ChartOptions = namedtuple("ChartOptions", ["query", "values_format"])
BRACKETS_COMMA_PATTERN = r"\{\s*,"


class XAxisLine(BaseModel):
Expand Down Expand Up @@ -87,8 +88,9 @@ def run_prometheus_query_range(
prom = get_prometheus_connect(prometheus_params)
params = {"timeout": PROMETHEUS_REQUEST_TIMEOUT_SECONDS}
prom.check_prometheus_connection(params)
result = prom.safe_custom_query_range(query=promql_query, start_time=starts_at, end_time=ends_at, step=step,
params=params)
result = prom.safe_custom_query_range(
query=promql_query, start_time=starts_at, end_time=ends_at, step=step, params=params
)

return PrometheusQueryResult(data=result)

Expand Down Expand Up @@ -378,7 +380,11 @@ def run_prometheus_query(prometheus_params: PrometheusParams, query: str) -> Pro
def __add_additional_labels(query: str, prometheus_params: PrometheusParams) -> str:
if not prometheus_params.prometheus_additional_labels or not prometheus_params.add_additional_labels:
return query
return query.replace("}", __get_additional_labels_str(prometheus_params) + "}")
fixed_query = query.replace("}", __get_additional_labels_str(prometheus_params) + "}")
fixed_query = re.sub(
BRACKETS_COMMA_PATTERN, "{", fixed_query
) # fix the case no labels in query, which results in " {, cluster="bla"} which is illegal
return fixed_query


def __get_additional_labels_str(prometheus_params: PrometheusParams) -> str:
Expand Down
8 changes: 4 additions & 4 deletions src/robusta/core/sinks/robusta/prometheus_discovery_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ def _get_query_prometheus_value(self, query: str) -> Optional[float]:
global_config = self.__global_config
prometheus_params = PrometheusParams(**global_config)
query_result = run_prometheus_query(prometheus_params=prometheus_params, query=query)
if query_result.result_type == "error" or query_result.vector_result is None:
logging.error(f"PrometheusDiscoveryUtils failed to get prometheus results.")
if query_result.result_type == "error" or not query_result.vector_result:
logging.error("PrometheusDiscoveryUtils failed to get prometheus results.")
return
value = query_result.vector_result[0].value.value
return_value = float('%.2f' % float(value))
return_value = float("%.2f" % float(value))
return return_value if return_value >= 0 else None
except:
logging.exception(f"PrometheusDiscoveryUtils failed to get prometheus results.")
logging.exception("PrometheusDiscoveryUtils failed to get prometheus results.")
return

def __run_checks(self):
Expand Down

0 comments on commit 621a000

Please sign in to comment.