@@ -45,6 +45,9 @@ defmodule Opencensus.Trace do
4545 end
4646 ```
4747 """
48+
49+ @ behaviour Opencensus.ProcessContext
50+
4851 defmacro with_child_span ( label , attributes \\ quote ( do: % { } ) , do: block ) do
4952 line = __CALLER__ . line
5053 module = __CALLER__ . module
@@ -60,21 +63,22 @@ defmodule Opencensus.Trace do
6063 } )
6164
6265 quote do
63- parent_span_ctx = :ocp . current_span_ctx ( )
66+ previous_span_ctx = Opencensus.Trace . get_span_ctx ( )
67+ parent_span_ctx = Opencensus.Trace . effective_span_ctx ( )
6468
6569 new_span_ctx =
6670 :oc_trace . start_span ( unquote ( label ) , parent_span_ctx , % {
6771 :attributes => unquote ( computed_attributes )
6872 } )
6973
70- _ = :ocp . with_span_ctx ( new_span_ctx )
74+ _ = Opencensus.Trace . put_span_ctx ( new_span_ctx )
7175 Opencensus.Logger . set_logger_metadata ( )
7276
7377 try do
7478 unquote ( block )
7579 after
7680 _ = :oc_trace . finish_span ( new_span_ctx )
77- _ = :ocp . with_span_ctx ( parent_span_ctx )
81+ _ = Opencensus.Trace . put_span_ctx ( previous_span_ctx )
7882 Opencensus.Logger . set_logger_metadata ( )
7983 end
8084 end
@@ -166,4 +170,62 @@ defmodule Opencensus.Trace do
166170 """
167171 @ spec await ( Task . t ( ) , :infinity | pos_integer ( ) ) :: term ( )
168172 defdelegate await ( task , timeout \\ 5000 ) , to: Task
173+
174+ @ doc """
175+ Put the current span context.
176+
177+ Replaces `:ocp.with_span_ctx/1`.
178+
179+ Callers [MAY] pass values from `get_span_ctx/0` to `put_span_ctx/1`.
180+
181+ Callers [MUST NOT] pass a value obtained via `recover_span_ctx/0` to `put_span_ctx/1`.
182+
183+ Uses the configured `process_context`. See also: `Opencensus.ProcessContext`.
184+
185+ [MAY]: https://tools.ietf.org/html/rfc2119#section-5
186+ """
187+ @ impl Opencensus.ProcessContext
188+ def put_span_ctx ( span_ctx ) , do: process_context ( ) |> apply ( :put_span_ctx , [ span_ctx ] )
189+
190+ @ doc """
191+ Get the current span context.
192+
193+ Replaces `:ocp.current_span_ctx/0`, along with `recover_span_ctx/0`.
194+
195+ Callers [MAY] pass values from `get_span_ctx/0` to `put_span_ctx/1`.
196+
197+ Uses the configured `process_context`. See also: `Opencensus.ProcessContext`.
198+
199+ [MAY]: https://tools.ietf.org/html/rfc2119#section-5
200+ """
201+ @ impl Opencensus.ProcessContext
202+ def get_span_ctx , do: process_context ( ) |> apply ( :get_span_ctx , [ ] )
203+
204+ @ doc """
205+ Recover the current span context by less reliable means.
206+
207+ Replaces `:ocp.current_span_ctx/0`, along with `get_span_ctx/0`.
208+
209+ Callers [MUST NOT] pass a value obtained via `recover_span_ctx/0` to `put_span_ctx/1`.
210+
211+ Uses the configured `process_context`. See also: `Opencensus.ProcessContext`.
212+ """
213+ @ impl Opencensus.ProcessContext
214+ def recover_span_ctx , do: process_context ( ) |> apply ( :recover_span_ctx , [ ] )
215+
216+ @ doc false
217+ def effective_span_ctx do
218+ case get_span_ctx ( ) do
219+ :undefined -> recover_span_ctx ( )
220+ span_ctx -> span_ctx
221+ end
222+ end
223+
224+ defp process_context do
225+ Application . get_env (
226+ :opencensus ,
227+ :process_context ,
228+ Opencensus.ProcessContext.DefaultImplementation
229+ )
230+ end
169231end
0 commit comments