Skip to content

Commit

Permalink
Resource param (#34900)
Browse files Browse the repository at this point in the history
* 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 <antisch@microsoft.com>

* tests

* Feedback: Add overwrite behavior to docstring

---------

Co-authored-by: Anna Tisch <antisch@microsoft.com>
  • Loading branch information
jeremydvoss and annatisch authored Apr 5, 2024
1 parent 687fb6f commit d51635b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 5 deletions.
2 changes: 2 additions & 0 deletions sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

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

### Breaking Changes

Expand Down
1 change: 1 addition & 0 deletions sdk/monitor/azure-monitor-opentelemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ You can use `configure_azure_monitor` to set up instrumentation for your app to
| `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` |
| `logger_name` | The name of the [Python logger][python_logger] under which telemetry is collected. | `N/A` |
| `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` |
| `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] |
| `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` |

You can configure further with [OpenTelemetry environment variables][ot_env_vars].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def configure_azure_monitor(**kwargs) -> None: # pylint: disable=C4758
`{"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.
:keyword ~opentelemetry.sdk.resources.Resource resource: OpenTelemetry Resource object. Passed in Resource
Attributes take priority over default attributes and those from Resource Detectors.
:keyword list[~opentelemetry.sdk.trace.SpanProcessor] span_processors: List of `SpanProcessor` objects
to process every span prior to exporting. Will be run sequentially.
:keyword str storage_directory: Storage directory in which to store retry files. Defaults to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ def _default_resource(configurations):
OTEL_EXPERIMENTAL_RESOURCE_DETECTORS,
",".join(_SUPPORTED_RESOURCE_DETECTORS)
)
configurations[RESOURCE_ARG] = Resource.create()
if RESOURCE_ARG not in configurations:
configurations[RESOURCE_ARG] = Resource.create()
else:
configurations[RESOURCE_ARG] = Resource.create(configurations[RESOURCE_ARG].attributes)


# TODO: remove when sampler uses env var instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def test_configure_azure_monitor_disable_tracing(
"requests": {
"enabled": False
},
}
},
"resource": TEST_RESOURCE,
}
config_mock.return_value = configurations
configure_azure_monitor()
Expand Down Expand Up @@ -140,6 +141,7 @@ def test_configure_azure_monitor_disable_logging(
"disable_tracing": False,
"disable_logging": True,
"disable_metrics": False,
"resource": TEST_RESOURCE,
}
config_mock.return_value = configurations
configure_azure_monitor()
Expand Down Expand Up @@ -176,6 +178,7 @@ def test_configure_azure_monitor_disable_metrics(
"disable_tracing": False,
"disable_logging": False,
"disable_metrics": True,
"resource": TEST_RESOURCE,
}
config_mock.return_value = configurations
configure_azure_monitor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,35 @@
"test.attributes.1": "test_value_1",
"test.attributes.2": "test_value_2"
})
TEST_CUSTOM_RESOURCE = Resource({
"test.attributes.2": "test_value_4",
"test.attributes.3": "test_value_3"
})
TEST_MERGED_RESOURCE = Resource({
"test.attributes.1": "test_value_1",
"test.attributes.2": "test_value_4",
"test.attributes.3": "test_value_3"
})


class TestConfigurations(TestCase):
@patch.dict("os.environ", {}, clear=True)
@patch("azure.monitor.opentelemetry._utils.configurations._PREVIEW_INSTRUMENTED_LIBRARIES", ("previewlib1", "previewlib2"))
@patch("opentelemetry.sdk.resources.Resource.create", return_value=TEST_DEFAULT_RESOURCE)
@patch("opentelemetry.sdk.resources.Resource.create", return_value=TEST_MERGED_RESOURCE)
def test_get_configurations(self, resource_create_mock):
configurations = _get_configurations(
connection_string="test_cs",
credential="test_credential",
resource=TEST_CUSTOM_RESOURCE
)

self.assertEqual(configurations["connection_string"], "test_cs")
self.assertEqual(configurations["disable_logging"], False)
self.assertEqual(configurations["disable_metrics"], False)
self.assertEqual(configurations["disable_tracing"], False)
self.assertEqual(configurations["resource"].attributes, TEST_DEFAULT_RESOURCE.attributes)
self.assertEqual(configurations["resource"].attributes, TEST_MERGED_RESOURCE.attributes)
self.assertEqual(environ[OTEL_EXPERIMENTAL_RESOURCE_DETECTORS], "azure_app_service,azure_vm")
resource_create_mock.assert_called_once_with()
resource_create_mock.assert_called_once_with(TEST_CUSTOM_RESOURCE.attributes)
self.assertEqual(configurations["sampling_ratio"], 1.0)
self.assertEqual(configurations["credential"], ("test_credential"))
self.assertEqual(configurations["instrumentation_options"], {
Expand Down

0 comments on commit d51635b

Please sign in to comment.