|
| 1 | +# ------------------------------------ |
| 2 | +# Copyright (c) Microsoft Corporation. |
| 3 | +# Licensed under the MIT License. |
| 4 | +# ------------------------------------ |
| 5 | +from datetime import datetime |
| 6 | +import sys |
| 7 | +import threading |
| 8 | + |
| 9 | +import pytest |
| 10 | +from azure.core.tracing.common import with_current_context |
| 11 | +from azure.eventhub import EventHubConsumerClient, EventHubProducerClient, EventData |
| 12 | +from opentelemetry.trace import SpanKind |
| 13 | + |
| 14 | + |
| 15 | +class TestEventHubsTracing: |
| 16 | + def _verify_span_attributes(self, *, span): |
| 17 | + # Ensure all attributes are set and have a value. |
| 18 | + for attr in span.attributes: |
| 19 | + assert span.attributes[attr] is not None and span.attributes[attr] != "" |
| 20 | + |
| 21 | + def _verify_message(self, *, span, dest, server_address): |
| 22 | + assert span.name == "EventHubs.message" |
| 23 | + assert span.kind == SpanKind.PRODUCER |
| 24 | + self._verify_span_attributes(span=span) |
| 25 | + assert span.attributes["az.namespace"] == "Microsoft.EventHub" |
| 26 | + assert span.attributes["messaging.system"] == "eventhubs" |
| 27 | + assert span.attributes["messaging.destination.name"] == dest |
| 28 | + assert span.attributes["server.address"] == server_address |
| 29 | + |
| 30 | + def _verify_send(self, *, span, dest, server_address, message_count): |
| 31 | + assert span.name == "EventHubs.send" |
| 32 | + assert span.kind == SpanKind.CLIENT |
| 33 | + self._verify_span_attributes(span=span) |
| 34 | + assert span.attributes["az.namespace"] == "Microsoft.EventHub" |
| 35 | + assert span.attributes["messaging.system"] == "eventhubs" |
| 36 | + assert span.attributes["messaging.destination.name"] == dest |
| 37 | + assert span.attributes["messaging.operation"] == "publish" |
| 38 | + assert span.attributes["server.address"] == server_address |
| 39 | + if message_count > 1: |
| 40 | + assert span.attributes["messaging.batch.message_count"] == message_count |
| 41 | + |
| 42 | + def _verify_receive(self, *, span, dest, server_address, message_count): |
| 43 | + assert span.name == "EventHubs.receive" |
| 44 | + assert span.kind == SpanKind.CLIENT |
| 45 | + self._verify_span_attributes(span=span) |
| 46 | + assert span.attributes["az.namespace"] == "Microsoft.EventHub" |
| 47 | + assert span.attributes["messaging.system"] == "eventhubs" |
| 48 | + assert span.attributes["messaging.destination.name"] == dest |
| 49 | + assert span.attributes["messaging.operation"] == "receive" |
| 50 | + assert span.attributes["server.address"] == server_address |
| 51 | + for link in span.links: |
| 52 | + assert "enqueuedTime" in link.attributes |
| 53 | + if message_count > 1: |
| 54 | + assert span.attributes["messaging.batch.message_count"] == message_count |
| 55 | + |
| 56 | + def _verify_process(self, *, span, dest, server_address, message_count): |
| 57 | + assert span.name == "EventHubs.process" |
| 58 | + assert span.kind == SpanKind.CONSUMER |
| 59 | + self._verify_span_attributes(span=span) |
| 60 | + assert span.attributes["az.namespace"] == "Microsoft.EventHub" |
| 61 | + assert span.attributes["messaging.system"] == "eventhubs" |
| 62 | + assert span.attributes["messaging.destination.name"] == dest |
| 63 | + assert span.attributes["messaging.operation"] == "process" |
| 64 | + assert span.attributes["server.address"] == server_address |
| 65 | + if message_count > 1: |
| 66 | + assert span.attributes["messaging.batch.message_count"] == message_count |
| 67 | + |
| 68 | + @pytest.mark.live_test_only |
| 69 | + @pytest.mark.skipif(sys.platform.startswith("darwin"), reason="threading issues on mac CI") |
| 70 | + def test_eventhubs_client_tracing(self, config, tracing_helper): |
| 71 | + |
| 72 | + connection_string = config["eventhub_connection_string"] |
| 73 | + eventhub_name = config["eventhub_name"] |
| 74 | + |
| 75 | + producer_client = EventHubProducerClient.from_connection_string( |
| 76 | + conn_str=connection_string, |
| 77 | + eventhub_name=eventhub_name, |
| 78 | + ) |
| 79 | + |
| 80 | + consumer_client = EventHubConsumerClient.from_connection_string( |
| 81 | + conn_str=connection_string, |
| 82 | + consumer_group="$Default", |
| 83 | + eventhub_name=eventhub_name, |
| 84 | + ) |
| 85 | + |
| 86 | + with tracing_helper.tracer.start_as_current_span(name="root"): |
| 87 | + |
| 88 | + current_date = datetime.now() |
| 89 | + |
| 90 | + with producer_client: |
| 91 | + |
| 92 | + # Send batch of events |
| 93 | + event_data_batch = producer_client.create_batch() |
| 94 | + event_data_batch.add(EventData("First message inside an EventDataBatch")) |
| 95 | + event_data_batch.add(EventData("Second message inside an EventDataBatch")) |
| 96 | + producer_client.send_batch(event_data_batch) |
| 97 | + |
| 98 | + send_spans = tracing_helper.exporter.get_finished_spans() |
| 99 | + |
| 100 | + # We expect 3 spans to have finished: 1 send spans, and 2 message spans. |
| 101 | + assert len(send_spans) == 3 |
| 102 | + |
| 103 | + server_address = producer_client._address.hostname |
| 104 | + dest_name = producer_client._address.path |
| 105 | + |
| 106 | + # Verify the spans from the batch send. |
| 107 | + self._verify_message(span=send_spans[0], dest=dest_name, server_address=server_address) |
| 108 | + self._verify_message(span=send_spans[1], dest=dest_name, server_address=server_address) |
| 109 | + self._verify_send(span=send_spans[2], dest=dest_name, server_address=server_address, message_count=2) |
| 110 | + |
| 111 | + # Verify span links from batch send. |
| 112 | + assert len(send_spans[2].links) == 2 |
| 113 | + link = send_spans[2].links[0] |
| 114 | + assert link.context.span_id == send_spans[0].context.span_id |
| 115 | + assert link.context.trace_id == send_spans[0].context.trace_id |
| 116 | + |
| 117 | + link = send_spans[2].links[1] |
| 118 | + assert link.context.span_id == send_spans[1].context.span_id |
| 119 | + assert link.context.trace_id == send_spans[1].context.trace_id |
| 120 | + |
| 121 | + tracing_helper.exporter.clear() |
| 122 | + |
| 123 | + def on_event_batch(partition_context, event_batch): |
| 124 | + pass |
| 125 | + |
| 126 | + # Receive batch of events. |
| 127 | + worker = threading.Thread( |
| 128 | + target=with_current_context(consumer_client.receive_batch), |
| 129 | + args=(on_event_batch,), |
| 130 | + kwargs={"starting_position": current_date}, |
| 131 | + ) |
| 132 | + worker.daemon = True |
| 133 | + worker.start() |
| 134 | + worker.join(timeout=3) |
| 135 | + |
| 136 | + receive_spans = tracing_helper.exporter.get_finished_spans() |
| 137 | + |
| 138 | + # We expect 2 spans to have finished: 1 receive span and 1 process span. |
| 139 | + assert len(receive_spans) == 2 |
| 140 | + |
| 141 | + self._verify_receive(span=receive_spans[0], dest=dest_name, server_address=server_address, message_count=2) |
| 142 | + self._verify_process(span=receive_spans[1], dest=dest_name, server_address=server_address, message_count=2) |
| 143 | + |
| 144 | + # Verify receive span links. |
| 145 | + assert len(receive_spans[0].links) == 2 |
| 146 | + assert receive_spans[0].links[0].context.span_id == send_spans[0].context.span_id |
| 147 | + assert receive_spans[0].links[1].context.span_id == send_spans[1].context.span_id |
| 148 | + |
| 149 | + # Verify process span links. |
| 150 | + assert len(receive_spans[1].links) == 2 |
| 151 | + assert receive_spans[1].links[0].context.span_id == send_spans[0].context.span_id |
| 152 | + assert receive_spans[1].links[1].context.span_id == send_spans[1].context.span_id |
0 commit comments