|
| 1 | +if Sentry.OpenTelemetry.VersionChecker.tracing_compatible?() do |
| 2 | + defmodule Sentry.OpenTelemetry.Setup do |
| 3 | + @moduledoc false |
| 4 | + |
| 5 | + require Logger |
| 6 | + |
| 7 | + @doc """ |
| 8 | + Configures the OpenTelemetry SDK with Sentry's span processor, sampler, and propagator. |
| 9 | +
|
| 10 | + Called during `Sentry.Application.start/2`, before the supervisor starts. |
| 11 | + Only applies configuration if the user hasn't already configured it via |
| 12 | + `config :opentelemetry` entries. |
| 13 | + """ |
| 14 | + @spec maybe_configure_otel_sdk(keyword()) :: :ok |
| 15 | + def maybe_configure_otel_sdk(otel_config) do |
| 16 | + unless Keyword.get(otel_config, :auto_setup) == false do |
| 17 | + maybe_set_span_processor() |
| 18 | + maybe_set_sampler(Keyword.get(otel_config, :sampler_opts, [])) |
| 19 | + maybe_set_propagators() |
| 20 | + end |
| 21 | + |
| 22 | + :ok |
| 23 | + end |
| 24 | + |
| 25 | + @doc """ |
| 26 | + Auto-detects and sets up available instrumentation libraries. |
| 27 | +
|
| 28 | + Called after the Sentry supervisor starts. Respects per-instrumentation config |
| 29 | + to enable/disable individual libraries. |
| 30 | + """ |
| 31 | + @spec maybe_setup_instrumentations(keyword()) :: :ok |
| 32 | + def maybe_setup_instrumentations(otel_config) do |
| 33 | + # Order matters: LiveView propagator MUST come before Phoenix |
| 34 | + maybe_setup(:live_view, otel_config, fn _opts -> |
| 35 | + setup_if_available(Sentry.OpenTelemetry.LiveViewPropagator, :setup, []) |
| 36 | + end) |
| 37 | + |
| 38 | + maybe_setup(:bandit, otel_config, fn _opts -> |
| 39 | + setup_if_available(OpentelemetryBandit, :setup, []) |
| 40 | + end) |
| 41 | + |
| 42 | + maybe_setup(:cowboy, otel_config, fn _opts -> |
| 43 | + setup_if_available(OpentelemetryCowboy, :setup, []) |
| 44 | + end) |
| 45 | + |
| 46 | + maybe_setup(:phoenix, otel_config, fn opts -> |
| 47 | + setup_if_available(OpentelemetryPhoenix, :setup, [opts]) |
| 48 | + end) |
| 49 | + |
| 50 | + maybe_setup(:oban, otel_config, fn _opts -> |
| 51 | + setup_if_available(OpentelemetryOban, :setup, []) |
| 52 | + end) |
| 53 | + |
| 54 | + maybe_setup(:ecto, otel_config, fn opts -> |
| 55 | + repos = Keyword.get(opts, :repos, []) |
| 56 | + ecto_opts = Keyword.drop(opts, [:repos]) |
| 57 | + |
| 58 | + for repo <- repos do |
| 59 | + setup_if_available(OpentelemetryEcto, :setup, [repo, ecto_opts]) |
| 60 | + end |
| 61 | + end) |
| 62 | + |
| 63 | + maybe_setup(:logger_metadata, otel_config, fn _opts -> |
| 64 | + setup_if_available(OpentelemetryLoggerMetadata, :setup, []) |
| 65 | + end) |
| 66 | + |
| 67 | + :ok |
| 68 | + end |
| 69 | + |
| 70 | + # OTel SDK configuration |
| 71 | + |
| 72 | + defp maybe_set_span_processor do |
| 73 | + if Application.get_env(:opentelemetry, :span_processor) == nil and |
| 74 | + Application.get_env(:opentelemetry, :processors) == nil do |
| 75 | + if otel_started?() do |
| 76 | + Logger.warning( |
| 77 | + "[Sentry] OpenTelemetry has already started. " <> |
| 78 | + "Cannot auto-configure span processor. " <> |
| 79 | + "Add `config :opentelemetry, span_processor: {Sentry.OpenTelemetry.SpanProcessor, []}` " <> |
| 80 | + "to your config or ensure :sentry starts before :opentelemetry." |
| 81 | + ) |
| 82 | + else |
| 83 | + Application.put_env( |
| 84 | + :opentelemetry, |
| 85 | + :span_processor, |
| 86 | + {Sentry.OpenTelemetry.SpanProcessor, []} |
| 87 | + ) |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + defp maybe_set_sampler(sampler_opts) do |
| 93 | + if Application.get_env(:opentelemetry, :sampler) == nil do |
| 94 | + if otel_started?() do |
| 95 | + Logger.warning( |
| 96 | + "[Sentry] OpenTelemetry has already started. " <> |
| 97 | + "Cannot auto-configure sampler. " <> |
| 98 | + "Add `config :opentelemetry, sampler: {Sentry.OpenTelemetry.Sampler, #{inspect(sampler_opts)}}` " <> |
| 99 | + "to your config or ensure :sentry starts before :opentelemetry." |
| 100 | + ) |
| 101 | + else |
| 102 | + Application.put_env( |
| 103 | + :opentelemetry, |
| 104 | + :sampler, |
| 105 | + {Sentry.OpenTelemetry.Sampler, sampler_opts} |
| 106 | + ) |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + defp maybe_set_propagators do |
| 112 | + if Application.get_env(:opentelemetry, :text_map_propagators) == nil do |
| 113 | + propagators = [:trace_context, :baggage, Sentry.OpenTelemetry.Propagator] |
| 114 | + |
| 115 | + if otel_started?() do |
| 116 | + # Propagators can be reconfigured at runtime |
| 117 | + composite = :otel_propagator_text_map_composite.create(propagators) |
| 118 | + :opentelemetry.set_text_map_propagator(composite) |
| 119 | + else |
| 120 | + Application.put_env(:opentelemetry, :text_map_propagators, propagators) |
| 121 | + end |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + # Instrumentation setup helpers |
| 126 | + |
| 127 | + defp maybe_setup(key, otel_config, setup_fn) do |
| 128 | + config_value = Keyword.get(otel_config, key, default_for(key)) |
| 129 | + |
| 130 | + unless config_value == false do |
| 131 | + opts = if is_list(config_value), do: config_value, else: [] |
| 132 | + |
| 133 | + try do |
| 134 | + setup_fn.(opts) |
| 135 | + rescue |
| 136 | + e -> |
| 137 | + Logger.warning( |
| 138 | + "[Sentry] Failed to auto-setup #{key} instrumentation: #{Exception.message(e)}" |
| 139 | + ) |
| 140 | + end |
| 141 | + end |
| 142 | + end |
| 143 | + |
| 144 | + defp setup_if_available(module, function, args) do |
| 145 | + if Code.ensure_loaded?(module) do |
| 146 | + apply(module, function, args) |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + defp default_for(:ecto), do: false |
| 151 | + defp default_for(_key), do: true |
| 152 | + |
| 153 | + defp otel_started? do |
| 154 | + case List.keyfind(Application.started_applications(), :opentelemetry, 0) do |
| 155 | + nil -> false |
| 156 | + _ -> true |
| 157 | + end |
| 158 | + end |
| 159 | + end |
| 160 | +end |
0 commit comments