-
Notifications
You must be signed in to change notification settings - Fork 5
Add basic metrics to the ETOS API #134
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,68 @@ | ||
| # Copyright Axis Communications AB. | ||
| # | ||
| # For a full list of individual contributors, please see the commit history. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """ETOS API metrics.""" | ||
|
|
||
| from enum import Enum | ||
| from functools import wraps | ||
| from logging import Logger | ||
| from typing import Callable | ||
|
|
||
| from fastapi import HTTPException | ||
| from prometheus_client import Counter, Histogram | ||
|
|
||
| OPERATIONS = Enum( | ||
| "OPERATIONS", | ||
| [ | ||
| "start_testrun", | ||
| "get_subsuite", | ||
| "stop_testrun", | ||
| ], | ||
| ) | ||
|
|
||
| REQUEST_TIME = Histogram( | ||
| "http_request_duration_seconds", | ||
| "Time spent processing request", | ||
| ["endpoint", "operation"], | ||
| ) | ||
| REQUESTS_TOTAL = Counter( | ||
| "http_requests_total", | ||
| "Total number of requests", | ||
| ["endpoint", "operation", "status"], | ||
| ) | ||
|
|
||
|
|
||
| # I like the idea of all operations in this file is upper-case. | ||
| def COUNT_REQUESTS(labels: dict, logger: Logger): # pylint:disable=invalid-name | ||
| """Count number of requests to server using the REQUESTS_TOTAL counter.""" | ||
|
|
||
| def decorator(func: Callable): | ||
| @wraps(func) | ||
| async def wrapper(*args, **kwargs): | ||
| try: | ||
| response = await func(*args, **kwargs) | ||
| REQUESTS_TOTAL.labels(**labels, status=200).inc() | ||
| return response | ||
| except HTTPException as http_exception: | ||
| REQUESTS_TOTAL.labels(**labels, status=http_exception.status_code).inc() | ||
| raise | ||
| except Exception: # pylint:disable=bare-except | ||
| logger.exception("Unhandled exception occurred, setting status to 500") | ||
| REQUESTS_TOTAL.labels(**labels, status=500).inc() | ||
| raise | ||
|
|
||
| return wrapper | ||
|
|
||
| return decorator | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
| from starlette.responses import RedirectResponse, Response | ||
|
|
||
| from etos_api.library.environment import Configuration, configure_testrun | ||
| from etos_api.library.metrics import COUNT_REQUESTS, OPERATIONS, REQUEST_TIME | ||
| from etos_api.library.opentelemetry import context | ||
| from etos_api.library.utilities import sync_to_async | ||
|
|
||
|
|
@@ -45,12 +46,22 @@ | |
| root_path_in_servers=False, | ||
| dependencies=[Depends(context)], | ||
| ) | ||
|
|
||
| API = f"/api/{ETOSV0.version}/etos" | ||
| START_LABELS = {"endpoint": API, "operation": OPERATIONS.start_testrun.name} | ||
| # The key {suite_id} is supposed to indicate that this is a path parameter, but | ||
| # we don't want to set the actual value in the metrics label since that would create | ||
| # a high cardinality metric. Therefore we use the literal string "{suite_id}". | ||
| STOP_LABELS = {"endpoint": f"{API}/{{suite_id}}", "operation": OPERATIONS.stop_testrun.name} | ||
|
|
||
| TRACER = trace.get_tracer("etos_api.routers.etos.router") | ||
| LOGGER = logging.getLogger(__name__) | ||
| logging.getLogger("pika").setLevel(logging.WARNING) | ||
| # pylint:disable=too-many-locals,too-many-statements | ||
|
|
||
|
|
||
| @REQUEST_TIME.labels(**START_LABELS).time() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will likely include decorator overhead too? Intended?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it is supposed to count request time it should be included. Not that the time we spend in decorators is enough to even notice. |
||
| @COUNT_REQUESTS(START_LABELS, LOGGER) | ||
| @ETOSV0.post("/etos", tags=["etos"], response_model=StartEtosResponse) | ||
| async def start_etos( | ||
| etos: StartEtosRequest, | ||
|
|
@@ -69,6 +80,8 @@ async def start_etos( | |
| return await _start(etos, span, otel_context.get_current()) | ||
|
|
||
|
|
||
| @REQUEST_TIME.labels(**STOP_LABELS).time() | ||
| @COUNT_REQUESTS(STOP_LABELS, LOGGER) | ||
| @ETOSV0.delete("/etos/{suite_id}", tags=["etos"], response_model=AbortEtosResponse) | ||
| async def abort_etos(suite_id: str, ctx: Annotated[otel_context.Context, Depends(context)]) -> dict: | ||
| """Abort ETOS execution on delete. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.