Sentry事件清理器及工具库
项目描述
Python sentry-sdk有一个before_send钩子,允许您在事件发送之前清理Sentry事件。Fillmore使得设置before_send清理器并测试它变得更加容易。
- 代码::
- 问题::
- 许可证::
- MPL v2 
- 文档::
目标
Fillmore的目标
- 使您能够以可推理的方式配置Sentry事件清理 
- 使您更容易测试您的清理代码,以便您知道它在一段时间内是否正常工作 
- 以弹性方式清理,并在发生错误时默认发出信号,以便您知道何时错误处理代码抛出错误 
基于此,Fillmore具有以下功能
- 允许您指定在Sentry事件中清理的键 
- 对错误具有弹性——如果它失败,它将发出您可以看到并提醒的信号 
- 链接到相关的Sentry文档、项目和其它内容 
- 为您的集成测试提供测试基础设施 
安装
运行
$ pip install fillmore
快速入门
示例
# myapp/app.py
import logging
import logging.config
from fillmore.libsentry import set_up_sentry
from fillmore.scrubber import Scrubber, Rule, build_scrub_query_string
# Set up logging to capture fillmore error messages
logging.getLogger("fillmore").setLevel(logging.ERROR)
# Create a scrubber
scrubber = Scrubber(
    rules=[
        Rule(
            path="request.headers",
            keys=["Auth-Token", "Cookie"],
            scrub="scrub",
        ),
        Rule(
            path="request",
            keys=["query_string"],
            scrub=build_scrub_query_string(params=["code", "state"]),
        ),
        Rule(
            path="exception.values.[].stacktrace.frames.[].vars",
            keys=["username", "password"],
            scrub="scrub",
        ),
    ]
)
# Set up Sentry with the scrubber and the default integrations which
# includes the LoggingIntegration which will capture messages with level
# logging.ERROR.
set_up_sentry(
    sentry_dsn="http://user@example.com/1",
    host_id="some host id",
    release="some release name",
    before_send=scrubber,
)
def kick_up_exception():
    username = "James"  # noqa
    try:
        raise Exception("internal exception")
    except Exception:
        logging.getLogger(__name__).exception("kick_up_exception exception")
现在您有了刮擦器,并且已经设置了Sentry客户端来使用它。您如何知道它正在刮除正确的内容?如果发生变化,它不再刮除正确的内容,您将如何知道?
您可以这样测试它
# myapp/test_app.py
import unittest
from fillmore.test import SentryTestHelper
from myapp.app import kick_up_exception
class TestApp(unittest.TestCase):
    def test_scrubber(self):
        # Reuse the existing Sentry configuration and set up the helper
        # to capture Sentry events
        sentry_test_helper = SentryTestHelper()
        with sentry_test_helper.reuse() as sentry_client:
            kick_up_exception()
            (event,) = sentry_client.events
            error = event["exception"]["values"][0]
            self.assertEqual(error["type"], "Exception")
            self.assertEqual(error["value"], "internal exception")
            self.assertEqual(
                error["stacktrace"]["frames"][0]["vars"]["username"], "[Scrubbed]"
            )
这将为这次测试创建一个特定的Sentry客户端,并在测试中引发一个异常,然后使用Sentry捕获它。
注意,这是一个使用为这次测试创建的Sentry客户端构建的假设上下文。您将需要编写使用为您的应用程序配置的Sentry客户端以及处理来自您应用程序不同点的事件的测试,以确保Sentry事件被正确刮除。
请参阅Fillmore文档以获取解释和示例。
为什么是这个?为什么不使用其他库?
其他库
- 具有难以理解的API。 - 我不是为了刮擦Sentry事件而刮擦。我需要能够编写非常清楚说明它在做什么和不做什么的刮擦配置。 
- 不涵盖Sentry事件结构的很大一部分。 - 我需要覆盖整个事件结构以及一些奇怪的情况,例如cookie信息会显示两次,并且可以编码为字符串。 
- 不够弹性。 - 刮擦器是在Sentry报告错误的情况下运行的。如果它也出错,那么您可能会陷入永远看不到错误且没有任何信号表明情况严重错误的情况。我们需要刮擦代码具有极高的弹性,并默认发出它已损坏的信号。 
- 不包含测试基础设施。 - 我不是为了刮擦Sentry事件而刮擦。我需要知道刮擦代码正在正常工作,并且在升级Python、sentry_sdk和其他事物时仍然继续工作。 - 拥有测试基础设施以简化此操作非常重要。 
项目详细信息
下载文件
下载适用于您平台文件的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。
源分发
构建分发
fillmore-2.0.1.tar.gz的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 28c1c47063d116909009e4b31f40098702ce1944df7793e8f47642c7c39a78cf | |
| MD5 | a273f66a9bc571469056c65e988a485b | |
| BLAKE2b-256 | 1b52d5ae87dc8be7101f329f1436eb80c720e56e3f121a15d9307893d3e34cfb |