Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions azure_monitor/tests/metrics/test_metrics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import os
import shutil
import unittest
from unittest import mock

Expand All @@ -19,6 +20,19 @@
from azure_monitor.options import ExporterOptions
from azure_monitor.protocol import Data, DataPoint, Envelope, MetricData

TEST_FOLDER = os.path.abspath(".test.exporter.trace")
STORAGE_PATH = os.path.join(TEST_FOLDER)


# pylint: disable=invalid-name
def setUpModule():
os.makedirs(TEST_FOLDER)


# pylint: disable=invalid-name
def tearDownModule():
shutil.rmtree(TEST_FOLDER)


def throw(exc_type, *args, **kwargs):
def func(*_args, **_kwargs):
Expand All @@ -34,6 +48,7 @@ def setUpClass(cls):
os.environ[
"APPINSIGHTS_INSTRUMENTATIONKEY"
] = "1234abcd-5678-4efa-8abc-1234567890ab"
cls._exporter = AzureMonitorMetricsExporter(storage_path=STORAGE_PATH)

metrics.set_meter_provider(MeterProvider())
cls._meter = metrics.get_meter(__name__)
Expand All @@ -55,14 +70,26 @@ def setUpClass(cls):
kvp = {"environment": "staging"}
cls._test_label_set = cls._meter.get_label_set(kvp)

def setUp(self):
for filename in os.listdir(STORAGE_PATH):
file_path = os.path.join(STORAGE_PATH, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except OSError as e:
print("Failed to delete %s. Reason: %s" % (file_path, e))

@classmethod
def tearDownClass(cls):
metrics._METER_PROVIDER = None

def test_constructor(self):
"""Test the constructor."""
exporter = AzureMonitorMetricsExporter(
instrumentation_key="4321abcd-5678-4efa-8abc-1234567890ab"
instrumentation_key="4321abcd-5678-4efa-8abc-1234567890ab",
storage_path=os.path.join(TEST_FOLDER, self.id()),
)
self.assertIsInstance(exporter.options, ExporterOptions)
self.assertEqual(
Expand All @@ -74,7 +101,7 @@ def test_export(self,):
record = MetricRecord(
CounterAggregator(), self._test_label_set, self._test_metric
)
exporter = AzureMonitorMetricsExporter()
exporter = self._exporter
with mock.patch(
"azure_monitor.export.metrics.AzureMonitorMetricsExporter._transmit"
) as transmit: # noqa: E501
Expand All @@ -86,7 +113,7 @@ def test_export_failed_retryable(self):
record = MetricRecord(
CounterAggregator(), self._test_label_set, self._test_metric
)
exporter = AzureMonitorMetricsExporter()
exporter = self._exporter
with mock.patch(
"azure_monitor.export.metrics.AzureMonitorMetricsExporter._transmit"
) as transmit: # noqa: E501
Expand All @@ -102,7 +129,7 @@ def test_export_exception(self, logger_mock):
record = MetricRecord(
CounterAggregator(), self._test_label_set, self._test_metric
)
exporter = AzureMonitorMetricsExporter()
exporter = self._exporter
with mock.patch(
"azure_monitor.export.metrics.AzureMonitorMetricsExporter._transmit",
throw(Exception),
Expand All @@ -112,7 +139,7 @@ def test_export_exception(self, logger_mock):
self.assertEqual(logger_mock.exception.called, True)

def test_metric_to_envelope_none(self):
exporter = AzureMonitorMetricsExporter()
exporter = self._exporter
self.assertIsNone(exporter._metric_to_envelope(None))

def test_metric_to_envelope(self):
Expand All @@ -122,7 +149,7 @@ def test_metric_to_envelope(self):
record = MetricRecord(
aggregator, self._test_label_set, self._test_metric
)
exporter = AzureMonitorMetricsExporter()
exporter = self._exporter
envelope = exporter._metric_to_envelope(record)
self.assertIsInstance(envelope, Envelope)
self.assertEqual(envelope.ver, 1)
Expand Down Expand Up @@ -162,7 +189,7 @@ def test_observer_to_envelope(self):
aggregator.update(123)
aggregator.take_checkpoint()
record = MetricRecord(aggregator, self._test_label_set, self._test_obs)
exporter = AzureMonitorMetricsExporter()
exporter = self._exporter
envelope = exporter._metric_to_envelope(record)
self.assertIsInstance(envelope, Envelope)
self.assertEqual(envelope.ver, 1)
Expand Down Expand Up @@ -205,7 +232,7 @@ def test_observer_to_envelope_value_none(self):
aggregator.update(None)
aggregator.take_checkpoint()
record = MetricRecord(aggregator, self._test_label_set, self._test_obs)
exporter = AzureMonitorMetricsExporter()
exporter = self._exporter
envelope = exporter._metric_to_envelope(record)
self.assertIsInstance(envelope, Envelope)
self.assertEqual(envelope.ver, 1)
Expand Down Expand Up @@ -251,7 +278,7 @@ def test_measure_to_envelope(self, logger_mock):
record = MetricRecord(
aggregator, self._test_label_set, self._test_measure
)
exporter = AzureMonitorMetricsExporter()
exporter = self._exporter
envelope = exporter._metric_to_envelope(record)
self.assertIsInstance(envelope, Envelope)
self.assertEqual(envelope.ver, 1)
Expand Down
30 changes: 22 additions & 8 deletions azure_monitor/tests/test_base_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
from azure_monitor.options import ExporterOptions
from azure_monitor.protocol import Data, Envelope

TEST_FOLDER = os.path.abspath(".test.exporter")
TEST_FOLDER = os.path.abspath(".test.exporter.base")
STORAGE_PATH = os.path.join(TEST_FOLDER)


# pylint: disable=invalid-name
Expand Down Expand Up @@ -47,6 +48,19 @@ def setUpClass(cls):
os.environ[
"APPINSIGHTS_INSTRUMENTATIONKEY"
] = "1234abcd-5678-4efa-8abc-1234567890ab"
cls._base = BaseExporter(storage_path=STORAGE_PATH)

def setUp(self):
for filename in os.listdir(STORAGE_PATH):
file_path = os.path.join(STORAGE_PATH, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except OSError as e:
print("Failed to delete %s. Reason: %s" % (file_path, e))
self._base.clear_telemetry_processors()

def test_constructor(self):
"""Test the constructor."""
Expand Down Expand Up @@ -77,19 +91,19 @@ def test_constructor_wrong_options(self):
BaseExporter(something_else=6)

def test_telemetry_processor_add(self):
base = BaseExporter()
base = self._base
base.add_telemetry_processor(lambda: True)
self.assertEqual(len(base._telemetry_processors), 1)

def test_telemetry_processor_clear(self):
base = BaseExporter()
base = self._base
base.add_telemetry_processor(lambda: True)
self.assertEqual(len(base._telemetry_processors), 1)
base.clear_telemetry_processors()
self.assertEqual(len(base._telemetry_processors), 0)

def test_telemetry_processor_apply(self):
base = BaseExporter()
base = self._base

def callback_function(envelope):
envelope.data.base_type += "_world"
Expand All @@ -100,7 +114,7 @@ def callback_function(envelope):
self.assertEqual(envelope.data.base_type, "type1_world")

def test_telemetry_processor_apply_multiple(self):
base = BaseExporter()
base = self._base
base._telemetry_processors = []

def callback_function(envelope):
Expand All @@ -116,7 +130,7 @@ def callback_function2(envelope):
self.assertEqual(envelope.data.base_type, "type1_world_world2")

def test_telemetry_processor_apply_exception(self):
base = BaseExporter()
base = self._base

def callback_function(envelope):
raise ValueError()
Expand All @@ -131,7 +145,7 @@ def callback_function2(envelope):
self.assertEqual(envelope.data.base_type, "type1_world2")

def test_telemetry_processor_apply_not_accepted(self):
base = BaseExporter()
base = self._base

def callback_function(envelope):
return envelope.data.base_type == "type2"
Expand Down Expand Up @@ -177,7 +191,7 @@ def test_transmission_lease_failure(self, requests_mock):
exporter._transmit_from_storage()
self.assertTrue(exporter.storage.get())

def test_transmit_response_exception(self):
def test_(self):
exporter = BaseExporter(
storage_path=os.path.join(TEST_FOLDER, self.id())
)
Expand Down
49 changes: 28 additions & 21 deletions azure_monitor/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from azure_monitor.options import ExporterOptions

TEST_FOLDER = os.path.abspath(".test.exporter.trace")
STORAGE_PATH = os.path.join(TEST_FOLDER)


# pylint: disable=invalid-name
Expand Down Expand Up @@ -48,29 +49,41 @@ def setUpClass(cls):
os.environ[
"APPINSIGHTS_INSTRUMENTATIONKEY"
] = "1234abcd-5678-4efa-8abc-1234567890ab"
cls._exporter = AzureMonitorSpanExporter(storage_path=STORAGE_PATH)

def setUp(self):
for filename in os.listdir(STORAGE_PATH):
file_path = os.path.join(STORAGE_PATH, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except OSError as e:
print("Failed to delete %s. Reason: %s" % (file_path, e))

def test_constructor(self):
"""Test the constructor."""
exporter = AzureMonitorSpanExporter(
instrumentation_key="4321abcd-5678-4efa-8abc-1234567890ab"
instrumentation_key="4321abcd-5678-4efa-8abc-1234567890ab",
storage_path=os.path.join(TEST_FOLDER, self.id()),
)
self.assertIsInstance(exporter.options, ExporterOptions)
self.assertEqual(
exporter.options.instrumentation_key,
"4321abcd-5678-4efa-8abc-1234567890ab",
)
self.assertEqual(
exporter.options.storage_path, os.path.join(TEST_FOLDER, self.id())
)

def test_export_empty(self):
exporter = AzureMonitorSpanExporter(
storage_path=os.path.join(TEST_FOLDER, self.id())
)
exporter = self._exporter
exporter.export([])
self.assertEqual(len(os.listdir(exporter.storage.path)), 0)

def test_export_failure(self):
exporter = AzureMonitorSpanExporter(
storage_path=os.path.join(TEST_FOLDER, self.id())
)
exporter = self._exporter
with mock.patch(
"azure_monitor.export.trace.AzureMonitorSpanExporter._transmit"
) as transmit: # noqa: E501
Expand All @@ -89,9 +102,7 @@ def test_export_failure(self):
self.assertIsNone(exporter.storage.get())

def test_export_success(self):
exporter = AzureMonitorSpanExporter(
storage_path=os.path.join(TEST_FOLDER, self.id())
)
exporter = self._exporter
test_span = Span(
name="test",
context=SpanContext(
Expand Down Expand Up @@ -122,9 +133,7 @@ def test_export_exception(self, logger_mock):
)
test_span.start()
test_span.end()
exporter = AzureMonitorSpanExporter(
storage_path=os.path.join(TEST_FOLDER, self.id())
)
exporter = self._exporter
with mock.patch(
"azure_monitor.export.trace.AzureMonitorSpanExporter._transmit",
throw(Exception),
Expand All @@ -134,9 +143,7 @@ def test_export_exception(self, logger_mock):
self.assertEqual(logger_mock.exception.called, True)

def test_export_not_retryable(self):
exporter = AzureMonitorSpanExporter(
storage_path=os.path.join(TEST_FOLDER, self.id())
)
exporter = self._exporter
test_span = Span(
name="test",
context=SpanContext(
Expand All @@ -154,15 +161,15 @@ def test_export_not_retryable(self):
self.assertEqual(result, SpanExportResult.FAILED_NOT_RETRYABLE)

def test_span_to_envelope_none(self):
exporter = AzureMonitorSpanExporter()
exporter = self._exporter
self.assertIsNone(exporter._span_to_envelope(None))

# pylint: disable=too-many-statements
def test_span_to_envelope(self):
options = {
"instrumentation_key": "12345678-1234-5678-abcd-12345678abcd"
}
exporter = AzureMonitorSpanExporter(**options)
exporter = AzureMonitorSpanExporter(
instrumentation_key="12345678-1234-5678-abcd-12345678abcd",
storage_path=os.path.join(TEST_FOLDER, self.id()),
)

parent_span = Span(
name="test",
Expand Down
1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ deps =
flake8
isort
black
psutil

commands_pre =
pip install ./azure_monitor
Expand Down