Skip to content

Commit

Permalink
Merge pull request #360 from asmorodskyi/namespace
Browse files Browse the repository at this point in the history
Add namespace tag when dumping cloud stat
  • Loading branch information
asmorodskyi authored Mar 11, 2024
2 parents 38d42f6 + 4f7f774 commit f752dfc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
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)

0 comments on commit f752dfc

Please sign in to comment.