-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Monitor deprecated API usage via Prometheus metrics
- Loading branch information
Showing
11 changed files
with
117 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
""" | ||
Utilities for marking deprecated features to monitor usage | ||
""" | ||
from enum import Enum | ||
from functools import wraps | ||
from typing import Optional | ||
|
||
from flask import g, request | ||
|
||
from mwdb.core.log import getLogger | ||
from mwdb.core.metrics import metric_deprecated_usage | ||
|
||
logger = getLogger() | ||
|
||
|
||
class DeprecatedFeature(Enum): | ||
# Unmanageable API keys, deprecated in v2.0.0 | ||
legacy_api_key_v1 = "legacy_api_key_v1" | ||
# API keys non-complaint with RFC7519 | ||
# Deprecated in v2.7.0 | ||
legacy_api_key_v2 = "legacy_api_key_v2" | ||
# Legacy PUT/POST /api/<object_type>/<parent> | ||
# Use POST /api/<object_type> instead | ||
# Deprecated in v2.0.0 | ||
legacy_object_upload = "legacy_file_upload" | ||
# Legacy /request/sample/<token> | ||
# Use /file/<id>/download instead | ||
# Deprecated in v2.2.0 | ||
legacy_file_download = "legacy_file_download" | ||
# Legacy /search | ||
# Use GET /<object_type> instead | ||
# Deprecated in v2.0.0 | ||
legacy_search = "legacy_search" | ||
# Legacy ?page parameter in object listing endpoints | ||
# Use "?older_than" instead | ||
# Deprecated in v2.0.0 | ||
legacy_page_parameter = "legacy_page_parameter" | ||
# Legacy Metakey API | ||
# Use Attribute API instead | ||
# Deprecated in v2.6.0 | ||
legacy_metakey_api = "legacy_metakey_api" | ||
# Legacy Metakey API | ||
# Use Attribute API instead | ||
# Deprecated in v2.6.0 | ||
legacy_metakeys_upload_option = "legacy_metakeys_upload_option" | ||
|
||
|
||
def uses_deprecated_api( | ||
feature: DeprecatedFeature, | ||
endpoint: Optional[str] = None, | ||
method: Optional[str] = None, | ||
): | ||
user = g.auth_user.login if g.auth_user is not None else None | ||
metric_deprecated_usage.inc( | ||
feature=str(feature.value), endpoint=endpoint, method=method, user=user | ||
) | ||
logger.debug( | ||
f"Used deprecated feature: {feature}" | ||
+ (f" ({method} {endpoint})" if endpoint is not None else "") | ||
) | ||
|
||
|
||
def deprecated_endpoint(feature: DeprecatedFeature): | ||
def method_wrapper(f): | ||
@wraps(f) | ||
def endpoint(*args, **kwargs): | ||
uses_deprecated_api( | ||
feature, endpoint=request.endpoint, method=request.method | ||
) | ||
return f(*args, **kwargs) | ||
|
||
return endpoint | ||
|
||
return method_wrapper |
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,11 +1,19 @@ | ||
from .redis_counter import RedisCounter, collect, metrics_enabled | ||
|
||
metric_api_requests = RedisCounter( | ||
"api_requests", "API request metrics", ("method", "endpoint", "user", "status_code") | ||
"mwdb_api_requests", | ||
"API request metrics", | ||
("method", "endpoint", "user", "status_code"), | ||
) | ||
metric_deprecated_usage = RedisCounter( | ||
"mwdb_deprecated_usage", | ||
"Deprecated API usage metrics", | ||
("feature", "method", "endpoint", "user"), | ||
) | ||
|
||
__all__ = [ | ||
"collect", | ||
"metrics_enabled", | ||
"metric_api_requests", | ||
"metric_deprecated_usage", | ||
] |
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
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