-
Notifications
You must be signed in to change notification settings - Fork 2
Add prometheus metrics #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
e244c90
Add setup + dependencies and barebones metrics.py
nss10 1140140
set additionalProperties to `True`
nss10 ec8bd64
Update config-default.yaml
nss10 47b0b11
Add api count metrics, add `get_user_id` method
nss10 236b812
Revert get_user_id with `get_token_claims` where exceptions are expected
nss10 0771fa1
Update `app.include_router` to `app.mount` for metrics
nss10 2e3b0c7
Add a debug log
nss10 2a1d4b8
Change metric name
nss10 367f1e3
Add run.sh and respond to PR comments
nss10 b3004eb
Update _common_setup setup_prometheus shell scripts
nss10 7dc52ec
Add metrics unit test and update `pyproject.toml`
nss10 e209cbb
Update poetry lock
nss10 021b0f0
Apply automatic documentation changes
nss10 afb1d51
Uncommenting database part for unit tests
nss10 83dd428
Seperate setting up database to be able to use it only for tests
nss10 d16fba8
Wrap metric tracking within a try-catchi
nss10 a6993f1
Respond to PR comments
nss10 2c6ba2c
Fix unit test
nss10 9a4f81a
Update the position for `_setup_db.sh` in `run.sh`
nss10 069412e
Remove #TODO
nss10 87565f2
Add a comment regardign cloud-auto dependency
nss10 d3588fd
Update comment
paulineribeyre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| echo "ensuring db exists" | ||
|
|
||
| # Get the username, password, host, port, and database name | ||
| db_settings=$(poetry run python $CURRENT_DIR/../gen3workflow/config.py | tail -1) | ||
| if [ -z "${db_settings}" ]; then | ||
| echo "'gen3workflow/config.py' did not return DB settings" | ||
| exit 1 | ||
| fi | ||
| db_settings_array=($db_settings) | ||
| HOST=${db_settings_array[0]} | ||
| PORT=${db_settings_array[1]} | ||
| USER=${db_settings_array[2]} | ||
| PASSWORD=${db_settings_array[3]} | ||
| DB_NAME=${db_settings_array[4]} | ||
|
|
||
| if [ -z "${HOST}" ] || [ -z "${PORT}" ] || [ -z "${USER}" ] || [ -z "${PASSWORD}" ] || [ -z "${DB_NAME}" ]; then | ||
| echo "Failed to extract one or more components from DB settings" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "Extracted database name: ${DB_NAME}" | ||
| echo "Extracted username: ${USER}" | ||
|
|
||
| # Check if the database exists | ||
| # Use the full connection string to connect directly | ||
| if [ "$( PGPASSWORD="${PASSWORD}" psql -h "${HOST}" -p "${PORT}" -U "${USER}" -d postgres -XtAc "SELECT 1 FROM pg_database WHERE datname='${DB_NAME}'" )" = '1' ] | ||
| then | ||
| echo "Database ${DB_NAME} already exists." | ||
| else | ||
| echo "Database ${DB_NAME} does not exist. Creating it..." | ||
| # Connect to the default postgres database to create the new database | ||
| PGPASSWORD="${PASSWORD}" psql -h "${HOST}" -p "${PORT}" -U "${USER}" -d postgres -c "CREATE DATABASE \"${DB_NAME}\";" | ||
| fi | ||
|
|
||
| echo "running db migration with 'poetry run alembic upgrade head'..." | ||
| poetry run alembic upgrade head |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #!/usr/bin/env bash | ||
| set -e | ||
|
|
||
| # Mostly simulates the production run of the app as described in the Dockerfile. | ||
| # Uses Gunicorn, multiple Uvicorn workers | ||
| # Small config overrides for local dev, like hot reload when the code is modified and logs to stdout | ||
|
|
||
| CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" | ||
| echo $CURRENT_DIR | ||
| export ENV="production" | ||
|
|
||
| source "${CURRENT_DIR}/bin/_common_setup.sh" | ||
|
|
||
| #TODO: if we need a DB later, run `source "${CURRENT_DIR}/_setup_db.sh` | ||
| #source "${CURRENT_DIR}/_setup_db.sh | ||
|
|
||
|
|
||
| poetry run gunicorn \ | ||
| gen3workflow.app:app \ | ||
| -k uvicorn.workers.UvicornWorker \ | ||
| -c gunicorn.conf.py \ | ||
| --reload \ | ||
| --access-logfile - \ | ||
| --error-logfile - |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #!/usr/bin/env bash | ||
| # Prepares the prometheus_multiproc_dir folder to store the metrics from separate workers (per PID) | ||
| # | ||
| # This script is called by: | ||
| # Dockerfile & run.py | ||
| # - So local runs setup necessary environment vars and folders for prometheus metrics | ||
| # Test framework in conftest | ||
| # - So test runs setup necessary environment vars and folders for prometheus metrics | ||
|
|
||
| # Usage: | ||
| # ./setup_prometheus [DIR] [true] | ||
|
|
||
| # Default directory if no argument is provided | ||
| DIR=${1:-/var/tmp/prometheus_metrics} | ||
|
|
||
| # Determine whether to wipe the directory (default is to wipe) | ||
| SETUP_DIR=${2:-true} | ||
|
|
||
| set -ex | ||
|
|
||
| if [[ "$SETUP_DIR" == "true" ]]; then | ||
| echo "setting up $PROMETHEUS_MULTIPROC_DIR. clearing existing files, ensuring it exists, chmod 755" | ||
| rm -Rf "$DIR" | ||
| mkdir -p "$DIR" | ||
| chmod 755 "$DIR" | ||
| fi | ||
|
|
||
| if id -u gen3 &>/dev/null; then | ||
| chown "$(id -u gen3)":"$(id -g gen3)" "$DIR" | ||
| fi | ||
|
|
||
| export PROMETHEUS_MULTIPROC_DIR="$DIR" | ||
| echo "PROMETHEUS_MULTIPROC_DIR is $PROMETHEUS_MULTIPROC_DIR" |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ## Metrics | ||
|
|
||
| Metrics can be exposed at a `/metrics` endpoint compatible with Prometheus scraping and visualized in Prometheus or | ||
| Graphana, etc. | ||
|
|
||
| The metrics are defined in `gen3workflow/metrics.py` as follows: | ||
|
|
||
| * **gen3_workflow_api_requests_total**: API requests for made to Gen3-Workflow service. | ||
| * ** **More metrics yet to be decided** ** | ||
|
|
||
| You can [run Prometheus locally](https://github.com/prometheus/prometheus) if you want to test or visualize these. |
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from typing import Any, Dict | ||
|
|
||
| from cdispyutils.metrics import BaseMetrics | ||
| from gen3workflow import logger | ||
|
|
||
| from gen3workflow.config import config | ||
|
|
||
|
|
||
| class Metrics(BaseMetrics): | ||
| def __init__( | ||
| self, | ||
| prometheus_dir: str = config["PROMETHEUS_MULTIPROC_DIR"], | ||
| enabled: bool = True, | ||
| ) -> None: | ||
| logger.info(f"Setting up Metrics with {prometheus_dir=} and {enabled=}") | ||
| super().__init__(prometheus_dir=prometheus_dir, enabled=enabled) | ||
|
|
||
| def add_create_task_api_interaction( | ||
| self, | ||
| **kwargs: Dict[str, Any], | ||
| ) -> None: | ||
| """ | ||
| Add a metric for create_task API interactions | ||
| """ | ||
| self.increment_counter(name="gen3_workflow_tasks_created", labels=kwargs) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.