-
-
Notifications
You must be signed in to change notification settings - Fork 207
Support for traces_sampler option #910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
solnic
merged 7 commits into
span-processor-with-new-sampler
from
traces-sampler-support
Jun 17, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
27bfc44
Add support for `traces_sampler` config option
solnic fb3bced
Update docs
solnic 8b597f2
Remove unnecessary attribute merging
solnic bf32449
Call traces_sampler only for root spans
solnic e616c21
Reuse sampling decision logic
solnic d3f7048
Add test for handling invalid traces_sampler return values
solnic 36a6b4d
Add tests for SamplingContext access functions
solnic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| [ | ||
| {"test/support/example_plug_application.ex"}, | ||
| {"test/support/test_helpers.ex"} | ||
| {"test/support/test_helpers.ex"}, | ||
| {"lib/sentry/opentelemetry/sampler.ex", :pattern_match, 1} | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| defmodule SamplingContext do | ||
| @moduledoc """ | ||
| The struct for the **sampling_context** that is passed to `traces_sampler`. | ||
|
|
||
| This is set up via `Sentry.OpenTelemetry.Sampler`. | ||
|
|
||
| See also <https://develop.sentry.dev/sdk/telemetry/traces/#sampling-context>. | ||
| """ | ||
|
|
||
| @moduledoc since: "11.0.0" | ||
|
|
||
| @typedoc """ | ||
| The sampling context struct that contains information needed for sampling decisions. | ||
|
|
||
| This matches the structure used in the Python SDK's create_sampling_context function. | ||
| """ | ||
| @type t :: %__MODULE__{ | ||
| transaction_context: %{ | ||
| name: String.t() | nil, | ||
| op: String.t(), | ||
| trace_id: String.t(), | ||
| attributes: map() | ||
| }, | ||
| parent_sampled: boolean() | nil | ||
| } | ||
|
|
||
| @enforce_keys [:transaction_context, :parent_sampled] | ||
| defstruct [:transaction_context, :parent_sampled] | ||
|
|
||
| @behaviour Access | ||
|
|
||
| @impl Access | ||
| def fetch(struct, key) do | ||
| case Map.fetch(struct, key) do | ||
| {:ok, value} -> {:ok, value} | ||
| :error -> :error | ||
| end | ||
| end | ||
|
|
||
| @impl Access | ||
| def get_and_update(struct, key, function) do | ||
| current_value = Map.get(struct, key) | ||
|
|
||
| case function.(current_value) do | ||
| {get_value, update_value} -> | ||
| {get_value, Map.put(struct, key, update_value)} | ||
|
|
||
| :pop -> | ||
| {current_value, Map.delete(struct, key)} | ||
| end | ||
| end | ||
|
|
||
| @impl Access | ||
| def pop(struct, key) do | ||
| {Map.get(struct, key), Map.delete(struct, key)} | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| defmodule Sentry.ConfigTracesSamplerTest do | ||
| use ExUnit.Case, async: true | ||
|
|
||
| import Sentry.TestHelpers | ||
|
|
||
| describe "traces_sampler configuration validation" do | ||
| defmodule TestSampler do | ||
| def sample(_context), do: 0.5 | ||
| end | ||
|
|
||
| test "accepts nil" do | ||
| assert :ok = put_test_config(traces_sampler: nil) | ||
| assert Sentry.Config.traces_sampler() == nil | ||
| end | ||
|
|
||
| test "accepts function with arity 1" do | ||
| fun = fn _context -> 0.5 end | ||
| assert :ok = put_test_config(traces_sampler: fun) | ||
| assert Sentry.Config.traces_sampler() == fun | ||
| end | ||
|
|
||
| test "accepts MFA tuple with exported function" do | ||
| assert :ok = put_test_config(traces_sampler: {TestSampler, :sample}) | ||
| assert Sentry.Config.traces_sampler() == {TestSampler, :sample} | ||
| end | ||
|
|
||
| test "rejects MFA tuple with non-exported function" do | ||
| assert_raise ArgumentError, ~r/function.*is not exported/, fn -> | ||
| put_test_config(traces_sampler: {TestSampler, :non_existent}) | ||
| end | ||
| end | ||
|
|
||
| test "rejects function with wrong arity" do | ||
| fun = fn -> 0.5 end | ||
|
|
||
| assert_raise ArgumentError, ~r/expected :traces_sampler to be/, fn -> | ||
| put_test_config(traces_sampler: fun) | ||
| end | ||
| end | ||
|
|
||
| test "rejects invalid types" do | ||
| assert_raise ArgumentError, ~r/expected :traces_sampler to be/, fn -> | ||
| put_test_config(traces_sampler: "invalid") | ||
| end | ||
|
|
||
| assert_raise ArgumentError, ~r/expected :traces_sampler to be/, fn -> | ||
| put_test_config(traces_sampler: 123) | ||
| end | ||
|
|
||
| assert_raise ArgumentError, ~r/expected :traces_sampler to be/, fn -> | ||
| put_test_config(traces_sampler: []) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "tracing? function" do | ||
| test "returns true when traces_sample_rate is set" do | ||
| put_test_config(traces_sample_rate: 0.5, traces_sampler: nil) | ||
|
|
||
| assert Sentry.Config.tracing?() | ||
| end | ||
|
|
||
| test "returns true when traces_sampler is set" do | ||
| put_test_config(traces_sample_rate: nil, traces_sampler: fn _ -> 0.5 end) | ||
|
|
||
| assert Sentry.Config.tracing?() | ||
| end | ||
|
|
||
| test "returns true when both are set" do | ||
| put_test_config(traces_sample_rate: 0.5, traces_sampler: fn _ -> 0.5 end) | ||
|
|
||
| assert Sentry.Config.tracing?() | ||
| end | ||
|
|
||
| test "returns false when neither is set" do | ||
| put_test_config(traces_sample_rate: nil, traces_sampler: nil) | ||
|
|
||
| refute Sentry.Config.tracing?() | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.