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

Export span status to Jaeger #367

Merged
merged 9 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from opentelemetry.ext.jaeger.gen.agent import Agent as agent
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

DEFAULT_AGENT_HOST_NAME = "localhost"
DEFAULT_AGENT_PORT = 6831
Expand Down Expand Up @@ -145,6 +146,8 @@ def _translate_to_jaeger(spans: Span):
start_time_us = _nsec_to_usec_round(span.start_time)
duration_us = _nsec_to_usec_round(span.end_time - span.start_time)

status = span.status

parent_id = 0
if isinstance(span.parent, trace_api.Span):
parent_id = span.parent.get_context().span_id
Expand All @@ -153,8 +156,19 @@ def _translate_to_jaeger(spans: Span):

tags = _extract_tags(span.attributes)

# TODO: status is missing:
# https://github.com/open-telemetry/opentelemetry-python/issues/98
if tags is None:
tags = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be easier to change _extract_tags to always return an empty list, rather than checking this here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And in fact we probably should since the only other place where it's used also ends up doing some juggling around this:

fields = []
if event.attributes is not None:
fields = _extract_tags(event.attributes)
fields.append(

Copy link
Contributor Author

@dgzlopes dgzlopes Jan 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! Added the check when span.status wasn't mandatory, but now it makes no sense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mind updating the in _extract_logs_from_span as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if we change _extract_logs_from_span to return an empty list when a span has no logs tests break because they expect logs to be None. I think that it's the same with refs.

# Example output form tests
Span([348 chars]Double=None, vBool=None, vLong=None, vBinary=None)], logs=[])
Span([348 chars]Double=None, vBool=None, vLong=None, vBinary=None)], logs=None)

Maybe I'm misunderstanding something. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant to update the code in _extract_logs_from_span to take advantage of this new empty list from _extract_tags, lines 239 and 240 are no longer needed:

fields = []
if event.attributes is not None:
fields = _extract_tags(event.attributes)
fields.append(

Copy link
Contributor Author

@dgzlopes dgzlopes Jan 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, okay! It should be updated now.


tags.extend(
[
_get_long_tag("status.code", status.canonical_code.value),
_get_string_tag("status.message", status.description),
]
)

# Ensure that if Status.Code is not OK, that we set the "error" tag on the Jaeger span.
if status.canonical_code is not StatusCanonicalCode.OK:
mauriciovasquezbernal marked this conversation as resolved.
Show resolved Hide resolved
tags.append(_get_bool_tag("error", True))

refs = _extract_refs_from_span(span)
logs = _extract_logs_from_span(span)
Expand Down Expand Up @@ -265,6 +279,18 @@ def _convert_attribute_to_tag(key, attr):
return None


def _get_long_tag(key, val):
return jaeger.Tag(key=key, vLong=val, vType=jaeger.TagType.LONG)


def _get_string_tag(key, val):
return jaeger.Tag(key=key, vStr=val, vType=jaeger.TagType.STRING)


def _get_bool_tag(key, val):
return jaeger.Tag(key=key, vBool=val, vType=jaeger.TagType.BOOL)


class AgentClientUDP:
"""Implement a UDP client to agent.

Expand Down
30 changes: 30 additions & 0 deletions ext/opentelemetry-ext-jaeger/tests/test_jaeger_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.trace.status import Status, StatusCanonicalCode


class TestJaegerSpanExporter(unittest.TestCase):
Expand Down Expand Up @@ -155,6 +156,17 @@ def test_translate_to_jaeger(self):
context=other_context, attributes=link_attributes
)

default_status_tags = [
jaeger.Tag(
key="status.code",
vType=jaeger.TagType.LONG,
vLong=StatusCanonicalCode.OK.value,
),
jaeger.Tag(
key="status.message", vType=jaeger.TagType.STRING, vStr=None,
),
]

otel_spans = [
trace.Span(
name=span_names[0],
Expand All @@ -174,6 +186,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].set_status(
Status(StatusCanonicalCode.UNKNOWN, "Example description")
)
otel_spans[0].end(end_time=end_times[0])

otel_spans[1].start(start_time=start_times[1])
Expand Down Expand Up @@ -209,6 +224,19 @@ def test_translate_to_jaeger(self):
vType=jaeger.TagType.DOUBLE,
vDouble=111.22,
),
jaeger.Tag(
key="status.code",
vType=jaeger.TagType.LONG,
vLong=StatusCanonicalCode.UNKNOWN.value,
),
jaeger.Tag(
key="status.message",
vType=jaeger.TagType.STRING,
vStr="Example description",
),
jaeger.Tag(
key="error", vType=jaeger.TagType.BOOL, vBool=True,
),
],
references=[
jaeger.SpanRef(
Expand Down Expand Up @@ -255,6 +283,7 @@ def test_translate_to_jaeger(self):
startTime=start_times[1] // 10 ** 3,
duration=durations[1] // 10 ** 3,
flags=0,
tags=default_status_tags,
),
jaeger.Span(
operationName=span_names[2],
Expand All @@ -265,6 +294,7 @@ def test_translate_to_jaeger(self):
startTime=start_times[2] // 10 ** 3,
duration=durations[2] // 10 ** 3,
flags=0,
tags=default_status_tags,
),
]

Expand Down