【发布时间】:2021-01-13 22:44:05
【问题描述】:
在我的 Laravel 的 middleware 中,我生成了一个自定义标头值。
$correlationId = Uuid::uuid4()->toString();
if ($request->headers->has('Correlation-ID') === false) {
$request->headers->set('Correlation-ID', $correlationId);
}
当我抛出异常时,我很容易在 Sentry 中得到它以及我的自定义标头。
但是使用 Sentry 和 Correlation Id 的强大之处在于,当您标记某些内容时,每个标记的值都会被编入索引,因此它会放大搜索功能以跟踪问题。
我找到了添加标签的逻辑:
\Sentry\configureScope(function (\Sentry\State\Scope $scope): void {
$scope->setTag('correlation_id', app(\Illuminate\Http\Request::class)->headers->get('Correlation-ID'));
});
问题是我不知道该放在哪里。当我将它添加到同一个中间件时,它不会将我的新 correlation_id 标签推送到哨兵中。当我将此代码放入目标微服务中AppServiceProvider 类中的boot 方法时也是如此。
我可以确认我得到了没有问题的 UUID app(\Illuminate\Http\Request::class)->headers->get('Correlation-ID') 但 Sentry 只显示内置标签:
我做错了什么?
【问题讨论】: