@@ -202,6 +202,11 @@ def __init__(
202
202
include_sensitive_data : bool = True ,
203
203
base_url : Optional [str ] = None ,
204
204
emit_legacy : bool = True ,
205
+ agent_name : Optional [str ] = None ,
206
+ agent_id : Optional [str ] = None ,
207
+ agent_description : Optional [str ] = None ,
208
+ server_address : Optional [str ] = None ,
209
+ server_port : Optional [int ] = None ,
205
210
):
206
211
"""Initialize processor with metrics support.
207
212
@@ -212,13 +217,42 @@ def __init__(
212
217
include_sensitive_data: Include model/tool IO when True
213
218
base_url: API endpoint for server.address/port
214
219
emit_legacy: Also emit deprecated attribute names
220
+ agent_name: Name of the agent (can be overridden by env var)
221
+ agent_id: ID of the agent (can be overridden by env var)
222
+ agent_description: Description of the agent (can be overridden by env var)
223
+ server_address: Server address (can be overridden by env var or base_url)
224
+ server_port: Server port (can be overridden by env var or base_url)
215
225
"""
216
226
self ._tracer = tracer
217
227
self ._event_logger = event_logger
218
228
self .system_name = normalize_provider (system_name ) or system_name
219
229
self .include_sensitive_data = include_sensitive_data
220
230
self .base_url = base_url
221
231
self .emit_legacy = emit_legacy
232
+
233
+ # Agent information - prefer environment variables, then init parameters
234
+ self .agent_name = os .getenv ("OTEL_GENAI_AGENT_NAME" , agent_name )
235
+ self .agent_id = os .getenv ("OTEL_GENAI_AGENT_ID" , agent_id )
236
+ self .agent_description = os .getenv ("OTEL_GENAI_AGENT_DESCRIPTION" , agent_description )
237
+
238
+ # Server information - prefer environment variables, then init parameters, then base_url
239
+ self .server_address = os .getenv ("OTEL_GENAI_SERVER_ADDRESS" , server_address )
240
+ self .server_port = os .getenv ("OTEL_GENAI_SERVER_PORT" )
241
+ if self .server_port :
242
+ try :
243
+ self .server_port = int (self .server_port )
244
+ except ValueError :
245
+ self .server_port = None
246
+ else :
247
+ self .server_port = server_port
248
+
249
+ # If server info not provided, try to extract from base_url
250
+ if (not self .server_address or not self .server_port ) and base_url :
251
+ server_attrs = _infer_server_attributes (base_url )
252
+ if not self .server_address :
253
+ self .server_address = server_attrs .get ("server.address" )
254
+ if not self .server_port :
255
+ self .server_port = server_attrs .get ("server.port" )
222
256
223
257
# Span tracking
224
258
self ._root_spans : dict [str , OtelSpan ] = {}
@@ -246,6 +280,15 @@ def __init__(
246
280
if self ._metrics_enabled :
247
281
self ._init_metrics ()
248
282
283
+ def _get_server_attributes (self ) -> dict [str , Any ]:
284
+ """Get server attributes from configured values."""
285
+ attrs = {}
286
+ if self .server_address :
287
+ attrs ["server.address" ] = self .server_address
288
+ if self .server_port :
289
+ attrs ["server.port" ] = self .server_port
290
+ return attrs
291
+
249
292
def _init_metrics (self ):
250
293
"""Initialize metric instruments."""
251
294
self ._meter = get_meter (
@@ -402,7 +445,15 @@ def on_trace_start(self, trace: Trace) -> None:
402
445
}
403
446
if self .emit_legacy :
404
447
attributes [GEN_AI_SYSTEM_LEGACY ] = self .system_name
405
- attributes .update (_infer_server_attributes (self .base_url ))
448
+
449
+ # Add configured agent and server attributes
450
+ if self .agent_name :
451
+ attributes [GEN_AI_AGENT_NAME ] = self .agent_name
452
+ if self .agent_id :
453
+ attributes [GEN_AI_AGENT_ID ] = self .agent_id
454
+ if self .agent_description :
455
+ attributes [GEN_AI_AGENT_DESCRIPTION ] = self .agent_description
456
+ attributes .update (self ._get_server_attributes ())
406
457
407
458
otel_span = self ._tracer .start_span (
408
459
name = trace .name ,
@@ -433,9 +484,12 @@ def on_span_start(self, span: Span[Any]) -> None:
433
484
# Get operation details for span naming
434
485
operation_name = self ._get_operation_name (span .span_data )
435
486
model = getattr (span .span_data , 'model' , None )
436
- agent_name = getattr (span .span_data , 'name' , None ) if isinstance (
437
- span .span_data , AgentSpanData
438
- ) else None
487
+
488
+ # Use configured agent name or get from span data
489
+ agent_name = self .agent_name
490
+ if not agent_name and isinstance (span .span_data , AgentSpanData ):
491
+ agent_name = getattr (span .span_data , 'name' , None )
492
+
439
493
tool_name = getattr (span .span_data , 'name' , None ) if isinstance (
440
494
span .span_data , FunctionSpanData
441
495
) else None
@@ -449,7 +503,15 @@ def on_span_start(self, span: Span[Any]) -> None:
449
503
}
450
504
if self .emit_legacy :
451
505
attributes [GEN_AI_SYSTEM_LEGACY ] = self .system_name
452
- attributes .update (_infer_server_attributes (self .base_url ))
506
+
507
+ # Add configured agent and server attributes
508
+ if self .agent_name :
509
+ attributes [GEN_AI_AGENT_NAME ] = self .agent_name
510
+ if self .agent_id :
511
+ attributes [GEN_AI_AGENT_ID ] = self .agent_id
512
+ if self .agent_description :
513
+ attributes [GEN_AI_AGENT_DESCRIPTION ] = self .agent_description
514
+ attributes .update (self ._get_server_attributes ())
453
515
454
516
otel_span = self ._tracer .start_span (
455
517
name = span_name ,
@@ -578,8 +640,16 @@ def _extract_genai_attributes(
578
640
if self .emit_legacy :
579
641
yield GEN_AI_SYSTEM_LEGACY , self .system_name
580
642
643
+ # Add configured agent attributes (always include when set)
644
+ if self .agent_name :
645
+ yield GEN_AI_AGENT_NAME , self .agent_name
646
+ if self .agent_id :
647
+ yield GEN_AI_AGENT_ID , self .agent_id
648
+ if self .agent_description :
649
+ yield GEN_AI_AGENT_DESCRIPTION , self .agent_description
650
+
581
651
# Server attributes
582
- for key , value in _infer_server_attributes ( self .base_url ).items ():
652
+ for key , value in self ._get_server_attributes ( ).items ():
583
653
yield key , value
584
654
585
655
# Process different span types
@@ -682,11 +752,12 @@ def _get_attributes_from_agent_span_data(
682
752
"""Extract attributes from agent span."""
683
753
yield GEN_AI_OPERATION_NAME , GenAIOperationName .INVOKE_AGENT
684
754
685
- if span_data .name :
755
+ # Use span data values only if not configured globally
756
+ if not self .agent_name and span_data .name :
686
757
yield GEN_AI_AGENT_NAME , span_data .name
687
- if hasattr (span_data , 'agent_id' ) and span_data .agent_id :
758
+ if not self . agent_id and hasattr (span_data , 'agent_id' ) and span_data .agent_id :
688
759
yield GEN_AI_AGENT_ID , span_data .agent_id
689
- if hasattr (span_data , 'description' ) and span_data .description :
760
+ if not self . agent_description and hasattr (span_data , 'description' ) and span_data .description :
690
761
yield GEN_AI_AGENT_DESCRIPTION , span_data .description
691
762
if hasattr (span_data , 'conversation_id' ) and span_data .conversation_id :
692
763
yield GEN_AI_CONVERSATION_ID , span_data .conversation_id
0 commit comments