Skip to content

Commit

Permalink
Merge pull request #156 from fvaleye/feature/not-launch-none-metric
Browse files Browse the repository at this point in the history
Avoid sending None metric value
  • Loading branch information
fvaleye authored Mar 22, 2023
2 parents 1049e44 + 3e3f22f commit 9c90ee2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 35 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "tracarbon"
authors = ["Florian Valeye <[email protected]>"]
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"
Expand Down
17 changes: 10 additions & 7 deletions tracarbon/exporters/datadog_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
38 changes: 20 additions & 18 deletions tracarbon/exporters/json_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 7 additions & 6 deletions tracarbon/exporters/prometheus_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 5 additions & 3 deletions tracarbon/exporters/stdout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 9c90ee2

Please sign in to comment.