diff --git a/pyproject.toml b/pyproject.toml index 5a39886..e7aa27a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [tool.poetry] name = "tracarbon" authors = ["Florian Valeye "] -version = "0.6.2" +version = "0.6.3" description = "Tracarbon is a Python library that tracks your device's energy consumption and calculates your carbon emissions." readme = "README.md" license = "Apache-2.0" diff --git a/tracarbon/exporters/datadog_exporter.py b/tracarbon/exporters/datadog_exporter.py index 76e452d..e097744 100755 --- a/tracarbon/exporters/datadog_exporter.py +++ b/tracarbon/exporters/datadog_exporter.py @@ -45,13 +45,16 @@ async def launch(self, metric_generator: MetricGenerator) -> None: """ async for metric in metric_generator.generate(): metric_value = await metric.value() - metric_name = metric.format_name( - metric_prefix_name=self.metric_prefix_name - ) - logger.info( - f"Sending metric[{metric_name}] with value [{metric_value}] and tags{metric.format_tags()} to Datadog." - ) - self.stats.gauge(metric_name, metric_value, tags=metric.format_tags()) + if metric_value: + metric_name = metric.format_name( + metric_prefix_name=self.metric_prefix_name + ) + logger.info( + f"Sending metric[{metric_name}] with value [{metric_value}] and tags{metric.format_tags()} to Datadog." + ) + self.stats.gauge( + metric_name, metric_value, tags=metric.format_tags() + ) @classmethod def get_name(cls) -> str: diff --git a/tracarbon/exporters/json_exporter.py b/tracarbon/exporters/json_exporter.py index 7168f9c..26d8770 100644 --- a/tracarbon/exporters/json_exporter.py +++ b/tracarbon/exporters/json_exporter.py @@ -36,25 +36,27 @@ async def launch(self, metric_generator: MetricGenerator) -> None: :param metric_generator: the metric generator """ async for metric in metric_generator.generate(): - file_exists = os.path.isfile(self.path) - async with aiofiles.open(self.path, "a+") as file: - if file_exists: - await file.write(f",{os.linesep}") - else: - await file.write(f"[{os.linesep}") - await file.write( - ujson.dumps( - { - "timestamp": str(datetime.utcnow()), - "metric_name": metric.format_name( - metric_prefix_name=self.metric_prefix_name - ), - "metric_value": await metric.value(), - "metric_tags": metric.format_tags(), - }, - indent=self.indent, + metric_value = await metric.value() + if metric_value: + file_exists = os.path.isfile(self.path) + async with aiofiles.open(self.path, "a+") as file: + if file_exists: + await file.write(f",{os.linesep}") + else: + await file.write(f"[{os.linesep}") + await file.write( + ujson.dumps( + { + "timestamp": str(datetime.utcnow()), + "metric_name": metric.format_name( + metric_prefix_name=self.metric_prefix_name + ), + "metric_value": metric_value, + "metric_tags": metric.format_tags(), + }, + indent=self.indent, + ) ) - ) @classmethod def get_name(cls) -> str: diff --git a/tracarbon/exporters/prometheus_exporter.py b/tracarbon/exporters/prometheus_exporter.py index b54cce0..00d7f49 100644 --- a/tracarbon/exporters/prometheus_exporter.py +++ b/tracarbon/exporters/prometheus_exporter.py @@ -52,12 +52,13 @@ async def launch(self, metric_generator: MetricGenerator) -> None: [tag.key for tag in metric.tags], ) metric_value = await metric.value() - logger.info( - f"Sending metric[{metric_name}] with value [{metric_value}] and labels{metric.format_tags()} to Prometheus." - ) - self.prometheus_metrics[metric_name].labels( - *[tag.value for tag in metric.tags] - ).set(metric_value) + if metric_value: + logger.info( + f"Sending metric[{metric_name}] with value [{metric_value}] and labels{metric.format_tags()} to Prometheus." + ) + self.prometheus_metrics[metric_name].labels( + *[tag.value for tag in metric.tags] + ).set(metric_value) @classmethod def get_name(cls) -> str: diff --git a/tracarbon/exporters/stdout.py b/tracarbon/exporters/stdout.py index 5fcab54..ba3fb82 100755 --- a/tracarbon/exporters/stdout.py +++ b/tracarbon/exporters/stdout.py @@ -15,9 +15,11 @@ async def launch(self, metric_generator: MetricGenerator) -> None: :param metric_generator: the metric generator """ async for metric in metric_generator.generate(): - logger.info( - f"Metric name[{metric.format_name(metric_prefix_name=self.metric_prefix_name)}], value[{await metric.value()}], tags{metric.format_tags()}" - ) + metric_value = await metric.value() + if metric_value: + logger.info( + f"Metric name[{metric.format_name(metric_prefix_name=self.metric_prefix_name)}], value[{metric_value}], tags{metric.format_tags()}" + ) @classmethod def get_name(cls) -> str: