Skip to content

Commit 7b8b7ad

Browse files
authored
Merge branch 'main' into status-logger
2 parents 20b23b6 + 62cb576 commit 7b8b7ad

6 files changed

Lines changed: 127 additions & 46 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
([#214](https://github.com/microsoft/ApplicationInsights-Python/pull/214))
1515
- Introduce Distro API
1616
([#215](https://github.com/microsoft/ApplicationInsights-Python/pull/215))
17+
- Rename to `configure_azure_monitor`, add sampler to config
18+
([#216](https://github.com/microsoft/ApplicationInsights-Python/pull/216))
1719

1820
## [1.0.0b8](https://github.com/microsoft/ApplicationInsights-Python/releases/tag/v1.0.0b8) - 2022-09-26
1921

azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/__init__.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,33 @@
44
# license information.
55
# --------------------------------------------------------------------------
66
from azure.monitor.opentelemetry.distro.util import get_configurations
7-
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
7+
from azure.monitor.opentelemetry.exporter import (
8+
ApplicationInsightsSampler,
9+
AzureMonitorTraceExporter,
10+
)
811
from opentelemetry import trace
912
from opentelemetry.sdk.resources import Resource
1013
from opentelemetry.sdk.trace import TracerProvider
1114
from opentelemetry.sdk.trace.export import BatchSpanProcessor
1215
from opentelemetry.semconv.resource import ResourceAttributes
1316

1417

15-
def configure_opentelemetry(**kwargs):
18+
def configure_azure_monitor(**kwargs):
1619
"""
1720
This function works as a configuration layer that allows the
1821
end user to configure OpenTelemetry and Azure monitor components. The
19-
configuration can be done via environment variables or
20-
via arguments passed to this function. Each argument has a 1:1
21-
correspondence with an environment variable.
22+
configuration can be done via arguments passed to this function.
2223
"""
2324

2425
configurations = get_configurations(**kwargs)
25-
connection_string = configurations["connection_string"]
26-
service_name = configurations["service_name"]
27-
service_namespace = configurations["service_namespace"]
28-
service_instance_id = configurations["service_instance_id"]
29-
disable_tracing = configurations["disable_tracing"]
26+
disable_tracing = configurations.get("disable_tracing", False)
27+
service_name = configurations.get("service_name", "")
28+
service_namespace = configurations.get("service_namespace", "")
29+
service_instance_id = configurations.get("service_instance_id", "")
30+
sampling_ratio = configurations.get("sampling_ratio", 1.0)
31+
tracing_export_interval_millis = configurations.get(
32+
"tracing_export_interval_millis", 30000
33+
)
3034

3135
if not disable_tracing:
3236
resource = Resource.create(
@@ -36,9 +40,14 @@ def configure_opentelemetry(**kwargs):
3640
ResourceAttributes.SERVICE_INSTANCE_ID: service_instance_id,
3741
}
3842
)
39-
trace.set_tracer_provider(TracerProvider(resource=resource))
40-
exporter = AzureMonitorTraceExporter(
41-
connection_string=connection_string
43+
tracer_provider = TracerProvider(
44+
sampler=ApplicationInsightsSampler(sampling_ratio=sampling_ratio),
45+
resource=resource,
46+
)
47+
trace.set_tracer_provider(tracer_provider)
48+
exporter = AzureMonitorTraceExporter(**kwargs)
49+
span_processor = BatchSpanProcessor(
50+
exporter,
51+
export_timeout_millis=tracing_export_interval_millis,
4252
)
43-
span_processor = BatchSpanProcessor(exporter)
4453
trace.get_tracer_provider().add_span_processor(span_processor)

azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/util/__init__.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,10 @@
1010
def get_configurations(**kwargs) -> Dict[str, Any]:
1111
configurations = {}
1212

13-
# In-code configurations take priority
14-
configurations["connection_string"] = kwargs.get("connection_string", None)
15-
configurations["disable_tracing"] = kwargs.get("disable_tracing", False)
16-
configurations["service_name"] = kwargs.get("service_name", "")
17-
configurations["service_namespace"] = kwargs.get("service_namespace", "")
18-
configurations["service_instance_id"] = kwargs.get(
19-
"service_instance_id", ""
20-
)
21-
22-
# TODO: Support addtional env vars configurations
23-
# if configurations.get("disable_tracing") is None:
24-
# configurations["disable_tracing"] = False
13+
for key, val in kwargs.items():
14+
configurations[key] = val
2515

2616
return configurations
17+
18+
19+
# TODO: Add env var configuration

azure-monitor-opentelemetry-distro/samples/simple.py renamed to azure-monitor-opentelemetry-distro/samples/tracing/simple.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
# license information.
55
# --------------------------------------------------------------------------
66

7-
from azure.monitor.opentelemetry.distro import configure_opentelemetry
7+
from azure.monitor.opentelemetry.distro import configure_azure_monitor
88
from opentelemetry import trace
99

10-
configure_opentelemetry()
10+
configure_azure_monitor(
11+
connection_string="<your-connection-string>",
12+
service_name="foo_service",
13+
tracing_export_interval_millis=15000,
14+
)
1115

1216
tracer = trace.get_tracer(__name__)
1317

azure-monitor-opentelemetry-distro/tests/configuration/test_configure.py

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import unittest
1616
from unittest.mock import Mock, patch
1717

18-
from azure.monitor.opentelemetry.distro import configure_opentelemetry
18+
from azure.monitor.opentelemetry.distro import configure_azure_monitor
1919
from opentelemetry.semconv.resource import ResourceAttributes
2020

2121

@@ -30,16 +30,20 @@ class TestConfigure(unittest.TestCase):
3030
"azure.monitor.opentelemetry.distro.TracerProvider",
3131
autospec=True,
3232
)
33+
@patch(
34+
"azure.monitor.opentelemetry.distro.ApplicationInsightsSampler",
35+
)
3336
@patch(
3437
"azure.monitor.opentelemetry.distro.Resource",
3538
)
3639
@patch(
3740
"azure.monitor.opentelemetry.distro.trace",
3841
)
39-
def test_configure_opentelemetry(
42+
def test_configure_azure_monitor(
4043
self,
4144
trace_mock,
4245
resource_mock,
46+
sampler_mock,
4347
tp_mock,
4448
exporter_mock,
4549
bsp_mock,
@@ -50,14 +54,17 @@ def test_configure_opentelemetry(
5054
exporter_mock.return_value = exp_init_mock
5155
resource_init_mock = Mock()
5256
resource_mock.create.return_value = resource_init_mock
57+
sampler_init_mock = Mock()
58+
sampler_mock.return_value = sampler_init_mock
5359
bsp_init_mock = Mock()
5460
bsp_mock.return_value = bsp_init_mock
55-
configure_opentelemetry(
56-
connection_string="test_cs",
61+
configure_azure_monitor(
5762
disable_tracing=False,
5863
service_name="test_service_name",
5964
service_namespace="test_namespace",
6065
service_instance_id="test_id",
66+
sampling_ratio=0.5,
67+
tracing_export_interval_millis=15000,
6168
)
6269
resource_mock.create.assert_called_once_with(
6370
{
@@ -66,10 +73,17 @@ def test_configure_opentelemetry(
6673
ResourceAttributes.SERVICE_INSTANCE_ID: "test_id",
6774
}
6875
)
69-
tp_mock.assert_called_once_with(resource=resource_init_mock)
76+
tp_mock.assert_called_once_with(
77+
sampler=sampler_init_mock,
78+
resource=resource_init_mock,
79+
)
7080
trace_mock.set_tracer_provider.assert_called_once_with(tp_init_mock)
71-
exporter_mock.assert_called_once_with(connection_string="test_cs")
72-
bsp_mock.assert_called_once_with(exp_init_mock)
81+
exporter_mock.assert_called_once()
82+
sampler_mock.assert_called_once_with(sampling_ratio=0.5)
83+
bsp_mock.assert_called_once_with(
84+
exp_init_mock,
85+
export_timeout_millis=15000,
86+
)
7387

7488
@patch(
7589
"azure.monitor.opentelemetry.distro.BatchSpanProcessor",
@@ -81,26 +95,87 @@ def test_configure_opentelemetry(
8195
"azure.monitor.opentelemetry.distro.TracerProvider",
8296
autospec=True,
8397
)
98+
@patch(
99+
"azure.monitor.opentelemetry.distro.ApplicationInsightsSampler",
100+
)
84101
@patch(
85102
"azure.monitor.opentelemetry.distro.Resource",
86103
)
87104
@patch(
88105
"azure.monitor.opentelemetry.distro.trace",
89106
)
90-
def test_configure_opentelemetry_disable_tracing(
107+
def test_configure_azure_monitor_disable_tracing(
91108
self,
92109
trace_mock,
93110
resource_mock,
111+
sampler_mock,
94112
tp_mock,
95113
exporter_mock,
96114
bsp_mock,
97115
):
98-
configure_opentelemetry(
99-
connection_string="test_cs",
116+
configure_azure_monitor(
100117
disable_tracing=True,
101118
)
102119
resource_mock.assert_not_called()
103120
tp_mock.assert_not_called()
104121
trace_mock.set_tracer_provider.assert_not_called()
122+
sampler_mock.assert_not_called()
105123
exporter_mock.assert_not_called()
106124
bsp_mock.assert_not_called()
125+
126+
@patch(
127+
"azure.monitor.opentelemetry.distro.BatchSpanProcessor",
128+
)
129+
@patch(
130+
"azure.monitor.opentelemetry.distro.AzureMonitorTraceExporter",
131+
)
132+
@patch(
133+
"azure.monitor.opentelemetry.distro.TracerProvider",
134+
autospec=True,
135+
)
136+
@patch(
137+
"azure.monitor.opentelemetry.distro.ApplicationInsightsSampler",
138+
)
139+
@patch(
140+
"azure.monitor.opentelemetry.distro.Resource",
141+
)
142+
@patch(
143+
"azure.monitor.opentelemetry.distro.trace",
144+
)
145+
def test_configure_azure_monitor_exporter(
146+
self,
147+
trace_mock,
148+
resource_mock,
149+
sampler_mock,
150+
tp_mock,
151+
exporter_mock,
152+
bsp_mock,
153+
):
154+
tp_init_mock = Mock()
155+
tp_mock.return_value = tp_init_mock
156+
exp_init_mock = Mock()
157+
exporter_mock.return_value = exp_init_mock
158+
resource_init_mock = Mock()
159+
resource_mock.create.return_value = resource_init_mock
160+
sampler_init_mock = Mock()
161+
sampler_mock.return_value = sampler_init_mock
162+
bsp_init_mock = Mock()
163+
bsp_mock.return_value = bsp_init_mock
164+
kwargs = {
165+
"connection_string": "test_cs",
166+
"api_version": "1.0",
167+
"disable_offline_storage": True,
168+
"storage_maintenance_period": 50,
169+
"storage_max_size": 1024,
170+
"storage_min_retry_interval": 30,
171+
"storage_directory": "/tmp",
172+
"storage_retention_period": 60,
173+
"timeout": 30,
174+
}
175+
configure_azure_monitor(**kwargs)
176+
resource_mock.create.assert_called_once()
177+
tp_mock.assert_called_once()
178+
trace_mock.set_tracer_provider.assert_called_once()
179+
exporter_mock.assert_called_once_with(**kwargs)
180+
sampler_mock.assert_called_once()
181+
bsp_mock.assert_called_once()

azure-monitor-opentelemetry-distro/tests/configuration/test_util.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,16 @@ def test_get_configurations(self):
2525
service_name="test_service_name",
2626
service_namespace="test_namespace",
2727
service_instance_id="test_id",
28+
sampling_ratio="test_sample_ratio",
29+
tracing_export_interval="test_interval",
2830
)
2931

3032
self.assertEqual(configurations["connection_string"], "test_cs")
3133
self.assertEqual(configurations["disable_tracing"], "test_disable")
3234
self.assertEqual(configurations["service_name"], "test_service_name")
3335
self.assertEqual(configurations["service_namespace"], "test_namespace")
3436
self.assertEqual(configurations["service_instance_id"], "test_id")
35-
36-
def test_get_configurations_default(self):
37-
configurations = get_configurations()
38-
self.assertEqual(configurations["connection_string"], None)
39-
self.assertEqual(configurations["disable_tracing"], False)
40-
self.assertEqual(configurations["service_name"], "")
41-
self.assertEqual(configurations["service_namespace"], "")
42-
self.assertEqual(configurations["service_instance_id"], "")
37+
self.assertEqual(configurations["sampling_ratio"], "test_sample_ratio")
38+
self.assertEqual(
39+
configurations["tracing_export_interval"], "test_interval"
40+
)

0 commit comments

Comments
 (0)