|
13 | 13 | from copr.v3 import CoprException |
14 | 14 | from ogr import __version__ as ogr_version |
15 | 15 | from ogr.exceptions import OgrException |
| 16 | +from ogr.metrics import get_metrics_tracker |
16 | 17 | from packit import __version__ as packit_version |
17 | 18 | from packit.exceptions import PackitException |
18 | 19 | from sqlalchemy import __version__ as sqlal_version |
|
99 | 100 | update_vm_image_build, |
100 | 101 | ) |
101 | 102 | from packit_service.worker.jobs import SteveJobs |
| 103 | +from packit_service.worker.monitoring import Pushgateway |
102 | 104 | from packit_service.worker.result import TaskResults |
103 | 105 |
|
104 | 106 | logger = logging.getLogger(__name__) |
@@ -940,3 +942,37 @@ def get_usage_statistics() -> None: |
940 | 942 | logger.debug(f"Getting usage data from datetime_from {day}.") |
941 | 943 | get_usage_data(datetime_from=day) |
942 | 944 | logger.debug("Got usage data.") |
| 945 | + |
| 946 | + |
| 947 | +@celery_app.task |
| 948 | +def push_ogr_namespace_metrics() -> None: |
| 949 | + """ |
| 950 | + Collect ogr namespace request metrics and push them to pushgateway. |
| 951 | +
|
| 952 | + This task queries the ogr metrics tracker for request counts per namespace |
| 953 | + and service type, then updates the Prometheus metrics and pushes them. |
| 954 | + After pushing, the counters are reset for the next collection period. |
| 955 | + """ |
| 956 | + logger.debug("Collecting ogr namespace request metrics.") |
| 957 | + |
| 958 | + try: |
| 959 | + metrics_tracker = get_metrics_tracker() |
| 960 | + counts = metrics_tracker.get_all_counts() |
| 961 | + |
| 962 | + pushgateway = Pushgateway() |
| 963 | + |
| 964 | + for (service_type, namespace), count in counts.items(): |
| 965 | + pushgateway.ogr_namespace_requests.labels( |
| 966 | + namespace=namespace, |
| 967 | + service_type=service_type, |
| 968 | + ).set(count) |
| 969 | + |
| 970 | + pushgateway.push() |
| 971 | + |
| 972 | + metrics_tracker.reset() |
| 973 | + |
| 974 | + logger.info( |
| 975 | + f"Pushed ogr namespace metrics: {len(counts)} namespace/service combinations", |
| 976 | + ) |
| 977 | + except Exception as e: |
| 978 | + logger.error(f"Failed to push ogr namespace metrics: {e}", exc_info=True) |
0 commit comments