From de3417252cea82f089e49bd681c35dff07463222 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Tue, 14 Jul 2020 12:02:30 -0600 Subject: [PATCH] Implement get_attribute and set_attribute Fixes #239 --- .../ext/opentracing_shim/__init__.py | 26 +++---------------- .../src/opentelemetry/trace/span.py | 10 +++++++ .../src/opentelemetry/sdk/trace/__init__.py | 4 +++ 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/ext/opentelemetry-ext-opentracing-shim/src/opentelemetry/ext/opentracing_shim/__init__.py b/ext/opentelemetry-ext-opentracing-shim/src/opentelemetry/ext/opentracing_shim/__init__.py index 0b948216ff3..37ba2668bd3 100644 --- a/ext/opentelemetry-ext-opentracing-shim/src/opentelemetry/ext/opentracing_shim/__init__.py +++ b/ext/opentelemetry-ext-opentracing-shim/src/opentelemetry/ext/opentracing_shim/__init__.py @@ -270,31 +270,13 @@ def log(self, **kwargs): def log_event(self, event, payload=None): super().log_event(event, payload=payload) - def set_baggage_item(self, key, value): # pylint:disable=unused-argument - """Implements the ``set_baggage_item()`` method from the base class. + def set_baggage_item(self, key, value): - Warning: - Not implemented yet. - """ - - logger.warning( - "Calling unimplemented method set_baggage_item() on class %s", - self.__class__.__name__, - ) - # TODO: Implement. + return self._otel_span.set_attribute(key, value) - def get_baggage_item(self, key): # pylint:disable=unused-argument - """Implements the ``get_baggage_item()`` method from the base class. + def get_baggage_item(self, key): - Warning: - Not implemented yet. - """ - - logger.warning( - "Calling unimplemented method get_baggage_item() on class %s", - self.__class__.__name__, - ) - # TODO: Implement. + return self._otel_span.get_attribute(key) class ScopeShim(opentracing.Scope): diff --git a/opentelemetry-api/src/opentelemetry/trace/span.py b/opentelemetry-api/src/opentelemetry/trace/span.py index baea6670f63..53db6220c1b 100644 --- a/opentelemetry-api/src/opentelemetry/trace/span.py +++ b/opentelemetry-api/src/opentelemetry/trace/span.py @@ -37,6 +37,13 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None: Sets a single Attribute with the key and value passed as arguments. """ + @abc.abstractmethod + def get_attribute(self, key: str) -> types.AttributeValue: + """Gets an Attribute. + + Gets a single Attribute with the key passed as argument. + """ + @abc.abstractmethod def add_event( self, @@ -235,6 +242,9 @@ def end(self, end_time: typing.Optional[int] = None) -> None: def set_attribute(self, key: str, value: types.AttributeValue) -> None: pass + def get_attribute(self, key: str) -> types.AttributeValue: + pass + def add_event( self, name: str, diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 1118b424888..dadaf2f40cf 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -553,6 +553,10 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None: with self._lock: self.attributes[key] = value + def get_attribute(self, key: str) -> types.AttributeValue: + + return self.attributes[key] + @staticmethod def _filter_attribute_values(attributes: types.Attributes): if attributes: