Skip to content
Prev Previous commit
Next Next commit
utils
  • Loading branch information
lzchen committed Mar 8, 2024
commit f55641535763a1f9d88db25ef721253d75347afd
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _get_span_document(span: ReadableSpan) -> Union[RemoteDependencyDocument, Re
url = _get_url(span_kind, span.attributes)
if span_kind in (SpanKind.CLIENT, SpanKind.PRODUCER, SpanKind.INTERNAL):
document = RemoteDependencyDocument(
document_type=_DocumentIngressDocumentType.RemoteDependency,
document_type=_DocumentIngressDocumentType.RemoteDependency.value,
name=span.name,
command_name=url,
result_code=str(status_code),
Expand All @@ -88,7 +88,7 @@ def _get_span_document(span: ReadableSpan) -> Union[RemoteDependencyDocument, Re
else:
code = str(grpc_status_code)
document = RequestDocument(
document_type=_DocumentIngressDocumentType.Request,
document_type=_DocumentIngressDocumentType.Request.value,
name=span.name,
url=url,
response_code=code,
Expand All @@ -98,17 +98,17 @@ def _get_span_document(span: ReadableSpan) -> Union[RemoteDependencyDocument, Re

# mypy: disable-error-code="assignment"
def _get_log_record_document(log_data: LogData) -> Union[ExceptionDocument, TraceDocument]:
exc_type = log_data.log_record.attributes.get(SpanAttributes.EXCEPTION_TYPE, "")
exc_message = log_data.log_record.attributes.get(SpanAttributes.EXCEPTION_MESSAGE, "")
exc_type = log_data.log_record.attributes.get(SpanAttributes.EXCEPTION_TYPE)
exc_message = log_data.log_record.attributes.get(SpanAttributes.EXCEPTION_MESSAGE)
if exc_type is not None or exc_message is not None:
document = ExceptionDocument(
document_type=_DocumentIngressDocumentType.Exception,
document_type=_DocumentIngressDocumentType.Exception.value,
exception_type=str(exc_type),
exception_message=str(exc_message),
)
else:
document = TraceDocument(
document_type=_DocumentIngressDocumentType.Trace,
document_type=_DocumentIngressDocumentType.Trace.value,
message=log_data.log_record.body,
)
return document
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@
from unittest import mock

from opentelemetry.sdk.metrics.export import HistogramDataPoint, NumberDataPoint
from azure.monitor.opentelemetry.exporter._quickpulse._constants import _COMMITTED_BYTES_NAME
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace import SpanKind

from azure.monitor.opentelemetry.exporter._quickpulse._constants import (
_COMMITTED_BYTES_NAME,
_DocumentIngressDocumentType,
)
from azure.monitor.opentelemetry.exporter._quickpulse._generated.models._models import (
Exception,
MetricPoint,
MonitoringDataPoint,
RemoteDependency,
Request,
Trace,
)
from azure.monitor.opentelemetry.exporter._quickpulse._utils import (
_get_span_document,
_get_log_record_document,
_metric_to_quick_pulse_data_points,
)

Expand Down Expand Up @@ -99,4 +111,94 @@ def test_metric_to_qp_data_point_num(self, datetime_mock):
self.assertEqual(mdp.stream_id, self.base_mdp.stream_id)
self.assertEqual(mdp.timestamp, date_now)
self.assertEqual(mdp.metrics, [metric_point])
self.assertEqual(mdp.documents, documents)
self.assertEqual(mdp.documents, documents)

@mock.patch("azure.monitor.opentelemetry.exporter._quickpulse._utils._ns_to_iso8601_string")
@mock.patch("azure.monitor.opentelemetry.exporter._quickpulse._utils._get_url")
def test_get_span_document_client(self, url_mock, iso_mock):
span_mock = mock.Mock()
span_mock.name = "test_span"
span_mock.end_time = 10
span_mock.start_time = 4
span_mock.attributes = {
SpanAttributes.HTTP_STATUS_CODE: "200",
SpanAttributes.RPC_GRPC_STATUS_CODE: "400",
}
span_mock.kind = SpanKind.CLIENT
url_mock.return_value = "test_url"
iso_mock.return_value = "1000"
doc = _get_span_document(span_mock)
self.assertTrue(isinstance(doc, RemoteDependency))
self.assertEqual(doc.document_type, _DocumentIngressDocumentType.RemoteDependency.value)
self.assertEqual(doc.name, "test_span")
self.assertEqual(doc.command_name, "test_url")
self.assertEqual(doc.result_code, "200")
self.assertEqual(doc.duration, "1000")

@mock.patch("azure.monitor.opentelemetry.exporter._quickpulse._utils._ns_to_iso8601_string")
@mock.patch("azure.monitor.opentelemetry.exporter._quickpulse._utils._get_url")
def test_get_span_document_server(self, url_mock, iso_mock):
span_mock = mock.Mock()
span_mock.name = "test_span"
span_mock.end_time = 10
span_mock.start_time = 4
span_mock.attributes = {
SpanAttributes.HTTP_STATUS_CODE: "200",
SpanAttributes.RPC_GRPC_STATUS_CODE: "400",
}
span_mock.kind = SpanKind.SERVER
url_mock.return_value = "test_url"
iso_mock.return_value = "1000"
doc = _get_span_document(span_mock)
self.assertTrue(isinstance(doc, Request))
self.assertEqual(doc.document_type, _DocumentIngressDocumentType.Request.value)
self.assertEqual(doc.name, "test_span")
self.assertEqual(doc.url, "test_url")
self.assertEqual(doc.response_code, "200")
self.assertEqual(doc.duration, "1000")

@mock.patch("azure.monitor.opentelemetry.exporter._quickpulse._utils._ns_to_iso8601_string")
@mock.patch("azure.monitor.opentelemetry.exporter._quickpulse._utils._get_url")
def test_get_span_document_server_grpc_status(self, url_mock, iso_mock):
span_mock = mock.Mock()
span_mock.name = "test_span"
span_mock.end_time = 10
span_mock.start_time = 4
span_mock.attributes = {
SpanAttributes.RPC_GRPC_STATUS_CODE: "400",
}
span_mock.kind = SpanKind.SERVER
url_mock.return_value = "test_url"
iso_mock.return_value = "1000"
doc = _get_span_document(span_mock)
self.assertTrue(isinstance(doc, Request))
self.assertEqual(doc.document_type, _DocumentIngressDocumentType.Request.value)
self.assertEqual(doc.name, "test_span")
self.assertEqual(doc.url, "test_url")
self.assertEqual(doc.response_code, "400")
self.assertEqual(doc.duration, "1000")

def test_get_log_record_document_server_exc(self):
log_record = mock.Mock()
log_record.attributes = {
SpanAttributes.EXCEPTION_TYPE: "exc_type",
SpanAttributes.EXCEPTION_MESSAGE: "exc_message",
}
log_data = mock.Mock()
log_data.log_record = log_record
doc = _get_log_record_document(log_data)
self.assertTrue(isinstance(doc, Exception))
self.assertEqual(doc.document_type, _DocumentIngressDocumentType.Exception.value)
self.assertEqual(doc.exception_type, "exc_type")
self.assertEqual(doc.exception_message, "exc_message")

def test_get_log_record_document_server_exc(self):
log_record = mock.Mock()
log_record.attributes = {}
log_record.body = "body"
log_data = mock.Mock()
log_data.log_record = log_record
doc = _get_log_record_document(log_data)
self.assertTrue(isinstance(doc, Trace))
self.assertEqual(doc.document_type, _DocumentIngressDocumentType.Trace.value)
self.assertEqual(doc.message, "body")