File tree Expand file tree Collapse file tree 1 file changed +11
-2
lines changed
Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -285,6 +285,10 @@ pub fn setup_open_telemetry(
285285 use opentelemetry:: KeyValue ;
286286 use opentelemetry_sdk:: { Resource , metrics:: Temporality } ;
287287
288+ // Build the SDK-default resource first to discover any pre-configured attributes
289+ // (e.g. via OTEL_RESOURCE_ATTRIBUTES), so we only add our fallback values when absent.
290+ let default_resource = Resource :: builder ( ) . build ( ) ;
291+
288292 let service_name =
289293 var ( "OTEL_SERVICE_NAME" ) . unwrap_or_else ( |_| DEFAULT_OTLP_SERVICE_NAME . to_string ( ) ) . trim ( ) . to_string ( ) ;
290294 let service_instance_id: Option < String > =
@@ -294,8 +298,13 @@ pub fn setup_open_telemetry(
294298 let port = listen_addr. trim ( ) . rsplit ( ':' ) . next ( ) ?;
295299 Some ( format ! ( "{hostname}:{port}" ) )
296300 } ) ;
297- let mut attributes = vec ! [ KeyValue :: new( SERVICE_NAME , service_name. clone( ) ) ] ;
298- if let Some ( instance_id) = service_instance_id {
301+ let mut attributes = Vec :: new ( ) ;
302+ if default_resource. get ( & opentelemetry:: Key :: from_static_str ( SERVICE_NAME ) ) . is_none ( ) {
303+ attributes. push ( KeyValue :: new ( SERVICE_NAME , service_name. clone ( ) ) ) ;
304+ }
305+ if let Some ( instance_id) = service_instance_id
306+ && default_resource. get ( & opentelemetry:: Key :: from_static_str ( SERVICE_INSTANCE_ID ) ) . is_none ( )
307+ {
299308 attributes. push ( KeyValue :: new ( SERVICE_INSTANCE_ID , instance_id) ) ;
300309 }
301310 let resource = Resource :: builder ( ) . with_attributes ( attributes) . build ( ) ;
You can’t perform that action at this time.
0 commit comments