Skip to content

Commit 78911eb

Browse files
authored
Merge branch 'main' into spec-compliance
2 parents b94ff50 + b5764e6 commit 78911eb

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

apps/opentelemetry_exporter/src/opentelemetry_exporter.erl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,20 @@ headers_to_grpc_metadata(Headers) ->
371371

372372
%% make all headers into list strings
373373
headers(List) when is_list(List) ->
374-
[{unicode:characters_to_list(X), unicode:characters_to_list(Y)} || {X, Y} <- List];
374+
Headers =[{unicode:characters_to_list(X), unicode:characters_to_list(Y)} || {X, Y} <- List],
375+
add_user_agent(Headers);
375376
headers(_) ->
376-
[].
377+
add_user_agent([]).
378+
379+
add_user_agent(Headers) ->
380+
case lists:search(fun({Header, _}) -> string:to_lower(Header) == "user-agent" end, Headers) of
381+
{value, _} -> Headers;
382+
false -> [{"User-Agent", user_agent()} | Headers]
383+
end.
384+
385+
user_agent() ->
386+
{ok, ExporterVsn} = application:get_key(opentelemetry_exporter, vsn),
387+
lists:flatten(io_lib:format("OTel-OTLP-Exporter-erlang/~s", [ExporterVsn])).
377388

378389
-spec endpoints([endpoint()], list() | undefined) -> [endpoint_map()].
379390
endpoints(List, DefaultSSLOpts) when is_list(List) ->

apps/opentelemetry_exporter/test/opentelemetry_exporter_SUITE.erl

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ groups() ->
1818
[{functional, [], [configuration, span_round_trip, ets_instrumentation_info]},
1919
{grpc, [], [verify_export, verify_metrics_export]},
2020
{grpc_gzip, [], [verify_export]},
21-
{http_protobuf, [], [verify_export]},
21+
{http_protobuf, [], [verify_export, user_agent]},
2222
{http_protobuf_gzip, [], [verify_export]}].
2323

2424
init_per_suite(Config) ->
@@ -520,3 +520,45 @@ verify_export(Config) ->
520520
?assertMatch(ok, opentelemetry_exporter:export(traces, Tid, Resource, State)),
521521

522522
ok.
523+
524+
user_agent(Config) ->
525+
Protocol = ?config(protocol, Config),
526+
Compression = ?config(compression, Config),
527+
Port = 4318,
528+
529+
{ok, State} = opentelemetry_exporter:init(#{protocol => Protocol,
530+
compression => Compression,
531+
endpoints => [{http, "localhost", Port, []}]}),
532+
533+
Tid = ets:new(span_tab, [duplicate_bag, {keypos, #span.instrumentation_scope}]),
534+
535+
TraceId = otel_id_generator:generate_trace_id(),
536+
SpanId = otel_id_generator:generate_span_id(),
537+
538+
ParentSpan =
539+
#span{name = <<"span-1">>,
540+
trace_id = TraceId,
541+
span_id = SpanId,
542+
kind = ?SPAN_KIND_CLIENT,
543+
start_time = opentelemetry:timestamp(),
544+
end_time = opentelemetry:timestamp(),
545+
status = #status{code=?OTEL_STATUS_UNSET, message = <<"hello I'm unset">>},
546+
links = otel_links:new([], 128, 128, 128),
547+
events = otel_events:new(128, 128, 128),
548+
instrumentation_scope = #instrumentation_scope{name = <<"tracer-1">>,
549+
version = <<"0.0.1">>},
550+
attributes = otel_attributes:new([{<<"attr-2">>, <<"value-2">>}], 128, 128)},
551+
true = ets:insert(Tid, ParentSpan),
552+
Resource = otel_resource_env_var:get_resource([]),
553+
554+
meck:new(httpc),
555+
meck:expect(httpc, request, fun(post, {_, Headers, "application/x-protobuf", _}, _, _, _) ->
556+
{_, UserAgent} = lists:keyfind("User-Agent", 1, Headers),
557+
{ok, ExporterVsn} = application:get_key(opentelemetry_exporter, vsn),
558+
ExpectedUserAgent = lists:flatten(io_lib:format("OTel-OTLP-Exporter-erlang/~s", [ExporterVsn])),
559+
?assertEqual(ExpectedUserAgent, UserAgent),
560+
{ok, {{"1.1", 200, ""}, [], <<>>}}
561+
end),
562+
?assertMatch(ok, opentelemetry_exporter:export(traces, Tid, Resource, State)),
563+
?assert(meck:validate(httpc)),
564+
meck:unload(httpc).

rebar.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232

3333
{profiles,
3434
[{test, [{erl_opts, [nowarn_export_all]},
35-
{ct_opts, [{ct_hooks, [cth_surefire]}]}]},
35+
{ct_opts, [{ct_hooks, [cth_surefire]}]},
36+
{deps, [{meck, ">= 0.0.0"}]}]},
3637

3738
{interop, [{deps, [jsone]},
3839
{extra_src_dirs, ["interop"]}]},

0 commit comments

Comments
 (0)