@@ -3,48 +3,105 @@ defmodule OpentelemetryCommanded.EventStore do
33
44 require OpenTelemetry.Tracer
55
6- alias OpenTelemetry . { Tracer , Span }
6+ import OpentelemetryCommanded.Util
7+
8+ alias OpenTelemetry.Span
9+
10+ @ tracer_id __MODULE__
711
812 def setup do
9- :telemetry . attach_many (
10- { __MODULE__ , :stop } ,
11- [
12- [ :commanded , :event_store , :stream_forward , :stop ] ,
13- [ :commanded , :event_store , :append_to_stream , :stop ]
14- ] ,
15- & __MODULE__ . handle_stop / 4 ,
16- [ ]
17- )
13+ ~w(
14+ ack_event
15+ adapter
16+ append_to_stream
17+ delete_snapshot
18+ delete_subscription
19+ read_snapshot
20+ record_snapshot
21+ stream_forward
22+ stream_forward
23+ stream_forward
24+ subscribe
25+ subscribe_to
26+ subscribe_to
27+ unsubscribe
28+ ) a
29+ |> Enum . each ( fn event ->
30+ :telemetry . attach (
31+ { __MODULE__ , :start } ,
32+ [ :commanded , :event_store , event , :start ] ,
33+ & __MODULE__ . handle_start / 4 ,
34+ [ ]
35+ )
1836
19- :telemetry . attach_many (
20- { __MODULE__ , :exception } ,
21- [
22- [ :commanded , :event_store , :stream_forward , :exception ] ,
23- [ :commanded , :event_store , :append_to_stream , :exception ]
24- ] ,
25- & __MODULE__ . handle_stop / 4 ,
26- [ ]
27- )
37+ :telemetry . attach (
38+ { __MODULE__ , :stop } ,
39+ [ :commanded , :event_store , event , :stop ] ,
40+ & __MODULE__ . handle_stop / 4 ,
41+ [ ]
42+ )
43+
44+ :telemetry . attach (
45+ { __MODULE__ , :exception } ,
46+ [ :commanded , :event_store , event , :exception ] ,
47+ & __MODULE__ . handle_exception / 4 ,
48+ [ ]
49+ )
50+ end )
2851 end
2952
30- def handle_stop ( [ _ , _ , action , type ] , measurements , meta , _ ) do
31- end_time = :opentelemetry . timestamp ( )
32- start_time = end_time - measurements . duration
33- attributes = meta |> Map . take ( [ :application , :stream_uuid ] ) |> Enum . to_list ( )
34- span_name = :"commanded.event_store.#{ action } "
53+ def handle_start ( [ _ , _ , action , _type ] , _measurements , meta , _ ) do
54+ event = meta . event
55+
56+ safe_context_propagation ( event . metadata [ "trace_ctx" ] )
57+
58+ attributes = [
59+ "commanded.application": meta . application ,
60+ "commanded.causation_id": event . causation_id ,
61+ "commanded.correlation_id": event . correlation_id ,
62+ "commanded.event": event . event_type ,
63+ "commanded.event_id": event . event_id ,
64+ "commanded.event_number": event . event_number ,
65+ "commanded.stream_id": event . stream_id ,
66+ "commanded.stream_version": event . stream_version
67+ ]
3568
36- Tracer . start_span ( span_name , % { start_time: start_time , attributes: attributes } )
69+ OpentelemetryTelemetry . start_telemetry_span (
70+ @ tracer_id ,
71+ "commanded.event_store.#{ action } " ,
72+ meta ,
73+ % {
74+ kind: :internal ,
75+ attributes: attributes
76+ }
77+ )
78+ end
3779
38- if type == :exception do
39- ctx = Tracer . current_span_ctx ( )
40- reason = meta [ :reason ]
41- stacktrace = meta [ :stacktrace ]
80+ def handle_stop ( _event , _measurements , meta , _ ) do
81+ ctx = OpentelemetryTelemetry . set_current_telemetry_span ( @ tracer_id , meta )
4282
43- exception = Exception . normalize ( meta [ :kind ] , reason , stacktrace )
44- Span . record_exception ( ctx , exception , stacktrace )
45- Span . set_status ( ctx , OpenTelemetry . status ( :error , "" ) )
83+ if error = meta [ :error ] do
84+ Span . set_status ( ctx , OpenTelemetry . status ( :error , inspect ( error ) ) )
4685 end
4786
48- Tracer . end_span ( )
87+ OpentelemetryTelemetry . end_telemetry_span ( @ tracer_id , meta )
88+ end
89+
90+ def handle_exception (
91+ _event ,
92+ _measurements ,
93+ % { kind: kind , reason: reason , stacktrace: stacktrace } = meta ,
94+ _config
95+ ) do
96+ ctx = OpentelemetryTelemetry . set_current_telemetry_span ( @ tracer_id , meta )
97+
98+ # try to normalize all errors to Elixir exceptions
99+ exception = Exception . normalize ( kind , reason , stacktrace )
100+
101+ # record exception and mark the span as errored
102+ Span . record_exception ( ctx , exception , stacktrace )
103+ Span . set_status ( ctx , OpenTelemetry . status ( :error , inspect ( reason ) ) )
104+
105+ OpentelemetryTelemetry . end_telemetry_span ( @ tracer_id , meta )
49106 end
50107end
0 commit comments