From 3c4df06006adc0a4cd21d393f8b3b0b6d4030768 Mon Sep 17 00:00:00 2001 From: Krystof Woldrich <31292499+krystofwoldrich@users.noreply.github.com> Date: Mon, 3 Feb 2025 19:22:42 +0100 Subject: [PATCH] chore(browser): Export ipAddress helpers for use in other SDKs (#15079) - Related to https://github.com/getsentry/sentry-javascript/pull/15008 Export helpers to avoid duplications in sentry/react-native. - RN PR https://github.com/getsentry/sentry-react-native/pull/4466 --- packages/browser/src/client.ts | 33 +++++++------------------- packages/core/src/index.ts | 1 + packages/core/src/utils/ipAddress.ts | 35 ++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 packages/core/src/utils/ipAddress.ts diff --git a/packages/browser/src/client.ts b/packages/browser/src/client.ts index 20b43ca6ddac..0e5b3fb6214c 100644 --- a/packages/browser/src/client.ts +++ b/packages/browser/src/client.ts @@ -9,7 +9,13 @@ import type { Scope, SeverityLevel, } from '@sentry/core'; -import { Client, applySdkMetadata, getSDKSource } from '@sentry/core'; +import { + Client, + addAutoIpAddressToSession, + addAutoIpAddressToUser, + applySdkMetadata, + getSDKSource, +} from '@sentry/core'; import { eventFromException, eventFromMessage } from './eventbuilder'; import { WINDOW } from './helpers'; import type { BrowserTransportOptions } from './transports/types'; @@ -84,29 +90,8 @@ export class BrowserClient extends Client { } if (this._options.sendDefaultPii) { - this.on('postprocessEvent', event => { - if (event.user?.ip_address === undefined) { - event.user = { - ...event.user, - ip_address: '{{auto}}', - }; - } - }); - - this.on('beforeSendSession', session => { - if ('aggregates' in session) { - if (session.attrs?.['ip_address'] === undefined) { - session.attrs = { - ...session.attrs, - ip_address: '{{auto}}', - }; - } - } else { - if (session.ipAddress === undefined) { - session.ipAddress = '{{auto}}'; - } - } - }); + this.on('postprocessEvent', addAutoIpAddressToUser); + this.on('beforeSendSession', addAutoIpAddressToSession); } } diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 2c89d0e8a60b..fcc1d55128cf 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -72,6 +72,7 @@ export { hasTracingEnabled } from './utils/hasTracingEnabled'; export { isSentryRequestUrl } from './utils/isSentryRequestUrl'; export { handleCallbackErrors } from './utils/handleCallbackErrors'; export { parameterize } from './utils/parameterize'; +export { addAutoIpAddressToSession, addAutoIpAddressToUser } from './utils/ipAddress'; export { spanToTraceHeader, spanToJSON, diff --git a/packages/core/src/utils/ipAddress.ts b/packages/core/src/utils/ipAddress.ts new file mode 100644 index 000000000000..8ca389dc68a1 --- /dev/null +++ b/packages/core/src/utils/ipAddress.ts @@ -0,0 +1,35 @@ +import type { Session, SessionAggregates, User } from '../types-hoist'; + +// By default, we want to infer the IP address, unless this is explicitly set to `null` +// We do this after all other processing is done +// If `ip_address` is explicitly set to `null` or a value, we leave it as is + +/** + * @internal + */ +export function addAutoIpAddressToUser(objWithMaybeUser: { user?: User | null }): void { + if (objWithMaybeUser.user?.ip_address === undefined) { + objWithMaybeUser.user = { + ...objWithMaybeUser.user, + ip_address: '{{auto}}', + }; + } +} + +/** + * @internal + */ +export function addAutoIpAddressToSession(session: Session | SessionAggregates): void { + if ('aggregates' in session) { + if (session.attrs?.['ip_address'] === undefined) { + session.attrs = { + ...session.attrs, + ip_address: '{{auto}}', + }; + } + } else { + if (session.ipAddress === undefined) { + session.ipAddress = '{{auto}}'; + } + } +}