@@ -2,7 +2,13 @@ import { NodeSDK } from "@opentelemetry/sdk-node";
22import { SpanProcessor } from "@opentelemetry/sdk-trace-node" ;
33import { context , diag } from "@opentelemetry/api" ;
44import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto" ;
5- import { resourceFromAttributes , Resource } from "@opentelemetry/resources" ;
5+ import {
6+ resourceFromAttributes ,
7+ Resource ,
8+ detectResources ,
9+ envDetector ,
10+ processDetector ,
11+ } from "@opentelemetry/resources" ;
612import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions" ;
713import { Instrumentation } from "@opentelemetry/instrumentation" ;
814import { InitializeOptions } from "../interfaces" ;
@@ -307,23 +313,22 @@ export const startTracing = (options: InitializeOptions) => {
307313 spanProcessors . push ( options . processor ) ;
308314 }
309315
310- // Create resource with defensive handling for OTLP serialization
316+ // Create resource with proper detection and defensive handling for OTLP serialization
311317 const serviceName =
312318 options . appName || process . env . npm_package_name || "unknown-service" ;
313319 let resource : Resource ;
314320
315321 try {
322+ // Create our custom resource with service name and let NodeSDK handle default detection
316323 resource = resourceFromAttributes ( {
317324 [ ATTR_SERVICE_NAME ] : serviceName ,
318325 } ) ;
319326
320327 // Defensive check to prevent OTLP serialization errors
321- // Ensure the resource has the expected structure for createResourceMap
322328 if ( ! resource || typeof resource !== "object" ) {
323329 throw new Error ( "Invalid resource object" ) ;
324330 }
325331
326- // Ensure resource has required properties for OTLP export
327332 if ( ! resource . attributes || typeof resource . attributes !== "object" ) {
328333 throw new Error ( "Resource missing attributes" ) ;
329334 }
@@ -333,12 +338,9 @@ export const startTracing = (options: InitializeOptions) => {
333338 "Failed to create resource with resourceFromAttributes, using fallback" ,
334339 error ,
335340 ) ;
336- resource = resourceFromAttributes ( { } ) ;
337- // Manually set the service name on the fallback resource
338- ( resource as any ) . attributes = {
341+ resource = resourceFromAttributes ( {
339342 [ ATTR_SERVICE_NAME ] : serviceName ,
340- ...( ( resource as any ) . attributes || { } ) ,
341- } ;
343+ } ) ;
342344 }
343345
344346 _sdk = new NodeSDK ( {
0 commit comments