This commit is contained in:
parent
79ec5a1841
commit
994469c2e3
62
main.py
62
main.py
|
@ -2,14 +2,8 @@ import os
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
from os.path import exists
|
from os.path import exists
|
||||||
|
|
||||||
import sentry_sdk
|
|
||||||
from ariadne import load_schema_from_path, make_executable_schema
|
from ariadne import load_schema_from_path, make_executable_schema
|
||||||
from ariadne.asgi import GraphQL
|
from ariadne.asgi import GraphQL
|
||||||
from sentry_sdk.integrations.aiohttp import AioHttpIntegration
|
|
||||||
from sentry_sdk.integrations.ariadne import AriadneIntegration
|
|
||||||
from sentry_sdk.integrations.redis import RedisIntegration
|
|
||||||
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
|
|
||||||
from sentry_sdk.integrations.starlette import StarletteIntegration
|
|
||||||
from starlette.applications import Starlette
|
from starlette.applications import Starlette
|
||||||
from starlette.routing import Route
|
from starlette.routing import Route
|
||||||
|
|
||||||
|
@ -18,57 +12,27 @@ from services.schema import resolvers
|
||||||
from services.search import search_service
|
from services.search import search_service
|
||||||
from services.viewed import ViewedStorage
|
from services.viewed import ViewedStorage
|
||||||
from services.webhook import WebhookEndpoint
|
from services.webhook import WebhookEndpoint
|
||||||
from settings import DEV_SERVER_PID_FILE_NAME, MODE, SENTRY_DSN
|
from settings import DEV_SERVER_PID_FILE_NAME, MODE
|
||||||
|
|
||||||
|
|
||||||
import_module('resolvers')
|
import_module('resolvers')
|
||||||
|
|
||||||
schema = make_executable_schema(load_schema_from_path('schema/'), resolvers)
|
schema = make_executable_schema(load_schema_from_path('schema/'), resolvers)
|
||||||
|
|
||||||
|
|
||||||
async def start_up():
|
async def dev_pid():
|
||||||
print(f'[main] starting in {MODE} mode')
|
# pid file management
|
||||||
|
if MODE == 'development' and not exists(DEV_SERVER_PID_FILE_NAME):
|
||||||
with sentry_sdk.start_transaction(op='task', name='Redis Connection'):
|
with open(DEV_SERVER_PID_FILE_NAME, 'w', encoding='utf-8') as f:
|
||||||
await redis.connect()
|
f.write(str(os.getpid()))
|
||||||
|
print(f'[main] started in {MODE} mode')
|
||||||
# start viewed service
|
|
||||||
await ViewedStorage.init()
|
|
||||||
|
|
||||||
# start search service
|
|
||||||
search_service.info()
|
|
||||||
|
|
||||||
if MODE == 'development':
|
|
||||||
# pid file management
|
|
||||||
if not exists(DEV_SERVER_PID_FILE_NAME):
|
|
||||||
with open(DEV_SERVER_PID_FILE_NAME, 'w', encoding='utf-8') as f:
|
|
||||||
f.write(str(os.getpid()))
|
|
||||||
|
|
||||||
if MODE == 'production':
|
|
||||||
# sentry monitoring
|
|
||||||
try:
|
|
||||||
sentry_sdk.init(
|
|
||||||
SENTRY_DSN,
|
|
||||||
enable_tracing=True,
|
|
||||||
integrations=[
|
|
||||||
StarletteIntegration(),
|
|
||||||
AriadneIntegration(),
|
|
||||||
SqlalchemyIntegration(),
|
|
||||||
RedisIntegration(),
|
|
||||||
AioHttpIntegration(),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
print('[sentry] init error')
|
|
||||||
print(e)
|
|
||||||
|
|
||||||
|
|
||||||
async def shutdown():
|
|
||||||
await redis.disconnect()
|
|
||||||
|
|
||||||
|
|
||||||
routes = [
|
routes = [
|
||||||
Route('/', GraphQL(schema, debug=True)),
|
Route('/', GraphQL(schema, debug=True)),
|
||||||
Route('/new-author', WebhookEndpoint),
|
Route('/new-author', WebhookEndpoint),
|
||||||
]
|
]
|
||||||
app = Starlette(routes=routes, debug=True, on_startup=[start_up], on_shutdown=[shutdown])
|
app = Starlette(routes=routes, debug=True, on_startup=[
|
||||||
|
redis.connect,
|
||||||
|
ViewedStorage.init,
|
||||||
|
search_service.info,
|
||||||
|
dev_pid
|
||||||
|
], on_shutdown=[redis.disconnect])
|
||||||
|
|
|
@ -11,7 +11,7 @@ python = "^3.12"
|
||||||
SQLAlchemy = "^2.0.22"
|
SQLAlchemy = "^2.0.22"
|
||||||
psycopg2-binary = "^2.9.9"
|
psycopg2-binary = "^2.9.9"
|
||||||
redis = {extras = ["hiredis"], version = "^5.0.1"}
|
redis = {extras = ["hiredis"], version = "^5.0.1"}
|
||||||
sentry-sdk = "^1.4.1"
|
sentry-sdk = { version = "^1.4.1", extras = ["starlette", "aiohttp", "ariadne", "sqlalchemy"] }
|
||||||
starlette = "^0.36.1"
|
starlette = "^0.36.1"
|
||||||
gql = "^3.4.1"
|
gql = "^3.4.1"
|
||||||
ariadne = "^0.21"
|
ariadne = "^0.21"
|
||||||
|
|
33
services/sentry.py
Normal file
33
services/sentry.py
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
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
|
||||||
|
|
||||||
|
from settings import SENTRY_DSN
|
||||||
|
|
||||||
|
|
||||||
|
def start_sentry():
|
||||||
|
# sentry monitoring
|
||||||
|
try:
|
||||||
|
sentry_sdk.init(
|
||||||
|
SENTRY_DSN,
|
||||||
|
# Set traces_sample_rate to 1.0 to capture 100%
|
||||||
|
# of transactions for performance monitoring.
|
||||||
|
traces_sample_rate=1.0,
|
||||||
|
# Set profiles_sample_rate to 1.0 to profile 100%
|
||||||
|
# of sampled transactions.
|
||||||
|
# We recommend adjusting this value in production.
|
||||||
|
profiles_sample_rate=1.0,
|
||||||
|
enable_tracing=True,
|
||||||
|
integrations=[
|
||||||
|
StarletteIntegration(),
|
||||||
|
AriadneIntegration(),
|
||||||
|
SqlalchemyIntegration(),
|
||||||
|
# RedisIntegration(),
|
||||||
|
AioHttpIntegration()
|
||||||
|
]
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print('[sentry] init error')
|
||||||
|
print(e)
|
Loading…
Reference in New Issue
Block a user