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

Expose trace IDs in hex format #270

Merged
merged 2 commits into from
Sep 21, 2021
Merged

Conversation

indrekj
Copy link
Contributor

@indrekj indrekj commented Sep 1, 2021

According to the [spec][1]

The API MUST allow retrieving the TraceId and SpanId in the following forms:

  • Hex - returns the lowercase hex-encoded TraceId (result MUST be a 32-hex-character lowercase string) or SpanId (result MUST be a 16-hex-character lowercase string).
  • Binary - returns the binary representation of the TraceId (result MUST be a 16-byte array) or SpanId (result MUST be an 8-byte array).

The API SHOULD NOT expose details about how they are internally stored.

This commit takes care of the Hex part.

[1]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#retrieving-the-traceid-and-spanid)

Copy link
Member

@tsloughter tsloughter left a comment

Choose a reason for hiding this comment

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

Since the spec requires the trace id to be 32 characters and the span id to be 16 characters the integer_to_binary function can't be used -- or at least can't be used without also adding padding to the beginning of the string. My suggested change is using io_lib:format to get a string of the correct length that is in hex.

@@ -69,6 +71,14 @@ trace_id(#span_ctx{trace_id=TraceId }) ->
span_id(#span_ctx{span_id=SpanId }) ->
SpanId.

-spec hex_trace_id(opentelemetry:span_ctx()) -> opentelemetry:hex_trace_id().
hex_trace_id(#span_ctx{trace_id=TraceId }) ->
string:lowercase(integer_to_binary(TraceId, 16)).
Copy link
Member

@tsloughter tsloughter Sep 1, 2021

Choose a reason for hiding this comment

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

Suggested change
string:lowercase(integer_to_binary(TraceId, 16)).
iolist_to_binary(io_lib:format("~32.16.0b", [TraceId])).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also added a test for that.


-spec hex_span_id(opentelemetry:span_ctx()) -> opentelemetry:hex_span_id().
hex_span_id(#span_ctx{span_id=SpanId }) ->
string:lowercase(integer_to_binary(SpanId, 16)).
Copy link
Member

@tsloughter tsloughter Sep 1, 2021

Choose a reason for hiding this comment

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

Suggested change
string:lowercase(integer_to_binary(SpanId, 16)).
iolist_to_binary(io_lib:format("~16.16.0b", [SpanId])).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

apps/opentelemetry_api/src/otel_span.erl Outdated Show resolved Hide resolved
apps/opentelemetry_api/src/otel_span.erl Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Sep 1, 2021

Codecov Report

Merging #270 (61dd8bf) into main (17d9c70) will increase coverage by 0.08%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #270      +/-   ##
==========================================
+ Coverage   36.42%   36.50%   +0.08%     
==========================================
  Files          41       41              
  Lines        3165     3169       +4     
==========================================
+ Hits         1153     1157       +4     
  Misses       2012     2012              
Flag Coverage Δ
api 63.38% <100.00%> (+0.31%) ⬆️
elixir 16.70% <100.00%> (+0.71%) ⬆️
erlang 36.46% <100.00%> (+0.04%) ⬆️
exporter 19.60% <ø> (ø)
sdk 79.27% <ø> (ø)

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

Impacted Files Coverage Δ
apps/opentelemetry_api/src/opentelemetry.erl 74.19% <ø> (ø)
apps/opentelemetry_api/lib/open_telemetry/span.ex 27.77% <100.00%> (+9.02%) ⬆️
apps/opentelemetry_api/src/otel_span.erl 75.86% <100.00%> (+1.78%) ⬆️

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 17d9c70...61dd8bf. Read the comment docs.

@tsloughter
Copy link
Member

Thanks!

My initial thought was an api like otel_span:trace_id(Span, Format) might be nicer -- where trace_id/1 would call trace_id(Span, integer)... but maybe not since we are unlikely to have to expose more than hex and binary formats.

According to the [spec][1]
> The API MUST allow retrieving the TraceId and SpanId in the following forms:
>
>    Hex - returns the lowercase hex-encoded TraceId (result MUST be a 32-hex-character lowercase string) or SpanId (result MUST be a 16-hex-character lowercase string).
>    Binary - returns the binary representation of the TraceId (result MUST be a 16-byte array) or SpanId (result MUST be an 8-byte array).
>
> The API SHOULD NOT expose details about how they are internally stored.

This commit takes care of the Hex part.

[1]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#retrieving-the-traceid-and-spanid)
@indrekj
Copy link
Contributor Author

indrekj commented Sep 1, 2021

but maybe not since we are unlikely to have to expose more than hex and binary formats

I don't have a strong opinion. I followed how it was done in ruby, but it seems that different language implementations do it differently (e.g. python, java).

@tsloughter tsloughter merged commit f0cf709 into open-telemetry:main Sep 21, 2021
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.

2 participants