Skip to content

Commit 4cc659a

Browse files
authored
Merge pull request #277 from jeremydvoss/remove-configuration
Remove most code configuration
2 parents c1ba59c + 97fb414 commit 4cc659a

25 files changed

+138
-674
lines changed

CHANGELOG.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
## Unreleased
44

5-
## [1.0.0b11](https://github.com/microsoft/ApplicationInsights-Python/releases/tag/v1.0.0b11) - 2023-04-12
6-
5+
- Remove most configuration for Public Preview
6+
([#277](https://github.com/microsoft/ApplicationInsights-Python/pull/277))
77
- Infer telemetry category disablement from exporter environment variables
88
([#278](https://github.com/microsoft/ApplicationInsights-Python/pull/278))
9+
10+
## [1.0.0b11](https://github.com/microsoft/ApplicationInsights-Python/releases/tag/v1.0.0b11) - 2023-04-12
11+
912
- Reverse default behavior of instrumentations and implement configuration for exclusion
1013
([#253](https://github.com/microsoft/ApplicationInsights-Python/pull/253))
1114
- Use entrypoints instead of importlib to load instrumentations

azure-monitor-opentelemetry/README.md

+14-32
Original file line numberDiff line numberDiff line change
@@ -55,38 +55,20 @@ You can use `configure_azure_monitor` to set up instrumentation for your app to
5555
| Parameter | Description | Environment Variable |
5656
|-------------------|----------------------------------------------------|----------------------|
5757
| `connection_string` | The [connection string][connection_string_doc] for your Application Insights resource. The connection string will be automatically populated from the `APPLICATIONINSIGHTS_CONNECTION_STRING` environment variable if not explicitly passed in. | `APPLICATIONINSIGHTS_CONNECTION_STRING` |
58-
| `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-
| `resource` | Specifies the OpenTelemetry [resource][opentelemetry_spec_resource] associated with your application. See [this][ot_sdk_python_resource] for default behavior. | [OTEL_SERVICE_NAME][opentelemetry_spec_service_name], [OTEL_RESOURCE_ATTRIBUTES][opentelemetry_spec_resource_attributes] |
60-
| `disable_logging` | If set to `True`, disables collection and export of logging telemetry. Defaults to `False`. | |
61-
| `disable_metrics` | If set to `True`, disables collection and export of metric telemetry. Defaults to `False`. | |
62-
| `disable_tracing` | If set to `True`, disables collection and export of distributed tracing telemetry. Defaults to `False`. | |
63-
| `logging_level` | Specifies the [logging level][logging_level] of the logs you would like to collect for your logging pipeline. Defaults to 0 which is `logging.NOTSET`. | |
64-
| `logger_name` | Specifies the [logger name][logger_name_hierarchy_doc] under which logging will be instrumented. Defaults to "" which corresponds to the root logger. | |
65-
| `logging_export_interval_ms`| Specifies the logging export interval in milliseconds. Defaults to 5000. | `OTEL_BLRP_SCHEDULE_DELAY` |
66-
| `metric_readers` | Specifies the [metric readers][ot_metric_reader] that you would like to use for your metric pipeline. Accepts a list of [metric readers][ot_sdk_python_metric_reader]. | |
67-
| `views` | Specifies the list of [views][opentelemetry_spec_view] to configure for the metric pipeline. See [here][ot_sdk_python_view_examples] for example usage. | |
68-
| `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. | `OTEL_TRACES_SAMPLER_ARG` |
69-
| `tracing_export_interval_ms`| Specifies the distributed tracing export interval in milliseconds. Defaults to 5000. | `OTEL_BSP_SCHEDULE_DELAY` |
70-
| `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. Refer to the `Supported Library` section [above](#officially-supported-instrumentations) for the list of supported library names. | |
71-
72-
Example for use of `instrumentation_config`:
73-
```python
74-
...
75-
configure_azure_monitor(
76-
connection_string="<your-connection-string>",
77-
instrumentation_config={
78-
"flask": {
79-
"excluded_urls": "http://localhost:8080/ignore",
80-
},
81-
"requests": {
82-
"excluded_urls": "http://example.com"
83-
}
84-
}
85-
)
86-
...
87-
```
8858

89-
Take a look at the specific [instrumenation][ot_instrumentations] documentation for available configurations.
59+
60+
You can configure further with [OpenTelemetry environment variables][ot_env_vars] such as:
61+
| Environment Variable | Description |
62+
|-------------|----------------------|
63+
| [OTEL_SERVICE_NAME][opentelemetry_spec_service_name], [OTEL_RESOURCE_ATTRIBUTES][opentelemetry_spec_resource_attributes] | Specifies the OpenTelemetry [resource][opentelemetry_spec_resource] associated with your application. |
64+
| `OTEL_LOGS_EXPORTER` | If set to `None`, disables collection and export of logging telemetry. |
65+
| `OTEL_METRICS_EXPORTER` | If set to `None`, disables collection and export of metric telemetry. |
66+
| `OTEL_TRACES_EXPORTER` | If set to `None`, disables collection and export of distributed tracing telemetry. |
67+
| `OTEL_BLRP_SCHEDULE_DELAY` | Specifies the logging export interval in milliseconds. Defaults to 5000. |
68+
| `OTEL_BSP_SCHEDULE_DELAY` | Specifies the distributed tracing export interval in milliseconds. Defaults to 5000. |
69+
| `OTEL_TRACES_SAMPLER_ARG` | 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. |
70+
<!-- TODO: add once OTEL_LOG_LEVEL is supported in sdk -->
71+
<!-- Specifies the [logging level][logging_level] of the logs you would like to collect for your logging pipeline. Defaults to 0 which is `logging.NOTSET`. | OTEL_LOG_LEVEL | -->
9072

9173
#### Azure monitor OpenTelemetry Exporter configurations
9274

@@ -123,12 +105,12 @@ Samples are available [here][samples] to demonstrate how to utilize the above co
123105
[exporter_configuration_docs]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-opentelemetry-exporter#configuration
124106
[logging_level]: https://docs.python.org/3/library/logging.html#levels
125107
[logger_name_hierarchy_doc]: https://docs.python.org/3/library/logging.html#logger-objects
108+
[ot_env_vars]: https://opentelemetry.io/docs/reference/specification/sdk-environment-variables/
126109
[ot_instrumentations]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation
127110
[ot_metric_reader]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#metricreader
128111
[ot_python_docs]: https://opentelemetry.io/docs/instrumentation/python/
129112
[ot_sdk_python]: https://github.com/open-telemetry/opentelemetry-python
130113
[ot_sdk_python_metric_reader]: https://opentelemetry-python.readthedocs.io/en/stable/sdk/metrics.export.html#opentelemetry.sdk.metrics.export.MetricReader
131-
[ot_sdk_python_resource]: https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py#L153
132114
[ot_sdk_python_view_examples]: https://github.com/open-telemetry/opentelemetry-python/tree/main/docs/examples/metrics/views
133115
[ot_instrumentation_django]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-django
134116
[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

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

+16-75
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,14 @@
44
# license information.
55
# --------------------------------------------------------------------------
66
from logging import getLogger
7-
from typing import Any, Dict
7+
from typing import Dict
88

99
from azure.monitor.opentelemetry._constants import (
1010
DISABLE_LOGGING_ARG,
1111
DISABLE_METRICS_ARG,
1212
DISABLE_TRACING_ARG,
13-
EXCLUDE_INSTRUMENTATIONS_ARG,
14-
INSTRUMENTATION_CONFIG_ARG,
15-
LOGGER_NAME_ARG,
1613
LOGGING_EXPORT_INTERVAL_MS_ARG,
17-
LOGGING_LEVEL_ARG,
18-
METRIC_READERS_ARG,
19-
RESOURCE_ARG,
2014
SAMPLING_RATIO_ARG,
21-
TRACING_EXPORT_INTERVAL_MS_ARG,
22-
VIEWS_ARG,
2315
)
2416
from azure.monitor.opentelemetry._types import ConfigurationValue
2517
from azure.monitor.opentelemetry.exporter import (
@@ -39,7 +31,6 @@
3931
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
4032
from opentelemetry.sdk.metrics import MeterProvider
4133
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
42-
from opentelemetry.sdk.resources import Resource
4334
from opentelemetry.sdk.trace import TracerProvider
4435
from opentelemetry.sdk.trace.export import BatchSpanProcessor
4536
from opentelemetry.trace import get_tracer_provider, set_tracer_provider
@@ -59,30 +50,13 @@
5950
)
6051

6152

62-
InstrumentationConfig = Dict[str, Dict[str, Any]]
63-
64-
6553
def configure_azure_monitor(**kwargs) -> None:
6654
"""
6755
This function works as a configuration layer that allows the
6856
end user to configure OpenTelemetry and Azure monitor components. The
6957
configuration can be done via arguments passed to this function.
7058
:keyword str connection_string: Connection string for your Application Insights resource.
71-
:keyword Sequence[str] exclude_instrumentations: Specifies instrumentations you want to disable.
72-
:keyword Resource resource: Specified the OpenTelemetry [resource][opentelemetry_spec_resource] associated with your application.
73-
:keyword bool disable_logging: If set to `True`, disables collection and export of logging telemetry. Defaults to `False`.
74-
:keyword bool disable_metrics: If set to `True`, disables collection and export of metric telemetry. Defaults to `False`.
75-
:keyword bool disable_tracing: If set to `True`, disables collection and export of distributed tracing telemetry. Defaults to `False`.
76-
:keyword int logging_level: Specifies the logging of the logs you would like to collect for your logging pipeline.
77-
:keyword str logger_name: Specifies the logger name under which logging will be instrumented. Defaults to "" which corresponds to the root logger.
78-
:keyword int logging_export_interval_ms: Specifies the logging export interval in milliseconds. Defaults to 5000.
79-
:keyword Sequence[MetricReader] metric_readers: Specifies the metric readers that you would like to use for your metric pipeline.
80-
:keyword Sequence[View] views: Specifies the list of views to configure for the metric pipeline.
81-
: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.
82-
:keyword int tracing_export_interval_ms: Specifies the distributed tracing export interval in milliseconds. Defaults to 5000.
83-
:keyword InstrumentationConfig instrumentation_config: Specifies a dictionary of kwargs that will be applied to instrumentation configuration. You can specify which instrumentation you want to
84-
configure by name in the key field and value as a dictionary representing `kwargs` for the corresponding instrumentation.
85-
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.
59+
:keyword ManagedIdentityCredential/ClientSecretCredential credential: Token credential, such as ManagedIdentityCredential or ClientSecretCredential, used for Azure Active Directory (AAD) authentication. Defaults to None.
8660
:keyword bool disable_offline_storage: Boolean value to determine whether to disable storing failed telemetry records for retry. Defaults to `False`.
8761
:keyword str storage_directory: Storage directory in which to store retry files. Defaults to `<tempfile.gettempdir()>/Microsoft/AzureMonitor/opentelemetry-python-<your-instrumentation-key>`.
8862
:rtype: None
@@ -94,97 +68,67 @@ def configure_azure_monitor(**kwargs) -> None:
9468
disable_logging = configurations[DISABLE_LOGGING_ARG]
9569
disable_metrics = configurations[DISABLE_METRICS_ARG]
9670

97-
resource = None
98-
if not disable_logging or not disable_tracing or not disable_metrics:
99-
resource = _get_resource(configurations)
100-
10171
# Setup tracing pipeline
10272
if not disable_tracing:
103-
_setup_tracing(resource, configurations)
73+
_setup_tracing(configurations)
10474

10575
# Setup logging pipeline
10676
if not disable_logging:
107-
_setup_logging(resource, configurations)
77+
_setup_logging(configurations)
10878

10979
# Setup metrics pipeline
11080
if not disable_metrics:
111-
_setup_metrics(resource, configurations)
81+
_setup_metrics(configurations)
11282

11383
# Setup instrumentations
11484
# Instrumentations need to be setup last so to use the global providers
11585
# instanstiated in the other setup steps
116-
_setup_instrumentations(configurations)
117-
86+
_setup_instrumentations()
11887

119-
def _get_resource(configurations: Dict[str, ConfigurationValue]) -> Resource:
120-
return configurations.get(RESOURCE_ARG, Resource.create())
12188

122-
123-
def _setup_tracing(
124-
resource: Resource, configurations: Dict[str, ConfigurationValue]
125-
):
89+
def _setup_tracing(configurations: Dict[str, ConfigurationValue]):
12690
sampling_ratio = configurations[SAMPLING_RATIO_ARG]
127-
tracing_export_interval_ms = configurations[TRACING_EXPORT_INTERVAL_MS_ARG]
12891
tracer_provider = TracerProvider(
12992
sampler=ApplicationInsightsSampler(sampling_ratio=sampling_ratio),
130-
resource=resource,
13193
)
13294
set_tracer_provider(tracer_provider)
13395
trace_exporter = AzureMonitorTraceExporter(**configurations)
13496
span_processor = BatchSpanProcessor(
13597
trace_exporter,
136-
schedule_delay_millis=tracing_export_interval_ms,
13798
)
13899
get_tracer_provider().add_span_processor(span_processor)
139100

140101

141-
def _setup_logging(
142-
resource: Resource, configurations: Dict[str, ConfigurationValue]
143-
):
144-
logger_name = configurations[LOGGER_NAME_ARG]
145-
logging_level = configurations[LOGGING_LEVEL_ARG]
102+
def _setup_logging(configurations: Dict[str, ConfigurationValue]):
103+
# TODO: Remove after upgrading to OTel SDK 1.18
146104
logging_export_interval_ms = configurations[LOGGING_EXPORT_INTERVAL_MS_ARG]
147-
logger_provider = LoggerProvider(resource=resource)
105+
logger_provider = LoggerProvider()
148106
set_logger_provider(logger_provider)
149107
log_exporter = AzureMonitorLogExporter(**configurations)
150108
log_record_processor = BatchLogRecordProcessor(
151109
log_exporter,
152110
schedule_delay_millis=logging_export_interval_ms,
153111
)
154112
get_logger_provider().add_log_record_processor(log_record_processor)
155-
handler = LoggingHandler(
156-
level=logging_level, logger_provider=get_logger_provider()
157-
)
158-
getLogger(logger_name).addHandler(handler)
113+
handler = LoggingHandler(logger_provider=get_logger_provider())
114+
getLogger().addHandler(handler)
159115

160116

161-
def _setup_metrics(
162-
resource: Resource, configurations: Dict[str, ConfigurationValue]
163-
):
164-
views = configurations[VIEWS_ARG]
165-
metric_readers = configurations[METRIC_READERS_ARG]
117+
def _setup_metrics(configurations: Dict[str, ConfigurationValue]):
166118
metric_exporter = AzureMonitorMetricExporter(**configurations)
167119
reader = PeriodicExportingMetricReader(metric_exporter)
168120
meter_provider = MeterProvider(
169-
metric_readers=[reader] + metric_readers,
170-
resource=resource,
171-
views=views,
121+
metric_readers=[reader],
172122
)
173123
set_meter_provider(meter_provider)
174124

175125

176-
def _setup_instrumentations(configurations: Dict[str, ConfigurationValue]):
177-
exclude_instrumentations = configurations[EXCLUDE_INSTRUMENTATIONS_ARG]
178-
instrumentation_configs = configurations[INSTRUMENTATION_CONFIG_ARG]
179-
126+
def _setup_instrumentations():
180127
# use pkg_resources for now until https://github.com/open-telemetry/opentelemetry-python/pull/3168 is merged
181128
for entry_point in iter_entry_points("opentelemetry_instrumentor"):
182129
lib_name = entry_point.name
183130
if lib_name not in _SUPPORTED_INSTRUMENTED_LIBRARIES:
184131
continue
185-
if lib_name in exclude_instrumentations:
186-
_logger.debug("Instrumentation excluded for library %s", lib_name)
187-
continue
188132
try:
189133
# Check if dependent libraries/version are installed
190134
conflict = get_dist_dependency_conflicts(entry_point.dist)
@@ -197,11 +141,8 @@ def _setup_instrumentations(configurations: Dict[str, ConfigurationValue]):
197141
continue
198142
# Load the instrumentor via entrypoint
199143
instrumentor: BaseInstrumentor = entry_point.load()
200-
# Call instrument() with configuration
201-
config = instrumentation_configs.get(lib_name, {})
202144
# tell instrumentation to not run dep checks again as we already did it above
203-
config["skip_dep_check"] = True
204-
instrumentor().instrument(**config)
145+
instrumentor().instrument(skip_dep_check=True)
205146
except Exception as ex:
206147
_logger.warning(
207148
"Exception occured when instrumenting: %s.",

azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py

-9
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,11 @@
1616
# --------------------Configuration------------------------------------------
1717

1818
CONNECTION_STRING_ARG = "connection_string"
19-
EXCLUDE_INSTRUMENTATIONS_ARG = "exclude_instrumentations"
20-
RESOURCE_ARG = "resource"
2119
DISABLE_LOGGING_ARG = "disable_logging"
2220
DISABLE_METRICS_ARG = "disable_metrics"
2321
DISABLE_TRACING_ARG = "disable_tracing"
24-
# TODO: Consider Log Level to match env var
25-
LOGGING_LEVEL_ARG = "logging_level"
26-
LOGGER_NAME_ARG = "logger_name"
2722
LOGGING_EXPORT_INTERVAL_MS_ARG = "logging_export_interval_ms"
28-
METRIC_READERS_ARG = "metric_readers"
29-
VIEWS_ARG = "views"
3023
SAMPLING_RATIO_ARG = "sampling_ratio"
31-
TRACING_EXPORT_INTERVAL_MS_ARG = "tracing_export_interval_ms"
32-
INSTRUMENTATION_CONFIG_ARG = "instrumentation_config"
3324

3425

3526
# --------------------Diagnostic/status logging------------------------------

0 commit comments

Comments
 (0)