Skip to content

Commit 673ab6e

Browse files
author
Muriel
authored
fix: Set grape.formatter.type to 'custom' for non-Grape formatters (#444)
1 parent 2473649 commit 673ab6e

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

instrumentation/grape/lib/opentelemetry/instrumentation/grape/event_handler.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,16 @@ def path(endpoint)
100100
end
101101

102102
def formatter_type(formatter)
103+
return 'custom' unless built_in_grape_formatter?(formatter)
104+
103105
basename = formatter.name.split('::').last
104106
# Convert from CamelCase to snake_case
105107
basename.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
106108
end
109+
110+
def built_in_grape_formatter?(formatter)
111+
formatter.respond_to?('name') && formatter.name.include?('Grape::Formatter')
112+
end
107113
end
108114
end
109115
end

instrumentation/grape/test/opentelemetry/instrumentation/grape_test.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,35 @@ class FilteredAPI < Grape::API
184184
end
185185
end
186186

187+
describe 'when an API endpoint uses a custom formatter' do
188+
class CustomFormatterAPI < Grape::API
189+
format :txt
190+
formatter :txt, ->(object, _) { object.to_s }
191+
192+
get :hello do
193+
{ message: 'Hello, world!' }
194+
end
195+
end
196+
197+
let(:app) { build_rack_app(CustomFormatterAPI) }
198+
let(:request_path) { '/hello' }
199+
let(:expected_span_name) { 'HTTP GET /hello' }
200+
201+
before { app.get request_path }
202+
203+
it 'produces a Rack span with the expected name' do
204+
_(spans.length).must_equal 1
205+
_(span.name).must_equal expected_span_name
206+
end
207+
208+
it 'adds a format_response span event with the formatter type attribute set to custom' do
209+
format_events = events_per_name('grape.format_response')
210+
211+
_(format_events.length).must_equal 1
212+
_(format_events.first.attributes['grape.formatter.type']).must_equal 'custom'
213+
end
214+
end
215+
187216
describe 'when an API endpoint receives params that raise a validation error' do
188217
class ValidationErrorAPI < Grape::API
189218
format :json

0 commit comments

Comments
 (0)