文章目录
- 一、注册账号:
- 1、能创建多个项目:
- 2、接收的监控:
- 二、安装模块
- 三、简单使用:
- 1、简单测试1:
- 报警:
- 2、下面我模仿一个爬虫失败的监控:
一、注册账号:
注册地址 进入之后使用邮箱注册,我使用QQ邮箱注册就能使用。
注册成功之后,会跳转让选择语言界面,我这里选择python:
我的跳转后地址:(https://sentry.io/onboarding/b4754e1b71/get-started/
)
记得把b4754e1b71
替换成你的项目id。
如果不小心点没了,也可以:登录之后浏览器有缓存登录的cookie,直接访问:
https://sentry.io/,会自动跳转你的项目。
1、能创建多个项目:
登录之后进入首页,点击进入project是即可进入查看已有的项目或者创建新的项目。
2、接收的监控:
二、安装模块
pip install --upgrade sentry-sdk==0.13.4
三、简单使用:
1、简单测试1:
上面就是寻找这个简单测试的代码的步骤截图:
import sentry_sdk
import requests
sentry_sdk.init("https://改为你的@sentry.io/你的")
division_by_zero = 1 / 0
运行之后:
报警:
最上面的是最新的报警:
2、下面我模仿一个爬虫失败的监控:
其实我刚刚测试过一次了,在测试一次:
import sentry_sdk
import requests
sentry_sdk.init("https://改为你的@sentry.io/改为你的")
# division_by_zero = 1 / 0
requests.get(url='https://sentry.io11')
运行之后:
报错内容:
Traceback (most recent call last):
File "D:\tools\Python3.6\lib\site-packages\urllib3\connection.py", line 157, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw
File "D:\tools\Python3.6\lib\site-packages\urllib3\util\connection.py", line 61, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "D:\tools\Python3.6\lib\socket.py", line 745, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\tools\Python3.6\lib\site-packages\urllib3\connectionpool.py", line 672, in urlopen
chunked=chunked,
File "D:\tools\Python3.6\lib\site-packages\urllib3\connectionpool.py", line 376, in _make_request
self._validate_conn(conn)
File "D:\tools\Python3.6\lib\site-packages\urllib3\connectionpool.py", line 994, in _validate_conn
conn.connect()
File "D:\tools\Python3.6\lib\site-packages\urllib3\connection.py", line 334, in connect
conn = self._new_conn()
File "D:\tools\Python3.6\lib\site-packages\urllib3\connection.py", line 169, in _new_conn
self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x00000219B7F66A90>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\tools\Python3.6\lib\site-packages\requests\adapters.py", line 449, in send
timeout=timeout
File "D:\tools\Python3.6\lib\site-packages\urllib3\connectionpool.py", line 720, in urlopen
method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
File "D:\tools\Python3.6\lib\site-packages\urllib3\util\retry.py", line 436, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='sentry.io11', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x00000219B7F66A90>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/zjf_workspace/000、爬虫代码-基础的/scrapy_100_工具/18、sentry监控系统/1、demo.py", line 6, in <module>
requests.get(url='https://sentry.io11')
File "D:\tools\Python3.6\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "D:\tools\Python3.6\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "D:\tools\Python3.6\lib\site-packages\requests\sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "D:\tools\Python3.6\lib\site-packages\requests\sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "D:\tools\Python3.6\lib\site-packages\requests\adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='sentry.io11', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x00000219B7F66A90>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))
Sentry is attempting to send 0 pending error messages
Waiting up to 2 seconds
Press Ctrl-Break to quit
可以看到,最后的:
Sentry is attempting to send 0 pending error messages
Waiting up to 2 seconds
Press Ctrl-Break to quit
这几句就是监控系统的几录发送了。可以进入查看:
可以看出监控系统几录,最新的刚刚是events是1,现在变为2了,而且很明显是链接不上ConnectionError
,常见的爬虫错误,因为我给的url是个错误的,主要是为了模仿禁止ip的网站,或者网站已经生效。进入查看一下报错。
后面的就没有什么讲解了,可以根据场合需要,自己改写代码。
注意点:
记得如果加try就不会监控报错记录了哈:
try:
requests.get(url='https://sentry.io11')
except:
pass
比如这种就不会再有报错的监控记录了。
使用场所:
我觉得适用在不确定会报什么错误,如果知道会有什么错误,也就不用监控了,直接对特定错误处理就行了,代码中使用监控,如果结合远程监控系统测试下如果报错之后,可以进行远程操作,进行代码远程改写,这样会对工作效率有个很好的改善,不过远程的操作我还不会,我会有时间就学习一下,其实可以结合邮件发送效果更好,因为有邮件提醒,可以时时知道是否报警错误,监控系统可以知道具体报错文件和报错位置,加上远程控制更方便的工作,下次我就要学习一下远程操作电脑了。