mlco2 / codecarbon

Track emissions from Compute and recommend ways to reduce their impact on the environment.
https://mlco2.github.io/codecarbon
MIT License
1.18k stars 178 forks source link

Add project name as a label into prometheus metrics #449

Closed ncarkaci closed 1 year ago

ncarkaci commented 1 year ago

Description: This pull request aims to enhance CodeCarbon Prometheus metric collection by including the project name as a label. Currently, the metrics lack this important contextual information, which can be valuable for monitoring and debugging purposes. By adding the project name as a label, we can easily correlate metrics with specific projects, making it easier to identify and address issues.

Changes Proposed:

Expected Benefits:

Example Result After Changing: Assume we have two project preprocess and execute. If we use project name as label we can differentiate them with project label like below.

from codecarbon import track_emissions

@track_emissions()
def preprocess():
  # your code

@track_emissions()
def execute():
  # your code

In prometheus result you should see two result which divided to function name

# HELP codecarbon_duration Duration from last measure (s)
# TYPE codecarbon_duration gauge
codecarbon_duration{project_name="preprocess"} 7.0
codecarbon_duration{project_name="execute"} 8.0
# HELP codecarbon_emissions Emissions as CO₂-equivalents [CO₂eq] (kg)
# TYPE codecarbon_emissions gauge
codecarbon_emissions{project_name="preprocess"} 3.9615106448922145e-05
codecarbon_emissions{project_name="execute"} 4.549728923307108e-05
# HELP codecarbon_emissions_rate Emissions divided per duration (Kg/s)
# TYPE codecarbon_emissions_rate gauge
codecarbon_emissions_rate{project_name="preprocess"} 5.093587305663857e-06
codecarbon_emissions_rate{project_name="execute"} 5.093526441942135e-06
# HELP codecarbon_cpu_power CPU power (W)
# TYPE codecarbon_cpu_power gauge
codecarbon_cpu_power{project_name="preprocess"} 42.5
codecarbon_cpu_power{project_name="execute"} 42.5
# HELP codecarbon_gpu_power GPU power (W)
# TYPE codecarbon_gpu_power gauge
codecarbon_gpu_power{project_name="preprocess"} 0.0
codecarbon_gpu_power{project_name="execute"} 0.0
# HELP codecarbon_ram_power RAM power (W)
# TYPE codecarbon_ram_power gauge
codecarbon_ram_power{project_name="preprocess"} 5.852972030639648
codecarbon_ram_power{project_name="execute"} 5.852972030639648
# HELP codecarbon_cpu_energy Energy used per CPU (kWh)
# TYPE codecarbon_cpu_energy gauge
codecarbon_cpu_energy{project_name="preprocess"} 9.181379493739871e-05
codecarbon_cpu_energy{project_name="execute"} 0.00010544852167367936
# HELP codecarbon_gpu_energy Energy used per GPU (kWh)
# TYPE codecarbon_gpu_energy gauge
codecarbon_gpu_energy{project_name="preprocess"} 0.0
codecarbon_gpu_energy{project_name="execute"} 0.0
# HELP codecarbon_ram_energy Energy used per RAM (kWh)
# TYPE codecarbon_ram_energy gauge
codecarbon_ram_energy{project_name="preprocess"} 1.2639895008607029e-05
codecarbon_ram_energy{project_name="execute"} 1.4514799617970389e-05
codecarbon_ram_energy{project_name="compute_box_score_class"} 1.3019203165964279e-05
# HELP codecarbon_energy_consumed Sum of cpu_energy, gpu_energy and ram_energy (kW)
# TYPE codecarbon_energy_consumed gauge
codecarbon_energy_consumed{project_name="preprocess"} 0.00010445368994600576
codecarbon_energy_consumed{project_name="execute"} 0.00011996332129164974

Please review and provide feedback on this pull request. Your input is valuable in ensuring that these enhancements are implemented effectively.

benoit-cty commented 1 year ago

Thank you for this contribution !

ncarkaci commented 1 year ago

@inimaz code was reformatted for black format check. You can review it.

inimaz commented 1 year ago

Looks good! Thanks for taking the time for this @ncarkaci