@@ -10,7 +10,11 @@ import {
1010 ConsoleMetricExporter ,
1111 PeriodicExportingMetricReader ,
1212} from "@opentelemetry/sdk-metrics" ;
13- import { resourceFromAttributes } from "@opentelemetry/resources" ;
13+ import {
14+ envDetector ,
15+ processDetector ,
16+ resourceFromAttributes ,
17+ } from "@opentelemetry/resources" ;
1418import { NodeSDK } from "@opentelemetry/sdk-node" ;
1519import {
1620 AlwaysOnSampler ,
@@ -19,7 +23,6 @@ import {
1923 ParentBasedSampler ,
2024 SimpleSpanProcessor ,
2125} from "@opentelemetry/sdk-trace-node" ;
22- import { KnexInstrumentation } from "@opentelemetry/instrumentation-knex" ;
2326import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions" ;
2427import { diag , DiagConsoleLogger , DiagLogLevel } from "@opentelemetry/api" ;
2528import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node" ;
@@ -56,6 +59,15 @@ export async function nodeSDKBuilder() {
5659
5760 const sentryClient = Sentry . getClient ( ) ;
5861
62+ // All required resources are injected through env vars
63+ // in cloud deployment, but locally we can use detectors
64+ // The host detector has a known issue with async attributes
65+ // and isn't super important
66+ // https://github.com/open-telemetry/opentelemetry-js/issues/4638
67+ const resourceDetectors = isLocalCollector
68+ ? [ processDetector , envDetector ]
69+ : undefined ;
70+
5971 if ( sentryClient ) {
6072 // https://github.com/getsentry/sentry-javascript/tree/develop/packages/opentelemetry
6173 setupEventContextTrace ( sentryClient ) ;
@@ -92,7 +104,6 @@ export async function nodeSDKBuilder() {
92104 const sdk = new NodeSDK ( {
93105 resource : resourceFromAttributes ( {
94106 [ ATTR_SERVICE_NAME ] : appConfig . otel . serviceName ,
95- ...appConfig . otel . resourceAttributes ,
96107 } ) ,
97108 metricReaders : [
98109 new PeriodicExportingMetricReader ( {
@@ -113,17 +124,17 @@ export async function nodeSDKBuilder() {
113124 // Ensure the correct subset of traces is sent to Sentry
114125 // This also ensures trace propagation works as expected
115126 sampler,
127+ // Kuberenetes infrastructure automatically injects
128+ // required resources through environment variables
129+ autoDetectResources : isLocalCollector ? true : false ,
130+ resourceDetectors,
116131 instrumentations : [
117132 getNodeAutoInstrumentations ( {
118133 "@opentelemetry/instrumentation-fs" : {
119134 // Disable unless required by parent span (noisy and memory-intensive)
120135 enabled : false ,
121136 requireParentSpan : true ,
122137 } ,
123- "@opentelemetry/instrumentation-pg" : {
124- // Disable because we're using @opentelemetry/instrumentation-knex
125- enabled : false ,
126- } ,
127138 // Node native fetch instrumentation
128139 "@opentelemetry/instrumentation-undici" : {
129140 // Ignore sending anonymous telemetry to next.js
@@ -146,13 +157,7 @@ export async function nodeSDKBuilder() {
146157 return false ;
147158 } ,
148159 } ,
149- // Similar to prometheus default metrics
150- "@opentelemetry/instrumentation-runtime-node" : {
151- enabled : true ,
152- } ,
153160 } ) ,
154- // Additional instrumentations
155- new KnexInstrumentation ( ) ,
156161 ] ,
157162 } ) ;
158163 sdk . start ( ) ;
0 commit comments