Skip to content
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

Introduce a configurable sample rate #149

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/spandex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -503,12 +503,14 @@ defmodule Spandex do
defp do_start_trace(name, opts) do
strategy = opts[:strategy]
adapter = opts[:adapter]
sample_rate = Keyword.get(opts, :sample_rate, 1.0)
priority = calculate_priority(sample_rate)
trace_id = adapter.trace_id()
span_context = %SpanContext{trace_id: trace_id}

with {:ok, span} <- span(name, opts, span_context, adapter) do
Logger.metadata(trace_id: to_string(trace_id), span_id: to_string(span.id))
trace = %Trace{spans: [], stack: [span], id: trace_id}
trace = %Trace{spans: [], stack: [span], id: trace_id, priority: priority}
strategy.put_trace(opts[:trace_key], trace)
end
end
Expand Down Expand Up @@ -548,6 +550,7 @@ defmodule Spandex do
|> Keyword.put(:parent_id, span_context.parent_id)
|> Keyword.put(:start, adapter.now())
|> Keyword.put(:id, adapter.span_id())
|> Keyword.put(:priority, span_context.priority)
|> Span.new()
end

Expand All @@ -557,4 +560,13 @@ defmodule Spandex do
{:ok, span} -> span
end
end

defp calculate_priority(sample_rate) do
cond do
sample_rate == 0 -> 0
sample_rate == 1 -> 1
:rand.uniform() <= sample_rate -> 1
true -> 0
end
end
end
7 changes: 5 additions & 2 deletions lib/tracer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ defmodule Spandex.Tracer do
service: :atom,
disabled?: :boolean,
env: :string,
sample_rate: :float,
service_version: :string,
services: {:keyword, :atom},
strategy: :atom,
Expand All @@ -61,7 +62,8 @@ defmodule Spandex.Tracer do
defaults: [
disabled?: false,
services: [],
strategy: Spandex.Strategy.Pdict
strategy: Spandex.Strategy.Pdict,
sample_rate: 1.0
],
describe: [
adapter: "The third party adapter to use",
Expand All @@ -73,7 +75,8 @@ defmodule Spandex.Tracer do
disabled?: "Allows for wholesale disabling a tracer",
env: "A name used to identify the environment name, e.g `prod` or `development`",
services: "A mapping of service name to the default span types.",
strategy: "The storage and tracing strategy. Currently only supports local process dictionary."
strategy: "The storage and tracing strategy. Currently only supports local process dictionary.",
sample_rate: "The rate at which to sample traces. 1.0 means 100% of traces are sampled."
]
)

Expand Down
27 changes: 27 additions & 0 deletions test/spandex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,33 @@ defmodule Spandex.Test.SpandexTest do
assert {:service, "is required"} in validation_errors
end

test "sets priority to 1 if no priority is set" do
assert {:ok, %Trace{priority: 1}} =
Spandex.start_trace("root_span", @base_opts ++ @span_opts ++ [sample_rate: nil])
end

test "sets priority to 1 if priority is set to 1" do
assert {:ok, %Trace{priority: 1}} = Spandex.start_trace("root_span", @base_opts ++ @span_opts ++ [sample_rate: 1])
end

test "sets priority to 0 if no priority is set to 0" do
assert {:ok, %Trace{priority: 0}} = Spandex.start_trace("root_span", @base_opts ++ @span_opts ++ [sample_rate: 0])
end

test "sets priority based on sample rate value" do
sample_rate = 0.10

total_priority =
Enum.reduce(1..5000, 0, fn _, acc ->
{:ok, trace} = Spandex.start_trace("root_span", @base_opts ++ @span_opts ++ [sample_rate: sample_rate])
Spandex.finish_trace(@base_opts)
acc + trace.priority
end)

diff = abs(total_priority - 500)
assert diff <= 50
end

test "adds span_id, trace_id to log metadata" do
opts = @base_opts ++ @span_opts

Expand Down