Skip to content

Commit a378d30

Browse files
[sync] fix(backend): add sentry tags for v2 requests (#1475) (#2787)
Synced from teableio/teable-ee@7350b83 Co-authored-by: nichenqin <nichenqin@hotmail.com>
1 parent 5b2c453 commit a378d30

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

apps/nestjs-backend/src/features/canary/interceptors/v2-indicator.interceptor.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type CallHandler,
77
Logger,
88
} from '@nestjs/common';
9+
import * as Sentry from '@sentry/nestjs';
910
import { trace } from '@opentelemetry/api';
1011
import type { Response } from 'express';
1112
import { ClsService } from 'nestjs-cls';
@@ -17,6 +18,36 @@ export const X_TEABLE_V2_HEADER = 'x-teable-v2';
1718
export const X_TEABLE_V2_REASON_HEADER = 'x-teable-v2-reason';
1819
export const X_TEABLE_V2_FEATURE_HEADER = 'x-teable-v2-feature';
1920

21+
type SentryScopeLike = {
22+
setTag(key: string, value: string): void;
23+
};
24+
25+
const getSentryScopes = (): SentryScopeLike[] => {
26+
const sentryApi = Sentry as unknown as {
27+
getCurrentScope?: () => SentryScopeLike | undefined;
28+
getIsolationScope?: () => SentryScopeLike | undefined;
29+
getCurrentHub?: () => { getScope?: () => SentryScopeLike | undefined };
30+
};
31+
32+
const scopes = [
33+
sentryApi.getCurrentScope?.(),
34+
sentryApi.getIsolationScope?.(),
35+
sentryApi.getCurrentHub?.()?.getScope?.(),
36+
].filter((scope): scope is SentryScopeLike => Boolean(scope));
37+
38+
return [...new Set(scopes)];
39+
};
40+
41+
const setSentryTag = (key: string, value: string | undefined) => {
42+
if (value == null) {
43+
return;
44+
}
45+
46+
for (const scope of getSentryScopes()) {
47+
scope.setTag(key, value);
48+
}
49+
};
50+
2051
/**
2152
* Interceptor that adds V2 indicator to response headers and logs.
2253
* When a request uses V2 implementation (determined by V2FeatureGuard),
@@ -51,6 +82,12 @@ export class V2IndicatorInterceptor implements NestInterceptor {
5182
response.setHeader(X_TEABLE_V2_FEATURE_HEADER, v2Feature);
5283
}
5384

85+
// Mirror V2 indicators into Sentry tags so issue search can distinguish v1/v2 requests.
86+
setSentryTag('teable.version', useV2 ? 'v2' : 'v1');
87+
setSentryTag('teable.v2.enabled', useV2 ? 'true' : 'false');
88+
setSentryTag('teable.v2.reason', v2Reason);
89+
setSentryTag('teable.v2.feature', v2Feature);
90+
5491
// Add span attributes for tracing
5592
const span = trace.getActiveSpan();
5693
if (span) {

0 commit comments

Comments
 (0)