@@ -6,24 +6,7 @@ defmodule BasicElixir.Worker do
66
77 # Client
88 def start_link ( default ) when is_list ( default ) do
9- # Span names can be a binary or an atom. Atoms are preferred
10- # for memory efficiency.
11- Tracer . with_span :start_link do
12- # Span name rules also apply to events
13- Tracer . add_event ( "Nice operation!" , % { "bogons" => 100 } )
14-
15- # Attributes can be a map or a keyword list
16- Tracer . set_attributes ( % { another_key: "yes" } )
17-
18- # Any attributes that can be known before starting a span
19- # should be passed on span creation so that they can be
20- # available for sampling decisions
21- Tracer . with_span "Sub operation..." , % { attributes: [ lemons_key: "five" ] } do
22- Tracer . add_event ( "Sub span event!" , [ ] )
23- end
24-
25- GenServer . start_link ( __MODULE__ , default )
26- end
9+ GenServer . start_link ( __MODULE__ , default )
2710 end
2811
2912 def push ( pid , element ) do
@@ -39,6 +22,7 @@ defmodule BasicElixir.Worker do
3922 def init ( stack ) do
4023 Tracer . with_span :init do
4124 Logger . info ( "Starting #{ __MODULE__ } ..." )
25+ send ( self ( ) , :perform )
4226 { :ok , stack }
4327 end
4428 end
@@ -52,4 +36,45 @@ defmodule BasicElixir.Worker do
5236 def handle_cast ( { :push , element } , state ) do
5337 { :noreply , [ element | state ] }
5438 end
39+
40+ @ impl true
41+ def handle_info ( :perform , state ) do
42+ if otlp_exporter_initialized? ( ) do
43+ Logger . info ( "#{ __MODULE__ } - OTLP exporter initialized, performing..." )
44+ perform ( )
45+ Logger . info ( "#{ __MODULE__ } - Perform completed" )
46+ else
47+ Logger . info ( "#{ __MODULE__ } - OTLP exporter not initialized, performing in 1 second..." )
48+ perform_in_one_second ( )
49+ end
50+
51+ { :noreply , state }
52+ end
53+
54+ defp perform do
55+ # Span names can be a binary or an atom. Atoms are preferred
56+ # for memory efficiency.
57+ Tracer . with_span :start_link do
58+ # Span name rules also apply to events
59+ Tracer . add_event ( "Nice operation!" , % { "bogons" => 100 } )
60+
61+ # Attributes can be a map or a keyword list
62+ Tracer . set_attributes ( % { another_key: "yes" } )
63+
64+ # Any attributes that can be known before starting a span
65+ # should be passed on span creation so that they can be
66+ # available for sampling decisions
67+ Tracer . with_span "Sub operation..." , % { attributes: [ lemons_key: "five" ] } do
68+ Tracer . add_event ( "Sub span event!" , [ ] )
69+ end
70+ end
71+ end
72+
73+ defp perform_in_one_second do
74+ one_second = 1_000
75+ Process . send_after ( self ( ) , :perform , one_second )
76+ end
77+
78+ defp otlp_exporter_initialized? ,
79+ do: :persistent_term . get ( { :otel_batch_processor , :enabled_key , :otel_batch_processor_global } )
5580end
0 commit comments