Skip to content

Commit e2dd6a6

Browse files
committed
create utility function for getting org id
1 parent 33385ff commit e2dd6a6

File tree

5 files changed

+82
-18
lines changed

5 files changed

+82
-18
lines changed

packages/core/src/tracing/dynamicSamplingContext.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ import {
1515
baggageHeaderToDynamicSamplingContext,
1616
dynamicSamplingContextToSentryBaggageHeader,
1717
} from '../utils-hoist/baggage';
18-
import { extractOrgIdFromDsnHost } from '../utils-hoist/dsn';
1918
import { addNonEnumerableProperty } from '../utils-hoist/object';
20-
import { getCapturedScopesOnSpan } from './utils';
19+
import { deriveOrgIdFromClient, getCapturedScopesOnSpan } from './utils';
2120

2221
/**
2322
* If you change this value, also update the terser plugin config to
@@ -45,14 +44,7 @@ export function freezeDscOnSpan(span: Span, dsc: Partial<DynamicSamplingContext>
4544
export function getDynamicSamplingContextFromClient(trace_id: string, client: Client): DynamicSamplingContext {
4645
const options = client.getOptions();
4746

48-
const { publicKey: public_key, host } = client.getDsn() || {};
49-
50-
let org_id: string | undefined;
51-
if (options.orgId) {
52-
org_id = String(options.orgId);
53-
} else if (host) {
54-
org_id = extractOrgIdFromDsnHost(host);
55-
}
47+
const { publicKey: public_key } = client.getDsn() || {};
5648

5749
// Instead of conditionally adding non-undefined values, we add them and then remove them if needed
5850
// otherwise, the order of baggage entries changes, which "breaks" a bunch of tests etc.
@@ -61,7 +53,7 @@ export function getDynamicSamplingContextFromClient(trace_id: string, client: Cl
6153
release: options.release,
6254
public_key,
6355
trace_id,
64-
org_id,
56+
org_id: deriveOrgIdFromClient(client),
6557
};
6658

6759
client.emit('createDsc', dsc);

packages/core/src/tracing/trace.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { parseSampleRate } from '../utils/parseSampleRate';
1717
import { _getSpanForScope, _setSpanForScope } from '../utils/spanOnScope';
1818
import { addChildSpanToSpan, getRootSpan, spanIsSampled, spanTimeInputToSeconds, spanToJSON } from '../utils/spanUtils';
1919
import { baggageHeaderToDynamicSamplingContext } from '../utils-hoist/baggage';
20-
import { extractOrgIdFromDsnHost } from '../utils-hoist/dsn';
2120
import { logger } from '../utils-hoist/logger';
2221
import { generateTraceId } from '../utils-hoist/propagationContext';
2322
import { propagationContextFromHeaders } from '../utils-hoist/tracing';
@@ -27,7 +26,7 @@ import { sampleSpan } from './sampling';
2726
import { SentryNonRecordingSpan } from './sentryNonRecordingSpan';
2827
import { SentrySpan } from './sentrySpan';
2928
import { SPAN_STATUS_ERROR } from './spanstatus';
30-
import { setCapturedScopesOnSpan } from './utils';
29+
import { deriveOrgIdFromClient, setCapturedScopesOnSpan } from './utils';
3130

3231
const SUPPRESS_TRACING_KEY = '__SENTRY_SUPPRESS_TRACING__';
3332

@@ -225,11 +224,7 @@ export const continueTrace = <V>(
225224
const incomingDsc = baggageHeaderToDynamicSamplingContext(baggage);
226225
const baggageOrgId = incomingDsc?.org_id;
227226

228-
let sdkOrgId: string | undefined;
229-
const dsn = client?.getDsn();
230-
if (dsn?.host) {
231-
sdkOrgId = extractOrgIdFromDsnHost(dsn.host);
232-
}
227+
const sdkOrgId = deriveOrgIdFromClient(client);
233228

234229
const shouldStartNewTrace = (): boolean => {
235230
// Case: baggage org ID and SDK org ID don't match - always start new trace

packages/core/src/tracing/utils.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import type { Client } from '../client';
12
import type { Scope } from '../scope';
23
import type { Span } from '../types-hoist/span';
4+
import { extractOrgIdFromDsnHost } from '../utils-hoist/dsn';
35
import { addNonEnumerableProperty } from '../utils-hoist/object';
46

57
const SCOPE_ON_START_SPAN_FIELD = '_sentryScope';
@@ -27,3 +29,24 @@ export function getCapturedScopesOnSpan(span: Span): { scope?: Scope; isolationS
2729
isolationScope: (span as SpanWithScopes)[ISOLATION_SCOPE_ON_START_SPAN_FIELD],
2830
};
2931
}
32+
33+
/**
34+
* Returns the organization ID of the client.
35+
*
36+
* The organization ID is extracted from the DSN. If the client options include a `orgId`, this will always take precedence.
37+
*/
38+
export function deriveOrgIdFromClient(client: Client | undefined): string | undefined {
39+
const options = client?.getOptions();
40+
41+
const { host } = client?.getDsn() || {};
42+
43+
let org_id: string | undefined;
44+
45+
if (options?.orgId) {
46+
org_id = String(options.orgId);
47+
} else if (host) {
48+
org_id = extractOrgIdFromDsnHost(host);
49+
}
50+
51+
return org_id;
52+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { beforeEach, describe, expect, test, vi } from 'vitest';
2+
import { deriveOrgIdFromClient } from '../../../src/tracing/utils';
3+
import { getDefaultTestClientOptions, TestClient } from '../../mocks/client';
4+
5+
describe('deriveOrgIdFromClient', () => {
6+
let client: TestClient;
7+
8+
beforeEach(() => {
9+
vi.clearAllMocks();
10+
});
11+
12+
test('returns orgId from client options when available', () => {
13+
client = new TestClient(
14+
getDefaultTestClientOptions({
15+
orgId: '00222111',
16+
dsn: 'https://[email protected]/1',
17+
}),
18+
);
19+
20+
const result = deriveOrgIdFromClient(client);
21+
expect(result).toBe('00222111');
22+
});
23+
24+
test('converts non-string orgId to string', () => {
25+
client = new TestClient(
26+
getDefaultTestClientOptions({
27+
orgId: 12345,
28+
dsn: 'https://[email protected]/1',
29+
}),
30+
);
31+
32+
const result = deriveOrgIdFromClient(client);
33+
expect(result).toBe('12345');
34+
});
35+
36+
test('extracts orgId from DSN host when options.orgId is not available', () => {
37+
client = new TestClient(
38+
getDefaultTestClientOptions({
39+
dsn: 'https://[email protected]/1',
40+
}),
41+
);
42+
43+
const result = deriveOrgIdFromClient(client);
44+
expect(result).toBe('012300');
45+
});
46+
47+
test('returns undefined when neither options.orgId nor DSN host are available', () => {
48+
client = new TestClient(getDefaultTestClientOptions({}));
49+
50+
const result = deriveOrgIdFromClient(client);
51+
expect(result).toBeUndefined();
52+
});
53+
});

packages/core/tsconfig.test.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
"compilerOptions": {
77
"lib": ["DOM", "ES2018"],
8+
"module": "ESNext", // support dynamic import()
89
// should include all types from `./tsconfig.json` plus types for all test frameworks used
910
"types": ["node"]
1011

0 commit comments

Comments
 (0)