Skip to content

Commit 8b4c7c1

Browse files
committed
add azure monitor exporter trace ai field
1 parent 9a3b2b3 commit 8b4c7c1

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/trace/_exporter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def _convert_span_to_envelope(span: ReadableSpan) -> TelemetryItem:
407407
span.attributes,
408408
)
409409
elif gen_ai_attributes.GEN_AI_SYSTEM in span.attributes: # GenAI
410-
data.type = span.attributes[gen_ai_attributes.GEN_AI_SYSTEM]
410+
data.type = "GenAI | {}".format(span.attributes[gen_ai_attributes.GEN_AI_SYSTEM])
411411
else:
412412
data.type = "N/A"
413413
elif span.kind is SpanKind.PRODUCER: # Messaging
@@ -428,6 +428,8 @@ def _convert_span_to_envelope(span: ReadableSpan) -> TelemetryItem:
428428
data.type = "InProc"
429429
if _AZURE_SDK_NAMESPACE_NAME in span.attributes:
430430
data.type += " | {}".format(span.attributes[_AZURE_SDK_NAMESPACE_NAME])
431+
elif gen_ai_attributes.GEN_AI_SYSTEM in span.attributes: # GenAI
432+
data.type = "GenAI | {}".format(span.attributes[gen_ai_attributes.GEN_AI_SYSTEM])
431433
# Apply truncation
432434
# See https://github.com/MohanGsk/ApplicationInsights-Home/tree/master/EndpointSpecs/Schemas/Bond
433435
if data.name:

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/trace/_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
user_agent_attributes,
1212
)
1313
from opentelemetry.semconv.trace import DbSystemValues, SpanAttributes
14+
from opentelemetry.semconv._incubating.attributes import gen_ai_attributes
1415
from opentelemetry.util.types import Attributes
1516

1617

@@ -202,6 +203,11 @@ def _get_target_and_path_for_http_dependency(
202203
target = parsed_url.hostname
203204
elif parsed_url.netloc:
204205
target = parsed_url.netloc
206+
elif gen_ai_attributes.GEN_AI_SYSTEM in attributes:
207+
# If no fields are available to set target using standard rules, set Dependency Target to gen_ai.system if present
208+
gen_ai_system = attributes.get(gen_ai_attributes.GEN_AI_SYSTEM)
209+
if gen_ai_system:
210+
target = gen_ai_system
205211
if not target:
206212
# Get target from peer.* attributes that are NOT peer.service
207213
target = _get_target_for_dependency_from_peer(attributes)

sdk/monitor/azure-monitor-opentelemetry-exporter/tests/trace/test_trace.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,8 +762,32 @@ def test_span_to_envelope_client_gen_ai(self):
762762
self.assertEqual(envelope.data.base_data.result_code, "0")
763763

764764
self.assertEqual(envelope.data.base_type, "RemoteDependencyData")
765-
self.assertEqual(envelope.data.base_data.type, "az.ai.inference")
765+
self.assertEqual(envelope.data.base_data.type, "GenAI | az.ai.inference")
766766
self.assertEqual(len(envelope.data.base_data.properties), 1)
767+
768+
def test_span_to_envelope_client_internal_genai_type(self):
769+
exporter = self._exporter
770+
start_time = 1575494316027613500
771+
end_time = start_time + 1001000000
772+
773+
span = trace._Span(
774+
name="genai-test",
775+
context=SpanContext(
776+
trace_id=36873507687745823477771305566750195431,
777+
span_id=12030755672171557337,
778+
is_remote=False,
779+
),
780+
attributes={
781+
"gen_ai.system": "az.ai.inference",
782+
},
783+
kind=SpanKind.INTERNAL,
784+
)
785+
span.start(start_time=start_time)
786+
span.end(end_time=end_time)
787+
span._status = Status(status_code=StatusCode.UNSET)
788+
envelope = exporter._span_to_envelope(span)
789+
790+
self.assertEqual(envelope.data.base_data.type, "GenAI | az.ai.inference")
767791

768792
def test_span_to_envelope_client_azure(self):
769793
exporter = self._exporter

0 commit comments

Comments
 (0)