Skip to content

chore(browser): Export ipAddress helpers for use in other SDKs #15079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 3 additions & 29 deletions packages/browser/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import type {
ParameterizedString,
Scope,
SeverityLevel,
User,
} from '@sentry/core';
import { Client, applySdkMetadata, getSDKSource } from '@sentry/core';
import { eventFromException, eventFromMessage } from './eventbuilder';
import { WINDOW } from './helpers';
import type { BrowserTransportOptions } from './transports/types';
import { addAutoIpAddressToSession, addAutoIpAddressToUser } from './utils/ipAddress';

/**
* Configuration options for the Sentry Browser SDK.
Expand Down Expand Up @@ -84,22 +84,8 @@ export class BrowserClient extends Client<BrowserClientOptions> {
});
}

this.on('postprocessEvent', event => {
addAutoIpAddressToUser(event);
});

this.on('beforeSendSession', session => {
if ('aggregates' in session) {
if (session.attrs?.['ip_address'] === undefined) {
session.attrs = {
...session.attrs,
ip_address: '{{auto}}',
};
}
} else {
addAutoIpAddressToUser(session);
}
});
this.on('postprocessEvent', addAutoIpAddressToUser);
this.on('beforeSendSession', addAutoIpAddressToSession);
}

/**
Expand Down Expand Up @@ -134,15 +120,3 @@ export class BrowserClient extends Client<BrowserClientOptions> {
return super._prepareEvent(event, hint, currentScope, isolationScope);
}
}

// 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
function addAutoIpAddressToUser(objWithMaybeUser: { user?: User | null }): void {
if (objWithMaybeUser.user?.ip_address === undefined) {
objWithMaybeUser.user = {
...objWithMaybeUser.user,
ip_address: '{{auto}}',
};
}
}
1 change: 1 addition & 0 deletions packages/browser/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,4 @@ export { linkedErrorsIntegration } from './integrations/linkederrors';
export { browserApiErrorsIntegration } from './integrations/browserapierrors';

export { lazyLoadIntegration } from './utils/lazyLoadIntegration';
export { addAutoIpAddressToSession, addAutoIpAddressToUser } from './utils/ipAddress';
34 changes: 34 additions & 0 deletions packages/browser/src/utils/ipAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Session, SessionAggregates, User } from '@sentry/core';

/**
* @internal
* 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
*/
export function addAutoIpAddressToUser(objWithMaybeUser: { user?: User | null }): void {
if (objWithMaybeUser.user?.ip_address === undefined) {
objWithMaybeUser.user = {
...objWithMaybeUser.user,
ip_address: '{{auto}}',
};
}
}

/**
* @internal
* Adds the `ip_address` attribute to the session if it is not explicitly set to `null` or a value.
* @param session The session to add the `ip_address` attribute to.
*/
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 {
addAutoIpAddressToUser(session);
}
}
Loading