|
| 1 | +# Copyright 2019, OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from opentelemetry.distributedcontext import DistributedContext |
| 16 | +from opentelemetry.trace import SpanContext |
| 17 | + |
| 18 | + |
| 19 | +class UnifiedContext: |
| 20 | + """A unified context object that contains all context relevant to |
| 21 | + telemetry. |
| 22 | +
|
| 23 | + The UnifiedContext is a single object that composes all contexts that |
| 24 | + are needed by the various forms of telemetry. It is intended to be an |
| 25 | + object that can be passed as the argument to any component that needs |
| 26 | + to read or modify content values (such as propagators). By unifying |
| 27 | + all context in a composed data structure, it expands the flexibility |
| 28 | + of the APIs that modify it. |
| 29 | +
|
| 30 | + As it is designed to carry context specific to all telemetry use |
| 31 | + cases, it's schema is explicit. Note that this is not intended to |
| 32 | + be an object that acts as a singleton that returns different results |
| 33 | + based on the thread or coroutine of execution. For that, see `Context`. |
| 34 | +
|
| 35 | +
|
| 36 | + Args: |
| 37 | + distributed: The DistributedContext for this instance. |
| 38 | + span: The SpanContext for this instance. |
| 39 | + """ |
| 40 | + __slots__ = ["distributed", "span"] |
| 41 | + |
| 42 | + def __init__(self, distributed: DistributedContext, span: SpanContext): |
| 43 | + self.distributed = distributed |
| 44 | + self.span = span |
| 45 | + |
| 46 | + @staticmethod |
| 47 | + def create(span: SpanContext) -> "UnifiedContext": |
| 48 | + """Create an unpopulated UnifiedContext object. |
| 49 | +
|
| 50 | + Example: |
| 51 | +
|
| 52 | + from opentelemetry.trace import tracer |
| 53 | + span = tracer.create_span("") |
| 54 | + context = UnifiedContext.create(span) |
| 55 | +
|
| 56 | +
|
| 57 | + Args: |
| 58 | + parent_span: the parent SpanContext that will be the |
| 59 | + parent of the span in the UnifiedContext. |
| 60 | + """ |
| 61 | + return UnifiedContext(DistributedContext(), span) |
| 62 | + |
| 63 | + def __repr__(self) -> str: |
| 64 | + return "{}(distributed={}, span={})".format( |
| 65 | + type(self).__name__, repr(self.distributed), repr(self.span)) |
0 commit comments