@@ -2,6 +2,7 @@ defmodule RIG.Tracing.CloudEvent do
22 @ moduledoc """
33 Distributed Tracing instrumenter for cloudevents
44 """
5+ alias RigCloudEvents.CloudEvent
56 alias RigCloudEvents.Parser.PartialParser
67
78 @ doc "Like Opencensus.Trace.with_child_span (https://hexdocs.pm/opencensus_elixir/Opencensus.Trace.html#with_child_span/3),
@@ -24,19 +25,7 @@ defmodule RIG.Tracing.CloudEvent do
2425 } )
2526
2627 quote do
27- tracecontext = [ ]
28-
29- tracecontext =
30- case PartialParser . context_attribute ( unquote ( event ) . parsed , "traceparent" ) do
31- { :ok , traceparent } -> Enum . concat ( tracecontext , % { "traceparent" => traceparent } )
32- _ -> tracecontext
33- end
34-
35- tracecontext =
36- case PartialParser . context_attribute ( unquote ( event ) . parsed , "tracestate" ) do
37- { :ok , tracestate } -> Enum . concat ( tracecontext , % { "tracestate" => tracestate } )
38- _ -> tracecontext
39- end
28+ tracecontext = compute_context ( unquote ( event ) )
4029
4130 parent_span_ctx = :oc_propagation_http_tracecontext . from_headers ( tracecontext )
4231
@@ -58,54 +47,37 @@ defmodule RIG.Tracing.CloudEvent do
5847
5948 # ---
6049
61- # temporary function to handle new cloudevents library for Kafka
62- defmacro with_child_span_temp ( label , event , attributes \\ quote ( do: % { } ) , do: block ) do
63- line = __CALLER__ . line
64- module = __CALLER__ . module
65- file = __CALLER__ . file
66- function = format_function ( __CALLER__ . function )
67-
68- computed_attributes =
69- compute_attributes ( attributes , % {
70- line: line ,
71- module: module ,
72- file: file ,
73- function: function
74- } )
75-
76- quote do
77- tracecontext = [ ]
78-
79- tracecontext =
80- case Map . get ( unquote ( event ) , "traceparent" ) do
81- nil -> tracecontext
82- traceparent -> Enum . concat ( tracecontext , % { "traceparent" => traceparent } )
83- end
84-
85- tracecontext =
86- case Map . get ( unquote ( event ) , "tracestate" ) do
87- nil -> tracecontext
88- tracestate -> Enum . concat ( tracecontext , % { "tracestate" => tracestate } )
89- end
50+ def compute_context ( % CloudEvent { } = event ) do
51+ tracecontext = [ ]
9052
91- parent_span_ctx = :oc_propagation_http_tracecontext . from_headers ( tracecontext )
92-
93- new_span_ctx =
94- :oc_trace . start_span ( unquote ( label ) , parent_span_ctx , % {
95- :attributes => unquote ( computed_attributes )
96- } )
53+ tracecontext =
54+ case PartialParser . context_attribute ( event . parsed , "traceparent" ) do
55+ { :ok , traceparent } -> Enum . concat ( tracecontext , % { "traceparent" => traceparent } )
56+ _ -> tracecontext
57+ end
9758
98- :ocp . with_span_ctx ( new_span_ctx )
59+ case PartialParser . context_attribute ( event . parsed , "tracestate" ) do
60+ { :ok , tracestate } -> Enum . concat ( tracecontext , % { "tracestate" => tracestate } )
61+ _ -> tracecontext
62+ end
63+ end
9964
100- try do
101- unquote ( block )
102- after
103- :oc_trace . finish_span ( new_span_ctx )
104- :ocp . with_span_ctx ( parent_span_ctx )
65+ def compute_context ( % { } = event ) do
66+ tracecontext = [ ]
67+ tracecontext =
68+ case Map . get ( event , "traceparent" ) do
69+ nil -> tracecontext
70+ traceparent -> Enum . concat ( tracecontext , % { "traceparent" => traceparent } )
10571 end
72+
73+ case Map . get ( event , "tracestate" ) do
74+ nil -> tracecontext
75+ tracestate -> Enum . concat ( tracecontext , % { "tracestate" => tracestate } )
10676 end
10777 end
10878
79+ # ---
80+
10981 defp format_function ( nil ) , do: nil
11082 defp format_function ( { name , arity } ) , do: "#{ name } /#{ arity } "
11183
0 commit comments