Skip to content

Commit

Permalink
api: add metrics.py
Browse files Browse the repository at this point in the history
Move `Metrics` class implementation to separate file
to reduce number of lines in `api.main` module and to
keep the various modules more organized.
Otherwise pylint will complain with the below error:
"api/main.py: C0302: Too many lines in module (1044/1000) (too-many-lines)"

Signed-off-by: Jeny Sadadia <[email protected]>
  • Loading branch information
Jeny Sadadia authored and JenySadadia committed Nov 8, 2024
1 parent 7c0b59a commit 63fb009
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 47 deletions.
48 changes: 1 addition & 47 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import os
import re
from typing import List, Union, Optional
import threading
from contextlib import asynccontextmanager
from fastapi import (
Depends,
Expand Down Expand Up @@ -54,6 +53,7 @@
UserUpdate,
UserGroup,
)
from .metrics import Metrics


@asynccontextmanager
Expand All @@ -69,53 +69,7 @@ async def lifespan(app: FastAPI): # pylint: disable=redefined-outer-name
# models etc.
API_VERSIONS = ['v0']


class Metrics():
'''
Class to store and update various metrics
'''
def __init__(self):
'''
Initialize metrics dictionary and lock
'''
self.metrics = {}
self.metrics['http_requests_total'] = 0
self.lock = threading.Lock()

# Various internal metrics
def update(self):
'''
Update metrics (reserved for future use)
'''

def add(self, key, value):
'''
Add a value to a metric
'''
with self.lock:
if key not in self.metrics:
self.metrics[key] = 0
self.metrics[key] += value

def get(self, key):
'''
Get the value of a metric
'''
self.update()
with self.lock:
return self.metrics.get(key, 0)

def all(self):
'''
Get all the metrics
'''
self.update()
with self.lock:
return self.metrics


metrics = Metrics()

app = FastAPI(lifespan=lifespan, debug=True)
db = Database(service=(os.getenv('MONGO_SERVICE') or 'mongodb://db:27017'))
auth = Authentication(token_url="user/login")
Expand Down
52 changes: 52 additions & 0 deletions api/metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# Copyright (C) 2024 Collabora Limited
# Author: enys Fedoryshchenko <[email protected]>

"""KernelCI API metrics module"""

import threading


class Metrics():
'''
Class to store and update various metrics
'''
def __init__(self):
'''
Initialize metrics dictionary and lock
'''
self.metrics = {}
self.metrics['http_requests_total'] = 0
self.lock = threading.Lock()

# Various internal metrics
def update(self):
'''
Update metrics (reserved for future use)
'''

def add(self, key, value):
'''
Add a value to a metric
'''
with self.lock:
if key not in self.metrics:
self.metrics[key] = 0
self.metrics[key] += value

def get(self, key):
'''
Get the value of a metric
'''
self.update()
with self.lock:
return self.metrics.get(key, 0)

def all(self):
'''
Get all the metrics
'''
self.update()
with self.lock:
return self.metrics

0 comments on commit 63fb009

Please sign in to comment.