Last active
September 27, 2024 18:13
-
-
Save steveandroulakis/09654dc33c73dd4cd8168921e02f27a6 to your computer and use it in GitHub Desktop.
Displays an action count (per second) and total for a time window from a Temporal dev server
This file contains hidden or 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
| import time | |
| import requests | |
| import argparse | |
| from prometheus_client.parser import text_string_to_metric_families | |
| # Prometheus scrape endpoint | |
| PROMETHEUS_SCRAPE_URL = "http://localhost:63626/metrics" | |
| EXCLUDED_NAMESPACE = "temporal_system" # Namespace to exclude | |
| def scrape_metrics(): | |
| """Fetches metrics from the Prometheus endpoint and returns parsed data.""" | |
| response = requests.get(PROMETHEUS_SCRAPE_URL) | |
| response.raise_for_status() # Raise an error if the request failed | |
| return list(text_string_to_metric_families(response.text)) | |
| def get_action_count(metrics_data): | |
| """Extracts and returns the total 'action' count from the parsed metrics data, excluding the specified namespace.""" | |
| total_actions = 0 | |
| for family in metrics_data: | |
| if family.name == 'action': | |
| for sample in family.samples: | |
| # Exclude the specified namespace | |
| if sample.labels.get('namespace') != EXCLUDED_NAMESPACE: | |
| total_actions += sample.value | |
| return total_actions | |
| def print_readme(): | |
| """Prints a README message when no argument is provided.""" | |
| readme_message = """ | |
| Usage: python temporal-dev-server-actions-count.py --time-window-seconds <time_window_in_seconds> | |
| This script monitors Prometheus action metrics and calculates the average actions per second | |
| and the total number of actions over the specified time window, excluding the 'temporal_system' namespace. | |
| Arguments: | |
| --time-window-seconds: The time period in seconds over which the total actions will be | |
| calculated and reported. | |
| Example: | |
| python temporal-dev-server-actions-count.py --time-window-seconds 120 | |
| """ | |
| print(readme_message) | |
| def main(time_window): | |
| print(f"Starting Prometheus action metric monitoring with a time window of {time_window} seconds...") | |
| print(f"Monitoring Prometheus endpoint: {PROMETHEUS_SCRAPE_URL} (excluding namespace: {EXCLUDED_NAMESPACE})") | |
| print(f"Please wait, the total number of actions will be reported after {time_window} seconds...") | |
| last_action_count = None | |
| total_action_delta = 0 | |
| start_time = time.time() | |
| while True: | |
| try: | |
| # Scrape and parse the metrics data | |
| metrics_data = scrape_metrics() | |
| # Get the current action count from the metrics data | |
| current_action_count = get_action_count(metrics_data) | |
| # Calculate the difference between the current and last action count | |
| if last_action_count is not None: | |
| action_delta = current_action_count - last_action_count | |
| total_action_delta += action_delta | |
| actions_per_second = action_delta / 1 # Calculating actions per second | |
| # Report the average actions per second every second | |
| print(f"Current average actions per second: {actions_per_second:.2f}") | |
| # Update the last action count | |
| last_action_count = current_action_count | |
| # Wait for 1 second before the next scrape | |
| time.sleep(1) | |
| # Check if the total time has reached the specified time window | |
| if time.time() - start_time >= time_window: | |
| break | |
| except Exception as e: | |
| print(f"An error occurred: {e}") | |
| break | |
| # Print the total number of actions after the time window | |
| print(f"Total actions in the last {time_window} seconds: {total_action_delta}") | |
| print("Monitoring completed.") | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description="Monitor Prometheus action metrics and calculate average actions per second and total actions over a given time window, excluding the 'temporal_system' namespace.") | |
| parser.add_argument("--time-window-seconds", type=int, help="The time period in seconds to capture the action metrics.") | |
| args = parser.parse_args() | |
| if args.time_window_seconds: | |
| main(args.time_window_seconds) | |
| else: | |
| print_readme() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment