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

Update to OpenTelemetry Proto 0.4.0 #889

Merged
merged 9 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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: 2 additions & 0 deletions ext/opentelemetry-ext-otlp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Update span exporter to use OpenTelemetry Proto v0.4.0 ([#872](https://github.com/open-telemetry/opentelemetry-python/pull/889))

## 0.9b0

Released 2020-06-10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
"""OTLP Span Exporter"""

import logging
from collections.abc import Mapping, Sequence
from time import sleep
from typing import Sequence
from typing import Sequence as TypingSequence

from backoff import expo
from google.rpc.error_details_pb2 import RetryInfo
Expand All @@ -34,7 +35,7 @@
from opentelemetry.proto.collector.trace.v1.trace_service_pb2_grpc import (
TraceServiceStub,
)
from opentelemetry.proto.common.v1.common_pb2 import AttributeKeyValue
from opentelemetry.proto.common.v1.common_pb2 import AnyValue, KeyValue
from opentelemetry.proto.resource.v1.resource_pb2 import Resource
from opentelemetry.proto.trace.v1.trace_pb2 import (
InstrumentationLibrarySpans,
Expand All @@ -49,26 +50,31 @@


def _translate_key_values(key, value):
key_value = {"key": key}

if isinstance(value, bool):
key_value["bool_value"] = value
any_value = AnyValue(bool_value=value)

elif isinstance(value, str):
key_value["string_value"] = value
any_value = AnyValue(string_value=value)

elif isinstance(value, int):
key_value["int_value"] = value
any_value = AnyValue(int_value=value)

elif isinstance(value, float):
key_value["double_value"] = value
any_value = AnyValue(double_value=value)

elif isinstance(value, Sequence):
any_value = AnyValue(array_value=value)

elif isinstance(value, Mapping):
any_value = AnyValue(kvlist_value=value)

else:
raise Exception(
"Invalid type {} of value {}".format(type(value), value)
)

return key_value
return KeyValue(key=key, value=any_value)


# pylint: disable=no-member
Expand Down Expand Up @@ -144,7 +150,7 @@ def _translate_attributes(self, sdk_span):

try:
self._collector_span_kwargs["attributes"].append(
AttributeKeyValue(**_translate_key_values(key, value))
_translate_key_values(key, value)
)
except Exception as error: # pylint: disable=broad-except
logger.exception(error)
Expand All @@ -163,9 +169,7 @@ def _translate_events(self, sdk_span):
for key, value in sdk_span_event.attributes.items():
try:
collector_span_event.attributes.append(
AttributeKeyValue(
**_translate_key_values(key, value)
)
_translate_key_values(key, value)
)
# pylint: disable=broad-except
except Exception as error:
Expand All @@ -191,9 +195,7 @@ def _translate_links(self, sdk_span):
for key, value in sdk_span_link.attributes.items():
try:
collector_span_link.attributes.append(
AttributeKeyValue(
**_translate_key_values(key, value)
)
_translate_key_values(key, value)
)
# pylint: disable=broad-except
except Exception as error:
Expand All @@ -211,7 +213,7 @@ def _translate_status(self, sdk_span):
)

def _translate_spans(
self, sdk_spans: Sequence[SDKSpan],
self, sdk_spans: TypingSequence[SDKSpan],
) -> ExportTraceServiceRequest:

sdk_resource_instrumentation_library_spans = {}
Expand Down Expand Up @@ -260,7 +262,7 @@ def _translate_spans(

try:
collector_resource.attributes.append(
AttributeKeyValue(**_translate_key_values(key, value))
_translate_key_values(key, value)
)
except Exception as error: # pylint: disable=broad-except
logger.exception(error)
Expand All @@ -276,7 +278,7 @@ def _translate_spans(

return ExportTraceServiceRequest(resource_spans=resource_spans)

def export(self, spans: Sequence[SDKSpan]) -> SpanExportResult:
def export(self, spans: TypingSequence[SDKSpan]) -> SpanExportResult:
# expo returns a generator that yields delay values which grow
# exponentially. Once delay is greater than max_value, the yielded
# value will remain constant.
Expand Down
46 changes: 31 additions & 15 deletions ext/opentelemetry-ext-otlp/tests/test_otlp_trace_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
TraceServiceServicer,
add_TraceServiceServicer_to_server,
)
from opentelemetry.proto.common.v1.common_pb2 import AttributeKeyValue
from opentelemetry.proto.common.v1.common_pb2 import AnyValue, KeyValue
from opentelemetry.proto.resource.v1.resource_pb2 import (
Resource as CollectorResource,
)
Expand Down Expand Up @@ -185,8 +185,10 @@ def test_translate_spans(self):
ResourceSpans(
resource=CollectorResource(
attributes=[
AttributeKeyValue(key="a", int_value=1),
AttributeKeyValue(key="b", bool_value=False),
KeyValue(key="a", value=AnyValue(int_value=1)),
KeyValue(
key="b", value=AnyValue(bool_value=False)
),
]
),
instrumentation_library_spans=[
Expand All @@ -211,23 +213,31 @@ def test_translate_spans(self):
),
kind=CollectorSpan.SpanKind.INTERNAL,
attributes=[
AttributeKeyValue(
key="a", int_value=1
KeyValue(
key="a",
value=AnyValue(int_value=1),
),
AttributeKeyValue(
key="b", bool_value=True
KeyValue(
key="b",
value=AnyValue(bool_value=True),
),
],
events=[
CollectorSpan.Event(
name="a",
time_unix_nano=1591240820506462784,
attributes=[
AttributeKeyValue(
key="a", int_value=1
KeyValue(
key="a",
value=AnyValue(
int_value=1
),
),
AttributeKeyValue(
key="b", int_value=False
KeyValue(
key="b",
value=AnyValue(
bool_value=False
),
),
],
)
Expand All @@ -240,11 +250,17 @@ def test_translate_spans(self):
),
span_id=int.to_bytes(2, 8, "big"),
attributes=[
AttributeKeyValue(
key="a", int_value=1
KeyValue(
key="a",
value=AnyValue(
int_value=1
),
),
AttributeKeyValue(
key="b", bool_value=False
KeyValue(
ocelotl marked this conversation as resolved.
Show resolved Hide resolved
key="b",
value=AnyValue(
bool_value=False
),
),
],
)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading