Skip to content

Commit d51635b

Browse files
Resource param (#34900)
* Tests pass * Readme * Merging custom resource into default * Clearer Resource Attribute priorities * lint * Fix broken link * reorder params in readme * Add resource to method signature * changelog * Update sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py Co-authored-by: Anna Tisch <[email protected]> * tests * Feedback: Add overwrite behavior to docstring --------- Co-authored-by: Anna Tisch <[email protected]>
1 parent 687fb6f commit d51635b

File tree

6 files changed

+26
-5
lines changed

6 files changed

+26
-5
lines changed

sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
- Adding diagnostic warning when distro detects RP attach
88
([#34971](https://github.com/Azure/azure-sdk-for-python/pull/34971))
9+
- Added `resource` parameter
10+
([#34900](https://github.com/Azure/azure-sdk-for-python/pull/34900))
911

1012
### Breaking Changes
1113

sdk/monitor/azure-monitor-opentelemetry/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ You can use `configure_azure_monitor` to set up instrumentation for your app to
6262
| `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` |
6363
| `logger_name` | The name of the [Python logger][python_logger] under which telemetry is collected. | `N/A` |
6464
| `instrumentation_options` | A nested dictionary that determines which instrumentations to enable or disable. Instrumentations are referred to by their [Library Names](#officially-supported-instrumentations). For example, `{"azure_sdk": {"enabled": False}, "flask": {"enabled": False}, "django": {"enabled": True}}` will disable Azure Core Tracing and the Flask instrumentation but leave Django and the other default instrumentations enabled. The `OTEL_PYTHON_DISABLED_INSTRUMENTATIONS` environment variable explained below can also be used to disable instrumentations. | `N/A` |
65+
| `resource` | Specifies the OpenTelemetry [Resource][ot_spec_resource] associated with your application. Passed in [Resource Attributes][ot_spec_resource_attributes] take priority over default attributes and those from [Resource Detectors][ot_python_resource_detectors]. | [OTEL_SERVICE_NAME][ot_spec_service_name], [OTEL_RESOURCE_ATTRIBUTES][ot_spec_resource_attributes], [OTEL_EXPERIMENTAL_RESOURCE_DETECTORS][ot_python_resource_detectors] |
6566
| `span_processors` | A list of [span processors][ot_span_processor] that will perform processing on each of your spans before they are exported. Useful for filtering/modifying telemetry. | `N/A` |
6667

6768
You can configure further with [OpenTelemetry environment variables][ot_env_vars].

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def configure_azure_monitor(**kwargs) -> None: # pylint: disable=C4758
7474
`{"azure_sdk": {"enabled": False}, "flask": {"enabled": False}, "django": {"enabled": True}}`
7575
will disable Azure Core Tracing and the Flask instrumentation but leave Django and the other default
7676
instrumentations enabled.
77+
:keyword ~opentelemetry.sdk.resources.Resource resource: OpenTelemetry Resource object. Passed in Resource
78+
Attributes take priority over default attributes and those from Resource Detectors.
7779
:keyword list[~opentelemetry.sdk.trace.SpanProcessor] span_processors: List of `SpanProcessor` objects
7880
to process every span prior to exporting. Will be run sequentially.
7981
:keyword str storage_directory: Storage directory in which to store retry files. Defaults to

sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_utils/configurations.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ def _default_resource(configurations):
106106
OTEL_EXPERIMENTAL_RESOURCE_DETECTORS,
107107
",".join(_SUPPORTED_RESOURCE_DETECTORS)
108108
)
109-
configurations[RESOURCE_ARG] = Resource.create()
109+
if RESOURCE_ARG not in configurations:
110+
configurations[RESOURCE_ARG] = Resource.create()
111+
else:
112+
configurations[RESOURCE_ARG] = Resource.create(configurations[RESOURCE_ARG].attributes)
110113

111114

112115
# TODO: remove when sampler uses env var instead

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ def test_configure_azure_monitor_disable_tracing(
103103
"requests": {
104104
"enabled": False
105105
},
106-
}
106+
},
107+
"resource": TEST_RESOURCE,
107108
}
108109
config_mock.return_value = configurations
109110
configure_azure_monitor()
@@ -140,6 +141,7 @@ def test_configure_azure_monitor_disable_logging(
140141
"disable_tracing": False,
141142
"disable_logging": True,
142143
"disable_metrics": False,
144+
"resource": TEST_RESOURCE,
143145
}
144146
config_mock.return_value = configurations
145147
configure_azure_monitor()
@@ -176,6 +178,7 @@ def test_configure_azure_monitor_disable_metrics(
176178
"disable_tracing": False,
177179
"disable_logging": False,
178180
"disable_metrics": True,
181+
"resource": TEST_RESOURCE,
179182
}
180183
config_mock.return_value = configurations
181184
configure_azure_monitor()

sdk/monitor/azure-monitor-opentelemetry/tests/utils/test_configurations.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,35 @@
3636
"test.attributes.1": "test_value_1",
3737
"test.attributes.2": "test_value_2"
3838
})
39+
TEST_CUSTOM_RESOURCE = Resource({
40+
"test.attributes.2": "test_value_4",
41+
"test.attributes.3": "test_value_3"
42+
})
43+
TEST_MERGED_RESOURCE = Resource({
44+
"test.attributes.1": "test_value_1",
45+
"test.attributes.2": "test_value_4",
46+
"test.attributes.3": "test_value_3"
47+
})
3948

4049

4150
class TestConfigurations(TestCase):
4251
@patch.dict("os.environ", {}, clear=True)
4352
@patch("azure.monitor.opentelemetry._utils.configurations._PREVIEW_INSTRUMENTED_LIBRARIES", ("previewlib1", "previewlib2"))
44-
@patch("opentelemetry.sdk.resources.Resource.create", return_value=TEST_DEFAULT_RESOURCE)
53+
@patch("opentelemetry.sdk.resources.Resource.create", return_value=TEST_MERGED_RESOURCE)
4554
def test_get_configurations(self, resource_create_mock):
4655
configurations = _get_configurations(
4756
connection_string="test_cs",
4857
credential="test_credential",
58+
resource=TEST_CUSTOM_RESOURCE
4959
)
5060

5161
self.assertEqual(configurations["connection_string"], "test_cs")
5262
self.assertEqual(configurations["disable_logging"], False)
5363
self.assertEqual(configurations["disable_metrics"], False)
5464
self.assertEqual(configurations["disable_tracing"], False)
55-
self.assertEqual(configurations["resource"].attributes, TEST_DEFAULT_RESOURCE.attributes)
65+
self.assertEqual(configurations["resource"].attributes, TEST_MERGED_RESOURCE.attributes)
5666
self.assertEqual(environ[OTEL_EXPERIMENTAL_RESOURCE_DETECTORS], "azure_app_service,azure_vm")
57-
resource_create_mock.assert_called_once_with()
67+
resource_create_mock.assert_called_once_with(TEST_CUSTOM_RESOURCE.attributes)
5868
self.assertEqual(configurations["sampling_ratio"], 1.0)
5969
self.assertEqual(configurations["credential"], ("test_credential"))
6070
self.assertEqual(configurations["instrumentation_options"], {

0 commit comments

Comments
 (0)