@@ -55,9 +55,36 @@ defmodule Commanded.OpenTelemetry do
5555 """
5656 @ type span_relationship :: :link | :child | :none
5757
58+ @ typedoc """
59+ Callback used to decide the OpenTelemetry span status for returned `:stop` errors.
60+
61+ The callback is only invoked when Commanded emits stop metadata containing `:error`.
62+ Exception telemetry continues to use OpenTelemetry exception semantics directly.
63+ """
64+ @ type error_status_callback ::
65+ ( event_name :: [ atom ( ) ] ,
66+ measurements :: map ( ) ,
67+ metadata :: map ( ) ,
68+ config :: keyword ( ) ->
69+ OpenTelemetry . status_code ( ) | nil )
70+
71+ @ error_status_options [
72+ error_status: [
73+ type: { :fun , 4 } ,
74+ type_doc: "`t:error_status_callback/0`" ,
75+ doc:
76+ "Override the span status for returned `:stop` errors. Return `nil` to leave the status unset."
77+ ]
78+ ]
79+
5880 @ nimble_schema NimbleOptions . new! (
5981 aggregate: [
60- type: { :in , [ :disabled , [ ] ] } ,
82+ type:
83+ { :or ,
84+ [
85+ { :in , [ :disabled ] } ,
86+ keyword_list: @ error_status_options
87+ ] } ,
6188 default: [ ] ,
6289 doc: "Aggregate tracing configuration. Use `:disabled` to disable."
6390 ] ,
@@ -72,7 +99,12 @@ defmodule Commanded.OpenTelemetry do
7299 doc: "Aggregate snapshot tracing configuration. Use `:disabled` to disable."
73100 ] ,
74101 application: [
75- type: { :in , [ :disabled , [ ] ] } ,
102+ type:
103+ { :or ,
104+ [
105+ { :in , [ :disabled ] } ,
106+ keyword_list: @ error_status_options
107+ ] } ,
76108 default: [ ] ,
77109 doc:
78110 "Application dispatch tracing configuration. Use `:disabled` to disable."
@@ -123,6 +155,16 @@ defmodule Commanded.OpenTelemetry do
123155 # Disable event handler tracing
124156 Commanded.OpenTelemetry.setup(event_handler: :disabled)
125157
158+ # Leave returned domain errors unset for dispatch spans
159+ Commanded.OpenTelemetry.setup(
160+ application: [
161+ error_status: fn
162+ _event_name, _measurements, %{error: :validation_failed}, _config -> :unset
163+ _event_name, _measurements, _meta, _config -> :error
164+ end
165+ ]
166+ )
167+
126168 # Disable aggregate populate tracing
127169 Commanded.OpenTelemetry.setup(aggregate_populate: :disabled)
128170
@@ -142,7 +184,7 @@ defmodule Commanded.OpenTelemetry do
142184
143185 case opts [ :aggregate ] do
144186 :disabled -> :ok
145- _config -> Aggregate . setup ( )
187+ config -> Aggregate . setup ( config )
146188 end
147189
148190 case opts [ :aggregate_populate ] do
@@ -157,7 +199,7 @@ defmodule Commanded.OpenTelemetry do
157199
158200 case opts [ :application ] do
159201 :disabled -> :ok
160- _config -> OTelApplication . setup ( )
202+ config -> OTelApplication . setup ( config )
161203 end
162204
163205 case opts [ :event_handler ] do
0 commit comments