Skip to content

Commit

Permalink
feat: adjusted better
Browse files Browse the repository at this point in the history
  • Loading branch information
1101-1 committed Nov 25, 2024
1 parent 240fd91 commit ace0bc9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
6 changes: 3 additions & 3 deletions plugins/gcp/fix_plugin_gcp/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@

log = logging.getLogger("fix.plugins.gcp")
all_resources: List[Type[GcpResource]] = (
compute.resources
# compute.resources
# + container.resources
# + billing.resources
# + sqladmin.resources
# + storage.resources
# + aiplatform.resources
# + firestore.resources
# + filestore.resources
+ cloudfunctions.resources
cloudfunctions.resources
)


Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions plugins/gcp/fix_plugin_gcp/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 21 additions & 6 deletions plugins/gcp/fix_plugin_gcp/resources/cloudfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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(
Expand Down
14 changes: 1 addition & 13 deletions plugins/gcp/fix_plugin_gcp/resources/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"
Expand All @@ -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,
Expand Down

0 comments on commit ace0bc9

Please sign in to comment.