Skip to content

Commit 6c9d83d

Browse files
mydeaclaude
andcommitted
ref(opentelemetry): Replace @opentelemetry/resources with inline getSentryResource()
Add a minimal `SentryResource` class in `@sentry/opentelemetry` that satisfies the OTel `Resource` interface, and a `getSentryResource(serviceName)` helper that produces the exact same merged resource we previously built via `defaultResource().merge(resourceFromAttributes({...}))`. This removes direct imports of `@opentelemetry/resources` from `@sentry/node`, `@sentry/vercel-edge`, and test helpers, and drops the package from `node`'s dependencies and `node-core`'s peer/dev dependencies. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 73d51f2 commit 6c9d83d

8 files changed

Lines changed: 71 additions & 67 deletions

File tree

packages/node-core/package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
"@opentelemetry/api": "^1.9.0",
8181
"@opentelemetry/core": "^1.30.1 || ^2.1.0",
8282
"@opentelemetry/instrumentation": ">=0.57.1 <1",
83-
"@opentelemetry/resources": "^1.30.1 || ^2.1.0",
8483
"@opentelemetry/sdk-trace-base": "^1.30.1 || ^2.1.0",
8584
"@opentelemetry/semantic-conventions": "^1.39.0",
8685
"@opentelemetry/exporter-trace-otlp-http": ">=0.57.0 <1"
@@ -95,9 +94,6 @@
9594
"@opentelemetry/instrumentation": {
9695
"optional": true
9796
},
98-
"@opentelemetry/resources": {
99-
"optional": true
100-
},
10197
"@opentelemetry/sdk-trace-base": {
10298
"optional": true
10399
},
@@ -118,7 +114,6 @@
118114
"@opentelemetry/core": "^2.6.1",
119115
"@opentelemetry/exporter-trace-otlp-http": "^0.214.0",
120116
"@opentelemetry/instrumentation": "^0.214.0",
121-
"@opentelemetry/resources": "^2.6.1",
122117
"@opentelemetry/sdk-trace-base": "^2.6.1",
123118
"@opentelemetry/semantic-conventions": "^1.40.0",
124119
"@types/node": "^18.19.1"

packages/node-core/test/helpers/mockSdkInit.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import { context, propagation, ProxyTracerProvider, trace } from '@opentelemetry/api';
2-
import { defaultResource, resourceFromAttributes } from '@opentelemetry/resources';
32
import { BasicTracerProvider, type SpanProcessor } from '@opentelemetry/sdk-trace-base';
4-
import {
5-
ATTR_SERVICE_NAME,
6-
ATTR_SERVICE_VERSION,
7-
SEMRESATTRS_SERVICE_NAMESPACE,
8-
} from '@opentelemetry/semantic-conventions';
93
import {
104
createTransport,
115
debug,
@@ -14,9 +8,8 @@ import {
148
getGlobalScope,
159
getIsolationScope,
1610
resolvedSyncPromise,
17-
SDK_VERSION,
1811
} from '@sentry/core';
19-
import { SentryPropagator, SentrySampler, SentrySpanProcessor } from '@sentry/opentelemetry';
12+
import { getSentryResource, SentryPropagator, SentrySampler, SentrySpanProcessor } from '@sentry/opentelemetry';
2013
import type { NodeClient } from '../../src';
2114
import { SentryContextManager, validateOpenTelemetrySetup } from '../../src';
2215
import { init } from '../../src/sdk';
@@ -64,14 +57,7 @@ export function setupOtel(client: NodeClient): BasicTracerProvider | undefined {
6457
// Create and configure TracerProvider with same config as Node SDK
6558
const provider = new BasicTracerProvider({
6659
sampler: new SentrySampler(client),
67-
resource: defaultResource().merge(
68-
resourceFromAttributes({
69-
[ATTR_SERVICE_NAME]: 'node',
70-
// eslint-disable-next-line deprecation/deprecation
71-
[SEMRESATTRS_SERVICE_NAMESPACE]: 'sentry',
72-
[ATTR_SERVICE_VERSION]: SDK_VERSION,
73-
}),
74-
),
60+
resource: getSentryResource('node'),
7561
forceFlushTimeoutMillis: 500,
7662
spanProcessors: [
7763
new SentrySpanProcessor({

packages/node/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
8989
"@opentelemetry/instrumentation-redis": "0.62.0",
9090
"@opentelemetry/instrumentation-tedious": "0.33.0",
9191
"@opentelemetry/instrumentation-undici": "0.24.0",
92-
"@opentelemetry/resources": "^2.6.1",
9392
"@opentelemetry/sdk-trace-base": "^2.6.1",
9493
"@opentelemetry/semantic-conventions": "^1.40.0",
9594
"@prisma/instrumentation": "7.6.0",

packages/node/src/sdk/initOtel.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import { context, propagation, trace } from '@opentelemetry/api';
2-
import { defaultResource, resourceFromAttributes } from '@opentelemetry/resources';
32
import type { SpanProcessor } from '@opentelemetry/sdk-trace-base';
43
import { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
5-
import {
6-
ATTR_SERVICE_NAME,
7-
ATTR_SERVICE_VERSION,
8-
SEMRESATTRS_SERVICE_NAMESPACE,
9-
} from '@opentelemetry/semantic-conventions';
10-
import { debug as coreDebug, SDK_VERSION } from '@sentry/core';
4+
import { debug as coreDebug } from '@sentry/core';
115
import {
126
initializeEsmLoader,
137
type NodeClient,
@@ -16,6 +10,7 @@ import {
1610
} from '@sentry/node-core';
1711
import {
1812
type AsyncLocalStorageLookup,
13+
getSentryResource,
1914
SentryPropagator,
2015
SentrySampler,
2116
SentrySpanProcessor,
@@ -95,14 +90,7 @@ export function setupOtel(
9590
// Create and configure NodeTracerProvider
9691
const provider = new BasicTracerProvider({
9792
sampler: new SentrySampler(client),
98-
resource: defaultResource().merge(
99-
resourceFromAttributes({
100-
[ATTR_SERVICE_NAME]: 'node',
101-
// eslint-disable-next-line deprecation/deprecation
102-
[SEMRESATTRS_SERVICE_NAMESPACE]: 'sentry',
103-
[ATTR_SERVICE_VERSION]: SDK_VERSION,
104-
}),
105-
),
93+
resource: getSentryResource('node'),
10694
forceFlushTimeoutMillis: 500,
10795
spanProcessors: [
10896
new SentrySpanProcessor({

packages/opentelemetry/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export { SentrySampler, wrapSamplingDecision } from './sampler';
4949

5050
export { openTelemetrySetupCheck } from './utils/setupCheck';
5151

52+
export { getSentryResource } from './resource';
53+
5254
export { withStreamedSpan } from '@sentry/core';
5355

5456
// Legacy
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import type { Attributes, AttributeValue } from '@opentelemetry/api';
2+
import { SDK_INFO } from '@opentelemetry/core';
3+
import {
4+
ATTR_SERVICE_NAME,
5+
ATTR_SERVICE_VERSION,
6+
ATTR_TELEMETRY_SDK_LANGUAGE,
7+
ATTR_TELEMETRY_SDK_NAME,
8+
ATTR_TELEMETRY_SDK_VERSION,
9+
SEMRESATTRS_SERVICE_NAMESPACE,
10+
} from '@opentelemetry/semantic-conventions';
11+
import { SDK_VERSION } from '@sentry/core';
12+
13+
type RawResourceAttribute = [string, AttributeValue | undefined];
14+
15+
/**
16+
* Minimal Resource implementation that satisfies the OpenTelemetry Resource interface
17+
* used by BasicTracerProvider, without depending on `@opentelemetry/resources`.
18+
*/
19+
class SentryResource {
20+
private _attributes: Attributes;
21+
22+
public constructor(attributes: Attributes) {
23+
this._attributes = attributes;
24+
}
25+
26+
public get attributes(): Attributes {
27+
return this._attributes;
28+
}
29+
30+
public merge(other: SentryResource | null): SentryResource {
31+
if (!other) {
32+
return this;
33+
}
34+
return new SentryResource({ ...this._attributes, ...other.attributes });
35+
}
36+
37+
public getRawAttributes(): RawResourceAttribute[] {
38+
return Object.entries(this._attributes);
39+
}
40+
}
41+
42+
/**
43+
* Returns a Resource for use in Sentry's OpenTelemetry TracerProvider setup.
44+
*
45+
* Combines the default OTel SDK telemetry attributes with Sentry-specific
46+
* service attributes, equivalent to what was previously done via:
47+
* `defaultResource().merge(resourceFromAttributes({ ... }))`
48+
*/
49+
export function getSentryResource(serviceName: string): SentryResource {
50+
return new SentryResource({
51+
[ATTR_SERVICE_NAME]: serviceName,
52+
// eslint-disable-next-line deprecation/deprecation
53+
[SEMRESATTRS_SERVICE_NAMESPACE]: 'sentry',
54+
[ATTR_SERVICE_VERSION]: SDK_VERSION,
55+
[ATTR_TELEMETRY_SDK_LANGUAGE]: SDK_INFO[ATTR_TELEMETRY_SDK_LANGUAGE],
56+
[ATTR_TELEMETRY_SDK_NAME]: SDK_INFO[ATTR_TELEMETRY_SDK_NAME],
57+
[ATTR_TELEMETRY_SDK_VERSION]: SDK_INFO[ATTR_TELEMETRY_SDK_VERSION],
58+
});
59+
}

packages/opentelemetry/test/helpers/initOtel.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import { context, diag, DiagLogLevel, propagation, trace } from '@opentelemetry/api';
2-
import { defaultResource, resourceFromAttributes } from '@opentelemetry/resources';
32
import { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
4-
import {
5-
ATTR_SERVICE_NAME,
6-
ATTR_SERVICE_VERSION,
7-
SEMRESATTRS_SERVICE_NAMESPACE,
8-
} from '@opentelemetry/semantic-conventions';
9-
import { debug, getClient, SDK_VERSION } from '@sentry/core';
3+
import { debug, getClient } from '@sentry/core';
104
import { SentryAsyncLocalStorageContextManager } from '../../src/asyncLocalStorageContextManager';
115
import { DEBUG_BUILD } from '../../src/debug-build';
126
import { SentryPropagator } from '../../src/propagator';
7+
import { getSentryResource } from '../../src/resource';
138
import { SentrySampler } from '../../src/sampler';
149
import { setupEventContextTrace } from '../../src/setupEventContextTrace';
1510
import { SentrySpanProcessor } from '../../src/spanProcessor';
@@ -59,14 +54,7 @@ export function setupOtel(client: TestClientInterface): [BasicTracerProvider, Se
5954
// Create and configure NodeTracerProvider
6055
const provider = new BasicTracerProvider({
6156
sampler: new SentrySampler(client),
62-
resource: defaultResource().merge(
63-
resourceFromAttributes({
64-
[ATTR_SERVICE_NAME]: 'opentelemetry-test',
65-
// eslint-disable-next-line deprecation/deprecation
66-
[SEMRESATTRS_SERVICE_NAMESPACE]: 'sentry',
67-
[ATTR_SERVICE_VERSION]: SDK_VERSION,
68-
}),
69-
),
57+
resource: getSentryResource('opentelemetry-test'),
7058
forceFlushTimeoutMillis: 500,
7159
spanProcessors: [spanProcessor],
7260
});

packages/vercel-edge/src/sdk.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import { context, diag, DiagLogLevel, propagation, trace } from '@opentelemetry/api';
2-
import { defaultResource, resourceFromAttributes } from '@opentelemetry/resources';
32
import { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
4-
import {
5-
ATTR_SERVICE_NAME,
6-
ATTR_SERVICE_VERSION,
7-
SEMRESATTRS_SERVICE_NAMESPACE,
8-
} from '@opentelemetry/semantic-conventions';
93
import type { Client, Integration, Options } from '@sentry/core';
104
import {
115
consoleIntegration,
@@ -22,12 +16,12 @@ import {
2216
linkedErrorsIntegration,
2317
nodeStackLineParser,
2418
requestDataIntegration,
25-
SDK_VERSION,
2619
spanStreamingIntegration,
2720
stackParserFromStackParserOptions,
2821
} from '@sentry/core';
2922
import {
3023
enhanceDscWithOpenTelemetryRootSpanName,
24+
getSentryResource,
3125
openTelemetrySetupCheck,
3226
SentryPropagator,
3327
SentrySampler,
@@ -166,14 +160,7 @@ export function setupOtel(client: VercelEdgeClient): void {
166160
// Create and configure NodeTracerProvider
167161
const provider = new BasicTracerProvider({
168162
sampler: new SentrySampler(client),
169-
resource: defaultResource().merge(
170-
resourceFromAttributes({
171-
[ATTR_SERVICE_NAME]: 'edge',
172-
// eslint-disable-next-line deprecation/deprecation
173-
[SEMRESATTRS_SERVICE_NAMESPACE]: 'sentry',
174-
[ATTR_SERVICE_VERSION]: SDK_VERSION,
175-
}),
176-
),
163+
resource: getSentryResource('edge'),
177164
forceFlushTimeoutMillis: 500,
178165
spanProcessors: [
179166
new SentrySpanProcessor({

0 commit comments

Comments
 (0)