Repository: open-telemetry/opentelemetry-python-contrib

Issue:
Datadog and AWS SDK extension propagator fields attributes are not a property
Datadog and AWS SDK extension propagator fields attributes are not a property. They should be. Fix this.


Discussion:

Resolving patch:
diff --git a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/propagator.py b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/propagator.py
--- a/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/propagator.py
+++ b/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/propagator.py
@@ -106,6 +106,7 @@ def inject(
                 span.context.trace_state[constants.DD_ORIGIN],
             )
 
+    @property
     def fields(self):
         """Returns a set with the fields set in `inject`.
 
diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/trace/propagation/aws_xray_format.py b/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/trace/propagation/aws_xray_format.py
--- a/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/trace/propagation/aws_xray_format.py
+++ b/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/trace/propagation/aws_xray_format.py
@@ -275,6 +275,7 @@ def inject(
             carrier, TRACE_HEADER_KEY, trace_header,
         )
 
+    @property
     def fields(self):
         """Returns a set with the fields set in `inject`.
 


Test patch:
diff --git a/exporter/opentelemetry-exporter-datadog/tests/test_datadog_format.py b/exporter/opentelemetry-exporter-datadog/tests/test_datadog_format.py
--- a/exporter/opentelemetry-exporter-datadog/tests/test_datadog_format.py
+++ b/exporter/opentelemetry-exporter-datadog/tests/test_datadog_format.py
@@ -13,6 +13,7 @@
 # limitations under the License.
 
 import unittest
+from unittest.mock import Mock, patch
 
 from opentelemetry import trace as trace_api
 from opentelemetry.exporter.datadog import constants, propagator
@@ -169,3 +170,35 @@ def test_sampling_priority_auto_reject(self):
             child_carrier[FORMAT.SAMPLING_PRIORITY_KEY],
             str(constants.AUTO_REJECT),
         )
+
+    @patch("opentelemetry.exporter.datadog.propagator.get_current_span")
+    def test_fields(self, mock_get_current_span):
+        """Make sure the fields attribute returns the fields used in inject"""
+
+        tracer = trace.TracerProvider().get_tracer("sdk_tracer_provider")
+
+        mock_set_in_carrier = Mock()
+
+        mock_get_current_span.configure_mock(
+            **{
+                "return_value": Mock(
+                    **{
+                        "get_span_context.return_value": None,
+                        "context.trace_flags": 0,
+                        "context.trace_id": 1,
+                        "context.trace_state": {constants.DD_ORIGIN: 0},
+                    }
+                )
+            }
+        )
+
+        with tracer.start_as_current_span("parent"):
+            with tracer.start_as_current_span("child"):
+                FORMAT.inject(mock_set_in_carrier, {})
+
+        inject_fields = set()
+
+        for call in mock_set_in_carrier.mock_calls:
+            inject_fields.add(call[1][1])
+
+        self.assertEqual(FORMAT.fields, inject_fields)
diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/tests/trace/propagation/test_aws_xray_format.py b/sdk-extension/opentelemetry-sdk-extension-aws/tests/trace/propagation/test_aws_xray_format.py
--- a/sdk-extension/opentelemetry-sdk-extension-aws/tests/trace/propagation/test_aws_xray_format.py
+++ b/sdk-extension/opentelemetry-sdk-extension-aws/tests/trace/propagation/test_aws_xray_format.py
@@ -13,6 +13,7 @@
 # limitations under the License.
 
 import unittest
+from unittest.mock import Mock, patch
 
 from requests.structures import CaseInsensitiveDict
 
@@ -165,7 +166,7 @@ def test_inject_reported_fields_matches_carrier_fields(self):
         injected_keys = set(carrier.keys())
 
         self.assertEqual(
-            injected_keys, AwsXRayPropagatorTest.XRAY_PROPAGATOR.fields()
+            injected_keys, AwsXRayPropagatorTest.XRAY_PROPAGATOR.fields
         )
 
     # Extract Tests
@@ -372,3 +373,35 @@ def test_extract_invalid_non_numeric_sampled_flag(self):
             get_nested_span_context(context_with_extracted),
             INVALID_SPAN_CONTEXT,
         )
+
+    @patch(
+        "opentelemetry.sdk.extension.aws.trace."
+        "propagation.aws_xray_format.trace"
+    )
+    def test_fields(self, mock_trace):
+        """Make sure the fields attribute returns the fields used in inject"""
+
+        mock_trace.configure_mock(
+            **{
+                "get_current_span.return_value": Mock(
+                    **{
+                        "get_span_context.return_value": Mock(
+                            **{"is_valid": True, "trace_id": 1, "span_id": 1}
+                        )
+                    }
+                )
+            }
+        )
+
+        mock_set_in_carrier = Mock()
+
+        AwsXRayPropagatorTest.XRAY_PROPAGATOR.inject(mock_set_in_carrier, {})
+
+        inject_fields = set()
+
+        for call in mock_set_in_carrier.mock_calls:
+            inject_fields.add(call[1][1])
+
+        self.assertEqual(
+            AwsXRayPropagatorTest.XRAY_PROPAGATOR.fields, inject_fields
+        )