Skip to content

Commit 481e613

Browse files
gugahoaGustavo Aguiar
authored andcommitted
add filter and prefix configuration
1 parent 7cc91bc commit 481e613

File tree

3 files changed

+99
-25
lines changed

3 files changed

+99
-25
lines changed
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
defmodule OpentelemetryBaggageProcessor do
2-
@moduledoc """
3-
TODO: Add something here
4-
"""
2+
@external_resource readme = Path.join([__DIR__, "../README.md"])
53

6-
# TODO: Create an add_attribute_to_trace or something like that
4+
@moduledoc readme
5+
|> File.read!()
6+
|> String.split("<!-- MDOC -->")
7+
|> Enum.fetch!(1)
78
end

processors/opentelemetry_baggage_processor/src/otel_baggage_processor.erl

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,61 @@
55
-include_lib("opentelemetry/include/otel_span.hrl").
66
-include_lib("opentelemetry_api/include/opentelemetry.hrl").
77

8-
-export([
9-
on_start/3,
10-
on_end/2,
11-
force_flush/1]).
8+
-export([on_start/3, on_end/2, force_flush/1]).
129

1310
-type processor_config() :: term().
1411

15-
-spec on_start(otel_ctx:t(), opentelemetry:span(), processor_config()) -> opentelemetry:span().
16-
on_start(Ctx, Span, _Config) ->
12+
-spec on_start(otel_ctx:t(), opentelemetry:span(), processor_config()) ->
13+
opentelemetry:span().
14+
on_start(Ctx, Span, Config) ->
1715
Baggage = otel_baggage:get_all(Ctx),
18-
Attributes = maps:map(fun(_K, {Value, _Metadata}) -> Value end, Baggage),
16+
Attributes =
17+
maps:fold(fun(Key, {Value, Metadata}, Attributes) ->
18+
NewKey = add_prefix(Key, Config),
19+
case filter(Metadata, Config) of
20+
false -> Attributes;
21+
true -> [{NewKey, Value}] ++ Attributes
22+
end
23+
end,
24+
[],
25+
Baggage),
1926
add_attributes(Span, Attributes).
2027

2128
-spec on_end(opentelemetry:span(), processor_config()) ->
22-
true | dropped | {error, invalid_span} | {error, no_export_buffer}.
29+
true | dropped | {error, invalid_span} | {error, no_export_buffer}.
2330
on_end(_Span, _Config) ->
2431
true.
2532

2633
-spec force_flush(processor_config()) -> ok | {error, term()}.
2734
force_flush(_Config) ->
2835
ok.
2936

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)}.
37+
-spec add_attributes(opentelemetry:span(), opentelemetry:attributes_map()) ->
38+
opentelemetry:span().
39+
add_attributes(Span = #span{attributes = SpanAttributes}, AttributesMap) ->
40+
Span#span{attributes = otel_attributes:set(AttributesMap, SpanAttributes)}.
41+
42+
-spec filter(otel_baggage:metadata(), map()) -> boolean().
43+
filter(Metadata, #{filter := FilterKey}) ->
44+
case lists:search(fun (Key) when Key == FilterKey ->
45+
true;
46+
(_) ->
47+
false
48+
end,
49+
Metadata) of
50+
false ->
51+
false;
52+
{value, _} ->
53+
true
54+
end;
55+
filter(_Metadata, _Config) ->
56+
true.
57+
58+
-spec add_prefix(opentelemetry:attribute_key(), map()) -> opentelemetry:attribute_key().
59+
add_prefix(Key, #{prefix := Prefix}) when is_binary(Key), is_binary(Prefix) ->
60+
<<Prefix/binary, Key/binary>>;
61+
add_prefix(Key, #{prefix := Prefix}) when is_atom(Key), is_binary(Prefix) ->
62+
Key2 = atom_to_binary(Key),
63+
<<Prefix/binary, Key2/binary>>;
64+
add_prefix(Key, _Config) ->
65+
Key.

processors/opentelemetry_baggage_processor/test/otel_baggage_processor_SUITE.erl

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
-include_lib("opentelemetry/include/otel_span.hrl").
77
-include_lib("opentelemetry_api/include/otel_tracer.hrl").
88

9-
all() -> [
10-
baggage_handling
11-
].
9+
all() ->
10+
[baggage_handling, add_prefix_to_attributes, filter_baggage_attributes].
1211

1312
init_per_suite(Config) ->
1413
ok = application:load(opentelemetry_baggage_processor),
1514
ok = application:load(opentelemetry),
16-
application:set_env(opentelemetry, processors, [{otel_baggage_processor, #{}}, {otel_batch_processor, #{scheduled_delay_ms => 1}}]),
15+
application:set_env(opentelemetry,
16+
processors,
17+
[{otel_baggage_processor, #{}},
18+
{otel_batch_processor, #{scheduled_delay_ms => 1}}]),
1719
Config.
1820

1921
end_per_suite(_Config) ->
@@ -22,20 +24,22 @@ end_per_suite(_Config) ->
2224

2325
init_per_testcase(_, Config) ->
2426
{ok, _} = application:ensure_all_started(opentelemetry_baggage_processor),
25-
otel_batch_processor:set_exporter(otel_exporter_pid, self()),
2627
Config.
2728

2829
end_per_testcase(_, Config) ->
2930
application:stop(opentelemetry),
3031
Config.
3132

3233
baggage_handling(_Config) ->
34+
{ok, _} = application:ensure_all_started(opentelemetry),
35+
otel_batch_processor:set_exporter(otel_exporter_pid, self()),
3336
SpanCtx1 = ?start_span(<<"span-1">>),
3437
?set_current_span(SpanCtx1),
3538
Ctx = otel_ctx:get_current(),
3639
Ctx2 = otel_baggage:set(Ctx, <<"key">>, <<"value">>),
3740
_Token = otel_ctx:attach(Ctx2),
38-
SpanCtx2 = ?start_span(<<"span-2">>, #{attributes => #{<<"existing-attribute">> => true}}),
41+
SpanCtx2 =
42+
?start_span(<<"span-2">>, #{attributes => #{<<"existing-attribute">> => true}}),
3943
?end_span(),
4044
?set_current_span(SpanCtx2),
4145
?end_span(),
@@ -45,11 +49,47 @@ baggage_handling(_Config) ->
4549
?assertEqual(Attributes2, #{<<"key">> => <<"value">>, <<"existing-attribute">> => true}),
4650
ok.
4751

52+
add_prefix_to_attributes(_Config) ->
53+
application:set_env(opentelemetry,
54+
processors,
55+
[{otel_baggage_processor, #{prefix => <<"app.">>}},
56+
{otel_batch_processor, #{scheduled_delay_ms => 1}}]),
57+
{ok, _} = application:ensure_all_started(opentelemetry),
58+
otel_batch_processor:set_exporter(otel_exporter_pid, self()),
59+
Ctx = otel_ctx:get_current(),
60+
Ctx2 = otel_baggage:set(Ctx, <<"key">>, <<"value">>),
61+
Ctx3 = otel_baggage:set(Ctx2, atom_key, <<"value">>),
62+
_Token = otel_ctx:attach(Ctx3),
63+
SpanCtx1 = ?start_span(<<"span-1">>),
64+
?set_current_span(SpanCtx1),
65+
?end_span(),
66+
Attributes = get_span_attributes(<<"span-1">>),
67+
?assertEqual(#{<<"app.key">> => <<"value">>, <<"app.atom_key">> => <<"value">>},
68+
Attributes),
69+
ok.
70+
71+
filter_baggage_attributes(_Config) ->
72+
application:set_env(opentelemetry,
73+
processors,
74+
[{otel_baggage_processor, #{filter => <<"trace_field">>}},
75+
{otel_batch_processor, #{scheduled_delay_ms => 1}}]),
76+
{ok, _} = application:ensure_all_started(opentelemetry),
77+
otel_batch_processor:set_exporter(otel_exporter_pid, self()),
78+
Ctx = otel_ctx:get_current(),
79+
Ctx2 = otel_baggage:set(Ctx, <<"key">>, <<"value">>),
80+
Ctx3 = otel_baggage:set(Ctx2, atom_key, <<"value">>, [<<"trace_field">>]),
81+
_Token = otel_ctx:attach(Ctx3),
82+
SpanCtx1 = ?start_span(<<"span-1">>),
83+
?set_current_span(SpanCtx1),
84+
?end_span(),
85+
Attributes = get_span_attributes(<<"span-1">>),
86+
?assertEqual(#{<<"atom_key">> => <<"value">>}, Attributes),
87+
ok.
88+
4889
get_span_attributes(Name) ->
4990
receive
50-
{span, #span{name=Name, attributes=Attributes}} ->
91+
{span, #span{name = Name, attributes = Attributes}} ->
5192
otel_attributes:map(Attributes)
52-
after
53-
100 ->
54-
error(timeout)
93+
after 100 ->
94+
error(timeout)
5595
end.

0 commit comments

Comments
 (0)