Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add namespace tag when dumping cloud stat #360

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions ocw/lib/dump_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,36 @@


def dump_state():
for namespace in PCWConfig.get_namespaces_for('influxdb'):
for namespace in PCWConfig.get_namespaces_for("influxdb"):
try:
providers = PCWConfig.get_providers_for('influxdb', namespace)
logger.info("[%s] Dump state %s", namespace, ','.join(providers))
providers = PCWConfig.get_providers_for("influxdb", namespace)
logger.info("[%s] Dump state %s", namespace, ",".join(providers))
if ProviderChoice.AZURE in providers:
Influx().dump_resource(ProviderChoice.AZURE.value, Influx.VMS_QUANTITY, Azure(namespace).list_instances)
Influx().dump_resource(ProviderChoice.AZURE.value, Influx.IMAGES_QUANTITY, Azure(namespace).list_images)
Influx().dump_resource(ProviderChoice.AZURE.value, Influx.DISK_QUANTITY, Azure(namespace).list_disks)
Influx().dump_resource(ProviderChoice.AZURE.value, Influx.IMAGE_VERSION_QUANTITY, Azure(namespace).get_img_versions_count)
Influx().dump_resource(
ProviderChoice.AZURE.value,
Influx.VMS_QUANTITY,
namespace,
Azure(namespace).list_instances,
)
Influx().dump_resource(
ProviderChoice.AZURE.value,
Influx.IMAGES_QUANTITY,
namespace,
Azure(namespace).list_images,
)
Influx().dump_resource(
ProviderChoice.AZURE.value,
Influx.DISK_QUANTITY,
namespace,
Azure(namespace).list_disks,
)
Influx().dump_resource(
ProviderChoice.AZURE.value,
Influx.IMAGE_VERSION_QUANTITY,
namespace,
Azure(namespace).get_img_versions_count,
)
except Exception:
logger.exception("[%s] Dump state failed!: \n %s", namespace, traceback.format_exc())
logger.exception(
"[%s] Dump state failed!: \n %s", namespace, traceback.format_exc()
)
9 changes: 5 additions & 4 deletions ocw/lib/influx.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Influx:
IMAGES_QUANTITY: str = "images_quantity"
DISK_QUANTITY: str = "disk_quantity"
IMAGE_VERSION_QUANTITY: str = "img_version_quantity"
NAMESPACE_TAG: str = "namespace"

def __init__(self) -> None:
if self.__client is None:
Expand All @@ -39,15 +40,15 @@ def __new__(cls: type["Influx"]) -> "Influx":
cls.instance = super(Influx, cls).__new__(cls)
return cls.instance

def write(self, measurement: str, field: str, value: int) -> None:
def write(self, measurement: str, field: str, value: int, namespace: str) -> None:
if self.__client:
point = Point(measurement).field(field, value)
point = Point(measurement).field(field, value).tag(Influx.NAMESPACE_TAG, namespace)
try:
self.__client.write(bucket=self.bucket, org=self.org, record=point)
except (InfluxDBError, HTTPError) as exception:
logger.warning("Failed to write to influxdb(record=%s): %s", point, exception)

def dump_resource(self, provider: str, field: str, dump_method: Callable) -> None:
def dump_resource(self, provider: str, field: str, namespace: str, dump_method: Callable) -> None:
return_value = dump_method()
if isinstance(return_value, list):
items_cnt = len(return_value)
Expand All @@ -56,4 +57,4 @@ def dump_resource(self, provider: str, field: str, dump_method: Callable) -> Non
else:
raise ValueError(f"{dump_method} returned unsupported type {type(return_value)}")
logger.debug("%s=%d for %s", field, items_cnt, provider)
self.write(provider, field, items_cnt)
self.write(provider, field, items_cnt, namespace=namespace)
Loading