From 49ab640025ae9f610000d468ed2d47bfb98ef2d8 Mon Sep 17 00:00:00 2001 From: Ricky Date: Thu, 12 Nov 2020 23:23:17 -0500 Subject: [PATCH 1/9] Add __getnewargs__ to SpanContext class --- opentelemetry-api/src/opentelemetry/trace/span.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/opentelemetry-api/src/opentelemetry/trace/span.py b/opentelemetry-api/src/opentelemetry/trace/span.py index 681e9f7ec3f..bcbf9819e31 100644 --- a/opentelemetry-api/src/opentelemetry/trace/span.py +++ b/opentelemetry-api/src/opentelemetry/trace/span.py @@ -189,6 +189,17 @@ def __new__( (trace_id, span_id, is_remote, trace_flags, trace_state, is_valid), ) + def __getnewargs__( + self, + ) -> typing.Tuple[int, int, bool, "TraceFlags", "TraceState"]: + return ( + self.trace_id, + self.span_id, + self.is_remote, + self.trace_flags, + self.trace_state, + ) + @property def trace_id(self) -> int: return self[0] # pylint: disable=unsubscriptable-object From 042079f590e8aabd638e34b99270f5061b437d9e Mon Sep 17 00:00:00 2001 From: Ricky Date: Thu, 12 Nov 2020 23:27:31 -0500 Subject: [PATCH 2/9] Update changelog to include pickle info for SpanContext --- opentelemetry-api/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/opentelemetry-api/CHANGELOG.md b/opentelemetry-api/CHANGELOG.md index 80549373127..b231b5aed5b 100644 --- a/opentelemetry-api/CHANGELOG.md +++ b/opentelemetry-api/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Add optional parameter to `record_exception` method ([#1314](https://github.com/open-telemetry/opentelemetry-python/pull/1314)) +- Add pickle support to SpanContext class ([#1380](https://github.com/open-telemetry/opentelemetry-python/pull/1380)) ## Version 0.15b0 From 4cc337c3e4b9cff37cf02dc2b87e739b30048208 Mon Sep 17 00:00:00 2001 From: Ricky Date: Thu, 12 Nov 2020 23:31:30 -0500 Subject: [PATCH 3/9] Remove unnecessary spaces after the function __getnewargs__ --- opentelemetry-api/src/opentelemetry/trace/span.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-api/src/opentelemetry/trace/span.py b/opentelemetry-api/src/opentelemetry/trace/span.py index bcbf9819e31..e52ecb7e8b5 100644 --- a/opentelemetry-api/src/opentelemetry/trace/span.py +++ b/opentelemetry-api/src/opentelemetry/trace/span.py @@ -199,7 +199,7 @@ def __getnewargs__( self.trace_flags, self.trace_state, ) - + @property def trace_id(self) -> int: return self[0] # pylint: disable=unsubscriptable-object From 1cde30f467d212a1b764803d42f4da16ca6ac3ec Mon Sep 17 00:00:00 2001 From: Ricky Date: Fri, 13 Nov 2020 00:22:08 -0500 Subject: [PATCH 4/9] Add test case --- .../tests/trace/test_span_context.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 opentelemetry-api/tests/trace/test_span_context.py diff --git a/opentelemetry-api/tests/trace/test_span_context.py b/opentelemetry-api/tests/trace/test_span_context.py new file mode 100644 index 00000000000..b5c5d767572 --- /dev/null +++ b/opentelemetry-api/tests/trace/test_span_context.py @@ -0,0 +1,34 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest +import pickle + +from opentelemetry import trace + +class TestSpanContext(unittest.TestCase): + def test_span_context_pickle(self): + + sc = trace.SpanContext( + 1, + 2, + is_remote=False, + trace_flags=trace.DEFAULT_TRACE_OPTIONS, + trace_state=trace.DEFAULT_TRACE_STATE, + ) + + pickle_sc = pickle.loads(pickle.dumps(sc)) + + self.assertEqual(sc.trace_id, pickle_sc.trace_id) + self.assertEqual(sc.span_id, pickle_sc.span_id) \ No newline at end of file From 98c87efa746a4bdd86b36d6ad3ff66e9a88aa54a Mon Sep 17 00:00:00 2001 From: Ricky Date: Fri, 13 Nov 2020 00:28:50 -0500 Subject: [PATCH 5/9] fix lint --- opentelemetry-api/tests/trace/test_span_context.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/opentelemetry-api/tests/trace/test_span_context.py b/opentelemetry-api/tests/trace/test_span_context.py index b5c5d767572..3f33f4f10cd 100644 --- a/opentelemetry-api/tests/trace/test_span_context.py +++ b/opentelemetry-api/tests/trace/test_span_context.py @@ -17,9 +17,9 @@ from opentelemetry import trace + class TestSpanContext(unittest.TestCase): def test_span_context_pickle(self): - sc = trace.SpanContext( 1, 2, @@ -27,8 +27,6 @@ def test_span_context_pickle(self): trace_flags=trace.DEFAULT_TRACE_OPTIONS, trace_state=trace.DEFAULT_TRACE_STATE, ) - pickle_sc = pickle.loads(pickle.dumps(sc)) - self.assertEqual(sc.trace_id, pickle_sc.trace_id) self.assertEqual(sc.span_id, pickle_sc.span_id) \ No newline at end of file From 450e739f7c30636417ee6e2d7afaae728b8b050a Mon Sep 17 00:00:00 2001 From: Ricky Date: Fri, 13 Nov 2020 00:31:23 -0500 Subject: [PATCH 6/9] Add extra line to test case to fix lint error --- opentelemetry-api/tests/trace/test_span_context.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/opentelemetry-api/tests/trace/test_span_context.py b/opentelemetry-api/tests/trace/test_span_context.py index 3f33f4f10cd..709d773b4d9 100644 --- a/opentelemetry-api/tests/trace/test_span_context.py +++ b/opentelemetry-api/tests/trace/test_span_context.py @@ -29,4 +29,5 @@ def test_span_context_pickle(self): ) pickle_sc = pickle.loads(pickle.dumps(sc)) self.assertEqual(sc.trace_id, pickle_sc.trace_id) - self.assertEqual(sc.span_id, pickle_sc.span_id) \ No newline at end of file + self.assertEqual(sc.span_id, pickle_sc.span_id) + \ No newline at end of file From bc93898ba622f8c48551002f265ac6f1e2e5d97b Mon Sep 17 00:00:00 2001 From: Ricky Date: Fri, 13 Nov 2020 00:40:38 -0500 Subject: [PATCH 7/9] Remove extra space at the end of line to fix lint error --- opentelemetry-api/tests/trace/test_span_context.py | 1 - 1 file changed, 1 deletion(-) diff --git a/opentelemetry-api/tests/trace/test_span_context.py b/opentelemetry-api/tests/trace/test_span_context.py index 709d773b4d9..ccd84a139d9 100644 --- a/opentelemetry-api/tests/trace/test_span_context.py +++ b/opentelemetry-api/tests/trace/test_span_context.py @@ -30,4 +30,3 @@ def test_span_context_pickle(self): pickle_sc = pickle.loads(pickle.dumps(sc)) self.assertEqual(sc.trace_id, pickle_sc.trace_id) self.assertEqual(sc.span_id, pickle_sc.span_id) - \ No newline at end of file From 97b20964c83f05e2d9ece7c8ca55201b7c02b2a9 Mon Sep 17 00:00:00 2001 From: Ricky Date: Fri, 13 Nov 2020 00:46:34 -0500 Subject: [PATCH 8/9] rearrange import statement for lint error --- opentelemetry-api/tests/trace/test_span_context.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-api/tests/trace/test_span_context.py b/opentelemetry-api/tests/trace/test_span_context.py index ccd84a139d9..ecb8b49b79d 100644 --- a/opentelemetry-api/tests/trace/test_span_context.py +++ b/opentelemetry-api/tests/trace/test_span_context.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import unittest import pickle +import unittest from opentelemetry import trace From a21d256d25a0722bf606f02dd03412a46380e3c4 Mon Sep 17 00:00:00 2001 From: Ricky Date: Fri, 13 Nov 2020 12:24:12 -0500 Subject: [PATCH 9/9] Add docstring to explain why SpanContext needs to be pickleable --- opentelemetry-api/tests/trace/test_span_context.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/opentelemetry-api/tests/trace/test_span_context.py b/opentelemetry-api/tests/trace/test_span_context.py index ecb8b49b79d..c109d006a53 100644 --- a/opentelemetry-api/tests/trace/test_span_context.py +++ b/opentelemetry-api/tests/trace/test_span_context.py @@ -20,6 +20,10 @@ class TestSpanContext(unittest.TestCase): def test_span_context_pickle(self): + """ + SpanContext needs to be pickleable to support multiprocessing + so span can start as parent from the new spawned process + """ sc = trace.SpanContext( 1, 2,