Skip to content

Commit

Permalink
feat: added collection for sql instance
Browse files Browse the repository at this point in the history
  • Loading branch information
1101-1 committed Nov 22, 2024
1 parent 7179a4e commit 8bc5c09
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
2 changes: 2 additions & 0 deletions fixlib/fixlib/baseresources.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ def __str__(self) -> str:
DiskQueueDepth = "disk_queue_depth"
NetworkReceiveThroughput = "network_receive_throughput"
NetworkTransmitThroughput = "network_transmit_throughput"
NetworkBytesSent = "network_bytes_sent"
NetworkBytesReceived = "network_bytes_received"

# serverless
Invocations = "invocations"
Expand Down
5 changes: 4 additions & 1 deletion plugins/gcp/fix_plugin_gcp/resources/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class GcpMonitoringQuery:
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
normalization: Optional[MetricNormalization] = None # normalization info
region: Optional[GcpRegion] = None

Expand All @@ -77,6 +78,7 @@ def create(
metric_name: Union[str, MetricName],
stat: str,
label_name: str,
metric_lable_query: bool = True,
normalization: Optional[MetricNormalization] = None,
region: Optional[GcpRegion] = None,
) -> "GcpMonitoringQuery":
Expand All @@ -91,6 +93,7 @@ def create(
resource_name=resource_name,
metric_id=metric_id,
label_name=label_name,
metric_lable_query=metric_lable_query,
stat=stat,
region=region,
normalization=normalization,
Expand Down Expand Up @@ -194,7 +197,7 @@ def _query_for_chunk(
query_result = []
local_api_spec = deepcopy(api_spec)
local_api_spec.request_parameter["filter"] = (
f'metric.type = "{query.query_name}" AND metric.labels.{query.label_name} = "{query.resource_name}"'
f'metric.type = "{query.query_name}" AND {"metric" if query.metric_lable_query else "resource"}.labels.{query.label_name} = "{query.resource_name}"'
)
local_api_spec.request_parameter["aggregation_alignmentPeriod"] = f"{int(query.period.total_seconds())}s"
local_api_spec.request_parameter["aggregation_perSeriesAligner"] = query.stat
Expand Down
66 changes: 65 additions & 1 deletion plugins/gcp/fix_plugin_gcp/resources/sqladmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from fix_plugin_gcp.gcp_client import GcpApiSpec
from fix_plugin_gcp.resources.base import GcpResource, GcpDeprecationStatus, GraphBuilder
from fix_plugin_gcp.resources.compute import GcpSslCertificate
from fixlib.baseresources import BaseDatabase, DatabaseInstanceStatus, ModelReference
from fix_plugin_gcp.resources.monitoring import GcpMonitoringQuery, normalizer_factory, STAT_LIST
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 @@ -766,6 +767,69 @@ def collect_sql_resources(spec: GcpApiSpec, clazz: Type[GcpResource]) -> None:

graph_builder.submit_work(collect_sql_resources, spec, cls)

def collect_usage_metrics(self, builder: GraphBuilder) -> List[GcpMonitoringQuery]:
queries: List[GcpMonitoringQuery] = []
queries = []
delta = builder.metrics_delta
queries.extend(
[
GcpMonitoringQuery.create(
query_name="cloudsql.googleapis.com/database/cpu/utilization",
period=delta,
ref_id=self.id,
resource_name=f"{builder.project.id}:{self.id}",
metric_name=MetricName.CpuUtilization,
normalization=normalizer_factory.percent,
stat=stat,
label_name="database_id",
metric_lable_query=False,
)
for stat in STAT_LIST
]
)
queries.extend(
[
GcpMonitoringQuery.create(
query_name=name,
period=delta,
ref_id=self.id,
resource_name=f"{builder.project.id}:{self.id}",
metric_name=metric_name,
normalization=normalizer_factory.count,
stat=stat,
label_name="database_id",
metric_lable_query=False,
)
for stat in STAT_LIST
for name, metric_name in [
("cloudsql.googleapis.com/database/network/connections", MetricName.DatabaseConnections),
("cloudsql.googleapis.com/database/network/sent_bytes_count", MetricName.NetworkBytesSent),
("cloudsql.googleapis.com/database/network/received_bytes_count", MetricName.NetworkBytesReceived),
]
]
)
queries.extend(
[
GcpMonitoringQuery.create(
query_name=name,
period=delta,
ref_id=self.id,
resource_name=f"{builder.project.id}:{self.id}",
metric_name=metric_name,
normalization=normalizer_factory.iops,
stat=stat,
label_name="database_id",
metric_lable_query=False,
)
for stat in STAT_LIST
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),
]
]
)
return queries

@classmethod
def called_collect_apis(cls) -> List[GcpApiSpec]:
return [
Expand Down

0 comments on commit 8bc5c09

Please sign in to comment.