Skip to content

Commit

Permalink
chore(browser): Export ipAddress helpers for use in other SDKs (#15079)
Browse files Browse the repository at this point in the history
- Related to #15008

Export helpers to avoid duplications in sentry/react-native.

- RN PR getsentry/sentry-react-native#4466
  • Loading branch information
krystofwoldrich authored Feb 3, 2025
1 parent e8c0260 commit 3c4df06
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
33 changes: 9 additions & 24 deletions packages/browser/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -84,29 +90,8 @@ export class BrowserClient extends Client<BrowserClientOptions> {
}

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);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
35 changes: 35 additions & 0 deletions packages/core/src/utils/ipAddress.ts
Original file line number Diff line number Diff line change
@@ -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}}';
}
}
}

0 comments on commit 3c4df06

Please sign in to comment.