12
12
let ( :exporter ) { EXPORTER }
13
13
let ( :last_span ) { exporter . finished_spans . last }
14
14
let ( :span_kind ) { nil }
15
+ let ( :notification_name ) { 'bar.foo' }
15
16
let ( :subscriber ) do
16
17
OpenTelemetry ::Instrumentation ::ActiveSupport ::SpanSubscriber . new (
17
- name : 'bar.foo' ,
18
+ name : notification_name ,
18
19
tracer : tracer ,
19
20
kind : span_kind
20
21
)
@@ -36,7 +37,7 @@ def finish(name, id, payload)
36
37
37
38
it 'memoizes the span name' do
38
39
span , = subscriber . start ( 'oh.hai' , 'abc' , { } )
39
- _ ( span . name ) . must_equal ( 'foo bar' )
40
+ _ ( span . name ) . must_equal ( notification_name )
40
41
end
41
42
42
43
it 'uses the provided tracer' do
@@ -115,7 +116,7 @@ def finish(name, id, payload)
115
116
describe 'instrumentation option - disallowed_notification_payload_keys' do
116
117
let ( :subscriber ) do
117
118
OpenTelemetry ::Instrumentation ::ActiveSupport ::SpanSubscriber . new (
118
- name : 'bar.foo' ,
119
+ name : notification_name ,
119
120
tracer : tracer ,
120
121
notification_payload_transform : nil ,
121
122
disallowed_notification_payload_keys : [ :foo ]
@@ -153,7 +154,7 @@ def finish(name, id, payload)
153
154
let ( :transformer_proc ) { -> ( v ) { v . transform_values { 'optimus prime' } } }
154
155
let ( :subscriber ) do
155
156
OpenTelemetry ::Instrumentation ::ActiveSupport ::SpanSubscriber . new (
156
- name : 'bar.foo' ,
157
+ name : notification_name ,
157
158
tracer : tracer ,
158
159
notification_payload_transform : transformer_proc ,
159
160
disallowed_notification_payload_keys : [ :foo ]
@@ -205,58 +206,109 @@ def finish(name, id, payload)
205
206
206
207
describe 'instrument' do
207
208
before do
208
- ActiveSupport ::Notifications . unsubscribe ( 'bar.foo' )
209
+ ActiveSupport ::Notifications . unsubscribe ( notification_name )
209
210
end
210
211
211
212
it 'does not trace an event by default' do
212
- ActiveSupport ::Notifications . subscribe ( 'bar.foo' ) do
213
+ ActiveSupport ::Notifications . subscribe ( notification_name ) do
213
214
# pass
214
215
end
215
- ActiveSupport ::Notifications . instrument ( 'bar.foo' , extra : 'context' )
216
+ ActiveSupport ::Notifications . instrument ( notification_name , extra : 'context' )
216
217
_ ( last_span ) . must_be_nil
217
218
end
218
219
219
220
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' )
222
223
223
224
_ ( last_span ) . wont_be_nil
224
- _ ( last_span . name ) . must_equal ( 'foo bar' )
225
+ _ ( last_span . name ) . must_equal ( notification_name )
225
226
_ ( last_span . attributes [ 'extra' ] ) . must_equal ( 'context' )
226
227
_ ( last_span . kind ) . must_equal ( :internal )
227
228
end
228
229
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
+
229
281
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 )
232
284
233
285
expect do
234
- ActiveSupport ::Notifications . instrument ( 'bar.foo' , extra : 'context' )
286
+ ActiveSupport ::Notifications . instrument ( notification_name , extra : 'context' )
235
287
end . must_raise RuntimeError
236
288
237
289
_ ( last_span ) . wont_be_nil
238
- _ ( last_span . name ) . must_equal ( 'foo bar' )
290
+ _ ( last_span . name ) . must_equal ( notification_name )
239
291
_ ( last_span . attributes [ 'extra' ] ) . must_equal ( 'context' )
240
292
end
241
293
242
294
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 )
245
297
246
298
expect do
247
- ActiveSupport ::Notifications . instrument ( 'bar.foo' , extra : 'context' )
299
+ ActiveSupport ::Notifications . instrument ( notification_name , extra : 'context' )
248
300
end . must_raise RuntimeError
249
301
250
302
_ ( last_span ) . wont_be_nil
251
- _ ( last_span . name ) . must_equal ( 'foo bar' )
303
+ _ ( last_span . name ) . must_equal ( notification_name )
252
304
_ ( last_span . attributes [ 'extra' ] ) . must_equal ( 'context' )
253
305
end
254
306
255
307
it 'supports unsubscribe' do
256
- obj = OpenTelemetry ::Instrumentation ::ActiveSupport . subscribe ( tracer , 'bar.foo' )
308
+ obj = OpenTelemetry ::Instrumentation ::ActiveSupport . subscribe ( tracer , notification_name )
257
309
ActiveSupport ::Notifications . unsubscribe ( obj )
258
310
259
- ActiveSupport ::Notifications . instrument ( 'bar.foo' , extra : 'context' )
311
+ ActiveSupport ::Notifications . instrument ( notification_name , extra : 'context' )
260
312
261
313
_ ( obj . class ) . must_equal ( ActiveSupport ::Notifications ::Fanout ::Subscribers ::Evented )
262
314
_ ( last_span ) . must_be_nil
@@ -267,7 +319,7 @@ def finish(name, id, payload)
267
319
ActiveSupport ::Notifications . instrument ( 'bar.foo' , extra : 'context' )
268
320
269
321
_ ( last_span ) . wont_be_nil
270
- _ ( last_span . name ) . must_equal ( 'foo bar' )
322
+ _ ( last_span . name ) . must_equal ( 'bar.foo ' )
271
323
_ ( last_span . attributes [ 'extra' ] ) . must_equal ( 'context' )
272
324
_ ( last_span . kind ) . must_equal ( :client )
273
325
end
0 commit comments