Skip to content

Commit cf8982a

Browse files
author
Dhruv Paranjape
authored
fix: Grape Instrumentation handle status code symbol (#448)
Since grape 1.7.0 grape endpoints can return status codes as rack status code symbols. The symbols are converted to integers by the Grape error middleware. This performs the same conversion with `Rack::Utils`. Rack is already an implicit runtime dependency (and should remain implicit) of the instrumentation, but has now been added as an explicit development dependency for the tests. If the status_code is not valid the status code is assumed to be 0.
1 parent bdd4f64 commit cf8982a

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#
55
# SPDX-License-Identifier: Apache-2.0
66

7+
require 'rack'
8+
79
module OpenTelemetry
810
module Instrumentation
911
module Grape
@@ -79,7 +81,7 @@ def handle_payload_exception(span, exception)
7981
# Only record exceptions if they were not raised (i.e. do not have a status code in Grape)
8082
# or do not have a 5xx status code. These exceptions are recorded by Rack.
8183
# See instrumentation/rack/lib/opentelemetry/instrumentation/rack/middlewares/tracer_middleware.rb#L155
82-
return unless exception.respond_to?('status') && exception.status.to_i < 500
84+
return unless exception.respond_to?('status') && ::Rack::Utils.status_code(exception.status) < 500
8385

8486
span.record_exception(exception)
8587
span.status = OpenTelemetry::Trace::Status.error("Unhandled exception of type: #{exception.class}")

instrumentation/grape/opentelemetry-instrumentation-grape.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Gem::Specification.new do |spec|
3636
spec.add_development_dependency 'minitest', '~> 5.0'
3737
spec.add_development_dependency 'opentelemetry-sdk', '~> 1.0'
3838
spec.add_development_dependency 'opentelemetry-test-helpers'
39+
spec.add_development_dependency 'rack'
3940
spec.add_development_dependency 'rack-test'
4041
spec.add_development_dependency 'rake', '~> 13.0'
4142
spec.add_development_dependency 'rspec-mocks'

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,25 @@ class ErrorResponseAPI < Grape::API
376376
end
377377
end
378378

379+
describe 'when an API endpoint returns an error with the status as a symbol' do
380+
class ErrorResponseAPI < Grape::API
381+
get :error_response do
382+
error!('Not found', :not_found)
383+
end
384+
end
385+
386+
let(:app) { build_rack_app(ErrorResponseAPI) }
387+
let(:request_path) { '/error_response' }
388+
let(:expected_span_name) { 'HTTP GET /error_response' }
389+
390+
before { app.get request_path }
391+
392+
it 'does not set span status to error' do
393+
_(span.name).must_equal expected_span_name
394+
_(span.status.code).wont_equal OpenTelemetry::Trace::Status::ERROR
395+
end
396+
end
397+
379398
describe 'when an API endpoint receives a request and some events are ignored in the configs' do
380399
class IgnoredEventAPI < Grape::API
381400
get :success do

0 commit comments

Comments
 (0)