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.

Add property to request/dependency telemetry to indicate standard metrics + remove request metrics from Autocollection #124

Merged
merged 12 commits into from
Oct 2, 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ with tracer.start_as_current_span('hello'):
print('Hello World!')
```

#### Integrations
#### Instrumentations

OpenTelemetry also supports several [integrations](https://github.com/open-telemetry/opentelemetry-python/tree/master/ext) which allows to integrate with third party libraries.
OpenTelemetry also supports several [instrumentations](https://github.com/open-telemetry/opentelemetry-python/tree/master/instrumentation) which allows to instrument with third party libraries.

This example shows how to integrate with the [requests](https://2.python-requests.org/en/master/)_ library.
This example shows how to instrument with the [requests](https://2.python-requests.org/en/master/)_ library.

* Create an Azure Monitor resource and get the instrumentation key, more information can be found [here](https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource).
* Install the `requests` integration package using ``pip install opentelemetry-ext-http-requests``.
Expand Down
3 changes: 3 additions & 0 deletions azure_monitor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- Remove request metrics from auto-collection
([#124](https://github.com/microsoft/opentelemetry-azure-monitor-python/pull/124))

## 0.5b.0
Released 2020-09-24

Expand Down
2 changes: 1 addition & 1 deletion azure_monitor/examples/metrics/auto_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

testing_label_set = {"environment": "testing"}

# Automatically collect standard metrics
# Automatically collect performance counters
auto_collection = AutoCollection(meter=meter, labels=testing_label_set)

metrics.get_meter_provider().start_pipeline(meter, exporter, 2)
Expand Down
14 changes: 14 additions & 0 deletions azure_monitor/src/azure_monitor/export/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class AzureMonitorSpanExporter(BaseExporter, SpanExporter):
options: :doc:`export.options` to allow configuration for the exporter
"""

def __init__(self, **options):
super().__init__(**options)
self.add_telemetry_processor(indicate_processed_by_metric_extractors)

def export(self, spans: Sequence[Span]) -> SpanExportResult:
envelopes = list(map(self._span_to_envelope, spans))
envelopes = list(
Expand Down Expand Up @@ -122,6 +126,7 @@ def convert_span_to_envelope(span: Span) -> protocol.Envelope:
"component" in span.attributes
and span.attributes["component"] == "http"
):
# TODO: check other component types (e.g. db)
data.type = "HTTP"
if "http.url" in span.attributes:
url = span.attributes["http.url"]
Expand Down Expand Up @@ -157,3 +162,12 @@ def convert_span_to_envelope(span: Span) -> protocol.Envelope:
data.properties["_MS.links"] = json.dumps(links)
# TODO: tracestate, tags
return envelope


def indicate_processed_by_metric_extractors(envelope):
name = "Requests"
if envelope.data.base_type == "RemoteDependencyData":
name = "Dependencies"
envelope.data.base_data.properties["_MS.ProcessedByMetricExtractors"] = (
"(Name:'" + name + "',Ver:'1.1')"
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@


class AutoCollection:
"""Starts auto collection of standard metrics, including performance,
dependency and request metrics.
"""Starts auto collection of performance counters

Args:
meter: OpenTelemetry Meter
Expand All @@ -35,4 +34,3 @@ class AutoCollection:
def __init__(self, meter: Meter, labels: Dict[str, str]):
col_type = AutoCollectionType.PERF_COUNTER
self._performance_metrics = PerformanceMetrics(meter, labels, col_type)
self._request_metrics = RequestMetrics(meter, labels, col_type)
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def __init__(self, meter: Meter, labels: Dict[str, str]):
)

def _track_dependency_rate(self, observer: Observer) -> None:
""" Track Dependency rate
"""Track Dependency rate

Calculated by obtaining the number of outgoing requests made
using the requests library within an elapsed time and dividing
Expand Down Expand Up @@ -121,7 +121,7 @@ def _track_dependency_rate(self, observer: Observer) -> None:
observer.observe(last_result, self._labels)

def _track_dependency_duration(self, observer: Observer) -> None:
""" Track Dependency average duration
"""Track Dependency average duration

Calculated by getting the time it takes to make an outgoing request
and dividing over the amount of outgoing requests over an elapsed time.
Expand Down Expand Up @@ -149,7 +149,7 @@ def _track_dependency_duration(self, observer: Observer) -> None:
observer.observe(last_average_duration, self._labels)

def _track_failure_rate(self, observer: Observer) -> None:
""" Track Failed Dependency rate
"""Track Failed Dependency rate

Calculated by obtaining the number of failed outgoing requests made
using the requests library within an elapsed time and dividing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(
col_type = AutoCollectionType.LIVE_METRICS
self._performance_metrics = PerformanceMetrics(meter, labels, col_type)
self._dependency_metrics = DependencyMetrics(meter, labels)
self._request_metrics = RequestMetrics(meter, labels, col_type)
self._request_metrics = RequestMetrics(meter, labels)
self._manager = LiveMetricsManager(
meter, instrumentation_key, span_processor
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def __init__(
)

def _track_cpu(self, observer: Observer) -> None:
""" Track CPU time
"""Track CPU time

Processor time is defined as a float representing the current system
wide CPU utilization minus idle CPU time as a percentage. Idle CPU
Expand All @@ -90,15 +90,15 @@ def _track_cpu(self, observer: Observer) -> None:
observer.observe(100.0 - cpu_times_percent.idle, self._labels)

def _track_memory(self, observer: Observer) -> None:
""" Track Memory
"""Track Memory

Available memory is defined as memory that can be given instantly to
processes without the system going into swap.
"""
observer.observe(psutil.virtual_memory().available, self._labels)

def _track_process_cpu(self, observer: Observer) -> None:
""" Track Process CPU time
"""Track Process CPU time

Returns a derived gauge for the CPU usage for the current process.
Return values range from 0.0 to 100.0 inclusive.
Expand All @@ -113,7 +113,7 @@ def _track_process_cpu(self, observer: Observer) -> None:
logger.warning("Error handling get process cpu usage.")

def _track_process_memory(self, observer: Observer) -> None:
""" Track Memory
"""Track Memory

Available memory is defined as memory that can be given instantly to
processes without the system going into swap.
Expand All @@ -124,7 +124,7 @@ def _track_process_memory(self, observer: Observer) -> None:
logger.warning("Error handling get process private bytes.")

def _track_commited_memory(self, observer: Observer) -> None:
""" Track Commited Memory
"""Track Commited Memory

Available commited memory is defined as total memory minus available memory.
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from opentelemetry.metrics import Meter, Observer
from opentelemetry.sdk.metrics import UpDownSumObserver

from azure_monitor.sdk.auto_collection.utils import AutoCollectionType

_requests_lock = threading.Lock()
logger = logging.getLogger(__name__)
requests_map = dict()
Expand Down Expand Up @@ -79,29 +77,24 @@ class RequestMetrics:
Args:
meter: OpenTelemetry Meter
labels: Dictionary of labels
collection_type: Standard or Live Metrics
"""

def __init__(
self,
meter: Meter,
labels: Dict[str, str],
collection_type: AutoCollectionType,
self, meter: Meter, labels: Dict[str, str],
):
self._meter = meter
self._labels = labels
# Patch the HTTPServer handler to track request information
HTTPServer.__init__ = server_patch

if collection_type == AutoCollectionType.LIVE_METRICS:
meter.register_observer(
callback=self._track_request_failed_rate,
name="\\ApplicationInsights\\Requests Failed/Sec",
description="Incoming Requests Failed Rate",
unit="rps",
value_type=float,
observer_type=UpDownSumObserver,
)
meter.register_observer(
callback=self._track_request_failed_rate,
name="\\ApplicationInsights\\Requests Failed/Sec",
description="Incoming Requests Failed Rate",
unit="rps",
value_type=float,
observer_type=UpDownSumObserver,
)
meter.register_observer(
callback=self._track_request_duration,
name="\\ApplicationInsights\\Request Duration",
Expand All @@ -120,7 +113,7 @@ def __init__(
)

def _track_request_duration(self, observer: Observer) -> None:
""" Track Request execution time
"""Track Request execution time

Calculated by getting the time it takes to make an incoming request
and dividing over the amount of incoming requests over an elapsed time.
Expand All @@ -144,7 +137,7 @@ def _track_request_duration(self, observer: Observer) -> None:
observer.observe(last_average_duration, self._labels)

def _track_request_rate(self, observer: Observer) -> None:
""" Track Request execution rate
"""Track Request execution rate

Calculated by obtaining by getting the number of incoming requests
made to an HTTPServer within an elapsed time and dividing that value
Expand Down Expand Up @@ -174,7 +167,7 @@ def _track_request_rate(self, observer: Observer) -> None:
observer.observe(last_rate, self._labels)

def _track_request_failed_rate(self, observer: Observer) -> None:
""" Track Request failed execution rate
"""Track Request failed execution rate

Calculated by obtaining by getting the number of failed incoming requests
made to an HTTPServer within an elapsed time and dividing that value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@


class AutoCollectionType(Enum):
"""Automatic collection of metrics type
"""
"""Automatic collection of metrics type"""

PERF_COUNTER = 0
LIVE_METRICS = 1
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,10 @@ def tearDownClass(cls):
@mock.patch(
"azure_monitor.sdk.auto_collection.PerformanceMetrics", autospec=True
)
@mock.patch(
"azure_monitor.sdk.auto_collection.RequestMetrics", autospec=True
)
def test_constructor(self, mock_requests, mock_performance):
def test_constructor(self, mock_performance):
"""Test the constructor."""

AutoCollection(meter=self._meter, labels=self._test_labels)
self.assertEqual(mock_performance.called, True)
self.assertEqual(mock_requests.called, True)
self.assertEqual(mock_performance.call_args[0][0], self._meter)
self.assertEqual(mock_performance.call_args[0][1], self._test_labels)
self.assertEqual(mock_requests.call_args[0][0], self._meter)
self.assertEqual(mock_requests.call_args[0][1], self._test_labels)
Loading