Skip to content

Commit 7ad38bb

Browse files
committed
Add Sentry's Sampler for OTel
1 parent 9b4d700 commit 7ad38bb

File tree

5 files changed

+52
-4
lines changed

5 files changed

+52
-4
lines changed

config/config.exs

+3
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ end
1717

1818
config :opentelemetry, span_processor: {Sentry.OpenTelemetry.SpanProcessor, []}
1919

20+
config :opentelemetry,
21+
sampler: {Sentry.OpenTelemetry.Sampler, [drop: ["Elixir.Oban.Stager process"]]}
22+
2023
config :phoenix, :json_library, Jason

lib/sentry/opentelemetry/sampler.ex

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
defmodule Sentry.OpenTelemetry.Sampler do
2+
@moduledoc false
3+
4+
def setup(config) do
5+
config
6+
end
7+
8+
def description(_) do
9+
"SentrySampler"
10+
end
11+
12+
def should_sample(
13+
_ctx,
14+
_trace_id,
15+
_links,
16+
span_name,
17+
_span_kind,
18+
_attributes,
19+
config
20+
) do
21+
if span_name in config[:drop] do
22+
{:drop, [], []}
23+
else
24+
{:record_and_sample, [], []}
25+
end
26+
end
27+
end

lib/sentry/opentelemetry/span_processor.ex

-4
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ defmodule Sentry.OpenTelemetry.SpanProcessor do
6262
:ok
6363
end
6464

65-
# TODO: "Elixir.Oban.Stager process" is an internal span and it produces a lot of noise
66-
# so it makes sense to ignore it - should this be configurable?
67-
defp build_transaction(%{name: "Elixir.Oban.Stager process"}, _), do: nil
68-
6965
defp build_transaction(root_span_record, child_span_records) do
7066
root_span = build_span(root_span_record)
7167
child_spans = Enum.map(child_span_records, &build_span(&1))
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
defmodule Sentry.Opentelemetry.SamplerTest do
2+
use Sentry.Case, async: false
3+
4+
alias Sentry.OpenTelemetry.Sampler
5+
6+
test "drops spans with the given name" do
7+
assert {:drop, [], []} =
8+
Sampler.should_sample(nil, nil, nil, "Elixir.Oban.Stager process", nil, nil,
9+
drop: ["Elixir.Oban.Stager process"]
10+
)
11+
end
12+
13+
test "records and samples spans with the given name" do
14+
assert {:record_and_sample, [], []} =
15+
Sampler.should_sample(nil, nil, nil, "Elixir.Oban.Worker process", nil, nil,
16+
drop: []
17+
)
18+
end
19+
end

test_integrations/phoenix_app/config/config.exs

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ config :phoenix, :json_library, Jason
6363

6464
config :opentelemetry, span_processor: {Sentry.OpenTelemetry.SpanProcessor, []}
6565

66+
config :opentelemetry,
67+
sampler: {Sentry.OpenTelemetry.Sampler, [drop: ["Elixir.Oban.Stager process"]]}
68+
6669
# Import environment specific config. This must remain at the bottom
6770
# of this file so it overrides the configuration defined above.
6871
import_config "#{config_env()}.exs"

0 commit comments

Comments
 (0)