feat: init sentry monitoring

This commit is contained in:
swve 2023-04-13 20:06:09 +02:00 committed by Badr B
parent 43d2b469a4
commit 137a909347
13 changed files with 1064 additions and 30 deletions

View file

@ -4,6 +4,12 @@ import os
import yaml import yaml
class SentryConfig(BaseModel):
dsn: str
environment: str
release: str
class HostingConfig(BaseModel): class HostingConfig(BaseModel):
domain: str domain: str
port: int port: int
@ -12,6 +18,7 @@ class HostingConfig(BaseModel):
allowed_origins: list allowed_origins: list
allowed_regexp: str allowed_regexp: str
self_hosted: bool self_hosted: bool
sentry_config: Optional[SentryConfig]
class DatabaseConfig(BaseModel): class DatabaseConfig(BaseModel):
@ -48,7 +55,7 @@ def get_learnhouse_config() -> LearnHouseConfig:
env_port = os.environ.get('LEARNHOUSE_PORT') env_port = os.environ.get('LEARNHOUSE_PORT')
env_ssl = os.environ.get('LEARNHOUSE_SSL') env_ssl = os.environ.get('LEARNHOUSE_SSL')
env_use_default_org = os.environ.get('LEARNHOUSE_USE_DEFAULT_ORG') env_use_default_org = os.environ.get('LEARNHOUSE_USE_DEFAULT_ORG')
env_allowed_origins = os.environ.get('LEARNHOUSE_ALLOWED_ORIGINS') env_allowed_origins = os.environ.get('LEARNHOUSE_ALLOWED_ORIGINS')
# Allowed origins should be a comma separated string # Allowed origins should be a comma separated string
if env_allowed_origins: if env_allowed_origins:
env_allowed_origins = env_allowed_origins.split(',') env_allowed_origins = env_allowed_origins.split(',')
@ -62,6 +69,11 @@ def get_learnhouse_config() -> LearnHouseConfig:
env_mongodb_connection_string = os.environ.get( env_mongodb_connection_string = os.environ.get(
'LEARNHOUSE_MONGODB_CONNECTION_STRING') 'LEARNHOUSE_MONGODB_CONNECTION_STRING')
# Sentry Config
env_sentry_dsn = os.environ.get('LEARNHOUSE_SENTRY_DSN')
env_sentry_environment = os.environ.get('LEARNHOUSE_SENTRY_ENVIRONMENT')
env_sentry_release = os.environ.get('LEARNHOUSE_SENTRY_RELEASE')
# Fill in values with YAML file if they are not provided # Fill in values with YAML file if they are not provided
site_name = env_site_name or yaml_config.get('site_name') site_name = env_site_name or yaml_config.get('site_name')
site_description = env_site_description or yaml_config.get( site_description = env_site_description or yaml_config.get(
@ -90,6 +102,20 @@ def get_learnhouse_config() -> LearnHouseConfig:
mongodb_connection_string = env_mongodb_connection_string or yaml_config.get( mongodb_connection_string = env_mongodb_connection_string or yaml_config.get(
'database_config', {}).get('mongodb_connection_string') 'database_config', {}).get('mongodb_connection_string')
# Sentry config
sentry_dsn = env_sentry_dsn or yaml_config.get(
'hosting_config', {}).get('sentry_config', {}).get('dsn')
sentry_environment = env_sentry_environment or yaml_config.get(
'hosting_config', {}).get('sentry_config', {}).get('environment')
sentry_release = env_sentry_release or yaml_config.get(
'hosting_config', {}).get('sentry_config', {}).get('release')
sentry_config = SentryConfig(
dsn=sentry_dsn,
environment=sentry_environment,
release=sentry_release
)
# Create HostingConfig and DatabaseConfig objects # Create HostingConfig and DatabaseConfig objects
hosting_config = HostingConfig( hosting_config = HostingConfig(
domain=domain, domain=domain,
@ -98,7 +124,8 @@ def get_learnhouse_config() -> LearnHouseConfig:
use_default_org=bool(use_default_org), use_default_org=bool(use_default_org),
allowed_origins=list(allowed_origins), allowed_origins=list(allowed_origins),
allowed_regexp=allowed_regexp, allowed_regexp=allowed_regexp,
self_hosted=bool(self_hosted) self_hosted=bool(self_hosted),
sentry_config=sentry_config
) )
database_config = DatabaseConfig( database_config = DatabaseConfig(
host=host, host=host,

View file

@ -8,9 +8,13 @@ hosting_config:
ssl: true ssl: true
use_default_org: false use_default_org: false
default_org: learnhouse default_org: learnhouse
sentry_config:
dsn: "https://1a6aa22656224851af492aae5d4155a1@o4505007882436608.ingest.sentry.io/4505007884599296"
environment: dev
release: "0.1.0"
allowed_origins: allowed_origins:
- http://localhost:3000 - http://localhost:3000
- http://localhost:3001 - http://localhost:3001
allowed_regexp: '\b((?:https?://)[^\s/$.?#].[^\s]*)\b' allowed_regexp: '\b((?:https?://)[^\s/$.?#].[^\s]*)\b'
self_hosted: false self_hosted: false

6
front/.gitignore vendored
View file

@ -34,3 +34,9 @@ yarn-error.log*
# typescript # typescript
*.tsbuildinfo *.tsbuildinfo
next-env.d.ts next-env.d.ts
# Sentry
.sentryclirc
# Sentry
next.config.original.js

View file

@ -1,3 +1,9 @@
// This file sets a custom webpack configuration to use your Next.js app
// with Sentry.
// https://nextjs.org/docs/api-reference/next.config.js/introduction
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/
const { withSentryConfig } = require('@sentry/nextjs');
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
reactStrictMode: false, reactStrictMode: false,
@ -11,3 +17,9 @@ const nextConfig = {
}; };
module.exports = nextConfig; module.exports = nextConfig;
module.exports = withSentryConfig(
module.exports,
{ silent: true },
{ hideSourcemaps: true },
);

960
front/package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -13,6 +13,7 @@
"@radix-ui/react-dialog": "^1.0.2", "@radix-ui/react-dialog": "^1.0.2",
"@radix-ui/react-form": "^0.0.2", "@radix-ui/react-form": "^0.0.2",
"@radix-ui/react-icons": "^1.1.1", "@radix-ui/react-icons": "^1.1.1",
"@sentry/nextjs": "^7.47.0",
"@stitches/react": "^1.2.8", "@stitches/react": "^1.2.8",
"@tiptap/extension-collaboration": "^2.0.0-beta.199", "@tiptap/extension-collaboration": "^2.0.0-beta.199",
"@tiptap/extension-collaboration-cursor": "^2.0.0-beta.199", "@tiptap/extension-collaboration-cursor": "^2.0.0-beta.199",

View file

@ -0,0 +1,17 @@
// This file configures the initialization of Sentry on the browser.
// The config you add here will be used whenever a page is visited.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
import * as Sentry from '@sentry/nextjs';
const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;
Sentry.init({
dsn: SENTRY_DSN || 'https://5a456d54654c494b9a416c19e3b94573@o4505007882436608.ingest.sentry.io/4505008095625216',
// Adjust this value in production, or use tracesSampler for greater control
tracesSampleRate: 1.0,
// ...
// Note: if you want to override the automatic release value, do not set a
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
// that it will also get attached to your source maps
});

View file

@ -0,0 +1,17 @@
// This file configures the initialization of Sentry on the server.
// The config you add here will be used whenever middleware or an Edge route handles a request.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
import * as Sentry from '@sentry/nextjs';
const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;
Sentry.init({
dsn: SENTRY_DSN || 'https://5a456d54654c494b9a416c19e3b94573@o4505007882436608.ingest.sentry.io/4505008095625216',
// Adjust this value in production, or use tracesSampler for greater control
tracesSampleRate: 1.0,
// ...
// Note: if you want to override the automatic release value, do not set a
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
// that it will also get attached to your source maps
});

4
front/sentry.properties Normal file
View file

@ -0,0 +1,4 @@
defaults.url=https://sentry.io/
defaults.org=learnhouse
defaults.project=learnhouse-web
cli.executable=node_modules/@sentry/cli/bin/sentry-cli

View file

@ -0,0 +1,17 @@
// This file configures the initialization of Sentry on the server.
// The config you add here will be used whenever the server handles a request.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
import * as Sentry from '@sentry/nextjs';
const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;
Sentry.init({
dsn: SENTRY_DSN || 'https://5a456d54654c494b9a416c19e3b94573@o4505007882436608.ingest.sentry.io/4505008095625216',
// Adjust this value in production, or use tracesSampler for greater control
tracesSampleRate: 1.0,
// ...
// Note: if you want to override the automatic release value, do not set a
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
// that it will also get attached to your source maps
});

View file

@ -9,4 +9,5 @@ passlib
fastapi-jwt-auth fastapi-jwt-auth
faker faker
requests requests
pyyaml pyyaml
sentry-sdk[fastapi]

View file

@ -3,6 +3,7 @@ from fastapi import FastAPI
from config.config import LearnHouseConfig, get_learnhouse_config from config.config import LearnHouseConfig, get_learnhouse_config
from src.core.events.database import close_database, connect_to_db from src.core.events.database import close_database, connect_to_db
from src.core.events.logs import create_logs_dir from src.core.events.logs import create_logs_dir
from src.core.events.sentry import init_sentry
def startup_app(app: FastAPI) -> Callable: def startup_app(app: FastAPI) -> Callable:
@ -11,6 +12,9 @@ def startup_app(app: FastAPI) -> Callable:
learnhouse_config: LearnHouseConfig = get_learnhouse_config() learnhouse_config: LearnHouseConfig = get_learnhouse_config()
app.learnhouse_config = learnhouse_config # type: ignore app.learnhouse_config = learnhouse_config # type: ignore
# Init Sentry
await init_sentry(app)
# Connect to database # Connect to database
await connect_to_db(app) await connect_to_db(app)

16
src/core/events/sentry.py Normal file
View file

@ -0,0 +1,16 @@
from fastapi import FastAPI
import sentry_sdk
from config.config import LearnHouseConfig
async def init_sentry(app: FastAPI) -> None:
leanrhouse_config : LearnHouseConfig = app.learnhouse_config # type: ignore
if leanrhouse_config.hosting_config.sentry_config is not None:
sentry_sdk.init(
dsn=app.learnhouse_config.hosting_config.sentry_config.dsn, # type: ignore
environment=app.learnhouse_config.hosting_config.sentry_config.environment, # type: ignore
release=app.learnhouse_config.hosting_config.sentry_config.release, # type: ignore
traces_sample_rate=1.0,
)