diff --git a/plugins/gcp/fix_plugin_gcp/collector.py b/plugins/gcp/fix_plugin_gcp/collector.py index 300bcdbe1..050da8514 100644 --- a/plugins/gcp/fix_plugin_gcp/collector.py +++ b/plugins/gcp/fix_plugin_gcp/collector.py @@ -28,7 +28,7 @@ log = logging.getLogger("fix.plugins.gcp") all_resources: List[Type[GcpResource]] = ( - compute.resources + # compute.resources # + container.resources # + billing.resources # + sqladmin.resources @@ -36,7 +36,7 @@ # + aiplatform.resources # + firestore.resources # + filestore.resources - + cloudfunctions.resources + cloudfunctions.resources ) @@ -175,7 +175,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> None: # set unique GcpMonitoringQuery.ref_id lookup_map[f"{resource.kind}/{resource.id}/{region.id}"] = resource for query in resource_queries: - query_region = query.region or region + query_region = region start = builder.metrics_delta if query.period and query.period < thirty_minutes: start = min(start, two_hours) diff --git a/plugins/gcp/fix_plugin_gcp/resources/base.py b/plugins/gcp/fix_plugin_gcp/resources/base.py index 096d02ebe..3cbfd538a 100644 --- a/plugins/gcp/fix_plugin_gcp/resources/base.py +++ b/plugins/gcp/fix_plugin_gcp/resources/base.py @@ -203,6 +203,15 @@ def _standard_edges(self, node: GcpResourceType, source: Optional[Json] = None) if node._region: self.add_edge(node, node=node._region, reverse=True) return True + if "locations" in node.id: + parts = node.id.split("/") + loc_index = parts.index("locations") + if loc_index + 1 < len(parts): + location_name = parts[loc_index + 1] + if region := self.region_by_name.get(location_name): + node._region = region + self.add_edge(node, node=region, reverse=True) + return True if source is not None: if InternalZoneProp in source: diff --git a/plugins/gcp/fix_plugin_gcp/resources/cloudfunctions.py b/plugins/gcp/fix_plugin_gcp/resources/cloudfunctions.py index 07979e99c..0ddb47ddf 100644 --- a/plugins/gcp/fix_plugin_gcp/resources/cloudfunctions.py +++ b/plugins/gcp/fix_plugin_gcp/resources/cloudfunctions.py @@ -316,11 +316,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer stat=stat, project_id=builder.project.id, metric_filters={ - **( - {"metric.labels.status": "error"} - if metric_name == MetricName.Errors - else {"metric.labels.status": "ok"} - ), + "metric.labels.status": "ok", "resource.labels.function_name": self.resource_raw_name, "resource.labels.region": self.region().id, "resource.type": "cloud_function", @@ -329,10 +325,29 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer for stat in STAT_LIST for name, metric_name in [ ("cloudfunctions.googleapis.com/function/execution_count", MetricName.Invocations), - ("cloudfunctions.googleapis.com/function/execution_count", MetricName.Errors), ] ] ) + queries.extend( + [ + GcpMonitoringQuery.create( + query_name="cloudfunctions.googleapis.com/function/execution_count", + period=delta, + ref_id=f"{self.kind}/{self.id}/{self.region().id}", + metric_name=MetricName.Errors, + normalization=normalizer_factory.count, + stat=stat, + project_id=builder.project.id, + metric_filters={ + "metric.labels.status": "error", + "resource.labels.function_name": self.resource_raw_name, + "resource.labels.region": self.region().id, + "resource.type": "cloud_function", + }, + ) + for stat in STAT_LIST + ] + ) queries.extend( [ GcpMonitoringQuery.create( diff --git a/plugins/gcp/fix_plugin_gcp/resources/monitoring.py b/plugins/gcp/fix_plugin_gcp/resources/monitoring.py index b42827223..84be79249 100644 --- a/plugins/gcp/fix_plugin_gcp/resources/monitoring.py +++ b/plugins/gcp/fix_plugin_gcp/resources/monitoring.py @@ -58,17 +58,13 @@ def get_stat_value(self, key: str) -> Optional[StatName]: class GcpMonitoringQuery: metric_name: Union[str, MetricName] # final name of the metric query_name: str # name of the metric (e.g., GCP metric type) - # resource_name: str # name of resource period: timedelta # period of the metric ref_id: str # A unique identifier for the resource, formatted as `{resource_kind}/{resource_id}/{resource_region}`. # Example: "gcp_instance/12345/us-central1". This is used to uniquely reference resources across kinds and regions. metric_id: str # unique metric identifier (metric_name + instance_id) stat: str # aggregation type, supports ALIGN_MEAN, ALIGN_MAX, ALIGN_MIN - # label_name: str - # metric_lable_query: bool # `metric` by default. can be `resource` label project_id: str # GCP project name normalization: Optional[MetricNormalization] = None # normalization info - region: Optional[GcpRegion] = None metric_filters: Optional[Tuple[Tuple[str, str], ...]] = None # Immutable structure @staticmethod @@ -77,15 +73,11 @@ def create( query_name: str, period: timedelta, ref_id: str, - # resource_name: str, metric_name: Union[str, MetricName], stat: str, - # label_name: str, - # metric_lable_query: bool = True, project_id: str, + metric_filters: Dict[str, str], normalization: Optional[MetricNormalization] = None, - region: Optional[GcpRegion] = None, - metric_filters: Optional[Dict[str, str]] = None, ) -> "GcpMonitoringQuery": # Metric ID generation: metric query name + resource ID + stat metric_id = f"{query_name}/{ref_id}/{stat}" @@ -95,12 +87,8 @@ def create( query_name=query_name, period=period, ref_id=ref_id, - # resource_name=resource_name, metric_id=metric_id, - # label_name=label_name, - # metric_lable_query=metric_lable_query, stat=stat, - region=region, normalization=normalization, project_id=project_id, metric_filters=immutable_filters,