From 5d6482e35b43bbfb4ffc6a29cc5d034745669664 Mon Sep 17 00:00:00 2001 From: Mikael Date: Mon, 18 Mar 2024 16:46:05 +0100 Subject: [PATCH 1/5] fix: erlang & elixir sampler --- content/en/docs/languages/erlang/sampling.md | 30 +++++++++++--------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/content/en/docs/languages/erlang/sampling.md b/content/en/docs/languages/erlang/sampling.md index f7e6afe6ffd0..5547ff179092 100644 --- a/content/en/docs/languages/erlang/sampling.md +++ b/content/en/docs/languages/erlang/sampling.md @@ -159,12 +159,16 @@ description(_) -> <<"AttributeSampler">>. should_sample(_Ctx, _TraceId, _Links, _SpanName, _SpanKind, Attributes, ConfigAttributes) -> - case maps:intersect(Attributes, ConfigAttributes) of - Map when map_size(Map) > 0 -> - {?DROP, [], []}; - _ -> - {?RECORD_AND_SAMPLE, [], []} - end. + case maps:intersect(Attributes, ConfigAttributes) of + Intersection when map_size(Intersection) > 0 -> + Keys = maps:keys(Intersection), + ConfAttrPairs = [{maps:get(Key, Attr), maps:get(Key, Conf)} || Key <- Keys], + case lists:any(fun({A, B}) -> A == B end, ConfAttrPairs) of + true -> {?DROP, [], []}; + _ -> {?RECORD_AND_SAMPLE, [], []} + end; + _ -> {?RECORD_AND_SAMPLE, [], []} + end ``` {{% /tab %}} {{% tab Elixir %}} @@ -184,13 +188,13 @@ defmodule AttributesSampler do end def should_sample(_ctx, _trace_id, _links, _span_name, _span_kind, attributes, config_attributes) do - case :maps.intersect(attributes, config_attributes) do - map when map_size(map) > 0 -> - {:drop, [], []} - _ -> - {:record_and_sample, [], []} - end - end + attr_match = + Map.intersect(attributes, config_attributes) + |> Map.keys() + |> Enum.map(fn key -> {Map.get(attributes, key), Map.get(config_attributes, key)} end) + |> Enum.any?(fn {attr, conf} -> attr == conf end) + + if attr_match, do: {:drop, [], []}, else: {:record_and_sample, [], []} end ``` From ba9fc85ffb24e68053d236b272e0e9f77834f499 Mon Sep 17 00:00:00 2001 From: Mikael Date: Mon, 18 Mar 2024 17:12:00 +0100 Subject: [PATCH 2/5] refactor: leverage intersect_with --- content/en/docs/languages/erlang/sampling.md | 22 ++++++++------------ 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/content/en/docs/languages/erlang/sampling.md b/content/en/docs/languages/erlang/sampling.md index 5547ff179092..be2c7fa04716 100644 --- a/content/en/docs/languages/erlang/sampling.md +++ b/content/en/docs/languages/erlang/sampling.md @@ -159,16 +159,12 @@ description(_) -> <<"AttributeSampler">>. should_sample(_Ctx, _TraceId, _Links, _SpanName, _SpanKind, Attributes, ConfigAttributes) -> - case maps:intersect(Attributes, ConfigAttributes) of - Intersection when map_size(Intersection) > 0 -> - Keys = maps:keys(Intersection), - ConfAttrPairs = [{maps:get(Key, Attr), maps:get(Key, Conf)} || Key <- Keys], - case lists:any(fun({A, B}) -> A == B end, ConfAttrPairs) of - true -> {?DROP, [], []}; - _ -> {?RECORD_AND_SAMPLE, [], []} - end; - _ -> {?RECORD_AND_SAMPLE, [], []} + ConfAttrPairs = maps:intersect_with(fun (_key, Attr, Conf) -> {Attr, Conf} end, Attributes, ConfigAttributes), + case lists:any(fun({_, {A, B}}) -> A == B end, maps:to_list(ConfAttrPairs)) of + true -> {drop, [], []}; + _ -> {record_and_sample, [], []} end +end. ``` {{% /tab %}} {{% tab Elixir %}} @@ -189,12 +185,12 @@ defmodule AttributesSampler do def should_sample(_ctx, _trace_id, _links, _span_name, _span_kind, attributes, config_attributes) do attr_match = - Map.intersect(attributes, config_attributes) - |> Map.keys() - |> Enum.map(fn key -> {Map.get(attributes, key), Map.get(config_attributes, key)} end) - |> Enum.any?(fn {attr, conf} -> attr == conf end) + Map.intersect(attributes, config_attributes, fn _key, attr, conf -> {attr, conf} end) + |> Map.to_list() + |> Enum.any?(fn {_, {attr, conf}} -> attr == conf end) if attr_match, do: {:drop, [], []}, else: {:record_and_sample, [], []} + end end ``` From 41d2f32deaaf55cb2292470a04a83d099a140a18 Mon Sep 17 00:00:00 2001 From: Mikael Date: Mon, 18 Mar 2024 18:44:59 +0100 Subject: [PATCH 3/5] fix: use macro names --- content/en/docs/languages/erlang/sampling.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/en/docs/languages/erlang/sampling.md b/content/en/docs/languages/erlang/sampling.md index be2c7fa04716..7b61e6525de7 100644 --- a/content/en/docs/languages/erlang/sampling.md +++ b/content/en/docs/languages/erlang/sampling.md @@ -161,8 +161,8 @@ description(_) -> should_sample(_Ctx, _TraceId, _Links, _SpanName, _SpanKind, Attributes, ConfigAttributes) -> ConfAttrPairs = maps:intersect_with(fun (_key, Attr, Conf) -> {Attr, Conf} end, Attributes, ConfigAttributes), case lists:any(fun({_, {A, B}}) -> A == B end, maps:to_list(ConfAttrPairs)) of - true -> {drop, [], []}; - _ -> {record_and_sample, [], []} + true -> {?DROP, [], []}; + _ -> {?RECORD_AND_SAMPLE, [], []} end end. ``` From 8f40bcc100dbfd5b6d7c851adb0e8a2eac4df433 Mon Sep 17 00:00:00 2001 From: Mikael Date: Wed, 20 Mar 2024 12:36:58 +0000 Subject: [PATCH 4/5] refactor: use is_disjoint --- content/en/docs/languages/erlang/sampling.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/content/en/docs/languages/erlang/sampling.md b/content/en/docs/languages/erlang/sampling.md index 7b61e6525de7..39e30a71c0fd 100644 --- a/content/en/docs/languages/erlang/sampling.md +++ b/content/en/docs/languages/erlang/sampling.md @@ -159,11 +159,12 @@ description(_) -> <<"AttributeSampler">>. should_sample(_Ctx, _TraceId, _Links, _SpanName, _SpanKind, Attributes, ConfigAttributes) -> - ConfAttrPairs = maps:intersect_with(fun (_key, Attr, Conf) -> {Attr, Conf} end, Attributes, ConfigAttributes), - case lists:any(fun({_, {A, B}}) -> A == B end, maps:to_list(ConfAttrPairs)) of - true -> {?DROP, [], []}; - _ -> {?RECORD_AND_SAMPLE, [], []} - end + AttributesSet = sets:from_list(maps:to_list(Attributes)), + ConfigSet = sets:from_list(maps:to_list(ConfigAttributes)), + case sets:is_disjoint(AttributesSet, ConfigSet) of + true -> {?RECORD_AND_SAMPLE, [], []} + _ -> {?DROP, [], []}; + end. ``` @@ -184,12 +185,11 @@ defmodule AttributesSampler do end def should_sample(_ctx, _trace_id, _links, _span_name, _span_kind, attributes, config_attributes) do - attr_match = - Map.intersect(attributes, config_attributes, fn _key, attr, conf -> {attr, conf} end) - |> Map.to_list() - |> Enum.any?(fn {_, {attr, conf}} -> attr == conf end) + no_match = + Enum.into(attributes, %MapSet{}) + |> MapSet.disjoint?(Enum.into(config_attributes, %MapSet{})) - if attr_match, do: {:drop, [], []}, else: {:record_and_sample, [], []} + if no_match, do: {:record_and_sample, [], []}, else: {:drop, [], []} end end ``` From 0fd646d0da946a2f72b0fc2991485b75f5ec2278 Mon Sep 17 00:00:00 2001 From: Mikael Date: Wed, 20 Mar 2024 12:40:53 +0000 Subject: [PATCH 5/5] style: format --- content/en/docs/languages/erlang/sampling.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/content/en/docs/languages/erlang/sampling.md b/content/en/docs/languages/erlang/sampling.md index 39e30a71c0fd..2ea694ded240 100644 --- a/content/en/docs/languages/erlang/sampling.md +++ b/content/en/docs/languages/erlang/sampling.md @@ -159,12 +159,11 @@ description(_) -> <<"AttributeSampler">>. should_sample(_Ctx, _TraceId, _Links, _SpanName, _SpanKind, Attributes, ConfigAttributes) -> - AttributesSet = sets:from_list(maps:to_list(Attributes)), - ConfigSet = sets:from_list(maps:to_list(ConfigAttributes)), - case sets:is_disjoint(AttributesSet, ConfigSet) of - true -> {?RECORD_AND_SAMPLE, [], []} - _ -> {?DROP, [], []}; - + AttributesSet = sets:from_list(maps:to_list(Attributes)), + ConfigSet = sets:from_list(maps:to_list(ConfigAttributes)), + case sets:is_disjoint(AttributesSet, ConfigSet) of + true -> {?RECORD_AND_SAMPLE, [], []}; + _ -> {?DROP, [], []} end. ```