Skip to content

Commit

Permalink
Add __getnewargs__ to SpanContext class (#1380)
Browse files Browse the repository at this point in the history
  • Loading branch information
rickychu authored Nov 16, 2020
1 parent 1d03c34 commit f1db112
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions opentelemetry-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions opentelemetry-api/src/opentelemetry/trace/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,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
Expand Down
36 changes: 36 additions & 0 deletions opentelemetry-api/tests/trace/test_span_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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 pickle
import unittest

from opentelemetry import trace


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,
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)

0 comments on commit f1db112

Please sign in to comment.