gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:26:48 26 4
gpt4 key购买 nike

我有一个带有运行状况检查端点的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com