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

Refactor to opentelemetry-azure-monitor-python #64

Merged
merged 3 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# OpenTelemetry Azure Monitor Exporters
# OpenTelemetry Azure Monitor SDKs and Exporters

[![Gitter chat](https://img.shields.io/gitter/room/opentelemetry/opentelemetry-python)](https://gitter.im/Microsoft/azure-monitor-python)
[![Build status](https://travis-ci.org/microsoft/opentelemetry-exporters-python.svg?branch=master)](https://travis-ci.org/microsoft/opentelemetry-exporters-python)
[![Gitter chat](https://img.shields.io/gitter/room/Microsoft/azure-monitor-python)](https://gitter.im/Microsoft/azure-monitor-python)
[![Build status](https://travis-ci.org/microsoft/opentelemetry-azure-monitor-python.svg?branch=master)](https://travis-ci.org/microsoft/opentelemetry-exporters-python)
[![PyPI version](https://badge.fury.io/py/opentelemetry-azure-monitor-exporter.svg)](https://badge.fury.io/py/opentelemetry-azure-monitor-exporter)

## Installation
Expand Down
3 changes: 2 additions & 1 deletion azure_monitor/examples/metrics/auto_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export.controller import PushController

from azure_monitor import AutoCollection, AzureMonitorMetricsExporter
from azure_monitor import AzureMonitorMetricsExporter
from azure_monitor.sdk.auto_collection import AutoCollection

metrics.set_meter_provider(MeterProvider())
meter = metrics.get_meter(__name__)
Expand Down
26 changes: 26 additions & 0 deletions azure_monitor/examples/metrics/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# pylint: disable=import-error
# pylint: disable=no-member
# pylint: disable=no-name-in-module
import requests
from opentelemetry import trace
from opentelemetry.ext import http_requests
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor

from azure_monitor import AzureMonitorSpanExporter

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
http_requests.enable(trace.get_tracer_provider())
span_processor = BatchExportSpanProcessor(
AzureMonitorSpanExporter(
connection_string="InstrumentationKey=<INSTRUMENTATION KEY HERE>"
)
)
trace.get_tracer_provider().add_span_processor(span_processor)

response = requests.get(url="http://google.com")

input("Press any key to exit...")
2 changes: 1 addition & 1 deletion azure_monitor/examples/metrics/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
metrics.set_meter_provider(MeterProvider())
meter = metrics.get_meter(__name__)
exporter = AzureMonitorMetricsExporter(
connection_string="InstrumentationKey=<INSTRUMENTATION KEY HERE>"
# connection_string="InstrumentationKey=<INSTRUMENTATION KEY HERE>"
lzchen marked this conversation as resolved.
Show resolved Hide resolved
)
controller = PushController(meter, exporter, 5)

Expand Down
2 changes: 2 additions & 0 deletions azure_monitor/examples/traces/trace.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
Expand Down
9 changes: 1 addition & 8 deletions azure_monitor/src/azure_monitor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
from azure_monitor.auto_collection import AutoCollection
from azure_monitor.export.metrics import AzureMonitorMetricsExporter
from azure_monitor.export.trace import AzureMonitorSpanExporter
from azure_monitor.options import ExporterOptions

__all__ = [
"AutoCollection",
"AzureMonitorMetricsExporter",
"AzureMonitorSpanExporter",
"ExporterOptions",
]
__all__ = ["AzureMonitorMetricsExporter", "AzureMonitorSpanExporter"]
5 changes: 5 additions & 0 deletions azure_monitor/src/azure_monitor/sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
from . import auto_collection

__all__ = ["auto_collection"]
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#
from opentelemetry.metrics import LabelSet, Meter

from azure_monitor.auto_collection.dependency_metrics import DependencyMetrics
from azure_monitor.auto_collection.performance_metrics import (
from azure_monitor.sdk.auto_collection.dependency_metrics import (
DependencyMetrics,
)
from azure_monitor.sdk.auto_collection.performance_metrics import (
PerformanceMetrics,
)
from azure_monitor.auto_collection.request_metrics import RequestMetrics
from azure_monitor.sdk.auto_collection.request_metrics import RequestMetrics

__all__ = [
"AutoCollection",
Expand Down
10 changes: 6 additions & 4 deletions azure_monitor/tests/auto_collection/test_auto_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from opentelemetry import metrics
from opentelemetry.sdk.metrics import MeterProvider

from azure_monitor.auto_collection import AutoCollection
from azure_monitor.sdk.auto_collection import AutoCollection


# pylint: disable=protected-access
Expand All @@ -24,12 +24,14 @@ def tearDownClass(cls):
metrics._METER_PROVIDER = None

@mock.patch(
"azure_monitor.auto_collection.PerformanceMetrics", autospec=True
"azure_monitor.sdk.auto_collection.PerformanceMetrics", autospec=True
)
@mock.patch(
"azure_monitor.auto_collection.DependencyMetrics", autospec=True
"azure_monitor.sdk.auto_collection.DependencyMetrics", autospec=True
)
@mock.patch(
"azure_monitor.sdk.auto_collection.RequestMetrics", autospec=True
)
@mock.patch("azure_monitor.auto_collection.RequestMetrics", autospec=True)
def test_constructor(
self, mock_performance, mock_dependencies, mock_requests
):
Expand Down
40 changes: 27 additions & 13 deletions azure_monitor/tests/auto_collection/test_dependency_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from opentelemetry import metrics
from opentelemetry.sdk.metrics import MeterProvider, Observer

from azure_monitor.auto_collection import DependencyMetrics, dependency_metrics
from azure_monitor.sdk.auto_collection import dependency_metrics

ORIGINAL_FUNCTION = requests.Session.request
ORIGINAL_CONS = HTTPServer.__init__
Expand All @@ -27,6 +27,8 @@ def setUpClass(cls):
@classmethod
def tearDown(cls):
metrics._METER_PROVIDER = None
requests.Session.request = ORIGINAL_FUNCTION
dependency_metrics.ORIGINAL_CONSTRUCTOR = ORIGINAL_CONS

def setUp(self):
dependency_metrics.dependency_map.clear()
Expand All @@ -35,7 +37,7 @@ def setUp(self):

def test_constructor(self):
mock_meter = mock.Mock()
metrics_collector = DependencyMetrics(
metrics_collector = dependency_metrics.DependencyMetrics(
meter=mock_meter, label_set=self._test_label_set
)
self.assertEqual(metrics_collector._meter, mock_meter)
Expand All @@ -49,10 +51,10 @@ def test_constructor(self):
value_type=int,
)

@mock.patch("azure_monitor.auto_collection.dependency_metrics.time")
@mock.patch("azure_monitor.sdk.auto_collection.dependency_metrics.time")
def test_track_dependency_rate(self, time_mock):
time_mock.time.return_value = 100
metrics_collector = DependencyMetrics(
metrics_collector = dependency_metrics.DependencyMetrics(
meter=self._meter, label_set=self._test_label_set
)
obs = Observer(
Expand All @@ -68,10 +70,10 @@ def test_track_dependency_rate(self, time_mock):
metrics_collector._track_dependency_rate(obs)
self.assertEqual(obs.aggregators[self._test_label_set].current, 2)

@mock.patch("azure_monitor.auto_collection.dependency_metrics.time")
@mock.patch("azure_monitor.sdk.auto_collection.dependency_metrics.time")
def test_track_dependency_rate_time_none(self, time_mock):
time_mock.time.return_value = 100
metrics_collector = DependencyMetrics(
metrics_collector = dependency_metrics.DependencyMetrics(
meter=self._meter, label_set=self._test_label_set
)
dependency_metrics.dependency_map["last_time"] = None
Expand All @@ -86,10 +88,10 @@ def test_track_dependency_rate_time_none(self, time_mock):
metrics_collector._track_dependency_rate(obs)
self.assertEqual(obs.aggregators[self._test_label_set].current, 0)

@mock.patch("azure_monitor.auto_collection.dependency_metrics.time")
@mock.patch("azure_monitor.sdk.auto_collection.dependency_metrics.time")
def test_track_dependency_rate_error(self, time_mock):
time_mock.time.return_value = 100
metrics_collector = DependencyMetrics(
metrics_collector = dependency_metrics.DependencyMetrics(
meter=self._meter, label_set=self._test_label_set
)
dependency_metrics.dependency_map["last_time"] = 100
Expand All @@ -105,10 +107,22 @@ def test_track_dependency_rate_error(self, time_mock):
metrics_collector._track_dependency_rate(obs)
self.assertEqual(obs.aggregators[self._test_label_set].current, 5)

def test_dependency_patch(self):
dependency_metrics.ORIGINAL_REQUEST = lambda x: None
@mock.patch(
"azure_monitor.sdk.auto_collection.dependency_metrics.ORIGINAL_REQUEST"
)
def test_dependency_patch(self, request_mock):
session = requests.Session()
result = dependency_metrics.dependency_patch(session)

dependency_metrics.dependency_patch(session)
self.assertEqual(dependency_metrics.dependency_map["count"], 1)
self.assertIsNone(result)
request_mock.assert_called_with(session)

@mock.patch(
"azure_monitor.sdk.auto_collection.dependency_metrics.ORIGINAL_REQUEST"
)
@mock.patch("azure_monitor.sdk.auto_collection.dependency_metrics.context")
def test_dependency_patch_suppress(self, context_mock, request_mock):
context_mock.get_value.return_value = {}
session = requests.Session()
dependency_metrics.dependency_patch(session)
self.assertEqual(dependency_metrics.dependency_map.get("count"), None)
request_mock.assert_called_with(session)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from opentelemetry import metrics
from opentelemetry.sdk.metrics import MeterProvider, Observer

from azure_monitor.auto_collection import PerformanceMetrics
from azure_monitor.sdk.auto_collection import PerformanceMetrics


def throw(exc_type, *args, **kwargs):
Expand Down Expand Up @@ -111,10 +111,10 @@ def test_track_memory(self, psutil_mock):
performance_metrics_collector._track_memory(obs)
self.assertEqual(obs.aggregators[self._test_label_set].current, 100)

@mock.patch("azure_monitor.auto_collection.performance_metrics.psutil")
@mock.patch("azure_monitor.sdk.auto_collection.performance_metrics.psutil")
def test_track_process_cpu(self, psutil_mock):
with mock.patch(
"azure_monitor.auto_collection.performance_metrics.PROCESS"
"azure_monitor.sdk.auto_collection.performance_metrics.PROCESS"
) as process_mock:
performance_metrics_collector = PerformanceMetrics(
meter=self._meter, label_set=self._test_label_set
Expand All @@ -134,10 +134,10 @@ def test_track_process_cpu(self, psutil_mock):
obs.aggregators[self._test_label_set].current, 22.2
)

@mock.patch("azure_monitor.auto_collection.performance_metrics.logger")
@mock.patch("azure_monitor.sdk.auto_collection.performance_metrics.logger")
def test_track_process_cpu_exception(self, logger_mock):
with mock.patch(
"azure_monitor.auto_collection.performance_metrics.psutil"
"azure_monitor.sdk.auto_collection.performance_metrics.psutil"
) as psutil_mock:
performance_metrics_collector = PerformanceMetrics(
meter=self._meter, label_set=self._test_label_set
Expand All @@ -156,7 +156,7 @@ def test_track_process_cpu_exception(self, logger_mock):

def test_track_process_memory(self):
with mock.patch(
"azure_monitor.auto_collection.performance_metrics.PROCESS"
"azure_monitor.sdk.auto_collection.performance_metrics.PROCESS"
) as process_mock:
performance_metrics_collector = PerformanceMetrics(
meter=self._meter, label_set=self._test_label_set
Expand All @@ -177,10 +177,10 @@ def test_track_process_memory(self):
obs.aggregators[self._test_label_set].current, 100
)

@mock.patch("azure_monitor.auto_collection.performance_metrics.logger")
@mock.patch("azure_monitor.sdk.auto_collection.performance_metrics.logger")
def test_track_process_memory_exception(self, logger_mock):
with mock.patch(
"azure_monitor.auto_collection.performance_metrics.PROCESS",
"azure_monitor.sdk.auto_collection.performance_metrics.PROCESS",
throw(Exception),
):
performance_metrics_collector = PerformanceMetrics(
Expand Down
24 changes: 12 additions & 12 deletions azure_monitor/tests/auto_collection/test_request_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from opentelemetry import metrics
from opentelemetry.sdk.metrics import MeterProvider, Observer

from azure_monitor.auto_collection import RequestMetrics, request_metrics
from azure_monitor.sdk.auto_collection import request_metrics

ORIGINAL_FUNCTION = requests.Session.request
ORIGINAL_CONS = HTTPServer.__init__
Expand All @@ -35,7 +35,7 @@ def setUp(self):

def test_constructor(self):
mock_meter = mock.Mock()
request_metrics_collector = RequestMetrics(
request_metrics_collector = request_metrics.RequestMetrics(
meter=mock_meter, label_set=self._test_label_set
)
self.assertEqual(request_metrics_collector._meter, mock_meter)
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_constructor(self):
)

def test_track_request_duration(self):
request_metrics_collector = RequestMetrics(
request_metrics_collector = request_metrics.RequestMetrics(
meter=self._meter, label_set=self._test_label_set
)
request_metrics.requests_map["duration"] = 0.1
Expand All @@ -82,7 +82,7 @@ def test_track_request_duration(self):
self.assertEqual(obs.aggregators[self._test_label_set].current, 20)

def test_track_request_duration_error(self):
request_metrics_collector = RequestMetrics(
request_metrics_collector = request_metrics.RequestMetrics(
meter=self._meter, label_set=self._test_label_set
)
request_metrics.requests_map["duration"] = 0.1
Expand All @@ -99,9 +99,9 @@ def test_track_request_duration_error(self):
request_metrics_collector._track_request_duration(obs)
self.assertEqual(obs.aggregators[self._test_label_set].current, 0)

@mock.patch("azure_monitor.auto_collection.request_metrics.time")
@mock.patch("azure_monitor.sdk.auto_collection.request_metrics.time")
def test_track_request_rate(self, time_mock):
request_metrics_collector = RequestMetrics(
request_metrics_collector = request_metrics.RequestMetrics(
meter=self._meter, label_set=self._test_label_set
)
time_mock.time.return_value = 100
Expand All @@ -118,10 +118,10 @@ def test_track_request_rate(self, time_mock):
request_metrics_collector._track_request_rate(obs)
self.assertEqual(obs.aggregators[self._test_label_set].current, 2)

@mock.patch("azure_monitor.auto_collection.request_metrics.time")
@mock.patch("azure_monitor.sdk.auto_collection.request_metrics.time")
def test_track_request_rate_time_none(self, time_mock):
time_mock.time.return_value = 100
request_metrics_collector = RequestMetrics(
request_metrics_collector = request_metrics.RequestMetrics(
meter=self._meter, label_set=self._test_label_set
)
request_metrics.requests_map["last_time"] = None
Expand All @@ -136,9 +136,9 @@ def test_track_request_rate_time_none(self, time_mock):
request_metrics_collector._track_request_rate(obs)
self.assertEqual(obs.aggregators[self._test_label_set].current, 0)

@mock.patch("azure_monitor.auto_collection.request_metrics.time")
@mock.patch("azure_monitor.sdk.auto_collection.request_metrics.time")
def test_track_request_rate_error(self, time_mock):
request_metrics_collector = RequestMetrics(
request_metrics_collector = request_metrics.RequestMetrics(
meter=self._meter, label_set=self._test_label_set
)
time_mock.time.return_value = 100
Expand Down Expand Up @@ -168,7 +168,7 @@ def test_request_patch(self):
def test_server_patch(self):
request_metrics.ORIGINAL_CONSTRUCTOR = lambda x, y, z: None
with mock.patch(
"azure_monitor.auto_collection.request_metrics.request_patch"
"azure_monitor.sdk.auto_collection.request_metrics.request_patch"
) as request_mock:
handler = mock.Mock()
handler.do_DELETE.return_value = None
Expand All @@ -191,7 +191,7 @@ def test_server_patch(self):
def test_server_patch_no_methods(self):
request_metrics.ORIGINAL_CONSTRUCTOR = lambda x, y, z: None
with mock.patch(
"azure_monitor.auto_collection.request_metrics.request_patch"
"azure_monitor.sdk.auto_collection.request_metrics.request_patch"
) as request_mock:
handler = mock.Mock()
result = request_metrics.server_patch(None, None, handler)
Expand Down
Loading