Make OpenTelemetry sampler ratio configurable via env (#3514)

This commit is contained in:
Adrian Gruntkowski 2023-11-13 14:57:51 +01:00 committed by GitHub
parent 671904aefd
commit 3ba57a04fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 12 deletions

View File

@ -178,6 +178,11 @@ google_cid = get_var_from_path_or_env(config_dir, "GOOGLE_CLIENT_ID")
google_secret = get_var_from_path_or_env(config_dir, "GOOGLE_CLIENT_SECRET") google_secret = get_var_from_path_or_env(config_dir, "GOOGLE_CLIENT_SECRET")
postmark_api_key = get_var_from_path_or_env(config_dir, "POSTMARK_API_KEY") postmark_api_key = get_var_from_path_or_env(config_dir, "POSTMARK_API_KEY")
{otel_sampler_ratio, ""} =
config_dir
|> get_var_from_path_or_env("OTEL_SAMPLER_RATIO", "0.5")
|> Float.parse()
cron_enabled = cron_enabled =
config_dir config_dir
|> get_var_from_path_or_env("CRON_ENABLED", "false") |> get_var_from_path_or_env("CRON_ENABLED", "false")
@ -612,7 +617,7 @@ end
if honeycomb_api_key && honeycomb_dataset do if honeycomb_api_key && honeycomb_dataset do
config :opentelemetry, config :opentelemetry,
resource: Plausible.OpenTelemetry.resource_attributes(runtime_metadata), resource: Plausible.OpenTelemetry.resource_attributes(runtime_metadata),
sampler: {Plausible.OpenTelemetry.Sampler, nil}, sampler: {Plausible.OpenTelemetry.Sampler, %{ratio: otel_sampler_ratio}},
span_processor: :batch, span_processor: :batch,
traces_exporter: :otlp traces_exporter: :otlp

View File

@ -10,13 +10,10 @@ defmodule Plausible.OpenTelemetry.Sampler do
import Bitwise, only: [&&&: 2] import Bitwise, only: [&&&: 2]
# effective sampling ratio for non-ignored traces # mask for extracting first 64 bits of trace ID
@ratio 0.5
# 2^63 - 1 # 2^63 - 1
@max_value 9_223_372_036_854_775_807 @max_value 9_223_372_036_854_775_807
@id_upper_bound @ratio * @max_value
@behaviour :otel_sampler @behaviour :otel_sampler
require OpenTelemetry.Tracer, as: Tracer require OpenTelemetry.Tracer, as: Tracer
@ -24,13 +21,17 @@ defmodule Plausible.OpenTelemetry.Sampler do
@tables_to_ignore ["oban_jobs"] @tables_to_ignore ["oban_jobs"]
@impl true @impl true
def setup(_sampler_opts), do: [] def setup(%{ratio: ratio}) when is_number(ratio) do
%{ratio: ratio, id_upper_bound: ratio * @max_value}
end
@impl true @impl true
def description(_sampler_config), do: inspect(__MODULE__) def description(%{ratio: ratio}) do
"#{inspect(__MODULE__)}{ratio=#{ratio}}"
end
@impl true @impl true
def should_sample(context, trace_id, _links, _name, _kind, attributes, _config) do def should_sample(context, trace_id, _links, _name, _kind, attributes, config) do
tracestate = context |> Tracer.current_span_ctx() |> OpenTelemetry.Span.tracestate() tracestate = context |> Tracer.current_span_ctx() |> OpenTelemetry.Span.tracestate()
case attributes do case attributes do
@ -41,19 +42,19 @@ defmodule Plausible.OpenTelemetry.Sampler do
{:drop, [], tracestate} {:drop, [], tracestate}
_any -> _any ->
{decide(trace_id), [], tracestate} {decide(trace_id, config.id_upper_bound), [], tracestate}
end end
end end
defp decide(trace_id) when is_integer(trace_id) and trace_id > 0 do defp decide(trace_id, id_upper_bound) when is_integer(trace_id) and trace_id > 0 do
lower_64_bits = trace_id &&& @max_value lower_64_bits = trace_id &&& @max_value
if abs(lower_64_bits) < @id_upper_bound do if abs(lower_64_bits) < id_upper_bound do
:record_and_sample :record_and_sample
else else
:drop :drop
end end
end end
defp decide(_), do: :drop defp decide(_, _), do: :drop
end end