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 WORKDIR /app
COPY . /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 RUN curl -sSL https://install.python-poetry.org | python
ENV PATH="${PATH}:/root/.local/bin" ENV PATH="${PATH}:/root/.local/bin"
RUN poetry config virtualenvs.create false && poetry install --only main RUN poetry config virtualenvs.create false && poetry install --only main

View File

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

View File

@ -1,6 +1,4 @@
import logging import logging
import sys
import math
import time import time
from typing import Any, Callable, Dict, TypeVar from typing import Any, Callable, Dict, TypeVar
@ -15,24 +13,20 @@ from settings import DB_URL
# Настройка журнала # Настройка журнала
logging.basicConfig(level=logging.DEBUG) 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') @event.listens_for(Engine, 'before_cursor_execute')
def before_cursor_execute(conn, cursor, statement, parameters, context, executemany): def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
conn.info.setdefault('query_start_time', []).append(time.time()) conn.info.setdefault('query_start_time', []).append(time.time())
logger.debug(f" {statement}")
@event.listens_for(Engine, 'after_cursor_execute') @event.listens_for(Engine, 'after_cursor_execute')
def after_cursor_execute(conn, cursor, statement, parameters, context, executemany): def after_cursor_execute(conn, cursor, statement, parameters, context, executemany):
total = time.time() - conn.info['query_start_time'].pop(-1) total = time.time() - conn.info['query_start_time'].pop(-1)
total = math.floor(total * 10000) / 10000 logger.debug(f' Finished in {total*1000} ms ')
if total > 25:
logger.debug(f'Long running query: {statement}, Execution Time: {total} s')
engine = create_engine(DB_URL, echo=False, pool_size=10, max_overflow=20) engine = create_engine(DB_URL, echo=False, pool_size=10, max_overflow=20)

View File

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