Skip to content

Commit 057900f

Browse files
gugahoaGustavo Aguiar
authored andcommitted
add baggage span processor
1 parent fba8181 commit 057900f

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

processors/opentelemetry_baggage_processor/src/otel_baggage_processor.erl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
-behaviour(otel_span_processor).
44

5+
-include_lib("opentelemetry/include/otel_span.hrl").
56
-include_lib("opentelemetry_api/include/opentelemetry.hrl").
67

78
-export([
@@ -12,8 +13,10 @@
1213
-type processor_config() :: term().
1314

1415
-spec on_start(otel_ctx:t(), opentelemetry:span(), processor_config()) -> opentelemetry:span().
15-
on_start(_Ctx, Span, _Config) ->
16-
Span.
16+
on_start(Ctx, Span, _Config) ->
17+
Baggage = otel_baggage:get_all(Ctx),
18+
Attributes = maps:map(fun(_K, {Value, _Metadata}) -> Value end, Baggage),
19+
add_attributes(Span, Attributes).
1720

1821
-spec on_end(opentelemetry:span(), processor_config()) ->
1922
true | dropped | {error, invalid_span} | {error, no_export_buffer}.
@@ -23,3 +26,7 @@ on_end(_Span, _Config) ->
2326
-spec force_flush(processor_config()) -> ok | {error, term()}.
2427
force_flush(_Config) ->
2528
ok.
29+
30+
-spec add_attributes(opentelemetry:span(), opentelemetry:attributes_map()) -> opentelemetry:span().
31+
add_attributes(Span = #span{attributes=SpanAttributes}, AttributesMap) ->
32+
Span#span{attributes=otel_attributes:set(AttributesMap, SpanAttributes)}.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
-module(otel_baggage_processor_SUITE).
2+
3+
-compile(export_all).
4+
5+
-include_lib("common_test/include/ct.hrl").
6+
-include_lib("stdlib/include/assert.hrl").
7+
-include_lib("opentelemetry/include/otel_span.hrl").
8+
-include_lib("opentelemetry_api/include/otel_tracer.hrl").
9+
10+
all() -> [
11+
baggage_handling
12+
].
13+
14+
init_per_suite(Config) ->
15+
ok = application:load(opentelemetry_baggage_processor),
16+
ok = application:load(opentelemetry),
17+
application:set_env(opentelemetry, processors, [{otel_baggage_processor, #{}}, {otel_batch_processor, #{scheduled_delay_ms => 1}}]),
18+
Config.
19+
20+
end_per_suite(_Config) ->
21+
ok = application:unload(opentelemetry),
22+
ok.
23+
24+
init_per_testcase(_, Config) ->
25+
{ok, _} = application:ensure_all_started(opentelemetry_baggage_processor),
26+
otel_batch_processor:set_exporter(otel_exporter_pid, self()),
27+
Config.
28+
29+
end_per_testcase(_, Config) ->
30+
application:stop(opentelemetry),
31+
Config.
32+
33+
baggage_handling(_Config) ->
34+
SpanCtx1 = ?start_span(<<"span-1">>),
35+
?set_current_span(SpanCtx1),
36+
Ctx = otel_ctx:get_current(),
37+
Ctx2 = otel_baggage:set(Ctx, <<"key">>, <<"value">>),
38+
_Token = otel_ctx:attach(Ctx2),
39+
SpanCtx2 = ?start_span(<<"span-2">>),
40+
?end_span(),
41+
?set_current_span(SpanCtx2),
42+
?end_span(),
43+
Attributes = get_span_attributes(<<"span-1">>),
44+
?assertEqual(Attributes, #{}),
45+
Attributes2 = get_span_attributes(<<"span-2">>),
46+
?assertEqual(Attributes2, #{<<"key">> => <<"value">>}),
47+
ok.
48+
49+
get_span_attributes(Name) ->
50+
receive
51+
{span, #span{name=Name, attributes=Attributes}} ->
52+
otel_attributes:map(Attributes)
53+
after
54+
100 ->
55+
error(timeout)
56+
end.

0 commit comments

Comments
 (0)