-
Notifications
You must be signed in to change notification settings - Fork 624
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
Add initial DistributedContext implementation. #92
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
747d100
Add initial DistributedContext implementation.
a-feld 8595833
Put length check first.
a-feld 6b163db
Improve performance.
a-feld 51369ab
Update distributed context documentation.
a-feld f3b0096
Allow EntryKey and EntryValue to be used as str.
a-feld dc1fdeb
Update SDK to use context.
a-feld 30c47e8
Fix EntryMetadata.
a-feld d1bca0e
Change EntryMetadata to object type.
a-feld 94decac
Add api for distributed context propagation.
a-feld 9225b00
Add initial API testing for distributed context.
a-feld 633e13e
Fix lint issues.
a-feld face08d
Fix type errors.
a-feld a804f5e
Add SDK distributed context testing.
a-feld e5b52f5
Change the definition of printable.
a-feld 135b134
Move DistributedContext implementation into api level.
a-feld 5de7f64
Fix mypy issues.
a-feld 23ea35d
Update opentelemetry-api/src/opentelemetry/distributedcontext/__init_…
a-feld 7eab1e8
Return EntryValue from get_entry_value.
a-feld 6d86bd3
Fix pylint.
a-feld fd08d80
Remove extra lines.
a-feld 938a0b3
Remove magic numbers from distributed context.
a-feld ae4f570
Fail if key is empty.
a-feld 5794bb8
Disable len-as-condition for distributedcontext check.
a-feld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
opentelemetry-api/src/opentelemetry/distributedcontext/propagation/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Copyright 2019, 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. | ||
|
||
from .binaryformat import BinaryFormat | ||
from .httptextformat import HTTPTextFormat | ||
|
||
__all__ = ["BinaryFormat", "HTTPTextFormat"] |
61 changes: 61 additions & 0 deletions
61
opentelemetry-api/src/opentelemetry/distributedcontext/propagation/binaryformat.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Copyright 2019, 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 typing | ||
|
||
from opentelemetry.distributedcontext import DistributedContext | ||
|
||
|
||
class BinaryFormat(abc.ABC): | ||
"""API for serialization of span context into binary formats. | ||
|
||
This class provides an interface that enables converting span contexts | ||
to and from a binary format. | ||
""" | ||
|
||
@staticmethod | ||
@abc.abstractmethod | ||
def to_bytes(context: DistributedContext) -> bytes: | ||
"""Creates a byte representation of a DistributedContext. | ||
|
||
to_bytes should read values from a DistributedContext and return a data | ||
format to represent it, in bytes. | ||
|
||
Args: | ||
context: the DistributedContext to serialize | ||
|
||
Returns: | ||
A bytes representation of the DistributedContext. | ||
|
||
""" | ||
|
||
@staticmethod | ||
@abc.abstractmethod | ||
def from_bytes( | ||
byte_representation: bytes) -> typing.Optional[DistributedContext]: | ||
"""Return a DistributedContext that was represented by bytes. | ||
|
||
from_bytes should return back a DistributedContext that was constructed | ||
from the data serialized in the byte_representation passed. If it is | ||
not possible to read in a proper DistributedContext, return None. | ||
|
||
Args: | ||
byte_representation: the bytes to deserialize | ||
|
||
Returns: | ||
A bytes representation of the DistributedContext if it is valid. | ||
Otherwise return None. | ||
|
||
""" |
107 changes: 107 additions & 0 deletions
107
opentelemetry-api/src/opentelemetry/distributedcontext/propagation/httptextformat.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# Copyright 2019, 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 typing | ||
|
||
from opentelemetry.distributedcontext import DistributedContext | ||
|
||
Setter = typing.Callable[[object, str, str], None] | ||
Getter = typing.Callable[[object, str], typing.List[str]] | ||
|
||
|
||
class HTTPTextFormat(abc.ABC): | ||
"""API for propagation of span context via headers. | ||
|
||
This class provides an interface that enables extracting and injecting | ||
span context into headers of HTTP requests. HTTP frameworks and clients | ||
can integrate with HTTPTextFormat by providing the object containing the | ||
headers, and a getter and setter function for the extraction and | ||
injection of values, respectively. | ||
|
||
Example:: | ||
|
||
import flask | ||
import requests | ||
from opentelemetry.context.propagation import HTTPTextFormat | ||
|
||
PROPAGATOR = HTTPTextFormat() | ||
|
||
def get_header_from_flask_request(request, key): | ||
return request.headers.get_all(key) | ||
|
||
def set_header_into_requests_request(request: requests.Request, | ||
key: str, value: str): | ||
request.headers[key] = value | ||
|
||
def example_route(): | ||
distributed_context = PROPAGATOR.extract( | ||
get_header_from_flask_request, | ||
flask.request | ||
) | ||
request_to_downstream = requests.Request( | ||
"GET", "http://httpbin.org/get" | ||
) | ||
PROPAGATOR.inject( | ||
distributed_context, | ||
set_header_into_requests_request, | ||
request_to_downstream | ||
) | ||
session = requests.Session() | ||
session.send(request_to_downstream.prepare()) | ||
|
||
|
||
.. _Propagation API Specification: | ||
https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/api-propagators.md | ||
""" | ||
@abc.abstractmethod | ||
def extract(self, get_from_carrier: Getter, | ||
carrier: object) -> DistributedContext: | ||
"""Create a DistributedContext from values in the carrier. | ||
|
||
The extract function should retrieve values from the carrier | ||
object using get_from_carrier, and use values to populate a | ||
DistributedContext value and return it. | ||
|
||
Args: | ||
get_from_carrier: a function that can retrieve zero | ||
or more values from the carrier. In the case that | ||
the value does not exist, return an empty list. | ||
carrier: and object which contains values that are | ||
used to construct a DistributedContext. This object | ||
must be paired with an appropriate get_from_carrier | ||
which understands how to extract a value from it. | ||
Returns: | ||
A DistributedContext with configuration found in the carrier. | ||
|
||
""" | ||
@abc.abstractmethod | ||
def inject(self, context: DistributedContext, set_in_carrier: Setter, | ||
carrier: object) -> None: | ||
"""Inject values from a DistributedContext into a carrier. | ||
|
||
inject enables the propagation of values into HTTP clients or | ||
other objects which perform an HTTP request. Implementations | ||
should use the set_in_carrier method to set values on the | ||
carrier. | ||
|
||
Args: | ||
context: The DistributedContext to read values from. | ||
set_in_carrier: A setter function that can set values | ||
on the carrier. | ||
carrier: An object that a place to define HTTP headers. | ||
Should be paired with set_in_carrier, which should | ||
know how to set header values on the carrier. | ||
|
||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 2019, 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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that currently only
NO_PROPAGATION
andUNLIMITED_PROPAGATION
are allowed should be a check for that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure there's much value in doing a runtime check for these values, especially considering the intended purpose is to actually support positive integers in the future.