Skip to content

Commit 2fa1eda

Browse files
authored
Merge pull request #346 from tsloughter/strict-trace-context
Tracestate: Keep last value for duplicate key
2 parents 3e3281c + 4cc68b0 commit 2fa1eda

File tree

6 files changed

+30
-49
lines changed

6 files changed

+30
-49
lines changed

.github/workflows/w3c_interop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ jobs:
4747
erl -noinput -pa ./_build/interop/extras/interop/ $(rebar3 as interop path) -s w3c_trace_context_interop &
4848
sleep 1
4949
cd trace-context/test
50-
STRICT_LEVEL=1 python3 test.py http://127.0.0.1:5000/test
50+
STRICT_LEVEL=2 python3 test.py http://127.0.0.1:5000/test
5151
shell: bash

apps/opentelemetry/src/otel_tracer.hrl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
%% The `tracer' contains the modules to use for span creation,
33
%% the sampler to use at creation time and the processors to
44
%% run `OnStart' and `OnEnd'.
5-
%% See OpenTelemetry Tracer spec: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-tracing.md#tracer-creation
5+
%% See OpenTelemetry Tracer spec: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.8.0/specification/trace/sdk.md#tracer-creation
66
-record(tracer,
77
{
88
module :: module(),

apps/opentelemetry_api/lib/open_telemetry.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ defmodule OpenTelemetry do
134134
defdelegate timestamp_to_nano(timestamp), to: :opentelemetry
135135

136136
@doc """
137-
Convert a native monotonic timestamp to POSIX time of any `:erlang.time_unit/0`.
137+
Convert a native monotonic timestamp to POSIX time of any `t::erlang.time_unit/0`.
138138
Meaning the time since Epoch. Epoch is defined to be 00:00:00 UTC, 1970-01-01.
139139
"""
140140
@spec convert_timestamp(integer(), :erlang.time_unit()) :: integer()

apps/opentelemetry_api/src/otel_propagator_trace_context.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,13 @@ parse_pairs([Pair | Rest], Acc) ->
170170
case split(string:trim(Pair)) of
171171
{K, V} ->
172172
case re:run(K, ?KEY_MP) =/= nomatch
173-
andalso not lists:keymember(K, 1, Acc)
174173
andalso re:run(V, ?VALUE_MP) =/= nomatch
175174
of
176175
false ->
177176
[];
178177
true ->
179-
parse_pairs(Rest, Acc ++ [{K, V}])
178+
%% replace existing key value or append to the end of the list
179+
parse_pairs(Rest, lists:keystore(K, 1, Acc, {K, V}))
180180
end;
181181
undefined ->
182182
[]

apps/opentelemetry_api/test/otel_propagators_SUITE.erl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ rewrite(_Config) ->
6060
<<"00-10000000000000000000000000000000-1000000000000000-00">>},
6161
{<<"tracestate">>,<<"a=b">>}])),
6262

63-
64-
otel_propagator_text_map:extract([{<<"traceparent">>,
65-
<<"00-10000000000000000000000000000000-1000000000000000-00">>}]),
63+
otel_propagator_text_map:extract([{<<"tracestate">>,<<"a=j,c=d">>},
64+
{<<"traceparent">>,
65+
<<"00-10000000000000000000000000000000-1000000000000000-00">>},
66+
{<<"tracestate">>,<<"a=b,c=d">>}]),
6667
?assertEqual(RecordingSpanCtx#span_ctx{is_recording=false,
68+
tracestate=[{<<"a">>,<<"b">>},{<<"c">>,<<"d">>}],
6769
is_remote=true}, otel_tracer:current_span_ctx()),
6870

69-
% should not fail on empty Carrier
71+
%% should not fail on empty Carrier
7072
?assertMatch(Ctx,
7173
otel_propagator_trace_context:extract(Ctx, [],
7274
fun otel_propagator_text_map:default_carrier_keys/1,

apps/opentelemetry_exporter/src/opentelemetry_exporter.erl

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
%% <li>`OTEL_EXPORTER_OTLP_TRACES_HEADERS': Additional headers to add to only trace export requests.</li>
6060
%% <li>`OTEL_EXPORTER_OTLP_PROTOCOL': The transport protocol to use, supported values: `grpc' and `http_protobuf'. Defaults to `http_protobuf'.</li>
6161
%% <li>`OTEL_EXPORTER_OTLP_TRACES_PROTOCOL': The transport protocol to use for exporting traces, supported values: `grpc' and `http_protobuf'. Defaults to `http_protobuf'.</li>
62-
%% <li>`OTEL_EXPORTER_OTLP_COMPRESSION': Compression to use, supported value: gzip. Defaults to no compression./li>
62+
%% <li>`OTEL_EXPORTER_OTLP_COMPRESSION': Compression to use, supported value: gzip. Defaults to no compression.</li>
6363
%% <li>`OTEL_EXPORTER_OTLP_TRACES_COMPRESSION': Compression to use when exporting traces, supported value: gzip. Defaults to no compression.</li>
6464
%% </ul>
6565
%%
@@ -79,14 +79,6 @@
7979
merge_with_environment/1]).
8080
-endif.
8181

82-
%% dialyzer will warn about having catch all clauses on these
83-
%% functions because previous clauses cover all cases. But
84-
%% we want to not crash if something incorrect is passed
85-
%% through so we ignore those warnings.
86-
-dialyzer({nowarn_function, to_events/2}).
87-
-dialyzer({nowarn_function, to_links/2}).
88-
-dialyzer({nowarn_function, to_tracestate_string/1}).
89-
9082
-include_lib("kernel/include/logger.hrl").
9183
-include_lib("opentelemetry_api/include/opentelemetry.hrl").
9284
-include_lib("opentelemetry/include/otel_span.hrl").
@@ -576,41 +568,28 @@ to_status(_) ->
576568

577569
-spec to_events([#event{}]) -> [opentelemetry_exporter_trace_service_pb:event()].
578570
to_events(Events) ->
579-
to_events(Events, []).
580-
581-
to_events([], Acc)->
582-
Acc;
583-
to_events([#event{system_time_nano=Timestamp,
584-
name=Name,
585-
attributes=Attributes} | Rest], Acc) ->
586-
to_events(Rest, [#{time_unix_nano => to_unixnano(Timestamp),
587-
name => to_binary(Name),
588-
attributes => to_attributes(otel_attributes:map(Attributes))} | Acc]);
589-
to_events([_ | Rest], Acc) ->
590-
to_events(Rest, Acc).
571+
[#{time_unix_nano => to_unixnano(Timestamp),
572+
name => to_binary(Name),
573+
attributes => to_attributes(otel_attributes:map(Attributes))}
574+
|| #event{system_time_nano=Timestamp,
575+
name=Name,
576+
attributes=Attributes} <- Events].
591577

592578
-spec to_links([#link{}]) -> [opentelemetry_exporter_trace_service_pb:link()].
593579
to_links(Links) ->
594-
to_links(Links, []).
580+
[#{trace_id => <<TraceId:128>>,
581+
span_id => <<SpanId:64>>,
582+
trace_state => to_tracestate_string(TraceState),
583+
attributes => to_attributes(otel_attributes:map(Attributes)),
584+
dropped_attributes_count => 0} || #link{trace_id=TraceId,
585+
span_id=SpanId,
586+
attributes=Attributes,
587+
tracestate=TraceState} <- Links].
588+
589+
-spec to_tracestate_string(opentelemetry:tracestate()) -> string().
590+
to_tracestate_string(List) ->
591+
lists:join($,, [[Key, $=, Value] || {Key, Value} <- List]).
595592

596-
to_links([], Acc)->
597-
Acc;
598-
to_links([#link{trace_id=TraceId,
599-
span_id=SpanId,
600-
attributes=Attributes,
601-
tracestate=TraceState} | Rest], Acc) ->
602-
to_links(Rest, [#{trace_id => <<TraceId:128>>,
603-
span_id => <<SpanId:64>>,
604-
trace_state => to_tracestate_string(TraceState),
605-
attributes => to_attributes(otel_attributes:map(Attributes)),
606-
dropped_attributes_count => 0} | Acc]);
607-
to_links([_ | Rest], Acc) ->
608-
to_links(Rest, Acc).
609-
610-
to_tracestate_string(List) when is_list(List) ->
611-
lists:join($,, [[Key, $=, Value] || {Key, Value} <- List]);
612-
to_tracestate_string(_) ->
613-
[].
614593

615594
-spec to_otlp_kind(atom()) -> opentelemetry_exporter_trace_service_pb:'span.SpanKind'().
616595
to_otlp_kind(?SPAN_KIND_INTERNAL) ->

0 commit comments

Comments
 (0)