Skip to content

Commit

Permalink
move stat_map to monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias committed Nov 27, 2024
1 parent bd9774d commit 69bf8b2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 25 deletions.
3 changes: 0 additions & 3 deletions plugins/gcp/fix_plugin_gcp/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,6 @@ 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
Expand Down
6 changes: 3 additions & 3 deletions plugins/gcp/fix_plugin_gcp/resources/cloudfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from fix_plugin_gcp.gcp_client import GcpApiSpec
from fix_plugin_gcp.resources.base import GcpResource, GcpDeprecationStatus, GraphBuilder, GcpMonitoringQuery
from fix_plugin_gcp.resources.monitoring import normalizer_factory, STAT_LIST
from fix_plugin_gcp.resources.monitoring import normalizer_factory, STAT_MAP
from fixlib.baseresources import BaseServerlessFunction, MetricName
from fixlib.json_bender import Bender, S, Bend, ForallBend

Expand Down Expand Up @@ -321,7 +321,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
"resource.type": "cloud_function",
},
)
for stat in STAT_LIST
for stat in STAT_MAP
for name, metric_name in [
("cloudfunctions.googleapis.com/function/execution_count", MetricName.Invocations),
]
Expand All @@ -344,7 +344,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
"resource.type": "cloud_function",
},
)
for stat in STAT_LIST
for stat in STAT_MAP
]
)
queries.extend(
Expand Down
16 changes: 8 additions & 8 deletions plugins/gcp/fix_plugin_gcp/resources/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from fix_plugin_gcp.gcp_client import GcpApiSpec, InternalZoneProp
from fix_plugin_gcp.resources.base import GcpResource, GcpDeprecationStatus, GraphBuilder, GcpMonitoringQuery
from fix_plugin_gcp.resources.billing import GcpSku
from fix_plugin_gcp.resources.monitoring import STAT_LIST, normalizer_factory
from fix_plugin_gcp.resources.monitoring import STAT_MAP, normalizer_factory
from fixlib.baseresources import (
BaseAutoScalingGroup,
BaseBucket,
Expand Down Expand Up @@ -1221,7 +1221,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
project_id=builder.project.id,
metric_filters={"metric.labels.device_name": self.id, "resource.labels.zone": self.zone().id},
)
for stat in STAT_LIST
for stat in STAT_MAP
]
)

Expand All @@ -1237,7 +1237,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
project_id=builder.project.id,
metric_filters={"metric.labels.device_name": self.id, "resource.labels.zone": self.zone().id},
)
for stat in STAT_LIST
for stat in STAT_MAP
for name, metric_name in [
("compute.googleapis.com/instance/disk/read_ops_count", MetricName.DiskRead),
("compute.googleapis.com/instance/disk/write_ops_count", MetricName.DiskWrite),
Expand All @@ -1257,7 +1257,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
project_id=builder.project.id,
metric_filters={"metric.labels.device_name": self.id},
)
for stat in STAT_LIST
for stat in STAT_MAP
for name, metric_name in [
("compute.googleapis.com/instance/disk/read_bytes_count", MetricName.DiskRead),
("compute.googleapis.com/instance/disk/write_bytes_count", MetricName.DiskWrite),
Expand Down Expand Up @@ -3634,7 +3634,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
project_id=builder.project.id,
metric_filters={"metric.labels.instance_name": self.id, "resource.labels.zone": self.zone().id},
)
for stat in STAT_LIST
for stat in STAT_MAP
]
)
queries.extend(
Expand All @@ -3649,7 +3649,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
project_id=builder.project.id,
metric_filters={"metric.labels.instance_name": self.id, "resource.labels.zone": self.zone().id},
)
for stat in STAT_LIST
for stat in STAT_MAP
for name, metric_name in [
("compute.googleapis.com/instance/network/received_bytes_count", MetricName.NetworkIn),
("compute.googleapis.com/instance/network/sent_bytes_count", MetricName.NetworkOut),
Expand All @@ -3669,7 +3669,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
project_id=builder.project.id,
metric_filters={"metric.labels.instance_name": self.id, "resource.labels.zone": self.zone().id},
)
for stat in STAT_LIST
for stat in STAT_MAP
for name, metric_name in [
("compute.googleapis.com/instance/disk/read_ops_count", MetricName.DiskRead),
("compute.googleapis.com/instance/disk/write_ops_count", MetricName.DiskWrite),
Expand All @@ -3689,7 +3689,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
project_id=builder.project.id,
metric_filters={"metric.labels.instance_name": self.id, "resource.labels.zone": self.zone().id},
)
for stat in STAT_LIST
for stat in STAT_MAP
for name, metric_name in [
("compute.googleapis.com/instance/disk/read_bytes_count", MetricName.DiskRead),
("compute.googleapis.com/instance/disk/write_bytes_count", MetricName.DiskWrite),
Expand Down
11 changes: 4 additions & 7 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 STAT_MAP, GraphBuilder, GcpResource, GcpMonitoringQuery, MetricNormalization
from fix_plugin_gcp.resources.base import GraphBuilder, GcpResource, GcpMonitoringQuery, MetricNormalization
from fixlib.baseresources import MetricUnit, BaseResource, StatName
from fixlib.durations import duration_str
from fixlib.json import from_json
Expand All @@ -18,9 +18,9 @@
service_name = "monitoring"
log = logging.getLogger("fix.plugins.gcp")
T = TypeVar("T")
V = TypeVar("V", bound=BaseResource)


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


@define(eq=False, slots=False)
Expand Down Expand Up @@ -147,9 +147,6 @@ def _query_for_chunk(
raise e


V = TypeVar("V", bound=BaseResource)


def update_resource_metrics(
resource: GcpResource,
monitoring_metric_result: Dict[GcpMonitoringQuery, GcpMonitoringMetricData],
Expand All @@ -171,7 +168,7 @@ def update_resource_metrics(
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}")
log.warning(f"An error occurred while setting metric values: {e}")
raise


Expand Down
8 changes: 4 additions & 4 deletions plugins/gcp/fix_plugin_gcp/resources/sqladmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from fix_plugin_gcp.gcp_client import GcpApiSpec
from fix_plugin_gcp.resources.base import GcpResource, GcpDeprecationStatus, GraphBuilder, GcpMonitoringQuery
from fix_plugin_gcp.resources.compute import GcpSslCertificate
from fix_plugin_gcp.resources.monitoring import normalizer_factory, STAT_LIST
from fix_plugin_gcp.resources.monitoring import normalizer_factory, STAT_MAP
from fixlib.baseresources import BaseDatabase, DatabaseInstanceStatus, MetricName, ModelReference
from fixlib.json_bender import F, Bender, S, Bend, ForallBend, K, MapEnum, AsInt
from fixlib.types import Json
Expand Down Expand Up @@ -786,7 +786,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
"resource.labels.region": self.region().id,
},
)
for stat in STAT_LIST
for stat in STAT_MAP
]
)
queries.extend(
Expand All @@ -804,7 +804,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
"resource.labels.region": self.region().id,
},
)
for stat in STAT_LIST
for stat in STAT_MAP
for name, metric_name in [
("cloudsql.googleapis.com/database/network/connections", MetricName.DatabaseConnections),
("cloudsql.googleapis.com/database/network/sent_bytes_count", MetricName.NetworkBytesSent),
Expand All @@ -827,7 +827,7 @@ def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuer
"resource.labels.region": self.region().id,
},
)
for stat in STAT_LIST
for stat in STAT_MAP
for name, metric_name in [
("cloudsql.googleapis.com/database/disk/read_ops_count", MetricName.DiskRead),
("cloudsql.googleapis.com/database/disk/write_ops_count", MetricName.DiskWrite),
Expand Down

0 comments on commit 69bf8b2

Please sign in to comment.