Skip to content

Commit

Permalink
Add Sentry's Sampler for OTel
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Dec 27, 2024
1 parent 9b4d700 commit 7ad38bb
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
3 changes: 3 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ end

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

config :opentelemetry,
sampler: {Sentry.OpenTelemetry.Sampler, [drop: ["Elixir.Oban.Stager process"]]}

config :phoenix, :json_library, Jason
27 changes: 27 additions & 0 deletions lib/sentry/opentelemetry/sampler.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Sentry.OpenTelemetry.Sampler do
@moduledoc false

def setup(config) do
config
end

def description(_) do
"SentrySampler"
end

def should_sample(
_ctx,
_trace_id,
_links,
span_name,
_span_kind,
_attributes,
config
) do
if span_name in config[:drop] do
{:drop, [], []}
else
{:record_and_sample, [], []}
end
end
end
4 changes: 0 additions & 4 deletions lib/sentry/opentelemetry/span_processor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ defmodule Sentry.OpenTelemetry.SpanProcessor do
:ok
end

# TODO: "Elixir.Oban.Stager process" is an internal span and it produces a lot of noise
# so it makes sense to ignore it - should this be configurable?
defp build_transaction(%{name: "Elixir.Oban.Stager process"}, _), do: nil

defp build_transaction(root_span_record, child_span_records) do
root_span = build_span(root_span_record)
child_spans = Enum.map(child_span_records, &build_span(&1))
Expand Down
19 changes: 19 additions & 0 deletions test/sentry/opentelemetry/sampler_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Sentry.Opentelemetry.SamplerTest do
use Sentry.Case, async: false

alias Sentry.OpenTelemetry.Sampler

test "drops spans with the given name" do
assert {:drop, [], []} =
Sampler.should_sample(nil, nil, nil, "Elixir.Oban.Stager process", nil, nil,
drop: ["Elixir.Oban.Stager process"]
)
end

test "records and samples spans with the given name" do
assert {:record_and_sample, [], []} =
Sampler.should_sample(nil, nil, nil, "Elixir.Oban.Worker process", nil, nil,
drop: []
)
end
end
3 changes: 3 additions & 0 deletions test_integrations/phoenix_app/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ config :phoenix, :json_library, Jason

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

config :opentelemetry,
sampler: {Sentry.OpenTelemetry.Sampler, [drop: ["Elixir.Oban.Stager process"]]}

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{config_env()}.exs"

0 comments on commit 7ad38bb

Please sign in to comment.