|
| 1 | +defmodule Opencensus.Unstable do |
| 2 | + @moduledoc """ |
| 3 | + Experimental higher-level API built on proposed `ot_ctx` behaviour. |
| 4 | + """ |
| 5 | + |
| 6 | + @doc """ |
| 7 | + Get the current span context. |
| 8 | +
|
| 9 | + Uses the first configured `process_context` only to ensure the value is safe to pass to |
| 10 | + `with_span_ctx/1` and `with_span_ctx/2` after you've finished your work. |
| 11 | + """ |
| 12 | + @spec current_span_ctx() :: :opencensus.span_ctx() | :undefined |
| 13 | + def current_span_ctx do |
| 14 | + process_contexts() |
| 15 | + |> hd |
| 16 | + |> get_span_ctx_via() |
| 17 | + end |
| 18 | + |
| 19 | + @doc """ |
| 20 | + Recovers the span context. |
| 21 | +
|
| 22 | + Uses all configured `process_context`. |
| 23 | + Results MAY be used as the parent of a new span. |
| 24 | + Results MUST NOT be passed to `with_span_ctx/1` or `with_span_ctx/2`. |
| 25 | + """ |
| 26 | + @spec recover_span_ctx() :: :opencensus.span_ctx() | :undefined |
| 27 | + def recover_span_ctx do |
| 28 | + process_contexts() |
| 29 | + |> Enum.find_value(:undefined, &get_span_ctx_via/1) |
| 30 | + end |
| 31 | + |
| 32 | + @doc """ |
| 33 | + Sets the span context. Replaces `:ocp.with_span_ctx/1`. |
| 34 | +
|
| 35 | + Uses all configured `process_context`. |
| 36 | + Returns the previous value of `current_span_ctx/0`. |
| 37 | + """ |
| 38 | + @spec with_span_ctx(span_ctx :: :opencensus.span_ctx() | :undefined) :: |
| 39 | + :opencensus.span_ctx() | :undefined |
| 40 | + def with_span_ctx(span_ctx) do |
| 41 | + return_span_ctx = current_span_ctx() |
| 42 | + process_contexts() |> Enum.each(&put_span_ctx_via(&1, span_ctx)) |
| 43 | + return_span_ctx |
| 44 | + end |
| 45 | + |
| 46 | + defp get_span_ctx_via(module) do |
| 47 | + apply(module, :get, [span_ctx_key()]) |
| 48 | + |> case do |
| 49 | + nil -> :undefined |
| 50 | + span_ctx -> span_ctx |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + defp put_span_ctx_via(module, value) do |
| 55 | + apply(module, :with_value, [span_ctx_key(), value]) |
| 56 | + end |
| 57 | + |
| 58 | + @spec span_ctx_key() :: atom() |
| 59 | + defp span_ctx_key do |
| 60 | + Application.get_env(:opencensus, :span_ctx_key, :oc_span_ctx_key) |
| 61 | + end |
| 62 | + |
| 63 | + @spec process_contexts() :: list(module()) |
| 64 | + defp process_contexts do |
| 65 | + Application.get_env(:opencensus, :process_contexts, [ |
| 66 | + Opencensus.Unstable.ProcessContext.SeqTrace, |
| 67 | + Opencensus.Unstable.ProcessContext.ProcessDictionary, |
| 68 | + Opencensus.Unstable.ProcessContext.ProcessDictionaryWithRecovery |
| 69 | + ]) |
| 70 | + end |
| 71 | +end |
| 72 | + |
| 73 | +defmodule Opencensus.Unstable.ProcessContext do |
| 74 | + @moduledoc "Abstraction over process-local storage." |
| 75 | + |
| 76 | + @doc "Get a value." |
| 77 | + @callback get(key :: atom()) :: any() | nil |
| 78 | + |
| 79 | + @doc "Put a value." |
| 80 | + @callback with_value(key :: atom, value :: any()) :: :ok |
| 81 | +end |
| 82 | + |
| 83 | +defmodule Opencensus.Unstable.ProcessContext.SeqTrace do |
| 84 | + @moduledoc """ |
| 85 | + Process-local storage using `seq_trace`. |
| 86 | +
|
| 87 | + Shares well with any other use that maintains a namespace in the second element of a 2-tuple |
| 88 | + `{:shared_label, _map}`. Otherwise leaves the trace label alone to avoid disrupting the other |
| 89 | + usage. |
| 90 | + """ |
| 91 | + |
| 92 | + @behaviour Opencensus.Unstable.ProcessContext |
| 93 | + |
| 94 | + @doc "Get a value from the shared `seq_trace` label." |
| 95 | + @impl Opencensus.Unstable.ProcessContext |
| 96 | + def get(key) do |
| 97 | + case :seq_trace.get_token(:label) do |
| 98 | + {:label, {:shared_label, %{^key => value}}} -> |
| 99 | + value |
| 100 | + |
| 101 | + _ -> |
| 102 | + nil |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + @doc "Put a value to the shared `seq_trace` label if safe." |
| 107 | + @impl Opencensus.Unstable.ProcessContext |
| 108 | + def with_value(key, value) do |
| 109 | + case :seq_trace.get_token(:label) do |
| 110 | + [] -> |
| 111 | + :seq_trace.set_token(:label, {:shared_label, %{key => value}}) |
| 112 | + |
| 113 | + {:label, {:shared_label, map}} when is_map(map) -> |
| 114 | + :seq_trace.set_token(:label, {:shared_label, Map.put(map, key, value)}) |
| 115 | + |
| 116 | + _ -> |
| 117 | + nil |
| 118 | + end |
| 119 | + |
| 120 | + :ok |
| 121 | + end |
| 122 | +end |
| 123 | + |
| 124 | +defmodule Opencensus.Unstable.ProcessContext.ProcessDictionary do |
| 125 | + @moduledoc """ |
| 126 | + Process-local storage using the process dictionary. |
| 127 | + """ |
| 128 | + |
| 129 | + @behaviour Opencensus.Unstable.ProcessContext |
| 130 | + |
| 131 | + @doc "Get a value from the process dictionary." |
| 132 | + @impl Opencensus.Unstable.ProcessContext |
| 133 | + def get(key), do: Process.get(key) |
| 134 | + |
| 135 | + @impl Opencensus.Unstable.ProcessContext |
| 136 | + def with_value(key, value) do |
| 137 | + Process.put(key, value) |
| 138 | + :ok |
| 139 | + end |
| 140 | +end |
| 141 | + |
| 142 | +defmodule Opencensus.Unstable.ProcessContext.ProcessDictionaryWithRecovery do |
| 143 | + @moduledoc """ |
| 144 | + Process-local storage using the process dictionary. |
| 145 | + """ |
| 146 | + |
| 147 | + @behaviour Opencensus.Unstable.ProcessContext |
| 148 | + |
| 149 | + @doc "Get a value from the process dictionary." |
| 150 | + @impl Opencensus.Unstable.ProcessContext |
| 151 | + def get(key) do |
| 152 | + [self() | Process.get(:"$callers", [])] |
| 153 | + |> Enum.find_value(fn pid -> pid |> Process.info() |> get_in([:dictionary, key]) end) |
| 154 | + end |
| 155 | + |
| 156 | + @impl Opencensus.Unstable.ProcessContext |
| 157 | + defdelegate with_value(key, value), to: Opencensus.Unstable.ProcessContext.ProcessDictionary |
| 158 | +end |
0 commit comments