-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Expose metrics about used TeamForCapella license usage
A new gauge metric `t4c_licenses` was added to the `/metrics` endpoint, which contains the number of currently used licenses per TeamForCapella instance. The instance name is used as label for the metric. Note: If you have multiple TeamForCapella server instances, which point to the same license server, this is not detected. Therefore, make sure to filter the `t4c_licenses` for one instance first.
- Loading branch information
1 parent
21d4c9a
commit 91d5637
Showing
7 changed files
with
113 additions
and
31 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
39 changes: 39 additions & 0 deletions
39
backend/capellacollab/settings/modelsources/t4c/metrics.py
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,39 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import typing as t | ||
|
||
import prometheus_client | ||
import prometheus_client.core | ||
|
||
from capellacollab.core import database | ||
|
||
from . import crud, interface | ||
|
||
|
||
class UsedT4CLicensesCollector: | ||
def collect(self) -> t.Iterable[prometheus_client.core.Metric]: | ||
metric = prometheus_client.core.GaugeMetricFamily( | ||
"t4c_licenses", | ||
"Currently used T4C licenses per registered TeamForCapella instance.", | ||
) | ||
|
||
with database.SessionLocal() as db: | ||
instances = crud.get_t4c_instances(db) | ||
|
||
if not instances: | ||
return | ||
|
||
for instance in instances: | ||
try: | ||
t4c_status = interface.get_t4c_status(instance) | ||
used_licenses = t4c_status.total - t4c_status.free | ||
except Exception: | ||
used_licenses = -1 | ||
|
||
metric.add_metric([instance.name], used_licenses) | ||
yield metric | ||
|
||
|
||
def register() -> None: | ||
prometheus_client.REGISTRY.register(UsedT4CLicensesCollector()) | ||
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,42 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import pytest | ||
import responses | ||
from fastapi import status | ||
from sqlalchemy import orm | ||
|
||
from capellacollab.settings.modelsources.t4c import crud as t4c_crud | ||
from capellacollab.settings.modelsources.t4c import models as t4c_models | ||
from capellacollab.tools import models as tools_models | ||
|
||
|
||
@pytest.fixture(name="t4c_instance") | ||
def fixture_t4c_instance( | ||
db: orm.Session, | ||
test_tool_version: tools_models.DatabaseVersion, | ||
) -> t4c_models.DatabaseT4CInstance: | ||
server = t4c_models.DatabaseT4CInstance( | ||
name="test server", | ||
license="lic", | ||
host="localhost", | ||
usage_api="http://localhost:8086", | ||
rest_api="http://localhost:8080/api/v1.0", | ||
username="user", | ||
password="pass", | ||
protocol=t4c_models.Protocol.tcp, | ||
version=test_tool_version, | ||
) | ||
|
||
return t4c_crud.create_t4c_instance(db, server) | ||
|
||
|
||
@pytest.fixture(name="mock_license_server") | ||
def fixture_mock_license_server(): | ||
with responses.RequestsMock() as rsps: | ||
rsps.get( | ||
"http://localhost:8086/status/json", | ||
status=status.HTTP_200_OK, | ||
json={"status": {"used": 1, "free": 19, "total": 20}}, | ||
) | ||
yield rsps |
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,27 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import pytest | ||
|
||
from capellacollab.settings.modelsources.t4c import metrics | ||
|
||
|
||
@pytest.mark.usefixtures("mock_license_server", "t4c_instance") | ||
def test_t4c_license_metrics_collector(): | ||
collector = metrics.UsedT4CLicensesCollector() | ||
|
||
data = list(collector.collect()) | ||
|
||
sample = data[0].samples[0] | ||
assert sample.name == "t4c_licenses" | ||
assert sample.value == 1 | ||
|
||
|
||
def test_t4c_license_metrics_collector_error(): | ||
collector = metrics.UsedT4CLicensesCollector() | ||
|
||
data = list(collector.collect()) | ||
|
||
sample = data[0].samples[0] | ||
assert sample.name == "t4c_licenses" | ||
assert sample.value == -1 |
File renamed without changes.