Skip to content

Commit

Permalink
Merge branch 'main' into Fix-bug-when-labels-are-ordered-differently
Browse files Browse the repository at this point in the history
  • Loading branch information
soundofspace committed Mar 2, 2024
2 parents 18308db + ca73f3f commit 6a2fe89
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#3567](https://github.com/open-telemetry/opentelemetry-python/pull/3567))
- Fix flush error when no LoggerProvider configured for LoggingHandler
([#3608](https://github.com/open-telemetry/opentelemetry-python/pull/3608))
- Add `Span.add_link()` method to add link after span start
([#3618](https://github.com/open-telemetry/opentelemetry-python/pull/3618))
- Fix `OTLPMetricExporter` ignores `preferred_aggregation` property
([#3603](https://github.com/open-telemetry/opentelemetry-python/pull/3603))
- Logs: set `observed_timestamp` field
Expand Down
1 change: 1 addition & 0 deletions docs/examples/logs/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Start the Collector locally to see data being exported. Write the following file
pipelines:
logs:
receivers: [otlp]
processors: [batch]
exporters: [logging]
Then start the Docker container:
Expand Down
1 change: 1 addition & 0 deletions docs/examples/logs/otel-collector-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ service:
pipelines:
logs:
receivers: [otlp]
processors: [batch]
exporters: [logging]
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ def __init__(
)
self._shutdown = False

def _export(self, serialized_data: str):
def _export(self, serialized_data: bytes):
data = serialized_data
if self._compression == Compression.Gzip:
gzip_data = BytesIO()
with gzip.GzipFile(fileobj=gzip_data, mode="w") as gzip_stream:
gzip_stream.write(serialized_data)
data = gzip_data.getvalue()
elif self._compression == Compression.Deflate:
data = zlib.compress(bytes(serialized_data))
data = zlib.compress(serialized_data)

return self._session.post(
url=self._endpoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ def __init__(
preferred_temporality, preferred_aggregation
)

def _export(self, serialized_data: str):
def _export(self, serialized_data: bytes):
data = serialized_data
if self._compression == Compression.Gzip:
gzip_data = BytesIO()
with gzip.GzipFile(fileobj=gzip_data, mode="w") as gzip_stream:
gzip_stream.write(serialized_data)
data = gzip_data.getvalue()
elif self._compression == Compression.Deflate:
data = zlib.compress(bytes(serialized_data))
data = zlib.compress(serialized_data)

return self._session.post(
url=self._endpoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ def __init__(
)
self._shutdown = False

def _export(self, serialized_data: str):
def _export(self, serialized_data: bytes):
data = serialized_data
if self._compression == Compression.Gzip:
gzip_data = BytesIO()
with gzip.GzipFile(fileobj=gzip_data, mode="w") as gzip_stream:
gzip_stream.write(serialized_data)
data = gzip_data.getvalue()
elif self._compression == Compression.Deflate:
data = zlib.compress(bytes(serialized_data))
data = zlib.compress(serialized_data)

return self._session.post(
url=self._endpoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def content_type():

def serialize(
self, spans: Sequence[Span], local_endpoint: NodeEndpoint
) -> str:
) -> bytes:
encoded_local_endpoint = self._encode_local_endpoint(local_endpoint)
# pylint: disable=no-member
encoded_spans = zipkin_pb2.ListOfSpans()
Expand Down
5 changes: 1 addition & 4 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
from deprecated import deprecated

from opentelemetry import context as context_api
from opentelemetry.attributes import BoundedAttributes # type: ignore
from opentelemetry.context.context import Context
from opentelemetry.environment_variables import OTEL_PYTHON_TRACER_PROVIDER
from opentelemetry.trace.propagation import (
Expand Down Expand Up @@ -144,9 +143,7 @@ def __init__(
attributes: types.Attributes = None,
) -> None:
super().__init__(context)
self._attributes = BoundedAttributes(
attributes=attributes
) # type: types.Attributes
self._attributes = attributes

@property
def attributes(self) -> types.Attributes:
Expand Down
28 changes: 28 additions & 0 deletions opentelemetry-api/src/opentelemetry/trace/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import types as python_types
import typing
import warnings

from opentelemetry.trace.status import Status, StatusCode
from opentelemetry.util import types
Expand Down Expand Up @@ -117,6 +118,26 @@ def add_event(
timestamp if the `timestamp` argument is omitted.
"""

def add_link( # pylint: disable=no-self-use
self,
context: "SpanContext",
attributes: types.Attributes = None,
) -> None:
"""Adds a `Link`.
Adds a single `Link` with the `SpanContext` of the span to link to and,
optionally, attributes passed as arguments. Implementations may ignore
calls with an invalid span context.
Note: It is preferred to add links at span creation, instead of calling
this method later since samplers can only consider information already
present during span creation.
"""
warnings.warn(
"Span.add_link() not implemented and will be a no-op. "
"Use opentelemetry-sdk >= 1.23 to add links after span creation"
)

@abc.abstractmethod
def update_name(self, name: str) -> None:
"""Updates the `Span` name.
Expand Down Expand Up @@ -523,6 +544,13 @@ def add_event(
) -> None:
pass

def add_link(
self,
context: "SpanContext",
attributes: types.Attributes = None,
) -> None:
pass

def update_name(self, name: str) -> None:
pass

Expand Down
24 changes: 24 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,30 @@ def add_event(
)
)

@_check_span_ended
def _add_link(self, link: trace_api.Link) -> None:
self._links.append(link)

def add_link(
self,
context: SpanContext,
attributes: types.Attributes = None,
) -> None:
if context is None or not context.is_valid:
return

attributes = BoundedAttributes(
self._limits.max_link_attributes,
attributes,
max_value_len=self._limits.max_attribute_length,
)
self._add_link(
trace_api.Link(
context=context,
attributes=attributes,
)
)

def _readable_span(self) -> ReadableSpan:
return ReadableSpan(
name=self._name,
Expand Down
31 changes: 31 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,37 @@ def test_links(self):
with self.assertRaises(TypeError):
root.links[1].attributes["name"] = "new_neighbour"

def test_add_link(self):
id_generator = RandomIdGenerator()
other_context = trace_api.SpanContext(
trace_id=id_generator.generate_trace_id(),
span_id=id_generator.generate_span_id(),
is_remote=False,
)

with self.tracer.start_as_current_span("root") as root:
root.add_link(other_context, {"name": "neighbor"})

self.assertEqual(len(root.links), 1)
self.assertEqual(
root.links[0].context.trace_id, other_context.trace_id
)
self.assertEqual(
root.links[0].context.span_id, other_context.span_id
)
self.assertEqual(root.links[0].attributes, {"name": "neighbor"})

with self.assertRaises(TypeError):
root.links[0].attributes["name"] = "new_neighbour"

def test_add_link_with_invalid_span_context(self):
other_context = trace_api.INVALID_SPAN_CONTEXT

with self.tracer.start_as_current_span("root") as root:
root.add_link(other_context)

self.assertEqual(len(root.links), 0)

def test_update_name(self):
with self.tracer.start_as_current_span("root") as root:
# name
Expand Down

0 comments on commit 6a2fe89

Please sign in to comment.