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

Fix for byte type attributes crashing spans #775

Merged

Conversation

sethmaxwl
Copy link
Contributor

This pull request addresses the behavior when setting byte type attributes in a span. When a byte type attribute is set, the exporter fails to convert the span to a JSON structure because the byte type is not JSON serializable, and the exporter crashes. This change decodes byte type attributes before adding them to the attributes dictionary.

@sethmaxwl sethmaxwl requested a review from a team as a code owner June 3, 2020 21:16
@aabmass
Copy link
Member

aabmass commented Jun 3, 2020

I'm not super familiar with how the spec works but it seems like bytes is just not allowed? I think this is the relevant section: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/api.md#set-attributes

An Attribute is defined by the following properties:

  • (Required) The attribute key, which MUST be a non-null and non-empty string.
  • (Required) The attribute value, which is either:
    • A primitive type: string, boolean or numeric.
    • An array of primitive type values. The array MUST be homogeneous, i.e. it MUST NOT contain values of different types.

Does anyone know if this list of primitives is exhaustive?

@sethmaxwl this might end up as a protobuf struct which would tell you whether or not bytes is just not allowed. If you can't send the raw bytes, decoding them as a string might not be the best thing to do in case the user passes bytes that aren't encoding a string at all.

@toumorokoshi
Copy link
Member

@aabmass raises a good point here. @sethmaxwl what's your use case / example for needing to consume a bytes object? Definitely room to amend the spec, but might be good to understand why it might make sense to do so.

@sethmaxwl
Copy link
Contributor Author

@toumorokoshi A use case is mentioned in #623 and shows setting b"hi" as an attribute.

@@ -424,6 +424,8 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None:
# Freeze mutable sequences defensively
if isinstance(value, MutableSequence):
value = tuple(value)
if isinstance(value, bytes):
value = value.decode()
Copy link
Member

Choose a reason for hiding this comment

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

this could raise an exception due to mis-assuming the string-serializable encoding of the object.

It'd be great if we could choose an encoding that works universally, or maybe just ignore or skip issues: https://docs.python.org/3/library/codecs.html#codecs.decode

sethmaxwl and others added 7 commits June 4, 2020 20:51
This commit ports the OpenTracing testbed[1] to check that the ot-shim is
working as expected using different frameworks.

Gevent doesn't support context vars yet[2], so those tests are not compatible
with opentelemetry and were not ported.

[1] https://github.com/opentracing/opentracing-python/tree/master/testbed
[2] gevent/gevent#1407

Co-authored-by: Mauricio Vásquez <mauricio@kinvolk.io>
Co-authored-by: alrex <aboten@lightstep.com>
Co-authored-by: Yusuke Tsutsumi <yusuke@tsutsumi.io>
@aabmass
Copy link
Member

aabmass commented Jun 4, 2020

LGTM, would be nice to add a test case too

Copy link
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

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

Change looks good, if you could add a test as well as update the changelog to capture the change, that would be awesome!

@@ -487,6 +487,11 @@ def test_invalid_attribute_values(self):

Copy link
Member

Choose a reason for hiding this comment

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

Would be nice to also add an invalid byte sequence here to assert it doesn't get added

Copy link
Contributor

Choose a reason for hiding this comment

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

Would be good to check that it logs as well. Something like this:

with self.assertLogs(level=WARNING):
self.assertIs(instrumentor.uninstrument(), None)

Copy link
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

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

Nice! Thanks for addressing my comments.

@toumorokoshi
Copy link
Member

@sethmaxwl looks like travis is having issues with giving CI updates. Latest build on this PR is broken due to linting:

https://travis-ci.org/github/open-telemetry/opentelemetry-python/jobs/695110178

Can you fix? and we can force merge if CI isn't updating appopriately.

@sethmaxwl
Copy link
Contributor Author

@sethmaxwl looks like travis is having issues with giving CI updates. Latest build on this PR is broken due to linting:

https://travis-ci.org/github/open-telemetry/opentelemetry-python/jobs/695110178

Can you fix? and we can force merge if CI isn't updating appopriately.

Just pushed a fix.

@codeboten codeboten merged commit b108596 into open-telemetry:master Jun 5, 2020
srikanthccv pushed a commit to srikanthccv/opentelemetry-python that referenced this pull request Nov 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants