Skip to content

Commit

Permalink
feat: make unique queries if resources have same name
Browse files Browse the repository at this point in the history
  • Loading branch information
1101-1 committed Nov 22, 2024
1 parent 8bc5c09 commit 85d6183
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
3 changes: 2 additions & 1 deletion plugins/gcp/fix_plugin_gcp/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> None:
if region := cast(GcpRegion, resource.region()):
resource_queries: List[monitoring.GcpMonitoringQuery] = resource.collect_usage_metrics(builder)
if resource_queries:
lookup_map[resource.id] = resource
# 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
start = builder.metrics_delta
Expand Down
14 changes: 7 additions & 7 deletions plugins/gcp/fix_plugin_gcp/resources/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
GcpMonitoringQuery.create(
query_name="compute.googleapis.com/instance/disk/average_io_queue_depth",
period=delta,
ref_id=self.id,
ref_id=f"{self.kind}/{self.id}/{self.region().id}",
resource_name=self.id,
metric_name=MetricName.VolumeQueueLength,
normalization=normalizer_factory.count,
Expand All @@ -1230,7 +1230,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
GcpMonitoringQuery.create(
query_name=name,
period=delta,
ref_id=self.id,
ref_id=f"{self.kind}/{self.id}/{self.region().id}",
resource_name=self.id,
metric_name=metric_name,
normalization=normalizer_factory.count,
Expand All @@ -1250,7 +1250,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
GcpMonitoringQuery.create(
query_name=name,
period=delta,
ref_id=self.id,
ref_id=f"{self.kind}/{self.id}/{self.region().id}",
resource_name=self.id,
metric_name=metric_name,
normalization=normalizer_factory.count,
Expand Down Expand Up @@ -3627,7 +3627,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
GcpMonitoringQuery.create(
query_name="compute.googleapis.com/instance/cpu/utilization",
period=delta,
ref_id=self.id,
ref_id=f"{self.kind}/{self.id}/{self.region().id}",
resource_name=self.id,
metric_name=MetricName.CpuUtilization,
normalization=normalizer_factory.percent,
Expand All @@ -3642,7 +3642,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
GcpMonitoringQuery.create(
query_name=name,
period=delta,
ref_id=self.id,
ref_id=f"{self.kind}/{self.id}/{self.region().id}",
resource_name=self.id,
metric_name=metric_name,
normalization=normalizer_factory.count,
Expand All @@ -3662,7 +3662,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
GcpMonitoringQuery.create(
query_name=name,
period=delta,
ref_id=self.id,
ref_id=f"{self.kind}/{self.id}/{self.region().id}",
resource_name=self.id,
metric_name=metric_name,
normalization=normalizer_factory.count,
Expand All @@ -3682,7 +3682,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
GcpMonitoringQuery.create(
query_name=name,
period=delta,
ref_id=self.id,
ref_id=f"{self.kind}/{self.id}/{self.region().id}",
resource_name=self.id,
metric_name=metric_name,
normalization=normalizer_factory.count,
Expand Down
4 changes: 3 additions & 1 deletion plugins/gcp/fix_plugin_gcp/resources/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class GcpMonitoringQuery:
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 # reference ID for the resource (e.g., instance ID)
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
Expand Down Expand Up @@ -219,6 +220,7 @@ def update_resource_metrics(
resources_map: Dict[str, V],
monitoring_metric_result: Dict[GcpMonitoringQuery, GcpMonitoringMetricData],
) -> None:
a = None
for query, metric in monitoring_metric_result.items():
resource = resources_map.get(query.ref_id)
if resource is None:
Expand Down
6 changes: 3 additions & 3 deletions plugins/gcp/fix_plugin_gcp/resources/sqladmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
GcpMonitoringQuery.create(
query_name="cloudsql.googleapis.com/database/cpu/utilization",
period=delta,
ref_id=self.id,
ref_id=f"{self.kind}/{self.id}/{self.region().id}",
resource_name=f"{builder.project.id}:{self.id}",
metric_name=MetricName.CpuUtilization,
normalization=normalizer_factory.percent,
Expand All @@ -792,7 +792,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
GcpMonitoringQuery.create(
query_name=name,
period=delta,
ref_id=self.id,
ref_id=f"{self.kind}/{self.id}/{self.region().id}",
resource_name=f"{builder.project.id}:{self.id}",
metric_name=metric_name,
normalization=normalizer_factory.count,
Expand All @@ -813,7 +813,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
GcpMonitoringQuery.create(
query_name=name,
period=delta,
ref_id=self.id,
ref_id=f"{self.kind}/{self.id}/{self.region().id}",
resource_name=f"{builder.project.id}:{self.id}",
metric_name=metric_name,
normalization=normalizer_factory.iops,
Expand Down

0 comments on commit 85d6183

Please sign in to comment.