Skip to content

Commit

Permalink
refactor: leverage intersect_with
Browse files Browse the repository at this point in the history
  • Loading branch information
Graborg committed Mar 20, 2024
1 parent 5d6482e commit ba9fc85
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions content/en/docs/languages/erlang/sampling.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}}
Expand All @@ -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
```

Expand Down

0 comments on commit ba9fc85

Please sign in to comment.