1212 let ( :exporter ) { EXPORTER }
1313 let ( :last_span ) { exporter . finished_spans . last }
1414 let ( :span_kind ) { nil }
15+ let ( :notification_name ) { 'bar.foo' }
1516 let ( :subscriber ) do
1617 OpenTelemetry ::Instrumentation ::ActiveSupport ::SpanSubscriber . new (
17- name : 'bar.foo' ,
18+ name : notification_name ,
1819 tracer : tracer ,
1920 kind : span_kind
2021 )
@@ -36,7 +37,7 @@ def finish(name, id, payload)
3637
3738 it 'memoizes the span name' do
3839 span , = subscriber . start ( 'oh.hai' , 'abc' , { } )
39- _ ( span . name ) . must_equal ( 'foo bar' )
40+ _ ( span . name ) . must_equal ( notification_name )
4041 end
4142
4243 it 'uses the provided tracer' do
@@ -115,7 +116,7 @@ def finish(name, id, payload)
115116 describe 'instrumentation option - disallowed_notification_payload_keys' do
116117 let ( :subscriber ) do
117118 OpenTelemetry ::Instrumentation ::ActiveSupport ::SpanSubscriber . new (
118- name : 'bar.foo' ,
119+ name : notification_name ,
119120 tracer : tracer ,
120121 notification_payload_transform : nil ,
121122 disallowed_notification_payload_keys : [ :foo ]
@@ -153,7 +154,7 @@ def finish(name, id, payload)
153154 let ( :transformer_proc ) { -> ( v ) { v . transform_values { 'optimus prime' } } }
154155 let ( :subscriber ) do
155156 OpenTelemetry ::Instrumentation ::ActiveSupport ::SpanSubscriber . new (
156- name : 'bar.foo' ,
157+ name : notification_name ,
157158 tracer : tracer ,
158159 notification_payload_transform : transformer_proc ,
159160 disallowed_notification_payload_keys : [ :foo ]
@@ -205,58 +206,109 @@ def finish(name, id, payload)
205206
206207 describe 'instrument' do
207208 before do
208- ActiveSupport ::Notifications . unsubscribe ( 'bar.foo' )
209+ ActiveSupport ::Notifications . unsubscribe ( notification_name )
209210 end
210211
211212 it 'does not trace an event by default' do
212- ActiveSupport ::Notifications . subscribe ( 'bar.foo' ) do
213+ ActiveSupport ::Notifications . subscribe ( notification_name ) do
213214 # pass
214215 end
215- ActiveSupport ::Notifications . instrument ( 'bar.foo' , extra : 'context' )
216+ ActiveSupport ::Notifications . instrument ( notification_name , extra : 'context' )
216217 _ ( last_span ) . must_be_nil
217218 end
218219
219220 it 'traces an event when a span subscriber is used' do
220- OpenTelemetry ::Instrumentation ::ActiveSupport . subscribe ( tracer , 'bar.foo' )
221- ActiveSupport ::Notifications . instrument ( 'bar.foo' , extra : 'context' )
221+ OpenTelemetry ::Instrumentation ::ActiveSupport . subscribe ( tracer , notification_name )
222+ ActiveSupport ::Notifications . instrument ( notification_name , extra : 'context' )
222223
223224 _ ( last_span ) . wont_be_nil
224- _ ( last_span . name ) . must_equal ( 'foo bar' )
225+ _ ( last_span . name ) . must_equal ( notification_name )
225226 _ ( last_span . attributes [ 'extra' ] ) . must_equal ( 'context' )
226227 _ ( last_span . kind ) . must_equal ( :internal )
227228 end
228229
230+ describe 'when using a custom span name formatter' do
231+ describe 'when using the LEGACY_NAME_FORMATTER' do
232+ let ( :span_name_formatter ) { OpenTelemetry ::Instrumentation ::ActiveSupport ::LEGACY_NAME_FORMATTER }
233+ it 'uses the user supplied formatter' do
234+ OpenTelemetry ::Instrumentation ::ActiveSupport . subscribe ( tracer , notification_name , nil , nil , span_name_formatter : span_name_formatter )
235+ ActiveSupport ::Notifications . instrument ( notification_name , extra : 'context' )
236+
237+ _ ( last_span ) . wont_be_nil
238+ _ ( last_span . name ) . must_equal ( 'foo bar' )
239+ _ ( last_span . attributes [ 'extra' ] ) . must_equal ( 'context' )
240+ end
241+ end
242+
243+ describe 'when using a custom formatter' do
244+ let ( :span_name_formatter ) { -> ( name ) { "custom.#{ name } " } }
245+
246+ it 'uses the user supplied formatter' do
247+ OpenTelemetry ::Instrumentation ::ActiveSupport . subscribe ( tracer , notification_name , nil , nil , span_name_formatter : span_name_formatter )
248+ ActiveSupport ::Notifications . instrument ( notification_name , extra : 'context' )
249+
250+ _ ( last_span ) . wont_be_nil
251+ _ ( last_span . name ) . must_equal ( 'custom.bar.foo' )
252+ _ ( last_span . attributes [ 'extra' ] ) . must_equal ( 'context' )
253+ end
254+ end
255+
256+ describe 'when using a invalid formatter' do
257+ it 'defaults to the notification name' do
258+ OpenTelemetry ::Instrumentation ::ActiveSupport . subscribe ( tracer , notification_name , nil , nil , span_name_formatter : -> ( _ ) { } )
259+ ActiveSupport ::Notifications . instrument ( notification_name , extra : 'context' )
260+
261+ _ ( last_span ) . wont_be_nil
262+ _ ( last_span . name ) . must_equal ( notification_name )
263+ _ ( last_span . attributes [ 'extra' ] ) . must_equal ( 'context' )
264+ end
265+ end
266+
267+ describe 'when using a unstable formatter' do
268+ it 'defaults to the notification name' do
269+ allow ( OpenTelemetry ) . to receive ( :handle_error ) . with ( exception : RuntimeError , message : String )
270+
271+ OpenTelemetry ::Instrumentation ::ActiveSupport . subscribe ( tracer , notification_name , nil , nil , span_name_formatter : -> ( _ ) { raise 'boom' } )
272+ ActiveSupport ::Notifications . instrument ( notification_name , extra : 'context' )
273+
274+ _ ( last_span ) . wont_be_nil
275+ _ ( last_span . name ) . must_equal ( notification_name )
276+ _ ( last_span . attributes [ 'extra' ] ) . must_equal ( 'context' )
277+ end
278+ end
279+ end
280+
229281 it 'finishes spans even when block subscribers blow up' do
230- ActiveSupport ::Notifications . subscribe ( 'bar.foo' ) { raise 'boom' }
231- OpenTelemetry ::Instrumentation ::ActiveSupport . subscribe ( tracer , 'bar.foo' )
282+ ActiveSupport ::Notifications . subscribe ( notification_name ) { raise 'boom' }
283+ OpenTelemetry ::Instrumentation ::ActiveSupport . subscribe ( tracer , notification_name )
232284
233285 expect do
234- ActiveSupport ::Notifications . instrument ( 'bar.foo' , extra : 'context' )
286+ ActiveSupport ::Notifications . instrument ( notification_name , extra : 'context' )
235287 end . must_raise RuntimeError
236288
237289 _ ( last_span ) . wont_be_nil
238- _ ( last_span . name ) . must_equal ( 'foo bar' )
290+ _ ( last_span . name ) . must_equal ( notification_name )
239291 _ ( last_span . attributes [ 'extra' ] ) . must_equal ( 'context' )
240292 end
241293
242294 it 'finishes spans even when complex subscribers blow up' do
243- ActiveSupport ::Notifications . subscribe ( 'bar.foo' , CrashingEndSubscriber . new )
244- OpenTelemetry ::Instrumentation ::ActiveSupport . subscribe ( tracer , 'bar.foo' )
295+ ActiveSupport ::Notifications . subscribe ( notification_name , CrashingEndSubscriber . new )
296+ OpenTelemetry ::Instrumentation ::ActiveSupport . subscribe ( tracer , notification_name )
245297
246298 expect do
247- ActiveSupport ::Notifications . instrument ( 'bar.foo' , extra : 'context' )
299+ ActiveSupport ::Notifications . instrument ( notification_name , extra : 'context' )
248300 end . must_raise RuntimeError
249301
250302 _ ( last_span ) . wont_be_nil
251- _ ( last_span . name ) . must_equal ( 'foo bar' )
303+ _ ( last_span . name ) . must_equal ( notification_name )
252304 _ ( last_span . attributes [ 'extra' ] ) . must_equal ( 'context' )
253305 end
254306
255307 it 'supports unsubscribe' do
256- obj = OpenTelemetry ::Instrumentation ::ActiveSupport . subscribe ( tracer , 'bar.foo' )
308+ obj = OpenTelemetry ::Instrumentation ::ActiveSupport . subscribe ( tracer , notification_name )
257309 ActiveSupport ::Notifications . unsubscribe ( obj )
258310
259- ActiveSupport ::Notifications . instrument ( 'bar.foo' , extra : 'context' )
311+ ActiveSupport ::Notifications . instrument ( notification_name , extra : 'context' )
260312
261313 _ ( obj . class ) . must_equal ( ActiveSupport ::Notifications ::Fanout ::Subscribers ::Evented )
262314 _ ( last_span ) . must_be_nil
@@ -267,7 +319,7 @@ def finish(name, id, payload)
267319 ActiveSupport ::Notifications . instrument ( 'bar.foo' , extra : 'context' )
268320
269321 _ ( last_span ) . wont_be_nil
270- _ ( last_span . name ) . must_equal ( 'foo bar' )
322+ _ ( last_span . name ) . must_equal ( 'bar.foo ' )
271323 _ ( last_span . attributes [ 'extra' ] ) . must_equal ( 'context' )
272324 _ ( last_span . kind ) . must_equal ( :client )
273325 end
0 commit comments