From 91db3cdd522b9e467f00fe188e717fefb8af5e53 Mon Sep 17 00:00:00 2001 From: Nir Hadassi Date: Mon, 4 May 2020 15:21:36 +0300 Subject: [PATCH 1/5] ext/jaeger - Transform resource to tags when exporting --- .../src/opentelemetry/ext/jaeger/__init__.py | 2 ++ ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/__init__.py b/ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/__init__.py index 23ed6e725d8..91b17260600 100644 --- a/ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/__init__.py +++ b/ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/__init__.py @@ -197,6 +197,8 @@ def _translate_to_jaeger(spans: Span): parent_id = span.parent.span_id if span.parent else 0 tags = _extract_tags(span.attributes) + if span.resource is not None: + tags.extend(_extract_tags(span.resource.labels)) tags.extend( [ diff --git a/ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py b/ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py index 2c792fd5d16..9b37b5da531 100644 --- a/ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py +++ b/ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py @@ -22,6 +22,7 @@ from opentelemetry import trace as trace_api from opentelemetry.ext.jaeger.gen.jaeger import ttypes as jaeger from opentelemetry.sdk import trace +from opentelemetry.sdk.trace import Resource from opentelemetry.trace.status import Status, StatusCanonicalCode @@ -199,6 +200,7 @@ def test_translate_to_jaeger(self): otel_spans[0].set_attribute("key_bool", False) otel_spans[0].set_attribute("key_string", "hello_world") otel_spans[0].set_attribute("key_float", 111.22) + otel_spans[0].resource = Resource(labels={ "key_resource": "some_resource" }) otel_spans[0].set_status( Status(StatusCanonicalCode.UNKNOWN, "Example description") ) @@ -237,6 +239,11 @@ def test_translate_to_jaeger(self): vType=jaeger.TagType.DOUBLE, vDouble=111.22, ), + jaeger.Tag( + key="key_resource", + vType=jaeger.TagType.STRING, + vStr="some_resource" + ), jaeger.Tag( key="status.code", vType=jaeger.TagType.LONG, From d756f58ad879ae3926b70acbdeb6c5aaeee9728a Mon Sep 17 00:00:00 2001 From: Nir Hadassi Date: Tue, 5 May 2020 10:28:08 +0300 Subject: [PATCH 2/5] Check for empty resource instead of None --- .../src/opentelemetry/ext/jaeger/__init__.py | 3 ++- ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py | 7 ++++--- .../src/opentelemetry/sdk/resources/__init__.py | 6 +++--- opentelemetry-sdk/tests/metrics/test_metrics.py | 2 +- opentelemetry-sdk/tests/resources/test_resources.py | 6 +++--- opentelemetry-sdk/tests/trace/test_trace.py | 2 +- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/__init__.py b/ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/__init__.py index 91b17260600..4b5b05f8c8e 100644 --- a/ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/__init__.py +++ b/ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/__init__.py @@ -70,6 +70,7 @@ import opentelemetry.trace as trace_api from opentelemetry.ext.jaeger.gen.agent import Agent as agent +from opentelemetry.sdk.resources import EMPTY_RESOURCE from opentelemetry.ext.jaeger.gen.jaeger import Collector as jaeger from opentelemetry.sdk.trace.export import Span, SpanExporter, SpanExportResult from opentelemetry.trace.status import StatusCanonicalCode @@ -197,7 +198,7 @@ def _translate_to_jaeger(spans: Span): parent_id = span.parent.span_id if span.parent else 0 tags = _extract_tags(span.attributes) - if span.resource is not None: + if span.resource is not EMPTY_RESOURCE: tags.extend(_extract_tags(span.resource.labels)) tags.extend( diff --git a/ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py b/ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py index 9b37b5da531..76ba164034c 100644 --- a/ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py +++ b/ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py @@ -35,7 +35,7 @@ def setUp(self): is_remote=False, ) - self._test_span = trace.Span("test_span", context=context) + self._test_span = trace.Span("test_span", context=context, resource=Resource.create_empty()) self._test_span.start() self._test_span.end() @@ -190,9 +190,9 @@ def test_translate_to_jaeger(self): kind=trace_api.SpanKind.CLIENT, ), trace.Span( - name=span_names[1], context=parent_context, parent=None + name=span_names[1], context=parent_context, parent=None, resource=Resource.create_empty() ), - trace.Span(name=span_names[2], context=other_context, parent=None), + trace.Span(name=span_names[2], context=other_context, parent=None, resource=Resource.create_empty()), ] otel_spans[0].start(start_time=start_times[0]) @@ -200,6 +200,7 @@ def test_translate_to_jaeger(self): otel_spans[0].set_attribute("key_bool", False) otel_spans[0].set_attribute("key_string", "hello_world") otel_spans[0].set_attribute("key_float", 111.22) + # otel_spans[0].resource = Resource(labels={ "key_resource": "some_resource" }) otel_spans[0].set_status( Status(StatusCanonicalCode.UNKNOWN, "Example description") diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index 193f2831752..b3be0f55c86 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -25,12 +25,12 @@ def __init__(self, labels: Labels): @staticmethod def create(labels: Labels) -> "Resource": if not labels: - return _EMPTY_RESOURCE + return EMPTY_RESOURCE return Resource(labels) @staticmethod def create_empty() -> "Resource": - return _EMPTY_RESOURCE + return EMPTY_RESOURCE @property def labels(self) -> Labels: @@ -50,4 +50,4 @@ def __eq__(self, other: object) -> bool: return self._labels == other._labels -_EMPTY_RESOURCE = Resource({}) +EMPTY_RESOURCE = Resource({}) diff --git a/opentelemetry-sdk/tests/metrics/test_metrics.py b/opentelemetry-sdk/tests/metrics/test_metrics.py index 32980647055..c99e6c6795a 100644 --- a/opentelemetry-sdk/tests/metrics/test_metrics.py +++ b/opentelemetry-sdk/tests/metrics/test_metrics.py @@ -31,7 +31,7 @@ def test_resource_empty(self): meter_provider = metrics.MeterProvider() meter = meter_provider.get_meter(__name__) # pylint: disable=protected-access - self.assertIs(meter.resource, resources._EMPTY_RESOURCE) + self.assertIs(meter.resource, resources.EMPTY_RESOURCE) class TestMeter(unittest.TestCase): diff --git a/opentelemetry-sdk/tests/resources/test_resources.py b/opentelemetry-sdk/tests/resources/test_resources.py index 959e23f0def..e166ed5e174 100644 --- a/opentelemetry-sdk/tests/resources/test_resources.py +++ b/opentelemetry-sdk/tests/resources/test_resources.py @@ -33,13 +33,13 @@ def test_create(self): self.assertEqual(resource.labels, labels) resource = resources.Resource.create_empty() - self.assertIs(resource, resources._EMPTY_RESOURCE) + self.assertIs(resource, resources.EMPTY_RESOURCE) resource = resources.Resource.create(None) - self.assertIs(resource, resources._EMPTY_RESOURCE) + self.assertIs(resource, resources.EMPTY_RESOURCE) resource = resources.Resource.create({}) - self.assertIs(resource, resources._EMPTY_RESOURCE) + self.assertIs(resource, resources.EMPTY_RESOURCE) def test_resource_merge(self): left = resources.Resource({"service": "ui"}) diff --git a/opentelemetry-sdk/tests/trace/test_trace.py b/opentelemetry-sdk/tests/trace/test_trace.py index 1094f1afb98..8f350fa4e75 100644 --- a/opentelemetry-sdk/tests/trace/test_trace.py +++ b/opentelemetry-sdk/tests/trace/test_trace.py @@ -388,7 +388,7 @@ def test_default_span_resource(self): tracer = tracer_provider.get_tracer(__name__) span = tracer.start_span("root") # pylint: disable=protected-access - self.assertIs(span.resource, resources._EMPTY_RESOURCE) + self.assertIs(span.resource, resources.EMPTY_RESOURCE) def test_span_context_remote_flag(self): tracer = new_tracer() From 82e103a6d4e5c908bc46689c560bf68633480ddd Mon Sep 17 00:00:00 2001 From: Nir Hadassi Date: Tue, 5 May 2020 16:07:45 +0300 Subject: [PATCH 3/5] linting --- .../tests/test_jaeger_exporter.py | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py b/ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py index 76ba164034c..8c62795e90d 100644 --- a/ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py +++ b/ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py @@ -35,7 +35,9 @@ def setUp(self): is_remote=False, ) - self._test_span = trace.Span("test_span", context=context, resource=Resource.create_empty()) + self._test_span = trace.Span( + "test_span", context=context, resource=Resource.create_empty() + ) self._test_span.start() self._test_span.end() @@ -190,9 +192,17 @@ def test_translate_to_jaeger(self): kind=trace_api.SpanKind.CLIENT, ), trace.Span( - name=span_names[1], context=parent_context, parent=None, resource=Resource.create_empty() + name=span_names[1], + context=parent_context, + parent=None, + resource=Resource.create_empty(), + ), + trace.Span( + name=span_names[2], + context=other_context, + parent=None, + resource=Resource.create_empty(), ), - trace.Span(name=span_names[2], context=other_context, parent=None, resource=Resource.create_empty()), ] otel_spans[0].start(start_time=start_times[0]) @@ -200,8 +210,9 @@ def test_translate_to_jaeger(self): otel_spans[0].set_attribute("key_bool", False) otel_spans[0].set_attribute("key_string", "hello_world") otel_spans[0].set_attribute("key_float", 111.22) - # - otel_spans[0].resource = Resource(labels={ "key_resource": "some_resource" }) + otel_spans[0].resource = Resource( + labels={"key_resource": "some_resource"} + ) otel_spans[0].set_status( Status(StatusCanonicalCode.UNKNOWN, "Example description") ) @@ -243,7 +254,7 @@ def test_translate_to_jaeger(self): jaeger.Tag( key="key_resource", vType=jaeger.TagType.STRING, - vStr="some_resource" + vStr="some_resource", ), jaeger.Tag( key="status.code", From 12da24a870ef4e6149020e9f43824279c72a2dfd Mon Sep 17 00:00:00 2001 From: Nir Hadassi Date: Tue, 5 May 2020 17:26:23 +0300 Subject: [PATCH 4/5] linting --- .../src/opentelemetry/ext/jaeger/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/__init__.py b/ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/__init__.py index 4b5b05f8c8e..45b6ecf1ca7 100644 --- a/ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/__init__.py +++ b/ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/__init__.py @@ -70,8 +70,8 @@ import opentelemetry.trace as trace_api from opentelemetry.ext.jaeger.gen.agent import Agent as agent -from opentelemetry.sdk.resources import EMPTY_RESOURCE from opentelemetry.ext.jaeger.gen.jaeger import Collector as jaeger +from opentelemetry.sdk.resources import EMPTY_RESOURCE from opentelemetry.sdk.trace.export import Span, SpanExporter, SpanExportResult from opentelemetry.trace.status import StatusCanonicalCode From 3127e73c57e187517c2787962260f3aa0c517875 Mon Sep 17 00:00:00 2001 From: nirsky Date: Mon, 18 May 2020 17:22:09 +0300 Subject: [PATCH 5/5] Revert exposing resource --- .../src/opentelemetry/ext/jaeger/__init__.py | 3 +-- .../tests/test_jaeger_exporter.py | 16 +++------------- .../src/opentelemetry/sdk/resources/__init__.py | 6 +++--- opentelemetry-sdk/tests/metrics/test_metrics.py | 2 +- .../tests/resources/test_resources.py | 6 +++--- opentelemetry-sdk/tests/trace/test_trace.py | 2 +- 6 files changed, 12 insertions(+), 23 deletions(-) diff --git a/ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/__init__.py b/ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/__init__.py index 45b6ecf1ca7..852f94f610d 100644 --- a/ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/__init__.py +++ b/ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/__init__.py @@ -71,7 +71,6 @@ import opentelemetry.trace as trace_api from opentelemetry.ext.jaeger.gen.agent import Agent as agent from opentelemetry.ext.jaeger.gen.jaeger import Collector as jaeger -from opentelemetry.sdk.resources import EMPTY_RESOURCE from opentelemetry.sdk.trace.export import Span, SpanExporter, SpanExportResult from opentelemetry.trace.status import StatusCanonicalCode @@ -198,7 +197,7 @@ def _translate_to_jaeger(spans: Span): parent_id = span.parent.span_id if span.parent else 0 tags = _extract_tags(span.attributes) - if span.resource is not EMPTY_RESOURCE: + if span.resource: tags.extend(_extract_tags(span.resource.labels)) tags.extend( diff --git a/ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py b/ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py index 8c62795e90d..904ca9bff14 100644 --- a/ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py +++ b/ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py @@ -35,9 +35,7 @@ def setUp(self): is_remote=False, ) - self._test_span = trace.Span( - "test_span", context=context, resource=Resource.create_empty() - ) + self._test_span = trace.Span("test_span", context=context) self._test_span.start() self._test_span.end() @@ -192,17 +190,9 @@ def test_translate_to_jaeger(self): kind=trace_api.SpanKind.CLIENT, ), trace.Span( - name=span_names[1], - context=parent_context, - parent=None, - resource=Resource.create_empty(), - ), - trace.Span( - name=span_names[2], - context=other_context, - parent=None, - resource=Resource.create_empty(), + name=span_names[1], context=parent_context, parent=None ), + trace.Span(name=span_names[2], context=other_context, parent=None), ] otel_spans[0].start(start_time=start_times[0]) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index b3be0f55c86..193f2831752 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -25,12 +25,12 @@ def __init__(self, labels: Labels): @staticmethod def create(labels: Labels) -> "Resource": if not labels: - return EMPTY_RESOURCE + return _EMPTY_RESOURCE return Resource(labels) @staticmethod def create_empty() -> "Resource": - return EMPTY_RESOURCE + return _EMPTY_RESOURCE @property def labels(self) -> Labels: @@ -50,4 +50,4 @@ def __eq__(self, other: object) -> bool: return self._labels == other._labels -EMPTY_RESOURCE = Resource({}) +_EMPTY_RESOURCE = Resource({}) diff --git a/opentelemetry-sdk/tests/metrics/test_metrics.py b/opentelemetry-sdk/tests/metrics/test_metrics.py index c99e6c6795a..32980647055 100644 --- a/opentelemetry-sdk/tests/metrics/test_metrics.py +++ b/opentelemetry-sdk/tests/metrics/test_metrics.py @@ -31,7 +31,7 @@ def test_resource_empty(self): meter_provider = metrics.MeterProvider() meter = meter_provider.get_meter(__name__) # pylint: disable=protected-access - self.assertIs(meter.resource, resources.EMPTY_RESOURCE) + self.assertIs(meter.resource, resources._EMPTY_RESOURCE) class TestMeter(unittest.TestCase): diff --git a/opentelemetry-sdk/tests/resources/test_resources.py b/opentelemetry-sdk/tests/resources/test_resources.py index e166ed5e174..959e23f0def 100644 --- a/opentelemetry-sdk/tests/resources/test_resources.py +++ b/opentelemetry-sdk/tests/resources/test_resources.py @@ -33,13 +33,13 @@ def test_create(self): self.assertEqual(resource.labels, labels) resource = resources.Resource.create_empty() - self.assertIs(resource, resources.EMPTY_RESOURCE) + self.assertIs(resource, resources._EMPTY_RESOURCE) resource = resources.Resource.create(None) - self.assertIs(resource, resources.EMPTY_RESOURCE) + self.assertIs(resource, resources._EMPTY_RESOURCE) resource = resources.Resource.create({}) - self.assertIs(resource, resources.EMPTY_RESOURCE) + self.assertIs(resource, resources._EMPTY_RESOURCE) def test_resource_merge(self): left = resources.Resource({"service": "ui"}) diff --git a/opentelemetry-sdk/tests/trace/test_trace.py b/opentelemetry-sdk/tests/trace/test_trace.py index 3af391b54d2..e468652ec00 100644 --- a/opentelemetry-sdk/tests/trace/test_trace.py +++ b/opentelemetry-sdk/tests/trace/test_trace.py @@ -388,7 +388,7 @@ def test_default_span_resource(self): tracer = tracer_provider.get_tracer(__name__) span = tracer.start_span("root") # pylint: disable=protected-access - self.assertIs(span.resource, resources.EMPTY_RESOURCE) + self.assertIs(span.resource, resources._EMPTY_RESOURCE) def test_span_context_remote_flag(self): tracer = new_tracer()