query-time-log
Some checks failed
Deploy to core / deploy (push) Failing after 10s

This commit is contained in:
Untone 2024-02-19 11:10:12 +03:00
parent f01dde845c
commit 5772db6a36
4 changed files with 18 additions and 29 deletions

View File

@ -2,7 +2,7 @@ FROM python:alpine
WORKDIR /app
COPY . /app
RUN apk update && apk add --no-cache build-base git gcc curl python3-dev musl-dev postgresql-dev
RUN apk update && apk add --no-cache build-base icu-data-ru curl python3-dev musl-dev postgresql-dev
RUN curl -sSL https://install.python-poetry.org | python
ENV PATH="${PATH}:/root/.local/bin"
RUN poetry config virtualenvs.create false && poetry install --only main

View File

@ -1,11 +1,10 @@
import logging
from functools import wraps
import httpx
from aiohttp import ClientSession
from starlette.exceptions import HTTPException
from settings import AUTH_SECRET, AUTH_URL
from settings import ADMIN_SECRET, AUTH_URL
logger = logging.getLogger('\t[services.auth]\t')
logger.setLevel(logging.DEBUG)
@ -15,16 +14,15 @@ async def request_data(gql, headers=None):
if headers is None:
headers = {'Content-Type': 'application/json'}
try:
# Asynchronous HTTP request to the authentication server
async with ClientSession() as session:
async with session.post(AUTH_URL, json=gql, headers=headers) as response:
if response.status == 200:
data = await response.json()
errors = data.get('errors')
if errors:
logger.error(f'HTTP Errors: {errors}')
else:
return data
async with httpx.AsyncClient() as client:
response = await client.post(AUTH_URL, json=gql, headers=headers)
if response.status_code == 200:
data = response.json()
errors = data.get('errors')
if errors:
logger.error(f'HTTP Errors: {errors}')
else:
return data
except Exception as e:
# Handling and logging exceptions during authentication check
logger.error(f'[services.auth] request_data error: {e}')
@ -70,7 +68,7 @@ async def add_user_role(user_id):
operation = 'UpdateUserRoles'
headers = {
'Content-Type': 'application/json',
'x-authorizer-admin-secret': AUTH_SECRET,
'x-authorizer-admin-secret': ADMIN_SECRET,
}
variables = {'params': {'roles': 'author, reader', 'id': user_id}}
gql = {

View File

@ -1,6 +1,4 @@
import logging
import sys
import math
import time
from typing import Any, Callable, Dict, TypeVar
@ -15,24 +13,20 @@ from settings import DB_URL
# Настройка журнала
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('\t [services.db]\t')
logger.setLevel(logging.DEBUG)
# Создание обработчика журнала для записи сообщений в stdout
logger = logging.getLogger('sqlalchemy.profiler')
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.DEBUG)
logger.addHandler(console_handler)
@event.listens_for(Engine, 'before_cursor_execute')
def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
conn.info.setdefault('query_start_time', []).append(time.time())
logger.debug(f" {statement}")
@event.listens_for(Engine, 'after_cursor_execute')
def after_cursor_execute(conn, cursor, statement, parameters, context, executemany):
total = time.time() - conn.info['query_start_time'].pop(-1)
total = math.floor(total * 10000) / 10000
if total > 25:
logger.debug(f'Long running query: {statement}, Execution Time: {total} s')
logger.debug(f' Finished in {total*1000} ms ')
engine = create_engine(DB_URL, echo=False, pool_size=10, max_overflow=20)

View File

@ -1,5 +1,4 @@
import sentry_sdk
from sentry_sdk.integrations.aiohttp import AioHttpIntegration
from sentry_sdk.integrations.ariadne import AriadneIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
from sentry_sdk.integrations.starlette import StarletteIntegration
@ -23,9 +22,7 @@ def start_sentry():
integrations=[
StarletteIntegration(),
AriadneIntegration(),
SqlalchemyIntegration(),
# RedisIntegration(),
AioHttpIntegration()
SqlalchemyIntegration()
]
)
except Exception as e: