-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Edward Malinowski
authored and
Edward Malinowski
committed
Jun 26, 2024
1 parent
ee4040e
commit a17c76f
Showing
7 changed files
with
117 additions
and
106 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
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 |
---|---|---|
@@ -1,93 +1,83 @@ | ||
import tempfile | ||
import os | ||
from fence.config import config | ||
from fence import app | ||
from prometheus_client import ( | ||
CollectorRegistry, | ||
multiprocess, | ||
make_wsgi_app, | ||
Counter, | ||
Gauge, | ||
) | ||
|
||
|
||
# for some reason the temp dir does not get created properly if we move | ||
# this statement to `_setup_prometheus()` | ||
PROMETHEUS_TMP_COUNTER_DIR = tempfile.TemporaryDirectory() | ||
|
||
|
||
if config["ENABLE_PROMETHEUS_METRICS"]: | ||
from fence import logger | ||
|
||
app.prometheus_counters = {} | ||
from werkzeug.middleware.dispatcher import DispatcherMiddleware | ||
from prometheus_client import ( | ||
CollectorRegistry, | ||
multiprocess, | ||
make_wsgi_app, | ||
Counter, | ||
Gauge, | ||
) | ||
|
||
# This environment variable MUST be declared before importing the | ||
# prometheus modules (or unit tests fail) | ||
# More details on this awkwardness: https://github.com/prometheus/client_python/issues/250 | ||
os.environ["prometheus_multiproc_dir"] = PROMETHEUS_TMP_COUNTER_DIR.name | ||
|
||
app.prometheus_registry = CollectorRegistry() | ||
multiprocess.MultiProcessCollector(app.prometheus_registry) | ||
|
||
# Add prometheus wsgi middleware to route /metrics requests | ||
app.wsgi_app = DispatcherMiddleware( | ||
app.wsgi_app, {"/metrics": make_wsgi_app(registry=app.prometheus_registry)} | ||
) | ||
|
||
presigned_url_counter = Counter( | ||
"fence_presigned_url_requests_total", | ||
"Total number of presigned URL requests", | ||
registry=app.prometheus_registry, | ||
) | ||
|
||
login_counter = Counter( | ||
"fence_all_login_requests_total", | ||
"Total number of login requests", | ||
registry=app.prometheus_registry, | ||
) | ||
|
||
fence_login_counter = Counter( | ||
"fence_login_requests_total", | ||
"Total number of fence login requests", | ||
registry=app.prometheus_registry, | ||
) | ||
|
||
google_login_counter = Counter( | ||
"google_login_requests_total", | ||
"Total number of Google login requests", | ||
registry=app.prometheus_registry, | ||
) | ||
|
||
ras_login_counter = Counter( | ||
"fence_ras_login_requests_total", | ||
"Total number of RAS login requests", | ||
registry=app.prometheus_registry, | ||
) | ||
|
||
presigned_urls_ga4gh_drs_counter = Counter( | ||
"fence_presigned_urls_ga4gh_drs_requests_total", | ||
"Total number of presigned URL requests for GA4GH DRS", | ||
registry=app.prometheus_registry, | ||
) | ||
|
||
presigned_url_download_protocol_gcs_counter = Counter( | ||
"fence_presigned_url_download_protocol_gcs_requests_total", | ||
"Total number of presigned URL requests for GCS", | ||
registry=app.prometheus_registry, | ||
) | ||
|
||
presigned_url_download_protocol_s3_counter = Counter( | ||
"fence_presigned_url_download_protocol_s3_requests_total", | ||
"Total number of presigned URL requests for S3", | ||
registry=app.prometheus_registry, | ||
) | ||
|
||
presigned_url_data_metrics_size_gauge = Gauge( | ||
"fence_presigned_url_data_metrics_size_bytes", | ||
"Size of data metrics in bytes", | ||
registry=app.prometheus_registry, | ||
) | ||
|
||
else: | ||
logger.info("Prometheus metrics are NOT enabled.") | ||
class Metrics: | ||
def __init__(self): | ||
self.app = app | ||
self.presigned_url_counter = None | ||
self.login_counter = None | ||
self.fence_login_counter = None | ||
self.google_login_counter = None | ||
self.ras_login_counter = None | ||
self.presigned_urls_ga4gh_drs_counter = None | ||
self.presigned_url_download_protocol_gcs_counter = None | ||
self.presigned_url_download_protocol_s3_counter = None | ||
self.presigned_url_data_metrics_size_gauge = None | ||
|
||
def initialize_metrics(self, app): | ||
os.environ["prometheus_multiproc_dir"] = PROMETHEUS_TMP_COUNTER_DIR.name | ||
app.prometheus_registry = CollectorRegistry() | ||
multiprocess.MultiProcessCollector(app.prometheus_registry) | ||
|
||
# Add prometheus wsgi middleware to route /metrics requests | ||
app.wsgi_app = DispatcherMiddleware( | ||
app.wsgi_app, {"/metrics": make_wsgi_app(registry=app.prometheus_registry)} | ||
) | ||
self.presigned_url_counter = Counter( | ||
"fence_presigned_url_requests_total", | ||
"Total number of presigned URL requests", | ||
registry=self.app.prometheus_registry, | ||
) | ||
self.login_counter = Counter( | ||
"fence_all_login_requests_total", | ||
"Total number of login requests", | ||
registry=self.app.prometheus_registry, | ||
) | ||
self.fence_login_counter = Counter( | ||
"fence_login_requests_total", | ||
"Total number of fence login requests", | ||
registry=self.app.prometheus_registry, | ||
) | ||
self.google_login_counter = Counter( | ||
"google_login_requests_total", | ||
"Total number of Google login requests", | ||
registry=self.app.prometheus_registry, | ||
) | ||
self.ras_login_counter = Counter( | ||
"fence_ras_login_requests_total", | ||
"Total number of RAS login requests", | ||
registry=self.app.prometheus_registry, | ||
) | ||
self.presigned_urls_ga4gh_drs_counter = Counter( | ||
"fence_presigned_urls_ga4gh_drs_requests_total", | ||
"Total number of presigned URL requests for GA4GH DRS", | ||
registry=self.app.prometheus_registry, | ||
) | ||
self.presigned_url_download_protocol_gcs_counter = Counter( | ||
"fence_presigned_url_download_protocol_gcs_requests_total", | ||
"Total number of presigned URL requests for GCS", | ||
registry=self.app.prometheus_registry, | ||
) | ||
self.presigned_url_download_protocol_s3_counter = Counter( | ||
"fence_presigned_url_download_protocol_s3_requests_total", | ||
"Total number of presigned URL requests for S3", | ||
registry=self.app.prometheus_registry, | ||
) | ||
self.presigned_url_data_metrics_size_gauge = Gauge( | ||
"fence_presigned_url_data_metrics_size_bytes", | ||
"Size of data metrics in bytes", | ||
registry=self.app.prometheus_registry, | ||
) | ||
|
||
|
||
metrics = Metrics() |