1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-25 16:28:11 +00:00
This commit is contained in:
grossmj 2020-05-19 15:48:53 +09:30
parent dadbacd8a4
commit c63aad8eca
3 changed files with 52 additions and 44 deletions

View File

@ -15,22 +15,21 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
try:
import sentry_sdk
from sentry_sdk.integrations.aiohttp import AioHttpIntegration
SENTRY_SDK_AVAILABLE = True
except ImportError:
# Sentry SDK is not installed with deb package in order to simplify packaging
SENTRY_SDK_AVAILABLE = False
import os import os
import sys import sys
import struct import struct
import aiohttp
import platform import platform
import locale import locale
import distro import distro
try:
import raven
from raven.transport.http import HTTPTransport
RAVEN_AVAILABLE = True
except ImportError:
# raven is not installed with deb package in order to simplify packaging
RAVEN_AVAILABLE = False
from .version import __version__, __version_info__ from .version import __version__, __version_info__
from .config import Config from .config import Config
from .utils.get_resource import get_resource from .utils.get_resource import get_resource
@ -59,48 +58,45 @@ class CrashReport:
""" """
DSN = "https://dbfb677c73304b1286aef33dfbb749c6:93b9a937d4884426a1b15f37536fcd94@o19455.ingest.sentry.io/38482" DSN = "https://dbfb677c73304b1286aef33dfbb749c6:93b9a937d4884426a1b15f37536fcd94@o19455.ingest.sentry.io/38482"
if hasattr(sys, "frozen"):
cacert = get_resource("cacert.pem")
if cacert is not None and os.path.isfile(cacert):
DSN += "?ca_certs={}".format(cacert)
else:
log.warning("The SSL certificate bundle file '{}' could not be found".format(cacert))
_instance = None _instance = None
def __init__(self): def __init__(self):
self._client = None
# We don't want sentry making noise if an error is catched when you don't have internet # We don't want sentry making noise if an error is caught when you don't have internet
sentry_errors = logging.getLogger('sentry.errors') sentry_errors = logging.getLogger('sentry.errors')
sentry_errors.disabled = True sentry_errors.disabled = True
sentry_uncaught = logging.getLogger('sentry.errors.uncaught') sentry_uncaught = logging.getLogger('sentry.errors.uncaught')
sentry_uncaught.disabled = True sentry_uncaught.disabled = True
def capture_exception(self, request=None): if SENTRY_SDK_AVAILABLE:
if not RAVEN_AVAILABLE: cacert = None
return if hasattr(sys, "frozen"):
if os.path.exists(".git"): cacert_resource = get_resource("cacert.pem")
log.warning("A .git directory exist crash report is turn off for developers") if cacert_resource is not None and os.path.isfile(cacert_resource):
return cacert = cacert_resource
server_config = Config.instance().get_section_config("Server") else:
if server_config.getboolean("report_errors"): log.error("The SSL certificate bundle file '{}' could not be found".format(cacert_resource))
if self._client is None:
self._client = raven.Client(CrashReport.DSN, release=__version__, raise_send_errors=True, transport=HTTPTransport)
if request is not None:
self._client.http_context({
"method": request.method,
"url": request.path,
"data": request.json,
})
context = { sentry_sdk.init(dsn=CrashReport.DSN,
release=__version__,
ca_certs=cacert,
integrations=[AioHttpIntegration()])
tags = {
"os:name": platform.system(), "os:name": platform.system(),
"os:release": platform.release(), "os:release": platform.release(),
"os:win_32": " ".join(platform.win32_ver()), "os:win_32": " ".join(platform.win32_ver()),
"os:mac": "{} {}".format(platform.mac_ver()[0], platform.mac_ver()[2]), "os:mac": "{} {}".format(platform.mac_ver()[0], platform.mac_ver()[2]),
"os:linux": " ".join(distro.linux_distribution()), "os:linux": " ".join(distro.linux_distribution()),
"aiohttp:version": aiohttp.__version__,
}
with sentry_sdk.configure_scope() as scope:
for key, value in tags.items():
scope.set_tag(key, value)
extra_context = {
"python:version": "{}.{}.{}".format(sys.version_info[0], "python:version": "{}.{}.{}".format(sys.version_info[0],
sys.version_info[1], sys.version_info[1],
sys.version_info[2]), sys.version_info[2]),
@ -113,8 +109,8 @@ class CrashReport:
# add locale information # add locale information
try: try:
language, encoding = locale.getlocale() language, encoding = locale.getlocale()
context["locale:language"] = language extra_context["locale:language"] = language
context["locale:encoding"] = encoding extra_context["locale:encoding"] = encoding
except ValueError: except ValueError:
pass pass
@ -124,17 +120,28 @@ class CrashReport:
if os.path.isfile(gns3vm_version): if os.path.isfile(gns3vm_version):
try: try:
with open(gns3vm_version) as fd: with open(gns3vm_version) as fd:
context["gns3vm:version"] = fd.readline().strip() extra_context["gns3vm:version"] = fd.readline().strip()
except OSError: except OSError:
pass pass
self._client.tags_context(context) with sentry_sdk.configure_scope() as scope:
for key, value in extra_context.items():
scope.set_extra(key, value)
def capture_exception(self):
if not SENTRY_SDK_AVAILABLE:
return
if os.path.exists(".git"):
log.warning(".git directory detected, crash reporting is turned off for developers.")
return
server_config = Config.instance().get_section_config("Server")
if server_config.getboolean("report_errors"):
try: try:
report = self._client.captureException() sentry_sdk.capture_exception()
log.info("Crash report sent with event ID: {}".format(sentry_sdk.last_event_id()))
except Exception as e: except Exception as e:
log.error("Can't send crash report to Sentry: {}".format(e)) log.error("Can't send crash report to Sentry: {}".format(e))
return
log.info("Crash report sent with event ID: {}".format(self._client.get_ident(report)))
@classmethod @classmethod
def instance(cls): def instance(cls):

View File

@ -242,7 +242,7 @@ class Route(object):
log.error("Uncaught exception detected: {type}".format(type=type(e)), exc_info=1) log.error("Uncaught exception detected: {type}".format(type=type(e)), exc_info=1)
response = Response(request=request, route=route) response = Response(request=request, route=route)
response.set_status(500) response.set_status(500)
CrashReport.instance().capture_exception(request) CrashReport.instance().capture_exception()
exc_type, exc_value, exc_tb = sys.exc_info() exc_type, exc_value, exc_tb = sys.exc_info()
lines = traceback.format_exception(exc_type, exc_value, exc_tb) lines = traceback.format_exception(exc_type, exc_value, exc_tb)
if api_version is not None: if api_version is not None:

View File

@ -4,9 +4,10 @@ jsonschema==2.6.0; python_version < '3.8' # pyup: ignore
aiohttp==3.6.2 aiohttp==3.6.2
aiohttp-cors==0.7.0 aiohttp-cors==0.7.0
aiofiles==0.4.0 aiofiles==0.4.0
aiocontextvars==0.2.2
async_generator>=1.10 async_generator>=1.10
Jinja2>=2.7.3 Jinja2>=2.7.3
raven>=5.23.0 sentry-sdk>=0.14.4
psutil==5.6.6 psutil==5.6.6
async-timeout==3.0.1 async-timeout==3.0.1
distro>=1.3.0 distro>=1.3.0