|
| 1 | +# Copyright 2020, 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 | +import abc |
| 16 | +import itertools |
| 17 | +import string |
| 18 | +import typing |
| 19 | +from contextlib import contextmanager |
| 20 | + |
| 21 | +from opentelemetry.context import attach, get_value, set_value |
| 22 | +from opentelemetry.context.context import Context |
| 23 | + |
| 24 | +CORRELATION_CONTEXT_KEY = "correlation-context" |
| 25 | + |
| 26 | + |
| 27 | +class CorrelationContext(abc.ABC): |
| 28 | + """A container for correlation context""" |
| 29 | + |
| 30 | + @abc.abstractmethod |
| 31 | + def get_correlations(self, context: typing.Optional[Context] = None): |
| 32 | + """ Returns the name/value pairs in the CorrelationContext |
| 33 | + |
| 34 | + Args: |
| 35 | + context: the Context to use. If not set, uses current Context |
| 36 | +
|
| 37 | + Returns: |
| 38 | + name/value pairs in the CorrelationContext |
| 39 | + """ |
| 40 | + |
| 41 | + @abc.abstractmethod |
| 42 | + def get_correlation( |
| 43 | + self, name, context: typing.Optional[Context] = None |
| 44 | + ) -> typing.Optional[object]: |
| 45 | + """ Provides access to the value for a name/value pair by a prior event |
| 46 | + |
| 47 | + Args: |
| 48 | + name: the name of the value to retrieve |
| 49 | + context: the Context to use. If not set, uses current Context |
| 50 | +
|
| 51 | + Returns: |
| 52 | + the value associated with the given name, or null if the given name is |
| 53 | + not present. |
| 54 | + """ |
| 55 | + |
| 56 | + @abc.abstractmethod |
| 57 | + def set_correlation( |
| 58 | + self, name, value, context: typing.Optional[Context] = None |
| 59 | + ) -> Context: |
| 60 | + """ |
| 61 | +
|
| 62 | + Args: |
| 63 | + name: the name of the value to set |
| 64 | + value: the value to set |
| 65 | + context: the Context to use. If not set, uses current Context |
| 66 | +
|
| 67 | + Returns: |
| 68 | + a Context with the value updated |
| 69 | + """ |
| 70 | + |
| 71 | + @abc.abstractmethod |
| 72 | + def remove_correlation( |
| 73 | + self, name, context: typing.Optional[Context] = None |
| 74 | + ) -> Context: |
| 75 | + """ |
| 76 | +
|
| 77 | + Args: |
| 78 | + name: the name of the value to remove |
| 79 | + context: the Context to use. If not set, uses current Context |
| 80 | +
|
| 81 | + Returns: |
| 82 | + a Context with the name/value removed |
| 83 | + """ |
| 84 | + |
| 85 | + @abc.abstractmethod |
| 86 | + def clear_correlations( |
| 87 | + self, context: typing.Optional[Context] = None |
| 88 | + ) -> Context: |
| 89 | + """ |
| 90 | + Args: |
| 91 | + context: the Context to use. If not set, uses current Context |
| 92 | +
|
| 93 | + Returns: |
| 94 | + a Context with all correlations removed |
| 95 | + """ |
| 96 | + |
| 97 | + |
| 98 | +class DefaultCorrelationContext(CorrelationContext): |
| 99 | + """ Default no-op implementation of CorrelationContext """ |
| 100 | + |
| 101 | + def get_correlations( |
| 102 | + self, context: typing.Optional[Context] = None |
| 103 | + ) -> typing.Dict: |
| 104 | + return {} |
| 105 | + |
| 106 | + def get_correlation( |
| 107 | + self, name, context: typing.Optional[Context] = None |
| 108 | + ) -> typing.Optional[object]: |
| 109 | + return None |
| 110 | + |
| 111 | + def set_correlation( |
| 112 | + self, name, value, context: typing.Optional[Context] = None |
| 113 | + ) -> Context: |
| 114 | + return context |
| 115 | + |
| 116 | + def remove_correlation( |
| 117 | + self, name, context: typing.Optional[Context] = None |
| 118 | + ) -> Context: |
| 119 | + return context |
| 120 | + |
| 121 | + def clear_correlations( |
| 122 | + self, context: typing.Optional[Context] = None |
| 123 | + ) -> Context: |
| 124 | + return context |
0 commit comments