Skip to content

Commit

Permalink
Initial commit for Correlation Context API
Browse files Browse the repository at this point in the history
This change removes Distributed Context and replaces it with the Correlations Context API. Things to do:
- add more tests
- implement correlation context propagation and add it to the default propagator

Signed-off-by: Alex Boten <[email protected]>
  • Loading branch information
Alex Boten authored and ocelotl committed Mar 10, 2020
1 parent 4b6a52d commit 4d595f8
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 360 deletions.
124 changes: 124 additions & 0 deletions opentelemetry-api/src/opentelemetry/correlationcontext/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Copyright 2020, 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 abc
import itertools
import string
import typing
from contextlib import contextmanager

from opentelemetry.context import attach, get_value, set_value
from opentelemetry.context.context import Context

CORRELATION_CONTEXT_KEY = "correlation-context"


class CorrelationContext(abc.ABC):
"""A container for correlation context"""

@abc.abstractmethod
def get_correlations(self, context: typing.Optional[Context] = None):
""" Returns the name/value pairs in the CorrelationContext
Args:
context: the Context to use. If not set, uses current Context
Returns:
name/value pairs in the CorrelationContext
"""

@abc.abstractmethod
def get_correlation(
self, name, context: typing.Optional[Context] = None
) -> typing.Optional[object]:
""" Provides access to the value for a name/value pair by a prior event
Args:
name: the name of the value to retrieve
context: the Context to use. If not set, uses current Context
Returns:
the value associated with the given name, or null if the given name is
not present.
"""

@abc.abstractmethod
def set_correlation(
self, name, value, context: typing.Optional[Context] = None
) -> Context:
"""
Args:
name: the name of the value to set
value: the value to set
context: the Context to use. If not set, uses current Context
Returns:
a Context with the value updated
"""

@abc.abstractmethod
def remove_correlation(
self, name, context: typing.Optional[Context] = None
) -> Context:
"""
Args:
name: the name of the value to remove
context: the Context to use. If not set, uses current Context
Returns:
a Context with the name/value removed
"""

@abc.abstractmethod
def clear_correlations(
self, context: typing.Optional[Context] = None
) -> Context:
"""
Args:
context: the Context to use. If not set, uses current Context
Returns:
a Context with all correlations removed
"""


class DefaultCorrelationContext(CorrelationContext):
""" Default no-op implementation of CorrelationContext """

def get_correlations(
self, context: typing.Optional[Context] = None
) -> typing.Dict:
return {}

def get_correlation(
self, name, context: typing.Optional[Context] = None
) -> typing.Optional[object]:
return None

def set_correlation(
self, name, value, context: typing.Optional[Context] = None
) -> Context:
return context

def remove_correlation(
self, name, context: typing.Optional[Context] = None
) -> Context:
return context

def clear_correlations(
self, context: typing.Optional[Context] = None
) -> Context:
return context
145 changes: 0 additions & 145 deletions opentelemetry-api/src/opentelemetry/distributedcontext/__init__.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2020, 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

from opentelemetry import correlationcontext


class TestCorrelationContext(unittest.TestCase):
def test_correlation_context(self):
default_context = correlationcontext.DefaultCorrelationContext()
self.assertEqual(default_context.get_correlations(), {})
Loading

0 comments on commit 4d595f8

Please sign in to comment.