|
| 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 | +import copy |
| 16 | +import itertools |
| 17 | +import string |
| 18 | +import typing |
| 19 | +from contextlib import contextmanager |
| 20 | + |
| 21 | +from opentelemetry.context import ContextAPI |
| 22 | + |
| 23 | +CORRELATION_CONTEXT_KEY = "correlation-context-key" |
| 24 | + |
| 25 | + |
| 26 | +class CorrelationContextManager: |
| 27 | + """ |
| 28 | + Manages access to CorrelationContext entries |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__(self, entries: typing.Dict = None) -> None: |
| 32 | + if entries: |
| 33 | + self._correlation_context = copy.deepcopy(entries) |
| 34 | + else: |
| 35 | + self._correlation_context = {} |
| 36 | + |
| 37 | + def get_correlations(self) -> typing.Dict: |
| 38 | + """ |
| 39 | + Returns the entries in this CorrelationContext. The order of entries is not significant. Based |
| 40 | + on the languagespecification, the returned value can be either an immutable collection or an |
| 41 | + immutable iterator to the collection of entries in this CorrelationContext. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + An immutable iterator to the collection of entries in this CorrelationContext |
| 45 | + """ |
| 46 | + return self._correlation_context.items() |
| 47 | + |
| 48 | + def get_correlation( |
| 49 | + self, context: ContextAPI, name: str |
| 50 | + ) -> typing.Optional[object]: |
| 51 | + """ |
| 52 | + To access the value for an entry by a prior event, the Correlations API provides a |
| 53 | + function which takes a context and a name as input, and returns a value. Returns |
| 54 | + the Value associated with the given Name, or null if the given Name is not present. |
| 55 | +
|
| 56 | + Args: |
| 57 | + context: The context in which to find the CorrelationContext |
| 58 | + name: The name of the entry to retrieve |
| 59 | + Returns: |
| 60 | + The value of the entry matching the name |
| 61 | + """ |
| 62 | + # pylint: disable=no-self-use |
| 63 | + correlation_context = context.get_value(CORRELATION_CONTEXT_KEY) |
| 64 | + if correlation_context and name in correlation_context: |
| 65 | + return correlation_context[name] |
| 66 | + return None |
| 67 | + |
| 68 | + def set_correlation( |
| 69 | + self, context: ContextAPI, name: str, value: object |
| 70 | + ) -> ContextAPI: |
| 71 | + """ |
| 72 | + To record the value for an entry, the Correlations API provides a function which takes a |
| 73 | + context, a name, and a value as input. Returns an updated Context which contains the new value. |
| 74 | +
|
| 75 | + Args: |
| 76 | + context: The context in which to find the CorrelationContext |
| 77 | + name: The name of the entry to set |
| 78 | + value: The value of the entry |
| 79 | + Returns: |
| 80 | + A new Context object with updated correlations |
| 81 | + """ |
| 82 | + new_correlation_context = self._copy() |
| 83 | + new_correlation_context[name] = value |
| 84 | + return context.set_value( |
| 85 | + CORRELATION_CONTEXT_KEY, new_correlation_context |
| 86 | + ) |
| 87 | + |
| 88 | + def remove_correlation(self, context: ContextAPI, name: str) -> ContextAPI: |
| 89 | + """ |
| 90 | + To delete an entry, the Correlations API provides a function which takes a context and a name as |
| 91 | + input. Returns an updated Context which no longer contains the selected Name. |
| 92 | +
|
| 93 | + Args: |
| 94 | + context: The context in which to remove the CorrelationContext |
| 95 | + name: The name of the entry to remove |
| 96 | + Returns: |
| 97 | + A new Context object with the correlation removed |
| 98 | + """ |
| 99 | + new_correlation_context = self._copy() |
| 100 | + if name in new_correlation_context: |
| 101 | + del new_correlation_context[name] |
| 102 | + return context.set_value( |
| 103 | + CORRELATION_CONTEXT_KEY, frozenset(new_correlation_context) |
| 104 | + ) |
| 105 | + |
| 106 | + def clear_correlations(self, context: ContextAPI) -> ContextAPI: |
| 107 | + """ |
| 108 | + To avoid sending any entries to an untrusted process, the Correlation API provides a function |
| 109 | + to remove all Correlations from a context. Returns an updated Context with no correlations. |
| 110 | +
|
| 111 | + Args: |
| 112 | + context: The context in which to clear the CorrelationContext |
| 113 | + Returns: |
| 114 | + A new Context object with no correlations |
| 115 | + """ |
| 116 | + # pylint: disable=no-self-use |
| 117 | + return context.set_value(CORRELATION_CONTEXT_KEY, {}) |
| 118 | + |
| 119 | + def _copy(self) -> typing.Dict: |
| 120 | + """ |
| 121 | + Helper function to abstract the mechanism used to copy CorrelationContext values |
| 122 | +
|
| 123 | + Returns: |
| 124 | + A copy of the current entries in the CorrelationContext |
| 125 | + """ |
| 126 | + return copy.deepcopy(self._correlation_context) |
0 commit comments