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

url encode baggage keys and values #233

Merged
merged 11 commits into from
Apr 4, 2021

Conversation

tsloughter
Copy link
Member

Fix for #224 and #225

This may not be completely ready yet but opening now for review.

Possible changes needed:

  • Are keys supposed to be percent encoded
  • How to handle metadata? It may just be part of the value and thus nothing to do (except that I don't think the ? and ; should be percent encoded if that is the case so even if it is just part of the value it may need updates to act properly.)

Baggage spec: https://w3c.github.io/baggage/

@codecov
Copy link

codecov bot commented Mar 24, 2021

Codecov Report

Merging #233 (84615da) into main (3463415) will increase coverage by 0.80%.
The diff coverage is 63.80%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #233      +/-   ##
==========================================
+ Coverage   34.79%   35.59%   +0.80%     
==========================================
  Files          37       37              
  Lines        3052     3149      +97     
==========================================
+ Hits         1062     1121      +59     
- Misses       1990     2028      +38     
Flag Coverage Δ
api 61.48% <63.80%> (+0.37%) ⬆️
elixir 16.19% <9.52%> (-2.15%) ⬇️
erlang 35.58% <67.00%> (+0.93%) ⬆️
exporter 19.48% <ø> (ø)
sdk 77.14% <ø> (-0.37%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...ps/opentelemetry_api/lib/open_telemetry/baggage.ex 33.33% <0.00%> (-26.67%) ⬇️
apps/opentelemetry_api/src/otel_ctx.erl 72.41% <57.14%> (-6.76%) ⬇️
apps/opentelemetry_api/src/otel_baggage.erl 70.43% <67.74%> (-11.05%) ⬇️
apps/opentelemetry/src/otel_batch_processor.erl 66.32% <0.00%> (-2.05%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3463415...84615da. Read the comment docs.

Copy link
Contributor

@garthk garthk left a comment

Choose a reason for hiding this comment

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

Converting chardata baggage to binaries only in otel_propagator:text_map_inject/1 leaves us open to confusion if people mix binaries and chardata in their arguments to otel_baggage:set/1 and set/2 and then call get_all/0. Are you up for converting in the set functions? Failing that, erroring with badarg on chardata?

I'm blocked on waiting for Erlang 23.2.7 to install so I can continue testing, but next I'll be checking for the whitespace decoding.

Comment on lines 217 to 218
otel_baggage:set(<<"key-1">>, <<"value=1">>),
otel_baggage:set(<<"key-2">>, <<"value-2">>),
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're still accepting UTF-8 chardata as arguments, perhaps we could reflect that in the tests?

Suggested change
otel_baggage:set(<<"key-1">>, <<"value=1">>),
otel_baggage:set(<<"key-2">>, <<"value-2">>),
%% We prefer UTF-8 binary keys and values...
otel_baggage:set(<<"key-1">>, <<"value=1">>),
%% ... but still support UTF-8 character lists:
otel_baggage:set("key-2", "value-2"),

Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately the clean way of declaring the keys literally would be <<"key-1"/utf8>> since otherwise byte values > 255 get truncated

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, the smiley face [128512] for example would need encoding… as much as I'd prefer to avoid crashing out, it's more straightforward to throw a bad match than stack up on “friendly” special case handling.

In case I'm missing your point, though, <<"😀"/utf8>> would be a great example for people mining our tests for usage hints. I was surprised to see <<"😀">> == <<0>>.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point @ferd

@garthk yea, can't throw a bad match. Instrumentation is not program logic, we will not crash. There are currently still places we can crash that need to be fixed. You should be able to do all kinds of accidental nonsense and not crash.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair enough. Next-best: either dropping chardata arguments, or quietly converting them to binary.

apps/opentelemetry_api/src/otel_baggage.erl Outdated Show resolved Hide resolved
@garthk
Copy link
Contributor

garthk commented Mar 24, 2021

Whitespace strips OK, but we need to split off the opaque metadata from each value. Using the example from the spec:

:otel_propagator.text_map_extract([{"baggage", "key1=value1;property1;property2, key2 = value2, key3=value3; propertyKey=propertyValue"}]) 
:otel_baggage.get_all()
# result:                 
%{
  "key1" => "value1;property1;property2",
  "key2" => "value2",
  "key3" => "value3; propertyKey=propertyValue"
}
# expected:
%{
  "key1" => "value1",
  "key2" => "value2",
  "key3" => "value3"
}

The spec doesn't say we SHOULD preserve the metadata to pass it along late; let's just drop it?

@tsloughter
Copy link
Member Author

@garthk I added the functions for explicit context passing to baggage to this PR 9276fa5

decode_value(Value) ->
percent_decode(string:trim(unicode:characters_to_binary(Value))).

%% TODO: call `uri_string:percent_decode' and remove this when OTP-23 is
Copy link
Member

Choose a reason for hiding this comment

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

Should we deal with any licensing and copyright here?

@tsloughter
Copy link
Member Author

@garthk if we are going to parse out the metadata I don't see a reason to drop it. But this will mean the value will become a 2-tuple:

-type entry() :: #{key() => {value(), metadata()}.

@garthk
Copy link
Contributor

garthk commented Mar 25, 2021

It'd make for some entertaining API choices. :)

Least surprising there might be set/2 taking "value",{"value", "metadata"}, and {"value", nil} as arguments, and get_all/0 always returning the 2-tuple even if the metadata is absent %{"key" => {"value", nil}} so people don't get fooled into thinking it'll always be a straight binary-binary map. If we're trying to avoid crashes in our code, because instrumentation, may as well also help avoid crashes in the code people write to work with our code.

@tsloughter
Copy link
Member Author

Right. I was thinkingi the same. Except instead of nil it'll be [].

@garthk
Copy link
Contributor

garthk commented Mar 25, 2021

Oh, of course, because metadata is its own list of bare keys and key-value pairs.

@tsloughter
Copy link
Member Author

I've added support for metadata. I'd have made it a separate PR but realized this one still had open issues so added it on, hopefully aside from fixes this is it and I won't let it grow anymore :)

Comment on lines 236 to 237
?assertEqual(#{<<"key-1">> => {<<"value=1">>, []},
<<"key-2">> => {<<"value-2">>, [<<"metadata">>]}}, otel_baggage:get_all()),
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we show the {<<"metadata_key">>,<<"metadata_value">>} form, also?

Comment on lines +219 to +220
%% TODO: should the whole baggage entry be dropped if metadata is bad?
%% drop bad metadata (the `1').
Copy link
Contributor

Choose a reason for hiding this comment

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

Drop the bad metadata value only, I think.

@tsloughter tsloughter requested a review from ferd April 2, 2021 22:28
@tsloughter
Copy link
Member Author

@ferd @garthk anything more that needs to be done before I merge this? (aside from the failing CI of course)

1 similar comment
@tsloughter
Copy link
Member Author

@ferd @garthk anything more that needs to be done before I merge this? (aside from the failing CI of course)

@tsloughter tsloughter force-pushed the baggage-encoding branch 2 times, most recently from b98d946 to 70d1ac4 Compare April 4, 2021 12:49
@tsloughter tsloughter force-pushed the baggage-encoding branch 2 times, most recently from 3f23fb6 to 99d475d Compare April 4, 2021 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants