Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename CorrelationContext to Baggage #1060

Merged
merged 9 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ OpenTelemetry Python API
.. toctree::
:maxdepth: 1

baggage
configuration
context
correlationcontext
metrics
trace
7 changes: 7 additions & 0 deletions docs/api/baggage.propagation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
opentelemetry.baggage.propagation package
====================================================

Module contents
---------------

.. automodule:: opentelemetry.baggage.propagation
14 changes: 14 additions & 0 deletions docs/api/baggage.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
opentelemetry.baggage package
========================================

Subpackages
-----------

.. toctree::

baggage.propagation

Module contents
---------------

.. automodule:: opentelemetry.baggage
7 changes: 0 additions & 7 deletions docs/api/correlationcontext.propagation.rst

This file was deleted.

14 changes: 0 additions & 14 deletions docs/api/correlationcontext.rst

This file was deleted.

2 changes: 1 addition & 1 deletion docs/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Now run the above script, hit the root url (http://localhost:5000/) a few times,
python flask_example.py


Configure Your HTTP Propagator (b3, CorrelationContext)
Configure Your HTTP Propagator (b3, Baggage)
-------------------------------------------------------

A major feature of distributed tracing is the ability to correlate a trace across
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@
)

from opentelemetry import propagators
from opentelemetry.baggage import get_baggage, set_baggage
from opentelemetry.context import Context, attach, detach, get_value, set_value
from opentelemetry.correlationcontext import get_correlation, set_correlation
from opentelemetry.instrumentation.opentracing_shim import util
from opentelemetry.instrumentation.opentracing_shim.version import __version__
from opentelemetry.trace import INVALID_SPAN_CONTEXT, DefaultSpan, Link
Expand Down Expand Up @@ -290,7 +290,7 @@ def set_baggage_item(self, key: str, value: str):
value: A tag value.
"""
# pylint: disable=protected-access
self._context._baggage = set_correlation(
self._context._baggage = set_baggage(
key, value, context=self._context._baggage
)

Expand All @@ -303,7 +303,7 @@ def get_baggage_item(self, key: str) -> Optional[object]:
Returns this :class:`SpanShim` instance to allow call chaining.
"""
# pylint: disable=protected-access
return get_correlation(key, context=self._context._baggage)
return get_baggage(key, context=self._context._baggage)


class ScopeShim(Scope):
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
([#1034](https://github.com/open-telemetry/opentelemetry-python/pull/1034))
- Remove lazy Event and Link API from Span interface
([#1045](https://github.com/open-telemetry/opentelemetry-python/pull/1045))
- Rename CorrelationContext to Baggage
([#1060](https://github.com/open-telemetry/opentelemetry-python/pull/1060))

## Version 0.12b0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@
from opentelemetry.context import get_value, set_value
from opentelemetry.context.context import Context

_CORRELATION_CONTEXT_KEY = "correlation-context"
_BAGGAGE_KEY = "baggage"


def get_correlations(
def get_all(
ffe4 marked this conversation as resolved.
Show resolved Hide resolved
context: typing.Optional[Context] = None,
) -> typing.Mapping[str, object]:
"""Returns the name/value pairs in the CorrelationContext
"""Returns the name/value pairs in the Baggage

Args:
context: The Context to use. If not set, uses current Context

Returns:
Name/value pairs in the CorrelationContext
The name/value pairs in the Baggage
"""
correlations = get_value(_CORRELATION_CONTEXT_KEY, context=context)
if isinstance(correlations, dict):
return MappingProxyType(correlations.copy())
baggage = get_value(_BAGGAGE_KEY, context=context)
if isinstance(baggage, dict):
return MappingProxyType(baggage.copy())
return MappingProxyType({})


def get_correlation(
def get_baggage(
ffe4 marked this conversation as resolved.
Show resolved Hide resolved
name: str, context: typing.Optional[Context] = None
) -> typing.Optional[object]:
"""Provides access to the value for a name/value pair in the
CorrelationContext
Baggage

Args:
name: The name of the value to retrieve
Expand All @@ -52,13 +52,13 @@ def get_correlation(
The value associated with the given name, or null if the given name is
not present.
"""
return get_correlations(context=context).get(name)
return get_all(context=context).get(name)


def set_correlation(
def set_baggage(
name: str, value: object, context: typing.Optional[Context] = None
) -> Context:
"""Sets a value in the CorrelationContext
"""Sets a value in the Baggage

Args:
name: The name of the value to set
Expand All @@ -68,15 +68,15 @@ def set_correlation(
Returns:
A Context with the value updated
"""
correlations = dict(get_correlations(context=context))
correlations[name] = value
return set_value(_CORRELATION_CONTEXT_KEY, correlations, context=context)
baggage = dict(get_all(context=context))
baggage[name] = value
return set_value(_BAGGAGE_KEY, baggage, context=context)


def remove_correlation(
def remove_baggage(
name: str, context: typing.Optional[Context] = None
) -> Context:
"""Removes a value from the CorrelationContext
"""Removes a value from the Baggage

Args:
name: The name of the value to remove
Expand All @@ -85,19 +85,19 @@ def remove_correlation(
Returns:
A Context with the name/value removed
"""
correlations = dict(get_correlations(context=context))
correlations.pop(name, None)
baggage = dict(get_all(context=context))
baggage.pop(name, None)

return set_value(_CORRELATION_CONTEXT_KEY, correlations, context=context)
return set_value(_BAGGAGE_KEY, baggage, context=context)


def clear_correlations(context: typing.Optional[Context] = None) -> Context:
"""Removes all values from the CorrelationContext
def clear(context: typing.Optional[Context] = None) -> Context:
ffe4 marked this conversation as resolved.
Show resolved Hide resolved
"""Removes all values from the Baggage

Args:
context: The Context to use. If not set, uses current Context

Returns:
A Context with all correlations removed
A Context with all baggage entries removed
"""
return set_value(_CORRELATION_CONTEXT_KEY, {}, context=context)
return set_value(_BAGGAGE_KEY, {}, context=context)
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
import typing
import urllib.parse

from opentelemetry import correlationcontext
from opentelemetry import baggage
from opentelemetry.context import get_current
from opentelemetry.context.context import Context
from opentelemetry.trace.propagation import httptextformat


class CorrelationContextPropagator(httptextformat.HTTPTextFormat):
class BaggagePropagator(httptextformat.HTTPTextFormat):
MAX_HEADER_LENGTH = 8192
MAX_PAIR_LENGTH = 4096
MAX_PAIRS = 180
_CORRELATION_CONTEXT_HEADER_NAME = "otcorrelationcontext"
_BAGGAGE_HEADER_NAME = "otcorrelations"

def extract(
self,
Expand All @@ -35,7 +35,7 @@ def extract(
carrier: httptextformat.HTTPTextFormatT,
context: typing.Optional[Context] = None,
) -> Context:
"""Extract CorrelationContext from the carrier.
"""Extract Baggage from the carrier.

See
`opentelemetry.trace.propagation.httptextformat.HTTPTextFormat.extract`
Expand All @@ -45,25 +45,25 @@ def extract(
context = get_current()

header = _extract_first_element(
get_from_carrier(carrier, self._CORRELATION_CONTEXT_HEADER_NAME)
get_from_carrier(carrier, self._BAGGAGE_HEADER_NAME)
)

if not header or len(header) > self.MAX_HEADER_LENGTH:
return context

correlations = header.split(",")
total_correlations = self.MAX_PAIRS
for correlation in correlations:
if total_correlations <= 0:
baggage_entries = header.split(",")
total_baggage_entries = self.MAX_PAIRS
for entry in baggage_entries:
if total_baggage_entries <= 0:
return context
total_correlations -= 1
if len(correlation) > self.MAX_PAIR_LENGTH:
total_baggage_entries -= 1
if len(entry) > self.MAX_PAIR_LENGTH:
continue
try:
name, value = correlation.split("=", 1)
name, value = entry.split("=", 1)
except Exception: # pylint: disable=broad-except
continue
context = correlationcontext.set_correlation(
context = baggage.set_baggage(
urllib.parse.unquote(name).strip(),
urllib.parse.unquote(value).strip(),
context=context,
Expand All @@ -77,27 +77,25 @@ def inject(
carrier: httptextformat.HTTPTextFormatT,
context: typing.Optional[Context] = None,
) -> None:
"""Injects CorrelationContext into the carrier.
"""Injects Baggage into the carrier.

See
`opentelemetry.trace.propagation.httptextformat.HTTPTextFormat.inject`
"""
correlations = correlationcontext.get_correlations(context=context)
if not correlations:
baggage_entries = baggage.get_all(context=context)
if not baggage_entries:
return

correlation_context_string = _format_correlations(correlations)
baggage_string = _format_baggage(baggage_entries)
set_in_carrier(
carrier,
self._CORRELATION_CONTEXT_HEADER_NAME,
correlation_context_string,
carrier, self._BAGGAGE_HEADER_NAME, baggage_string,
)


def _format_correlations(correlations: typing.Mapping[str, object]) -> str:
def _format_baggage(baggage_entries: typing.Mapping[str, object]) -> str:
return ",".join(
key + "=" + urllib.parse.quote_plus(str(value))
for key, value in correlations.items()
for key, value in baggage_entries.items()
)


Expand Down
8 changes: 2 additions & 6 deletions opentelemetry-api/src/opentelemetry/propagators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,8 @@ def example_route():

import typing

import opentelemetry.trace as trace
from opentelemetry.context import get_current
from opentelemetry.baggage.propagation import BaggagePropagator
from opentelemetry.context.context import Context
from opentelemetry.correlationcontext.propagation import (
CorrelationContextPropagator,
)
from opentelemetry.propagators import composite
from opentelemetry.trace.propagation import httptextformat
from opentelemetry.trace.propagation.tracecontexthttptextformat import (
Expand Down Expand Up @@ -111,7 +107,7 @@ def inject(


_HTTP_TEXT_FORMAT = composite.CompositeHTTPPropagator(
[TraceContextHTTPTextFormat(), CorrelationContextPropagator()],
[TraceContextHTTPTextFormat(), BaggagePropagator()],
) # type: httptextformat.HTTPTextFormat


Expand Down
Loading