-
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
Changes from 16 commits
e244c90
1140140
ec8bd64
47b0b11
236b812
0771fa1
2e3b0c7
2a1d4b8
367f1e3
b3004eb
7dc52ec
e209cbb
021b0f0
afb1d51
83dd428
d16fba8
a6993f1
2c6ba2c
9a4f81a
069412e
87565f2
d3588fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #!/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" | ||
|
|
||
| if [ -f "${CURRENT_DIR}/.env" ]; then | ||
| echo "Loading environment variables from ${CURRENT_DIR}/.env" | ||
| source "${CURRENT_DIR}/.env" | ||
| else | ||
| echo "No .env file found in ${CURRENT_DIR}. Using default environment variables." | ||
paulineribeyre marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fi | ||
|
|
||
| source "${CURRENT_DIR}/bin/_common_setup.sh" | ||
|
|
||
| poetry run gunicorn \ | ||
| gen3workflow.app:app \ | ||
| -k uvicorn.workers.UvicornWorker \ | ||
| -c gunicorn.conf.py \ | ||
| --reload \ | ||
| --access-logfile - \ | ||
| --error-logfile - | ||
| 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" | ||
paulineribeyre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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" | ||
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from typing import Any, Dict | ||
|
|
||
| from cdispyutils.metrics import BaseMetrics | ||
|
|
||
| from gen3workflow.config import config | ||
|
|
||
|
|
||
| class Metrics(BaseMetrics): | ||
| def __init__(self, prometheus_dir: str, enabled: bool = True) -> None: | ||
|
||
| super().__init__( | ||
| prometheus_dir=config["PROMETHEUS_MULTIPROC_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) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mm the config is only at
/src/gen3-workflow-config.yamlfor cloud-automation deployments. If you run the service locally for example, it's not there.Also i don't see
PROMETHEUS_MULTIPROC_DIRin the default config, should we add it there?and i don't get the reference to
.envsince we don't use that hereUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
PROMETHEUS_MULTIPROC_DIRset before the application starts.For local development, we continue using
python run.py, where the Prometheus directory is set during the initialization of the metrics object..envcomment was an oversight -- I'll update that!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.