Skip to content

Commit b756968

Browse files
authored
Merge pull request #259 from lzchen/instr
2 parents 9bd01fa + b0ff65d commit b756968

File tree

7 files changed

+87
-52
lines changed

7 files changed

+87
-52
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
([#255](https://github.com/microsoft/ApplicationInsights-Python/pull/255))
1111
- Add support for Urllib3/Urllib instrumentation
1212
([#256](https://github.com/microsoft/ApplicationInsights-Python/pull/256))
13+
- Change instrumentation config to use TypedDict InstrumentationConfig
14+
([#259](https://github.com/microsoft/ApplicationInsights-Python/pull/259))
1315
- Change interval params to use `_ms` as suffix
1416
([#260](https://github.com/microsoft/ApplicationInsights-Python/pull/260))
1517

azure-monitor-opentelemetry/README.md

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ This distro automatically installs the following libraries:
88

99
## Officially supported instrumentations
1010

11-
The following OpenTelemetry instrumentations come bundled in with the Azure monitor distro. If you would like to add support for another OpenTelemetry instrumentation, please submit a feature [request][distro_feature_request]. In the meantime, you can use the OpenTelemetry instrumentation manually via it's own APIs (i.e. `instrument()`) in your code. See [this][samples_manual] for an example.
12-
13-
* [OpenTelemetry Django Instrumentation][opentelemetry_instrumentation_django]
14-
* [OpenTelemetry FastApi Instrumentation][opentelemetry_instrumentation_fastapi]
15-
* [OpenTelemetry Flask Instrumentation][opentelemetry_instrumentation_flask]
16-
* [OpenTelemetry Psycopg2 Instrumentation][opentelemetry_instrumentation_psycopg2]
17-
* [OpenTelemetry Requests Instrumentation][opentelemetry_instrumentation_requests]
18-
* [OpenTelemetry UrlLib Instrumentation][opentelemetry_instrumentation_urllib]
19-
* [OpenTelemetry UrlLib3 Instrumentation][opentelemetry_instrumentation_urllib3]
11+
OpenTelemetry instrumentations allow automatic collection of requests sent from underlying instrumented libraries. The following is a list of OpenTelemetry instrumentations that come bundled in with the Azure monitor distro. If you would like to add support for another OpenTelemetry instrumentation, please submit a feature [request][distro_feature_request]. In the meantime, you can use the OpenTelemetry instrumentation manually via it's own APIs (i.e. `instrument()`) in your code. See [this][samples_manual] for an example.
12+
13+
| Instrumentation | Supported library | Supported versions |
14+
| ------------------------------------- | ----------------- | ------------------ |
15+
| [OpenTelemetry Django Instrumentation][ot_instrumentation_django] | [django][pypi_django] | [link][ot_instrumentation_django_version]
16+
| [OpenTelemetry FastApi Instrumentation][ot_instrumentation_fastapi] | [fastapi][pypi_fastapi] | [link][ot_instrumentation_fastapi_version]
17+
| [OpenTelemetry Flask Instrumentation][ot_instrumentation_flask] | [flask][pypi_flask] | [link][ot_instrumentation_flask_version]
18+
| [OpenTelemetry Psycopg2 Instrumentation][ot_instrumentation_psycopg2] | [psycopg2][pypi_psycopg2] | [link][ot_instrumentation_psycopg2_version]
19+
| [OpenTelemetry Requests Instrumentation][ot_instrumentation_requests] | [requests][pypi_requests] | [link][ot_instrumentation_requests_version]
20+
| [OpenTelemetry UrlLib Instrumentation][ot_instrumentation_urllib] | [urllib][pypi_urllib] | All
21+
| [OpenTelemetry UrlLib3 Instrumentation][ot_instrumentation_urllib3] | [urllib3][pypi_urllib3] | [link][ot_instrumentation_urllib3_version]
2022

2123
## Getting started
2224

@@ -54,6 +56,21 @@ You can use `configure_azure_monitor` to set up instrumentation for your app to
5456
* disable_metrics - If set to `True`, disables collection and export of metric telemetry. Defaults to `False`.
5557
* disable_tracing - If set to `True`, disables collection and export of distributed tracing telemetry. Defaults to `False`.
5658
* exclude_instrumentations - By default, all supported [instrumentations](#officially-supported-instrumentations) are enabled to collect telemetry. Specify instrumentations you do not want to enable to collect telemetry by passing in a comma separated list of instrumented library names. e.g. `["requests", "flask"]`
59+
* instrumentation_config - Specifies a dictionary of kwargs that will be applied to instrumentation configuration. You can specify which instrumentation you want to configure by name in the key field and value as a dictionary representing `kwargs` for the corresponding instrumentation.
60+
Refer to the `Supported Library` section [above](#officially-supported-instrumentations) for the list of suppoprted library names.
61+
62+
```python
63+
...
64+
configure_azure_monitor(
65+
connection_string="<your-connection-string>",
66+
flask_config={"excluded_urls": "http://localhost:8080/ignore"},
67+
requests_config={"excluded_urls": "http://example.com"},
68+
)
69+
...
70+
```
71+
72+
Take a look at the specific [instrumenation][ot_instrumentations] documentation for available configurations.
73+
5774
* resource - Specified the OpenTelemetry [resource][opentelemetry_spec_resource] associated with your application. See [this][ot_sdk_python_resource] for default behavior.
5875
* logging_level - Specifies the [logging level][logging_level] of the logs you would like to collect for your logging pipeline. Defaults to logging.NOTSET.
5976
* logger_name = Specifies the [logger name][logger_name_hierarchy_doc] under which logging will be instrumented. Defaults to "" which corresponds to the root logger.
@@ -63,9 +80,9 @@ You can use `configure_azure_monitor` to set up instrumentation for your app to
6380
* sampling_ratio - Specifies the ratio of distributed tracing telemetry to be [sampled][application_insights_sampling]. Accepted values are in the range [0,1]. Defaults to 1.0, meaning no telemetry is sampled out.
6481
* tracing_export_interval_ms - Specifies the distributed tracing export interval in milliseconds. Defaults to 5000.
6582

66-
#### Exporter configurations
83+
#### Azure monitor OpenTelemetry Exporter configurations
6784

68-
You can pass exporter configuration parameters directly into `configure_azure_monitor`. See additional [configuration related to exporting here][exporter_configuration_docs].
85+
You can pass Azure monitor OpenTelemetry exporter configuration parameters directly into `configure_azure_monitor`. See additional [configuration related to exporting here][exporter_configuration_docs].
6986

7087
```python
7188
...
@@ -76,22 +93,6 @@ configure_azure_monitor(
7693
...
7794
```
7895

79-
#### Instrumentation configurations
80-
81-
You can pass in instrumentation specific configuration into `configure_azure_monitor` with the key `<instrumented-library-name>_config` and value as a dictionary representing `kwargs` for the corresponding instrumentation.
82-
83-
```python
84-
...
85-
configure_azure_monitor(
86-
connection_string="<your-connection-string>",
87-
flask_config={"excluded_urls": "http://localhost:8080/ignore"},
88-
requests_config={"excluded_urls": "http://example.com"},
89-
)
90-
...
91-
```
92-
93-
Take a look at the specific [instrumenation][ot_instrumentations] documentation for available configurations.
94-
9596
### Samples
9697

9798
Samples are available [here][samples] to demonstrate how to utilize the above configuration options.
@@ -119,16 +120,29 @@ Samples are available [here][samples] to demonstrate how to utilize the above co
119120
[ot_sdk_python_metric_reader]: https://opentelemetry-python.readthedocs.io/en/stable/sdk/metrics.export.html#opentelemetry.sdk.metrics.export.MetricReader
120121
[ot_sdk_python_resource]: https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py#L153
121122
[ot_sdk_python_view_examples]: https://github.com/open-telemetry/opentelemetry-python/tree/main/docs/examples/metrics/views
122-
[opentelemetry_instrumentation_django]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-django
123-
[opentelemetry_instrumentation_fastapi]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-fastapi
124-
[opentelemetry_instrumentation_flask]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-flask
125-
[opentelemetry_instrumentation_psycopg2]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-psycopg2
126-
[opentelemetry_instrumentation_requests]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-requests
127-
[opentelemetry_instrumentation_urllib]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-urllib
128-
[opentelemetry_instrumentation_urllib3]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-urllib3
123+
[ot_instrumentation_django]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-django
124+
[ot_instrumentation_django_version]: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/package.py#L16
125+
[ot_instrumentation_fastapi]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-fastapi
126+
[ot_instrumentation_fastapi_version]: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/package.py#L16
127+
[ot_instrumentation_flask]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-flask
128+
[ot_instrumentation_flask_version]: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/package.py#L16
129+
[ot_instrumentation_psycopg2]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-psycopg2
130+
[ot_instrumentation_psycopg2_version]: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py#L16
131+
[ot_instrumentation_requests]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-requests
132+
[ot_instrumentation_requests_version]: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/package.py#L16
133+
[ot_instrumentation_urllib]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-urllib3
134+
[ot_instrumentation_urllib3]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-urllib3
135+
[ot_instrumentation_urllib3_version]: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/package.py#L16
129136
[opentelemetry_spec_resource]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md#resource-sdk
130137
[opentelemetry_spec_view]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view
131-
[python]: https://www.python.org/downloads/
132138
[pip]: https://pypi.org/project/pip/
139+
[pypi_django]: https://pypi.org/project/Django/
140+
[pypi_fastapi]: https://pypi.org/project/fastapi/
141+
[pypi_flask]: https://pypi.org/project/Flask/
142+
[pypi_psycopg2]: https://pypi.org/project/psycopg2/
143+
[pypi_requests]: https://pypi.org/project/requests/
144+
[pypi_urllib]: https://docs.python.org/3/library/urllib.html
145+
[pypi_urllib3]: https://pypi.org/project/urllib3/
146+
[python]: https://www.python.org/downloads/
133147
[samples]: https://github.com/microsoft/ApplicationInsights-Python/tree/main/azure-monitor-opentelemetry/samples
134148
[samples_manual]: https://github.com/microsoft/ApplicationInsights-Python/tree/main/azure-monitor-opentelemetry/samples/tracing/manual.py

azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# license information.
55
# --------------------------------------------------------------------------
66
from logging import NOTSET, getLogger
7-
from typing import Dict
7+
from typing import Any, Dict
88

99
from azure.monitor.opentelemetry._types import ConfigurationValue
1010
from azure.monitor.opentelemetry.exporter import (
@@ -33,7 +33,6 @@
3333
_logger = getLogger(__name__)
3434

3535

36-
_INSTRUMENTATION_CONFIG_SUFFIX = "_config"
3736
_SUPPORTED_INSTRUMENTED_LIBRARIES = (
3837
"django",
3938
"fastapi",
@@ -45,6 +44,9 @@
4544
)
4645

4746

47+
InstrumentationConfig = Dict[str, Dict[str, Any]]
48+
49+
4850
def configure_azure_monitor(**kwargs) -> None:
4951
"""
5052
This function works as a configuration layer that allows the
@@ -63,7 +65,9 @@ def configure_azure_monitor(**kwargs) -> None:
6365
:keyword Sequence[View] views: Specifies the list of views to configure for the metric pipeline.
6466
:keyword float sampling_ratio: Specifies the ratio of distributed tracing telemetry to be sampled. Accepted values are in the range [0,1]. Defaults to 1.0, meaning no telemetry is sampled out.
6567
:keyword int tracing_export_interval_ms: Specifies the distributed tracing export interval in milliseconds. Defaults to 5000.
66-
:keyword Dict[str, Any] <instrumentation>_config: Specifies a dictionary of kwargs that will be applied to configuration for instrumentation <instrumentation>.
68+
:keyword InstrumentationConfig instrumentation_config: Specifies a dictionary of kwargs that will be applied to instrumentation configuration. You can specify which instrumentation you want to
69+
configure by name in the key field and value as a dictionary representing `kwargs` for the corresponding instrumentation.
70+
Refer to the `Supported Library` section of https://github.com/microsoft/ApplicationInsights-Python/tree/main/azure-monitor-opentelemetry#officially-supported-instrumentations for the list of suppoprted library names.
6771
:keyword bool disable_offline_storage: Boolean value to determine whether to disable storing failed telemetry records for retry. Defaults to `False`.
6872
:keyword str storage_directory: Storage directory in which to store retry files. Defaults to `<tempfile.gettempdir()>/Microsoft/AzureMonitor/opentelemetry-python-<your-instrumentation-key>`.
6973
:rtype: None
@@ -162,14 +166,7 @@ def _setup_instrumentations(configurations: Dict[str, ConfigurationValue]):
162166
exclude_instrumentations = configurations.get(
163167
"exclude_instrumentations", []
164168
)
165-
instrumentation_configs = {}
166-
167-
# Instrumentation specific configs
168-
# Format is {"<library_name>": {"<config>":<value>}}
169-
for k, v in configurations.items():
170-
if k.endswith(_INSTRUMENTATION_CONFIG_SUFFIX):
171-
lib_name = k.partition(_INSTRUMENTATION_CONFIG_SUFFIX)[0]
172-
instrumentation_configs[lib_name] = v
169+
instrumentation_configs = configurations.get("instrumentation_config", {})
173170

174171
# use pkg_resources for now until https://github.com/open-telemetry/opentelemetry-python/pull/3168 is merged
175172
for entry_point in iter_entry_points("opentelemetry_instrumentor"):

azure-monitor-opentelemetry/samples/tracing/http_fastapi.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
connection_string="<your-connection-string>",
1212
disable_logging=True,
1313
disable_metrics=True,
14-
fastapi_config={"excluded_urls": "http://127.0.0.1:8000/exclude"},
14+
instrumentation_config={
15+
"fastapi": {
16+
"excluded_urls": "http://127.0.0.1:8000/exclude",
17+
}
18+
},
1519
tracing_export_interval_ms=15000,
1620
)
1721

azure-monitor-opentelemetry/samples/tracing/http_flask.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
connection_string="<your-connection-string>",
1212
disable_logging=True,
1313
disable_metrics=True,
14-
flask_config={"excluded_urls": "http://localhost:8080/ignore"},
14+
instrumentation_config={
15+
"flask": {
16+
"excluded_urls": "http://localhost:8080/ignore",
17+
}
18+
},
1519
tracing_export_interval_ms=15000,
1620
)
1721

azure-monitor-opentelemetry/samples/tracing/http_requests.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
connection_string="<your-connection-string>",
1717
disable_logging=True,
1818
disable_metrics=True,
19-
requests_config={"excluded_urls": "http://example.com"},
19+
instrumentation_config={
20+
"requests": {
21+
"excluded_urls": "http://example.com",
22+
}
23+
},
2024
tracing_export_interval_ms=15000,
2125
)
2226

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,11 @@ def test_setup_instrumentations_custom_configuration(
544544
):
545545
libr_name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0]
546546
configurations = {
547-
libr_name + "_config": {"test_key": "test_value"},
547+
"instrumentation_config": {
548+
libr_name: {
549+
"test_key": "test_value",
550+
}
551+
},
548552
}
549553
ep_mock = Mock()
550554
iter_mock.return_value = [ep_mock]
@@ -574,8 +578,14 @@ def test_setup_instrumentations_custom_configuration_excluded(
574578
libr_name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0]
575579
libr_name2 = _SUPPORTED_INSTRUMENTED_LIBRARIES[1]
576580
configurations = {
577-
libr_name + "_config": {"test_key": "test_value"},
578-
libr_name2 + "_config": {"test_key2": "test_value2"},
581+
"instrumentation_config": {
582+
libr_name: {
583+
"test_key": "test_value",
584+
},
585+
libr_name2: {
586+
"test_key2": "test_value2",
587+
},
588+
},
579589
"exclude_instrumentations": libr_name,
580590
}
581591
ep_mock = Mock()

0 commit comments

Comments
 (0)