Skip to content

Commit

Permalink
feat: resolved issues
Browse files Browse the repository at this point in the history
  • Loading branch information
1101-1 committed Nov 27, 2024
1 parent 6d50044 commit bd9774d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
20 changes: 8 additions & 12 deletions plugins/gcp/fix_plugin_gcp/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,14 @@ def get_last_run() -> Optional[datetime]:
def collect_usage_metrics(self, builder: GraphBuilder) -> None:
for resource in builder.graph.nodes:
if isinstance(resource, GcpResource) and (mq := resource.collect_usage_metrics(builder)):

def collect_and_set_metrics() -> None:
start_at = builder.created_at - builder.metrics_delta
region = cast(GcpRegion, resource.region())
try:
rb = builder.for_region(region)
result = monitoring.GcpMonitoringMetricData.query_for(rb, mq, start_at, builder.created_at)
monitoring.update_resource_metrics(resource, result)
except Exception as e:
log.warning(f"Error occurred in region {region}: {e}")

builder.submit_work(collect_and_set_metrics)
start_at = builder.created_at - builder.metrics_delta
region = cast(GcpRegion, resource.region())
try:
rb = builder.for_region(region)
result = monitoring.GcpMonitoringMetricData.query_for(rb, mq, start_at, builder.created_at)
monitoring.update_resource_metrics(resource, result)
except Exception as e:
log.warning(f"Error occurred in region {region}: {e}")

def remove_unconnected_nodes(self, builder: GraphBuilder) -> None:
def rm_leaf_nodes(clazz: Any, ignore_kinds: Optional[Type[Any]] = None) -> None:
Expand Down
6 changes: 3 additions & 3 deletions plugins/gcp/fix_plugin_gcp/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,12 @@ def for_region(self, region: GcpRegion) -> GraphBuilder:
)


STAT_MAP: Dict[str, StatName] = {"ALIGN_MIN": StatName.min, "ALIGN_MEAN": StatName.avg, "ALIGN_MAX": StatName.max}


@frozen(kw_only=True)
class MetricNormalization:
unit: MetricUnit
stat_map: frozendict[str, StatName] = frozendict(
{"ALIGN_MIN": StatName.min, "ALIGN_MEAN": StatName.avg, "ALIGN_MAX": StatName.max}
)
normalize_value: Callable[[float], float] = lambda x: x
compute_stats: Callable[[List[float]], List[Tuple[float, Optional[StatName]]]] = lambda x: [(sum(x) / len(x), None)]

Expand Down
4 changes: 2 additions & 2 deletions plugins/gcp/fix_plugin_gcp/resources/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from attr import define, field

from fix_plugin_gcp.gcp_client import GcpApiSpec
from fix_plugin_gcp.resources.base import GraphBuilder, GcpResource, GcpMonitoringQuery, MetricNormalization
from fix_plugin_gcp.resources.base import STAT_MAP, GraphBuilder, GcpResource, GcpMonitoringQuery, MetricNormalization
from fixlib.baseresources import MetricUnit, BaseResource, StatName
from fixlib.durations import duration_str
from fixlib.json import from_json
Expand Down Expand Up @@ -168,7 +168,7 @@ def update_resource_metrics(
continue
name = metric_name + "_" + normalizer.unit
value = normalizer.normalize_value(metric_value)
stat_name = maybe_stat_name or normalizer.stat_map[query.stat]
stat_name = maybe_stat_name or STAT_MAP[query.stat]
resource._resource_usage[name][str(stat_name)] = value
except KeyError as e:
log.warning(f"An error occured while setting metric values: {e}")
Expand Down

0 comments on commit bd9774d

Please sign in to comment.