Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Implement proxies for Azure exporters #101

Merged
merged 10 commits into from
Jun 24, 2020
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
2 changes: 2 additions & 0 deletions azure_monitor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
([#92](https://github.com/microsoft/opentelemetry-azure-monitor-python/pull/92))
- Change default local storage directory
([#100](https://github.com/microsoft/opentelemetry-azure-monitor-python/pull/100))
- Implement proxies in exporter configuration
([#101](https://github.com/microsoft/opentelemetry-azure-monitor-python/pull/101))
- Remove request failed per second metrics from auto-collection
([#102](https://github.com/microsoft/opentelemetry-azure-monitor-python/pull/102))

Expand Down
4 changes: 2 additions & 2 deletions azure_monitor/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ package_dir=
=src
packages=find_namespace:
install_requires =
opentelemetry-api == 0.9b0
opentelemetry-sdk == 0.9b0
opentelemetry-api == 0.10b0
opentelemetry-sdk == 0.10b0
psutil >= 5.6.3
requests ~= 2.0

Expand Down
1 change: 1 addition & 0 deletions azure_monitor/src/azure_monitor/export/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def _transmit(self, envelopes: typing.List[Envelope]) -> ExportResult:
"Content-Type": "application/json; charset=utf-8",
},
timeout=self.options.timeout,
proxies=self.options.proxies,
)
except requests.Timeout:
logger.warning(
Expand Down
10 changes: 9 additions & 1 deletion azure_monitor/src/azure_monitor/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ExporterOptions(BaseObject):
Args:
connection_string: Azure Connection String.
instrumentation_key: Azure Instrumentation Key.
proxies: Proxies to pass Azure Monitor request through.
storage_maintenance_period: Local storage maintenance interval in seconds.
storage_max_size: Local storage maximum size in bytes.
storage_path: Local storage file path.
Expand All @@ -39,6 +40,7 @@ class ExporterOptions(BaseObject):
"connection_string",
"endpoint",
"instrumentation_key",
"proxies",
"storage_maintenance_period",
"storage_max_size",
"storage_path",
Expand All @@ -50,6 +52,7 @@ def __init__(
self,
connection_string: str = None,
instrumentation_key: str = None,
proxies: typing.Dict[str, str] = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe setting the default as {} instead of None to avoid check afterwards

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mutable defaults are a common pitfall in Python. https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments

storage_maintenance_period: int = 60,
storage_max_size: int = 50 * 1024 * 1024,
storage_path: str = None,
Expand All @@ -58,6 +61,7 @@ def __init__(
) -> None:
self.connection_string = connection_string
self.instrumentation_key = instrumentation_key
self.proxies = proxies
self.storage_maintenance_period = storage_maintenance_period
self.storage_max_size = storage_max_size
self.storage_path = storage_path
Expand All @@ -68,7 +72,7 @@ def __init__(
self._validate_instrumentation_key()

def _initialize(self) -> None:
# cs and ikey
# connection string and ikey
code_cs = parse_connection_string(self.connection_string)
code_ikey = self.instrumentation_key
env_cs = parse_connection_string(
Expand Down Expand Up @@ -98,6 +102,10 @@ def _initialize(self) -> None:
)
self.endpoint = endpoint + "/v2/track"

# proxies
if self.proxies is None:
self.proxies = {}

# storage path
if self.storage_path is None:
temp_suffix = self.instrumentation_key or ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
)
from opentelemetry.sdk.metrics.export import MetricRecord, MetricsExportResult
from opentelemetry.sdk.metrics.export.aggregate import (
CounterAggregator,
MinMaxSumCountAggregator,
SumAggregator,
ValueObserverAggregator,
)

Expand Down Expand Up @@ -79,7 +79,7 @@ def test_constructor(self):
def test_export(self):
"""Test export."""
record = MetricRecord(
self._test_metric, self._test_labels, CounterAggregator()
self._test_metric, self._test_labels, SumAggregator()
)
exporter = LiveMetricsExporter(
instrumentation_key=self._instrumentation_key,
Expand All @@ -96,7 +96,7 @@ def test_export(self):

def test_export_failed(self):
record = MetricRecord(
self._test_metric, self._test_labels, CounterAggregator()
self._test_metric, self._test_labels, SumAggregator()
)
exporter = LiveMetricsExporter(
instrumentation_key=self._instrumentation_key,
Expand All @@ -113,7 +113,7 @@ def test_export_failed(self):

def test_export_exception(self):
record = MetricRecord(
self._test_metric, self._test_labels, CounterAggregator()
self._test_metric, self._test_labels, SumAggregator()
)
exporter = LiveMetricsExporter(
instrumentation_key=self._instrumentation_key,
Expand Down Expand Up @@ -148,7 +148,7 @@ def test_live_metric_envelope_observer(self):
self.assertEqual(envelope.metrics[0].weight, 1)

def test_live_metric_envelope_counter(self):
aggregator = CounterAggregator()
aggregator = SumAggregator()
aggregator.update(123)
aggregator.take_checkpoint()
record = MetricRecord(self._test_metric, self._test_labels, aggregator)
Expand Down Expand Up @@ -184,7 +184,7 @@ def test_live_metric_envelope_value_recorder(self):
self.assertEqual(envelope.metrics[0].weight, 1)

def test_live_metric_envelope_documents(self):
aggregator = CounterAggregator()
aggregator = SumAggregator()
aggregator.update(123)
aggregator.take_checkpoint()
record = MetricRecord(self._test_metric, self._test_labels, aggregator)
Expand Down
10 changes: 5 additions & 5 deletions azure_monitor/tests/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
)
from opentelemetry.sdk.metrics.export import MetricRecord, MetricsExportResult
from opentelemetry.sdk.metrics.export.aggregate import (
CounterAggregator,
MinMaxSumCountAggregator,
SumAggregator,
ValueObserverAggregator,
)
from opentelemetry.sdk.util import ns_to_iso_str
Expand Down Expand Up @@ -109,7 +109,7 @@ def test_constructor(self):
)
def test_export(self, mte, transmit):
record = MetricRecord(
CounterAggregator(), self._test_labels, self._test_metric
SumAggregator(), self._test_labels, self._test_metric
)
exporter = self._exporter
mte.return_value = Envelope()
Expand All @@ -125,7 +125,7 @@ def test_export(self, mte, transmit):
)
def test_export_failed_retryable(self, mte, transmit):
record = MetricRecord(
CounterAggregator(), self._test_labels, self._test_metric
SumAggregator(), self._test_labels, self._test_metric
)
exporter = self._exporter
transmit.return_value = ExportResult.FAILED_RETRYABLE
Expand All @@ -145,7 +145,7 @@ def test_export_failed_retryable(self, mte, transmit):
)
def test_export_exception(self, mte, transmit, logger_mock):
record = MetricRecord(
CounterAggregator(), self._test_labels, self._test_metric
SumAggregator(), self._test_labels, self._test_metric
)
exporter = self._exporter
mte.return_value = Envelope()
Expand All @@ -159,7 +159,7 @@ def test_metric_to_envelope_none(self):
self.assertIsNone(exporter._metric_to_envelope(None))

def test_metric_to_envelope(self):
aggregator = CounterAggregator()
aggregator = SumAggregator()
aggregator.update(123)
aggregator.take_checkpoint()
record = MetricRecord(self._test_metric, self._test_labels, aggregator)
Expand Down
4 changes: 4 additions & 0 deletions azure_monitor/tests/test_base_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def test_constructor(self):
"""Test the constructor."""
base = BaseExporter(
instrumentation_key="4321abcd-5678-4efa-8abc-1234567890ab",
proxies={"https": "https://test-proxy.com"},
storage_maintenance_period=2,
storage_max_size=3,
storage_path=os.path.join(TEST_FOLDER, self.id()),
Expand All @@ -78,6 +79,9 @@ def test_constructor(self):
base.options.instrumentation_key,
"4321abcd-5678-4efa-8abc-1234567890ab",
)
self.assertEqual(
base.options.proxies, {"https": "https://test-proxy.com"},
)
self.assertEqual(base.options.storage_maintenance_period, 2)
self.assertEqual(base.options.storage_max_size, 3)
self.assertEqual(base.options.storage_retention_period, 4)
Expand Down
16 changes: 16 additions & 0 deletions azure_monitor/tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,22 @@ def test_process_options_endpoint_default(self):
options.endpoint, "https://dc.services.visualstudio.com/v2/track"
)

def test_process_options_proxies_default(self):
options = ExporterOptions(
connection_string=None,
instrumentation_key=self._valid_instrumentation_key,
proxies={},
)
self.assertEqual(options.proxies, {})

def test_process_options_proxies_set_proxies(self):
options = ExporterOptions(
connection_string=None,
instrumentation_key=self._valid_instrumentation_key,
proxies={"https": "https://test-proxy.com"},
)
self.assertEqual(options.proxies, {"https": "https://test-proxy.com"})

def test_parse_connection_string_invalid(self):
self.assertRaises(
ValueError, lambda: ExporterOptions(connection_string="asd")
Expand Down