-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve prometheus metrics generation reliability
That means it shouldn't crash with multiple request types to the same URI anymore. Additional changes: * Add esptherm_version header containing the current commit hash * Add constexpr uint to str utility * Add esptherm_build_info metric to metrics page * This metrics has the labels esptherm_commit, mcu_type, arduino_version, sdk_version, and cpp_std_version.
- Loading branch information
Showing
9 changed files
with
362 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
from os import path | ||
import subprocess | ||
import sys | ||
from typing import Final | ||
from subprocess import CalledProcessError | ||
|
||
try: | ||
Import ("env") # type: ignore[name-defined] | ||
except: | ||
print("Failed to load platformio environment!", file=sys.stderr) | ||
sys.exit(1) | ||
|
||
# The path of the header file to generated. | ||
version_header_path: Final[str] = path.join(env.subst('$PROJECT_SRC_DIR'), 'generated', 'esptherm_version.h') # type: ignore[name-defined] | ||
|
||
|
||
def check_git(): | ||
"""Checks whether the git command is available. | ||
Tries to run "git --version" and exits the script if it fails. | ||
""" | ||
try: | ||
subprocess.check_call(['git', '--version']) | ||
except CalledProcessError: | ||
print("Could not execute git. Is there a git executable on the PATH?", file=sys.stderr) | ||
sys.exit(1) | ||
|
||
def get_short_hash() -> str: | ||
"""Gets the current commit short-hash. | ||
Runs "git rev-parse --short HEAD" to get the current git commit short-hash. | ||
@return The current commit hash. | ||
@raise CalledProcessError: If the command returns a non-zero exit-code. | ||
""" | ||
return subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], text=True).strip() | ||
|
||
|
||
def generate_version_header(commit: str) -> None: | ||
"""Generates the header file containing the hashes of the static files. | ||
Generates a header file, in src/generated, containing constant definitions with the hashes of the static files. | ||
@raise IOError: If opening or writing the header file fails. | ||
""" | ||
|
||
print("Generating " + path.relpath(version_header_path, env.subst("$PROJECT_ROOT"))) # type: ignore[name-defined] | ||
|
||
with open(version_header_path, 'w') as header: | ||
header.write( | ||
"""/* | ||
* esptherm_version.h | ||
* | ||
* **Warning:** This file is automatically generated, and should not be edited manually. | ||
* | ||
* This file contains version information about the ESP-WiFi-Thermometer. | ||
* At the moment it only contains the commit hash, but once there are versioned releases it will also contain the version numbers. | ||
* | ||
* This project is licensed under the MIT License. | ||
* The MIT license can be found in the project root and at https://opensource.org/licenses/MIT. | ||
*/ | ||
#ifndef SRC_GENERATED_ESPTHERM_VERSION_H_ | ||
#define SRC_GENERATED_ESPTHERM_VERSION_H_ | ||
""") | ||
|
||
header.write( | ||
f"""/** | ||
* The short hash of the ESP-WiFi-Thermometer git commit that was used for this build. | ||
*/ | ||
static constexpr const char *ESPTHERM_COMMIT = "{commit}"; | ||
""") | ||
|
||
header.write("#endif /* SRC_GENERATED_ESPTHERM_VERSION_H_ */" + os.linesep) | ||
|
||
|
||
def main() -> int: | ||
"""The main entrypoint of this script. | ||
The main function generating the version header file. | ||
@return Zero if nothing goes wrong. | ||
@raise IOError: If opening or reading the file fails. | ||
""" | ||
check_git() | ||
commit: str = "" | ||
try: | ||
commit = get_short_hash() | ||
except CalledProcessError as e: | ||
print(f"Failed to determine git hash. Process failed with exit code {e.returncode}.", file=sys.stderr) | ||
return 1 | ||
|
||
generate_version_header(commit) | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == '__main__' or __name__ == 'SCons.Script': | ||
error: int = main() | ||
if error != 0: | ||
sys.exit(error) |
Oops, something went wrong.