Skip to content

Commit e244c90

Browse files
committed
Add setup + dependencies and barebones metrics.py
1 parent 554fc3e commit e244c90

File tree

8 files changed

+359
-166
lines changed

8 files changed

+359
-166
lines changed

bin/_common_setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set -e
77
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
88

99
# Source the environment variables from the metrics setup script
10-
# source "${CURRENT_DIR}/setup_prometheus"
10+
source "${CURRENT_DIR}/setup_prometheus"
1111

1212
echo "installing dependencies with 'poetry install -vv'..."
1313
poetry install -vv

bin/setup_prometheus

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
# Prepares the prometheus_multiproc_dir folder to store the metrics from separate workers (per PID)
3+
#
4+
# This script is called by:
5+
# Dockerfile & run.py
6+
# - So local runs setup necessary environment vars and folders for prometheus metrics
7+
# Test framework in conftest
8+
# - So test runs setup necessary environment vars and folders for prometheus metrics
9+
10+
# Usage:
11+
# ./setup_prometheus [DIR] [true]
12+
13+
# Default directory if no argument is provided
14+
DIR=${1:-/var/tmp/prometheus_metrics}
15+
16+
# Determine whether to wipe the directory (default is to wipe)
17+
SETUP_DIR=${2:-true}
18+
19+
set -ex
20+
21+
if [[ "$SETUP_DIR" == "true" ]]; then
22+
echo "setting up $PROMETHEUS_MULTIPROC_DIR. clearing existing files, ensuring it exists, chmod 755"
23+
rm -Rf "$DIR"
24+
mkdir -p "$DIR"
25+
chmod 755 "$DIR"
26+
fi
27+
28+
if id -u nginx &>/dev/null; then
29+
chown "$(id -u nginx)":"$(id -g nginx)" "$DIR"
30+
fi
31+
32+
export PROMETHEUS_MULTIPROC_DIR="$DIR"
33+
echo "PROMETHEUS_MULTIPROC_DIR is $PROMETHEUS_MULTIPROC_DIR"

docs/metrics.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## Metrics
2+
3+
Metrics can be exposed at a `/metrics` endpoint compatible with Prometheus scraping and visualize in Prometheus or
4+
Graphana, etc.
5+
6+
The metrics are defined in `gen3workflow/metrics.py` as follows:
7+
8+
* **gen3_workflow_api_requests_total**: API requests for made to Gen3-Workflow service.
9+
* ** **More metrics yet to be decided** **
10+
11+
You can [run Prometheus locally](https://github.com/prometheus/prometheus) if you want to test or visualize these.

gen3workflow/app.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from gen3workflow import logger
1010
from gen3workflow.config import config
11+
from gen3workflow.metrics import Metrics
1112
from gen3workflow.routes.ga4gh_tes import router as ga4gh_tes_router
1213
from gen3workflow.routes.s3 import router as s3_router
1314
from gen3workflow.routes.storage import router as storage_router
@@ -54,6 +55,16 @@ def get_app(httpx_client=None) -> FastAPI:
5455
logger=get_logger("gen3workflow.gen3authz", log_level=log_level),
5556
)
5657

58+
logger.info(
59+
f"Setting up Metrics with ENABLE_PROMETHEUS_METRICS flag set to {config['ENABLE_PROMETHEUS_METRICS']}"
60+
)
61+
app.metrics = Metrics(
62+
enabled=config["ENABLE_PROMETHEUS_METRICS"],
63+
prometheus_dir=config["PROMETHEUS_MULTIPROC_DIR"],
64+
)
65+
if app.metrics.enabled:
66+
app.include_router("/metrics", app.metrics.get_asgi_app())
67+
5768
return app
5869

5970

gen3workflow/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ def validate_top_level_configs(self) -> None:
6161
"DB_CONNECTION_STRING": {"type": "string"},
6262
"TASK_IMAGE_WHITELIST": {"type": "array", "items": {"type": "string"}},
6363
"TES_SERVER_URL": {"type": "string"},
64+
"ENABLE_PROMETHEUS_METRICS": {"type": "boolean", "default": False},
65+
"PROMETHEUS_MULTIPROC_DIR": {
66+
"type": "string",
67+
"default": "/var/tmp/prometheus_metrics",
68+
},
6469
},
6570
}
6671
validate(instance=self, schema=schema)

gen3workflow/metrics.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import Any, Dict
2+
3+
from cdispyutils.metrics import BaseMetrics
4+
5+
from gen3workflow.config import config
6+
7+
8+
class Metrics(BaseMetrics):
9+
def __init__(self, prometheus_dir: str, enabled: bool = True) -> None:
10+
super().__init__(
11+
prometheus_dir=config["PROMETHEUS_MULTIPROC_DIR"], enabled=enabled
12+
)

poetry.lock

Lines changed: 285 additions & 165 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ httpx = "<1"
2525
jsonschema = "<5"
2626
sqlalchemy = { extras = ["asyncio"], version = "<3" }
2727
uvicorn = "<1"
28+
cdispyutils = "^2.4.0"
2829

2930
[tool.poetry.dev-dependencies]
3031
freezegun = "<2"

0 commit comments

Comments
 (0)