Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

### Bugs Fixed

- Fixed sampleRate field in ApplicationInsightsSampler
([#26771](https://github.com/Azure/azure-sdk-for-python/pull/26771))

### Other Changes

- Update `README.md`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def __init__(self, **kwargs: Any) -> None:
"""Azure Monitor base exporter for OpenTelemetry.

:keyword str api_version: The service API version used. Defaults to latest.
:keyword str connection_string: The connection string used for your Application Insights resource.
:keyword bool enable_local_storage: Determines whether to store failed telemetry records for retry. Defaults to `True`.
:keyword str storage_path: Storage path in which to store retry files. Defaults to `<tempfile.gettempdir()>/opentelemetry-python-<your-instrumentation-key>`.
:rtype: None
"""
parsed_connection_string = ConnectionStringParser(kwargs.get('connection_string'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
"code.",
]

_STANDARD_AZURE_MONITOR_ATTRIBUTES = [
Comment thread
lzchen marked this conversation as resolved.
"sampleRate",
]


class AzureMonitorTraceExporter(BaseExporter, SpanExporter):
"""Azure Monitor Trace exporter for OpenTelemetry."""
Expand Down Expand Up @@ -420,9 +424,13 @@ def _convert_span_to_envelope(span: ReadableSpan) -> TelemetryItem:
if target:
data.target = str(target)[:1024]

# sampleRate
if "sampleRate" in span.attributes:
envelope.sample_rate = span.attributes["sampleRate"]

data.properties = _utils._filter_custom_properties(
span.attributes,
lambda key, val: not _is_opentelemetry_standard_attribute(key)
lambda key, val: not _is_standard_attribute(key)
)
if span.links:
# Max length for value is 8192
Expand Down Expand Up @@ -450,7 +458,7 @@ def _convert_span_events_to_envelopes(span: ReadableSpan) -> Sequence[TelemetryI
)
properties = _utils._filter_custom_properties(
event.attributes,
lambda key, val: not _is_opentelemetry_standard_attribute(key)
lambda key, val: not _is_standard_attribute(key)
)
if event.name == "exception":
envelope.name = 'Microsoft.ApplicationInsights.Exception'
Expand Down Expand Up @@ -545,11 +553,12 @@ def _check_instrumentation_span(span: ReadableSpan) -> None:
_utils.add_instrumentation(name)


def _is_opentelemetry_standard_attribute(key: str) -> bool:
def _is_standard_attribute(key: str) -> bool:
for prefix in _STANDARD_OPENTELEMETRY_ATTRIBUTE_PREFIXES:
if key.startswith(prefix):
return True
return False
return key in _STANDARD_AZURE_MONITOR_ATTRIBUTES


def _get_azure_sdk_target_source(attributes: Attributes) -> Optional[str]:
# Currently logic only works for ServiceBus and EventHub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ products:

These code samples show common champion scenario operations with the AzureMonitorMetricExporter.

* Attributes: [sample_attributes.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/metrics/sample_attributes.py)
* Instruments: [sample_instruments.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/metrics/sample_instruments.py)
* Views: [sample_views.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/samples/metrics/sample_views.py)

## Installation

Expand All @@ -20,6 +22,17 @@ $ pip install azure-monitor-opentelemetry-exporter --pre

## Run the Applications

### Metrics with attributes

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ # from this directory
$ python sample_attributes.py
```

### Instrument usage

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable
Expand All @@ -31,6 +44,17 @@ $ # from this directory
$ python sample_instruments.py
```

### Configuring metrics with views

* Update `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable

* Run the sample

```sh
$ # from this directory
$ python sample_views.py
```

## Explore the data

After running the applications, data would be available in [Azure](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
An example to show an application using different attributes with instruments in the OpenTelemetry SDK.
Metrics created and recorded using the sdk are tracked and telemetry is exported to application insights
with the AzureMonitorMetricsExporter.
with the AzureMonitorMetricExporter.
"""
import os

Expand All @@ -16,7 +16,8 @@
exporter = AzureMonitorMetricExporter.from_connection_string(
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
)
reader = PeriodicExportingMetricReader(exporter, export_interval_millis=5000)
# Metrics are reported every 1 minute
reader = PeriodicExportingMetricReader(exporter)
metrics.set_meter_provider(MeterProvider(metric_readers=[reader]))

attribute_set1 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# cSpell:disable
"""
An example to show an application using all instruments in the OpenTelemetry SDK. Metrics created
and recorded using the sdk are tracked and telemetry is exported to application insights with the
AzureMonitorMetricsExporter.
AzureMonitorMetricExporter.
"""
import os
from typing import Iterable
Expand All @@ -18,7 +19,8 @@
exporter = AzureMonitorMetricExporter.from_connection_string(
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
)
reader = PeriodicExportingMetricReader(exporter, export_interval_millis=5000)
# Metrics are reported every 1 minute
reader = PeriodicExportingMetricReader(exporter)
metrics.set_meter_provider(MeterProvider(metric_readers=[reader]))

# Create a namespaced meter
Expand Down Expand Up @@ -63,3 +65,5 @@ def observable_gauge_func(options: CallbackOptions) -> Iterable[Observation]:

# Async Gauge
gauge = meter.create_observable_gauge("gauge", [observable_gauge_func])

# cSpell:disable
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
This example shows how to customize the metrics that are output by the SDK using Views. Metrics created
and recorded using the sdk are tracked and telemetry is exported to application insights with the
AzureMonitorMetricsExporter.
AzureMonitorMetricExporter.
"""
import os

Expand All @@ -25,7 +25,8 @@
name="my.counter.total",
)

reader = PeriodicExportingMetricReader(exporter, export_interval_millis=5000)
# Metrics are reported every 1 minute
reader = PeriodicExportingMetricReader(exporter)
provider = MeterProvider(
metric_readers=[
reader,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@
# Approximately 25% of these spans should be sampled out
with tracer.start_as_current_span("hello"):
print("Hello, World!")

input()