Skip to content

Commit 4e2f0d4

Browse files
committed
PR feedback: Expose proper setupGlobalHub method
1 parent 63f0d51 commit 4e2f0d4

File tree

9 files changed

+44
-28
lines changed

9 files changed

+44
-28
lines changed

packages/node-experimental/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const span = Sentry.startSpan({ description: 'non-active span' });
8282

8383
doSomethingSlow();
8484

85-
span?.finish();
85+
span.finish();
8686
```
8787

8888
Finally you can also get the currently active span, if you need to do more with it:

packages/node-experimental/src/sdk/init.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { hasTracingEnabled } from '@sentry/core';
22
import type { NodeClient } from '@sentry/node';
33
import { defaultIntegrations as defaultNodeIntegrations, init as initNode } from '@sentry/node';
4-
import { getCurrentHub, setOpenTelemetryContextAsyncContextStrategy } from '@sentry/opentelemetry';
4+
import { setOpenTelemetryContextAsyncContextStrategy, setupGlobalHub } from '@sentry/opentelemetry';
55
import type { Integration } from '@sentry/types';
66
import { parseSemver } from '@sentry/utils';
77

@@ -29,9 +29,7 @@ if (NODE_VERSION.major && NODE_VERSION.major >= 16) {
2929
* Initialize Sentry for Node.
3030
*/
3131
export function init(options: NodeExperimentalOptions | undefined = {}): void {
32-
// Ensure we register our own global hub before something else does
33-
// This will register the OpenTelemetryHub as the global hub
34-
getCurrentHub();
32+
setupGlobalHub();
3533

3634
const isTracingEnabled = hasTracingEnabled(options);
3735

packages/node-experimental/test/integration/scope.test.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub } from '@sentry/opentelemetry';
1+
import { getCurrentHub, getSpanScope } from '@sentry/opentelemetry';
22

33
import * as Sentry from '../../src/';
44
import type { NodeExperimentalClient } from '../../src/types';
@@ -13,7 +13,7 @@ describe('Integration | Scope', () => {
1313
['with tracing', true],
1414
['without tracing', false],
1515
])('%s', (_name, enableTracing) => {
16-
it('correctly syncs OTEL context & Sentry hub/scope ', async () => {
16+
it('correctly syncs OTEL context & Sentry hub/scope', async () => {
1717
const beforeSend = jest.fn(() => null);
1818
const beforeSendTransaction = jest.fn(() => null);
1919

@@ -41,8 +41,10 @@ describe('Integration | Scope', () => {
4141
scope2.setTag('tag3', 'val3');
4242

4343
Sentry.startSpan({ name: 'outer' }, span => {
44-
spanId = span?.spanContext().spanId;
45-
traceId = span?.spanContext().traceId;
44+
expect(getSpanScope(span)).toBe(enableTracing ? scope2 : undefined);
45+
46+
spanId = span.spanContext().spanId;
47+
traceId = span.spanContext().traceId;
4648

4749
Sentry.setTag('tag4', 'val4');
4850

@@ -138,8 +140,8 @@ describe('Integration | Scope', () => {
138140
scope2.setTag('tag3', 'val3a');
139141

140142
Sentry.startSpan({ name: 'outer' }, span => {
141-
spanId1 = span?.spanContext().spanId;
142-
traceId1 = span?.spanContext().traceId;
143+
spanId1 = span.spanContext().spanId;
144+
traceId1 = span.spanContext().traceId;
143145

144146
Sentry.setTag('tag4', 'val4a');
145147

@@ -155,8 +157,8 @@ describe('Integration | Scope', () => {
155157
scope2.setTag('tag3', 'val3b');
156158

157159
Sentry.startSpan({ name: 'outer' }, span => {
158-
spanId2 = span?.spanContext().spanId;
159-
traceId2 = span?.spanContext().traceId;
160+
spanId2 = span.spanContext().spanId;
161+
traceId2 = span.spanContext().traceId;
160162

161163
Sentry.setTag('tag4', 'val4b');
162164

packages/opentelemetry/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ This package exposes a few building blocks you can add to your OpenTelemetry set
3838

3939
This is how you can use this in your app:
4040

41-
1. Call custom `getCurrentHub()` before anything else, to ensure we have the correct global hub registered
41+
1. Setup the global hub for OpenTelemetry compatibility - ensure `setupGlobalHub()` is called before anything else!
4242
1. Initialize Sentry, e.g. `@sentry/node` - make sure to set `instrumenter: 'otel'` in the SDK `init({})`!
4343
1. Call `setupEventContextTrace(client)`
4444
1. Add `SentrySampler` as sampler
@@ -53,6 +53,7 @@ For example, you could set this up as follows:
5353
import * as Sentry from '@sentry/node';
5454
import {
5555
getCurrentHub,
56+
setupGlobalHub,
5657
SentryPropagator,
5758
SentrySampler,
5859
SentrySpanProcessor,
@@ -63,7 +64,7 @@ import {
6364
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
6465

6566
function setupSentry() {
66-
getCurrentHub();
67+
setupGlobalHub();
6768

6869
Sentry.init({
6970
dsn: 'xxx',

packages/opentelemetry/src/custom/hub.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ export function getCurrentHub(): Hub {
7272
return getGlobalHub(registry);
7373
}
7474

75+
/**
76+
* Ensure the global hub is an OpenTelemetryHub.
77+
*/
78+
export function setupGlobalHub(): void {
79+
const globalRegistry = getMainCarrier();
80+
81+
if (getGlobalHub(globalRegistry) instanceof OpenTelemetryHub) {
82+
return;
83+
}
84+
85+
// If the current global hub is not correct, ensure we overwrite it
86+
setHubOnCarrier(globalRegistry, new OpenTelemetryHub());
87+
}
88+
7589
/**
7690
* This will create a new {@link Hub} and add to the passed object on
7791
* __SENTRY__.hub.

packages/opentelemetry/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export { isSentryRequestSpan } from './utils/isSentryRequest';
2424
export { getActiveSpan, getRootSpan } from './utils/getActiveSpan';
2525
export { startSpan, startInactiveSpan } from './trace';
2626

27-
export { getCurrentHub } from './custom/hub';
27+
export { getCurrentHub, setupGlobalHub } from './custom/hub';
2828
export { addTracingExtensions } from './custom/hubextensions';
2929
export { setupEventContextTrace } from './setupEventContextTrace';
3030

packages/opentelemetry/test/helpers/mockSdkInit.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { ClientOptions, Options } from '@sentry/types';
44
import { GLOBAL_OBJ } from '@sentry/utils';
55

66
import { setOpenTelemetryContextAsyncContextStrategy } from '../../src/asyncContextStrategy';
7-
import { getCurrentHub } from '../../src/custom/hub';
7+
import { setupGlobalHub } from '../../src/custom/hub';
88
import { initOtel } from './initOtel';
99
import { init as initTestClient } from './TestClient';
1010

@@ -14,9 +14,7 @@ const PUBLIC_DSN = 'https://username@domain/123';
1414
* Initialize Sentry for Node.
1515
*/
1616
function init(options: Partial<Options> | undefined = {}): void {
17-
// Ensure we register our own global hub before something else does
18-
// This will register the NodeExperimentalHub as the global hub
19-
getCurrentHub();
17+
setupGlobalHub();
2018

2119
const fullOptions: Partial<Options> = {
2220
instrumenter: 'otel',

packages/opentelemetry/test/integration/otelTimedEvents.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ describe('Integration | OTEL TimedEvents', () => {
2020
const client = hub.getClient() as TestClientInterface;
2121

2222
startSpan({ name: 'test' }, span => {
23-
span?.addEvent('exception', {
23+
span.addEvent('exception', {
2424
[SemanticAttributes.EXCEPTION_MESSAGE]: 'test-message',
2525
'test-span-event-attr': 'test-span-event-attr-value',
2626
});
2727

28-
span?.addEvent('other', {
28+
span.addEvent('other', {
2929
[SemanticAttributes.EXCEPTION_MESSAGE]: 'test-message-2',
3030
'test-span-event-attr': 'test-span-event-attr-value',
3131
});

packages/opentelemetry/test/integration/scope.test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { captureException, setTag, withScope } from '@sentry/core';
33
import { getCurrentHub, OpenTelemetryHub } from '../../src/custom/hub';
44
import { OpenTelemetryScope } from '../../src/custom/scope';
55
import { startSpan } from '../../src/trace';
6+
import { getSpanScope } from '../../src/utils/spanData';
67
import { cleanupOtel, mockSdkInit } from '../helpers/mockSdkInit';
78
import type { TestClientInterface } from '../helpers/TestClient';
89

@@ -46,8 +47,10 @@ describe('Integration | Scope', () => {
4647
scope2.setTag('tag3', 'val3');
4748

4849
startSpan({ name: 'outer' }, span => {
49-
spanId = span?.spanContext().spanId;
50-
traceId = span?.spanContext().traceId;
50+
expect(getSpanScope(span)).toBe(enableTracing ? scope2 : undefined);
51+
52+
spanId = span.spanContext().spanId;
53+
traceId = span.spanContext().traceId;
5154

5255
setTag('tag4', 'val4');
5356

@@ -147,8 +150,8 @@ describe('Integration | Scope', () => {
147150
scope2.setTag('tag3', 'val3a');
148151

149152
startSpan({ name: 'outer' }, span => {
150-
spanId1 = span?.spanContext().spanId;
151-
traceId1 = span?.spanContext().traceId;
153+
spanId1 = span.spanContext().spanId;
154+
traceId1 = span.spanContext().traceId;
152155

153156
setTag('tag4', 'val4a');
154157

@@ -164,8 +167,8 @@ describe('Integration | Scope', () => {
164167
scope2.setTag('tag3', 'val3b');
165168

166169
startSpan({ name: 'outer' }, span => {
167-
spanId2 = span?.spanContext().spanId;
168-
traceId2 = span?.spanContext().traceId;
170+
spanId2 = span.spanContext().spanId;
171+
traceId2 = span.spanContext().traceId;
169172

170173
setTag('tag4', 'val4b');
171174

0 commit comments

Comments
 (0)