Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/libs/telemetry/middlewares/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import type {EventHint, TransactionEvent} from '@sentry/core';
import emailDomainFilter from './emailDomainFilter';
import firebasePerformanceFilter from './firebasePerformanceFilter';
import minDurationFilter from './minDurationFilter';
import scopeTagsEnricher from './scopeTagsEnricher';

type TelemetryBeforeSend = (event: TransactionEvent, hint: EventHint) => TransactionEvent | null | Promise<TransactionEvent | null>;

const middlewares: TelemetryBeforeSend[] = [emailDomainFilter, firebasePerformanceFilter, minDurationFilter];
const middlewares: TelemetryBeforeSend[] = [emailDomainFilter, firebasePerformanceFilter, minDurationFilter, scopeTagsEnricher];

function processBeforeSendTransactions(event: TransactionEvent, hint: EventHint): Promise<TransactionEvent | null> {
return middlewares.reduce(
Expand Down
38 changes: 38 additions & 0 deletions src/libs/telemetry/middlewares/scopeTagsEnricher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type {TransactionEvent} from '@sentry/core';
import * as Sentry from '@sentry/react-native';
import CONST from '@src/CONST';
import type {TelemetryBeforeSend} from './index';

/**
* Enriches transaction with tags and contexts from the current Sentry scope.
* This ensures that tags set asynchronously (e.g. nudge_migration_cohort)
* are included in transactions that were created before the tags were set.
* This middleware applies the tag at send-time, ensuring early spans get the tags.
*/
const scopeTagsEnricher: TelemetryBeforeSend = (event: TransactionEvent): TransactionEvent => {
const scope = Sentry.getCurrentScope();
const scopeData = scope.getScopeData();

const enrichedEvent = {
...event,
tags: {
...event.tags,
...(scopeData.tags?.[CONST.TELEMETRY.TAG_NUDGE_MIGRATION_COHORT] && {
[CONST.TELEMETRY.TAG_NUDGE_MIGRATION_COHORT]: scopeData.tags[CONST.TELEMETRY.TAG_NUDGE_MIGRATION_COHORT],
}),
...(scopeData.tags?.[CONST.TELEMETRY.TAG_ACTIVE_POLICY] && {
[CONST.TELEMETRY.TAG_ACTIVE_POLICY]: scopeData.tags[CONST.TELEMETRY.TAG_ACTIVE_POLICY],
}),
},
contexts: {
...event.contexts,
...(scopeData.contexts?.[CONST.TELEMETRY.CONTEXT_POLICIES] && {
[CONST.TELEMETRY.CONTEXT_POLICIES]: scopeData.contexts[CONST.TELEMETRY.CONTEXT_POLICIES],
}),
},
};

return enrichedEvent;
};

export default scopeTagsEnricher;
Loading