django - 排除 Django 中的一些 URL 以进行 Sentry 性能跟踪

标签 django sentry

我有一个带有运行状况检查端点的 Django 应用程序,该端点使用 django-health-check .

url_patterns 中,我添加了以下行:

  url(r'^ht/', include('health_check.urls')),

问题在于健康检查正在填满所有 Sentry 事务限制。

如何在 Sentry 中排除健康检查端点?

最佳答案

处理这种情况的方法是使用 sampling function根据 URL 或其他参数控制采样率。

def traces_sampler(ctx):
    if 'wsgi_environ' in ctx:
        url = ctx['wsgi_environ'].get('PATH_INFO', '')
        if url.startswith('/ht/'):
            return 0  # Don't trace any
    return 1  # Trace all

sentry_sdk.init(
    # ...
    traces_sampler=traces_sampler,
)

这是一个更完整的例子。

def traces_sampler(ctx):
    if ctx['parent_sampled'] is not None:
        # If this transaction has a parent, we usually want to sample it
        # if and only if its parent was sampled.
        return ctx['parent_sampled']
    op = ctx['transaction_context']['op']
    if 'wsgi_environ' in ctx:
        # Get the URL for WSGI requests
        url = ctx['wsgi_environ'].get('PATH_INFO', '')
    elif 'asgi_scope' in ctx:
        # Get the URL for ASGI requests
        url = ctx['asgi_scope'].get('path', '')
    else:
        # Other kinds of transactions don't have a URL
        url = ''
    if op == 'http.server':
        # Conditions only relevant to operation "http.server"
        if url.startswith('/ht/'):
            return 0  # Don't trace any of these transactions
    return 0.1  # Trace 10% of other transactions

sentry_sdk.init(
    # ...
    traces_sampler=traces_sampler,
)

关于django - 排除 Django 中的一些 URL 以进行 Sentry 性能跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74368412/

相关文章:

python - django:自定义 token 认证

python - 带有值的 Django foo_display()

javascript - _oldOnerrorHandler 未定义; if 语句仍然运行

php - Laravel 和 Sentry : How to set a custom tag in Sentry?

javascript - jQuery 响应在浏览器中为空,但 curl 有效

django - python google sheets API 出现无效的 JSON 负载错误

python - Django & Redis : How do I properly use connection pooling?

java - 运行时从哪里获取 Sentry/Raven DSN?

sentry - 如何在 Sentry 中自定义用户反馈?

java - 如何使用 Sentry 优雅地处理错误尖峰?