diff --git a/CHANGELOG.md b/CHANGELOG.md index e279565f..cb7ed8ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,8 @@ ([#243](https://github.com/microsoft/ApplicationInsights-Python/pull/243)) - Move symbols to protected, add docstring for api, pin opentelemtry-api/sdk versions ([#244](https://github.com/microsoft/ApplicationInsights-Python/pull/244)) +- Replace service.X configurations with Resource + ([#246](https://github.com/microsoft/ApplicationInsights-Python/pull/246)) ## [1.0.0b8](https://github.com/microsoft/ApplicationInsights-Python/releases/tag/v1.0.0b8) - 2022-09-26 diff --git a/azure-monitor-opentelemetry-distro/README.md b/azure-monitor-opentelemetry-distro/README.md index 58441ee5..2d3f05c1 100644 --- a/azure-monitor-opentelemetry-distro/README.md +++ b/azure-monitor-opentelemetry-distro/README.md @@ -43,17 +43,15 @@ 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. * instrumentations - Specifies the libraries with [instrumentations][ot_instrumentations] that you would like to use. Accepts a comma separated list. e.g. `["requests", "flask"]` -* service_name - Specifies the [service][service_semantic_convention_doc] name. -* service_namespace - Specifies the [service][service_semantic_convention_doc] namespace. -* service_instance_id - Specifies the [service][service_semantic_convention_doc] instance id. * disable_logging - If set to `True`, disables collection and export of logging telemetry. Defaults to `False`. * disable_metrics - If set to `True`, disables collection and export of metric telemetry. Defaults to `False`. * disable_tracing - If set to `True`, disables collection and export of distributed tracing telemetry. Defaults to `False`. +* resource - Specified the OpenTelemetry [resource][opentelemetry_spec_resource] associated with your application. See [this][ot_sdk_python_resource] for default behavior. * logging_level - Specifies the [logging level][logging_level] of the logs you would like to collect for your logging pipeline. Defaults to logging.NOTSET. * logger_name = Specifies the [logger name][logger_name_hierarchy_doc] under which logging will be instrumented. Defaults to "" which corresponds to the root logger. * logging_export_interval_millis - Specifies the logging export interval in milliseconds. Defaults to 5000. * 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]. -* views - Specifies the list of [views][opentelemetry_specification_view] to configure for the metric pipeline. See [here][ot_sdk_python_view_examples] for example usage. +* 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. * 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. * tracing_export_interval_millis - Specifies the distributed tracing export interval in milliseconds. Defaults to 5000. @@ -111,13 +109,14 @@ Samples are available [here][samples] to demonstrate how to utilize the above co [ot_python_docs]: https://opentelemetry.io/docs/instrumentation/python/ [ot_sdk_python]: https://github.com/open-telemetry/opentelemetry-python [ot_sdk_python_metric_reader]: https://opentelemetry-python.readthedocs.io/en/stable/sdk/metrics.export.html#opentelemetry.sdk.metrics.export.MetricReader +[ot_sdk_python_resource]: https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py#L153 [ot_sdk_python_view_examples]: https://github.com/open-telemetry/opentelemetry-python/tree/main/docs/examples/metrics/views [opentelemetry_instrumentation_requests]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-requests [opentelemetry_instrumentation_django]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-django [opentelemetry_instrumentation_flask]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-flask [opentelemetry_instrumentation_psycopg2]: https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-psycopg2 -[opentelemetry_specification_view]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view +[opentelemetry_spec_resource]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md#resource-sdk +[opentelemetry_spec_view]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#view [python]: https://www.python.org/downloads/ [pip]: https://pypi.org/project/pip/ [samples]: https://github.com/microsoft/ApplicationInsights-Python/tree/main/azure-monitor-opentelemetry-distro/samples -[service_semantic_convention_doc]: https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/resource/semantic_conventions#service \ No newline at end of file diff --git a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/__init__.py b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/__init__.py index cacfad3f..32a8c665 100644 --- a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/__init__.py +++ b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/__init__.py @@ -24,7 +24,6 @@ from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor -from opentelemetry.semconv.resource import ResourceAttributes from opentelemetry.trace import get_tracer_provider, set_tracer_provider _logger = getLogger(__name__) @@ -46,9 +45,7 @@ def configure_azure_monitor(**kwargs) -> None: configuration can be done via arguments passed to this function. :keyword str connection_string: Connection string for your Application Insights resource. :keyword Sequence[str] connection_string: Specifies the libraries with instrumentations to be enabled. - :keyword str service_name: Specifies the service name. - :keyword str service_namespace: Specifies the service namespace. - :keyword str service_instance_id: Specifies the service instance id. + :keyword Resource resource: Specified the OpenTelemetry [resource][opentelemetry_spec_resource] associated with your application. :keyword bool disable_logging: If set to `True`, disables collection and export of logging telemetry. Defaults to `False`. :keyword bool disable_metrics: If set to `True`, disables collection and export of metric telemetry. Defaults to `False`. :keyword bool disable_tracing: If set to `True`, disables collection and export of distributed tracing telemetry. Defaults to `False`. @@ -94,16 +91,7 @@ def configure_azure_monitor(**kwargs) -> None: def _get_resource(configurations: Dict[str, ConfigurationValue]) -> Resource: - service_name = configurations.get("service_name", "") - service_namespace = configurations.get("service_namespace", "") - service_instance_id = configurations.get("service_instance_id", "") - return Resource.create( - { - ResourceAttributes.SERVICE_NAME: service_name, - ResourceAttributes.SERVICE_NAMESPACE: service_namespace, - ResourceAttributes.SERVICE_INSTANCE_ID: service_instance_id, - } - ) + return configurations.get("resource", Resource.create()) def _setup_tracing( diff --git a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/_types.py b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/_types.py index 874656f3..62f01fc9 100644 --- a/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/_types.py +++ b/azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/_types.py @@ -8,12 +8,14 @@ from opentelemetry.sdk.metrics.export import MetricReader from opentelemetry.sdk.metrics.view import View +from opentelemetry.sdk.resources import Resource ConfigurationValue = Union[ str, bool, int, float, + Resource, Sequence[str], Sequence[bool], Sequence[int], diff --git a/azure-monitor-opentelemetry-distro/samples/logging/correlated_logs.py b/azure-monitor-opentelemetry-distro/samples/logging/correlated_logs.py index acd943bc..9a77eb73 100644 --- a/azure-monitor-opentelemetry-distro/samples/logging/correlated_logs.py +++ b/azure-monitor-opentelemetry-distro/samples/logging/correlated_logs.py @@ -11,7 +11,7 @@ configure_azure_monitor( connection_string="", - service_name="foo_service", + logger_name=__name__, logging_level=WARNING, disable_metrics=True, disable_tracing=True, diff --git a/azure-monitor-opentelemetry-distro/samples/logging/custom_properties.py b/azure-monitor-opentelemetry-distro/samples/logging/custom_properties.py index 4e91b212..fbeab14e 100644 --- a/azure-monitor-opentelemetry-distro/samples/logging/custom_properties.py +++ b/azure-monitor-opentelemetry-distro/samples/logging/custom_properties.py @@ -10,7 +10,6 @@ configure_azure_monitor( connection_string="", - service_name="foo_service", logger_name=__name__, logging_level=DEBUG, disable_metrics=True, diff --git a/azure-monitor-opentelemetry-distro/samples/logging/exception_logs.py b/azure-monitor-opentelemetry-distro/samples/logging/exception_logs.py index d562fdf1..a46ea979 100644 --- a/azure-monitor-opentelemetry-distro/samples/logging/exception_logs.py +++ b/azure-monitor-opentelemetry-distro/samples/logging/exception_logs.py @@ -10,7 +10,7 @@ configure_azure_monitor( connection_string="", - service_name="foo_service", + logger_name=__name__, logging_level=WARNING, disable_metrics=True, disable_tracing=True, diff --git a/azure-monitor-opentelemetry-distro/samples/logging/logs_with_traces.py b/azure-monitor-opentelemetry-distro/samples/logging/logs_with_traces.py index d5a99dd2..f332a5a9 100644 --- a/azure-monitor-opentelemetry-distro/samples/logging/logs_with_traces.py +++ b/azure-monitor-opentelemetry-distro/samples/logging/logs_with_traces.py @@ -11,7 +11,7 @@ configure_azure_monitor( connection_string="", - service_name="flask_service_name", + logger_name=__name__, logging_level=WARNING, disable_metrics=True, instrumentations=["flask"], diff --git a/azure-monitor-opentelemetry-distro/samples/logging/simple.py b/azure-monitor-opentelemetry-distro/samples/logging/simple.py index d3d92bc5..1c41b1d8 100644 --- a/azure-monitor-opentelemetry-distro/samples/logging/simple.py +++ b/azure-monitor-opentelemetry-distro/samples/logging/simple.py @@ -7,11 +7,18 @@ from logging import WARNING, getLogger from azure.monitor.opentelemetry.distro import configure_azure_monitor +from opentelemetry.sdk.resources import Resource, ResourceAttributes configure_azure_monitor( connection_string="", - service_name="foo_service", + logger_name=__name__, logging_level=WARNING, + resource=Resource.create( + { + ResourceAttributes.SERVICE_NAME: "simple_service", + ResourceAttributes.SERVICE_INSTANCE_ID: "simple_logging_instance", + } + ), disable_metrics=True, disable_tracing=True, ) diff --git a/azure-monitor-opentelemetry-distro/samples/metrics/attributes.py b/azure-monitor-opentelemetry-distro/samples/metrics/attributes.py index 729d849d..2e566be1 100644 --- a/azure-monitor-opentelemetry-distro/samples/metrics/attributes.py +++ b/azure-monitor-opentelemetry-distro/samples/metrics/attributes.py @@ -6,7 +6,6 @@ # Configure Azure monitor collection telemetry pipeline configure_azure_monitor( connection_string="", - service_name="metric_attribute_service", disable_logging=True, disable_tracing=True, ) diff --git a/azure-monitor-opentelemetry-distro/samples/metrics/instruments.py b/azure-monitor-opentelemetry-distro/samples/metrics/instruments.py index 33caa6b4..461916d6 100644 --- a/azure-monitor-opentelemetry-distro/samples/metrics/instruments.py +++ b/azure-monitor-opentelemetry-distro/samples/metrics/instruments.py @@ -5,13 +5,19 @@ from azure.monitor.opentelemetry.distro import configure_azure_monitor from opentelemetry import metrics from opentelemetry.metrics import CallbackOptions, Observation +from opentelemetry.sdk.resources import Resource, ResourceAttributes # Configure Azure monitor collection telemetry pipeline configure_azure_monitor( connection_string="", - service_name="metric_instrument_service", disable_logging=True, disable_tracing=True, + resource=Resource.create( + { + ResourceAttributes.SERVICE_NAME: "metric_service", + ResourceAttributes.SERVICE_INSTANCE_ID: "instrument_instance", + } + ), ) diff --git a/azure-monitor-opentelemetry-distro/samples/metrics/views.py b/azure-monitor-opentelemetry-distro/samples/metrics/views.py index 10158f2f..e9f38910 100644 --- a/azure-monitor-opentelemetry-distro/samples/metrics/views.py +++ b/azure-monitor-opentelemetry-distro/samples/metrics/views.py @@ -17,7 +17,6 @@ # Configure Azure monitor collection telemetry pipeline configure_azure_monitor( connection_string="", - service_name="metric_views_service", disable_logging=True, disable_tracing=True, metrics_export_interval_millis=30000, diff --git a/azure-monitor-opentelemetry-distro/samples/tracing/client.py b/azure-monitor-opentelemetry-distro/samples/tracing/client.py index 0a7a97a8..c94279f5 100644 --- a/azure-monitor-opentelemetry-distro/samples/tracing/client.py +++ b/azure-monitor-opentelemetry-distro/samples/tracing/client.py @@ -14,7 +14,6 @@ # Configure Azure monitor collection telemetry pipeline configure_azure_monitor( connection_string="", - service_name="client_service_name", disable_logging=True, disable_metrics=True, instrumentations=["requests"], diff --git a/azure-monitor-opentelemetry-distro/samples/tracing/db_psycopg2.py b/azure-monitor-opentelemetry-distro/samples/tracing/db_psycopg2.py index ff9f3e8d..a398cffe 100644 --- a/azure-monitor-opentelemetry-distro/samples/tracing/db_psycopg2.py +++ b/azure-monitor-opentelemetry-distro/samples/tracing/db_psycopg2.py @@ -9,7 +9,6 @@ # Configure Azure monitor collection telemetry pipeline configure_azure_monitor( connection_string="", - service_name="psycopg2_service_name", disable_logging=True, disable_metrics=True, instrumentations=["psycopg2"], diff --git a/azure-monitor-opentelemetry-distro/samples/tracing/django/sample/example/views.py b/azure-monitor-opentelemetry-distro/samples/tracing/django/sample/example/views.py index cf279665..1b1a0baa 100644 --- a/azure-monitor-opentelemetry-distro/samples/tracing/django/sample/example/views.py +++ b/azure-monitor-opentelemetry-distro/samples/tracing/django/sample/example/views.py @@ -10,7 +10,6 @@ # Configure Azure monitor collection telemetry pipeline configure_azure_monitor( connection_string="", - service_name="django_service_name", instrumentations=["django"], disable_logging=True, disable_metrics=True, diff --git a/azure-monitor-opentelemetry-distro/samples/tracing/server_flask.py b/azure-monitor-opentelemetry-distro/samples/tracing/server_flask.py index c1c09de3..0f07512e 100644 --- a/azure-monitor-opentelemetry-distro/samples/tracing/server_flask.py +++ b/azure-monitor-opentelemetry-distro/samples/tracing/server_flask.py @@ -9,7 +9,6 @@ # Configure Azure monitor collection telemetry pipeline configure_azure_monitor( connection_string="", - service_name="flask_service_name", disable_logging=True, disable_metrics=True, instrumentations=["flask"], diff --git a/azure-monitor-opentelemetry-distro/samples/tracing/simple.py b/azure-monitor-opentelemetry-distro/samples/tracing/simple.py index 2756bcc0..5c6989d0 100644 --- a/azure-monitor-opentelemetry-distro/samples/tracing/simple.py +++ b/azure-monitor-opentelemetry-distro/samples/tracing/simple.py @@ -6,10 +6,16 @@ from azure.monitor.opentelemetry.distro import configure_azure_monitor from opentelemetry import trace +from opentelemetry.sdk.resources import Resource, ResourceAttributes configure_azure_monitor( connection_string="", - service_name="foo_service", + resource=Resource.create( + { + ResourceAttributes.SERVICE_NAME: "simple_service", + ResourceAttributes.SERVICE_INSTANCE_ID: "simple_tracing_instance", + } + ), tracing_export_interval_millis=15000, disable_logging=True, disable_metrics=True, diff --git a/azure-monitor-opentelemetry-distro/tests/configuration/test_configure.py b/azure-monitor-opentelemetry-distro/tests/configuration/test_configure.py index c4b12823..39da79a8 100644 --- a/azure-monitor-opentelemetry-distro/tests/configuration/test_configure.py +++ b/azure-monitor-opentelemetry-distro/tests/configuration/test_configure.py @@ -24,7 +24,6 @@ _setup_tracing, configure_azure_monitor, ) -from opentelemetry.semconv.resource import ResourceAttributes class TestConfigure(unittest.TestCase): @@ -216,23 +215,18 @@ def test_configure_azure_monitor_disable_metrics( metrics_mock.assert_not_called() instrumentation_mock.assert_called_once_with(kwargs) + def test_get_resource(self): + configuration = {"resource": "test_resource"} + res = _get_resource(configuration) + self.assertEqual(res, "test_resource") + @patch( "azure.monitor.opentelemetry.distro.Resource", ) - def test_get_resource(self, resource_mock): - configuration = { - "service_name": "test_service_name", - "service_namespace": "test_namespace", - "service_instance_id": "test_id", - } + def test_get_resource_default(self, resource_mock): + configuration = {} _get_resource(configuration) - resource_mock.create.assert_called_once_with( - { - ResourceAttributes.SERVICE_NAME: "test_service_name", - ResourceAttributes.SERVICE_NAMESPACE: "test_namespace", - ResourceAttributes.SERVICE_INSTANCE_ID: "test_id", - } - ) + resource_mock.create.assert_called_once_with() @patch( "azure.monitor.opentelemetry.distro.BatchSpanProcessor",