Skip to content

Commit

Permalink
[Monitor][Otel][Exporter] Fix for disabled storage shoutdown (Azure#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
macieyng authored Sep 19, 2023
1 parent a1df320 commit 112d6d5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def shutdown(self) -> None:
Called when the SDK is shut down.
"""
self.storage.close()
if self.storage:
self.storage.close()

def _log_to_envelope(self, log_data: LogData) -> TelemetryItem:
if not log_data:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import shutil
import unittest
from unittest import mock
from functools import partial

# pylint: disable=import-error
from opentelemetry.semconv.trace import SpanAttributes
Expand Down Expand Up @@ -38,14 +39,15 @@ def func(*_args, **_kwargs):
# pylint: disable=protected-access
# pylint: disable=too-many-lines
class TestAzureLogExporter(unittest.TestCase):
_exporter_class = AzureMonitorLogExporter
@classmethod
def setUpClass(cls):
os.environ.clear()
os.environ[
"APPINSIGHTS_INSTRUMENTATIONKEY"
] = "1234abcd-5678-4efa-8abc-1234567890ab"
os.environ["APPLICATIONINSIGHTS_STATSBEAT_DISABLED_ALL"] = "true"
cls._exporter = AzureMonitorLogExporter()
cls._exporter = cls._exporter_class()
cls._log_data = _logs.LogData(
_logs.LogRecord(
timestamp = 1646865018558419456,
Expand Down Expand Up @@ -152,6 +154,7 @@ def test_constructor(self):
exporter._instrumentation_key,
"4321abcd-5678-4efa-8abc-1234567890ab",
)
self.assertIsNotNone(exporter.storage)

def test_from_connection_string(self):
exporter = AzureMonitorLogExporter.from_connection_string(
Expand Down Expand Up @@ -302,7 +305,6 @@ def test_log_to_envelope_event(self):
self.assertEqual(envelope.data.base_data.name, record.body)
self.assertEqual(envelope.data.base_data.properties["event_key"], "event_attribute")


def test_log_to_envelope_timestamp(self):
exporter = self._exporter
old_record = self._log_data.log_record
Expand All @@ -312,6 +314,57 @@ def test_log_to_envelope_timestamp(self):
record = self._log_data.log_record
self.assertEqual(envelope.time, ns_to_iso_str(record.observed_timestamp))
self._log_data.log_record = old_record


class TestAzureLogExporterWithDisabledStorage(TestAzureLogExporter):
_exporter_class = partial(AzureMonitorLogExporter, disable_offline_storage=True)

@classmethod
def tearDownClass(cls):
pass

def test_constructor(self):
"""Test the constructor."""
exporter = AzureMonitorLogExporter(
connection_string="InstrumentationKey=4321abcd-5678-4efa-8abc-1234567890ab",
disable_offline_storage=True,
)
self.assertEqual(
exporter._instrumentation_key,
"4321abcd-5678-4efa-8abc-1234567890ab",
)
self.assertEqual(exporter.storage, None)

def test_shutdown(self):
exporter = self._exporter
exporter.shutdown()
self.assertEqual(exporter.storage, None)

def test_export_failure(self):
exporter = self._exporter
with mock.patch(
"azure.monitor.opentelemetry.exporter.AzureMonitorLogExporter._transmit"
) as transmit: # noqa: E501
transmit.return_value = ExportResult.FAILED_NOT_RETRYABLE
transmit_from_storage_mock = mock.Mock()
exporter._handle_transmit_from_storage = transmit_from_storage_mock
result = exporter.export([self._log_data])
self.assertEqual(result, LogExportResult.FAILURE)
self.assertEqual(exporter.storage, None)
self.assertEqual(transmit_from_storage_mock.call_count, 1)

def test_export_success(self):
exporter = self._exporter
with mock.patch(
"azure.monitor.opentelemetry.exporter.AzureMonitorLogExporter._transmit"
) as transmit: # noqa: E501
transmit.return_value = ExportResult.SUCCESS
storage_mock = mock.Mock()
exporter._transmit_from_storage = storage_mock
result = exporter.export([self._log_data])
self.assertEqual(result, LogExportResult.SUCCESS)
self.assertEqual(storage_mock.call_count, 0)


class TestAzureLogExporterUtils(unittest.TestCase):
def test_get_log_export_result(self):
Expand Down

0 comments on commit 112d6d5

Please sign in to comment.