Skip to content

Commit 0287ab6

Browse files
author
Muriel Picone Farías
committed
Improve documentation and naming based on review comments
1 parent 6877337 commit 0287ab6

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

instrumentation/grape/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The Grape instrumentation is a community-maintained instrumentation for [Grape](https://github.com/ruby-grape/grape), a REST-like API framework for Ruby.
44

5-
It relies mostly on the Grape built-in support for `ActiveSupport::Notifications` (more info [here](https://github.com/ruby-grape/grape#active-support-instrumentation)).
5+
It relies on the Grape built-in support for `ActiveSupport::Notifications` (more info [here](https://github.com/ruby-grape/grape#active-support-instrumentation)).
66

77
It currently supports the following events:
88

@@ -31,7 +31,7 @@ OpenTelemetry::SDK.configure do |c|
3131
end
3232
```
3333

34-
Since Grape is "designed to run on Rack or complement existing web application frameworks such as Rails and Sinatra", we recommend using it along with the Rack, Rails and/or Sinatra instrumentations.
34+
Since Grape is "designed to run on Rack or complement existing web application frameworks such as Rails and Sinatra", it is recommended to use this instrumentation along with the Rack, Rails and/or Sinatra instrumentations.
3535

3636
Alternatively, you can also call `use_all` to install all the available instrumentation.
3737

@@ -47,7 +47,7 @@ end
4747

4848
Indicate if any events should not produce spans.
4949

50-
- Accepted values: `:endpoint_render`, `:endpoint_run_filters`.
50+
- Accepted values: `:endpoint_render`, `:endpoint_run_filters`, `:format_response`.
5151
- Defaults to `[]` (no ignored events).
5252

5353
Example:
@@ -58,7 +58,7 @@ OpenTelemetry::SDK.configure do |c|
5858
end
5959
```
6060

61-
Note that the `endpoint_run` event cannot be disabled since it is the parent event. If you need to disable the instrumentation, set `:enabled` to `false`:
61+
Note that the `endpoint_run` event cannot be ignored. If you need to disable the instrumentation, set `:enabled` to `false`:
6262

6363
```ruby
6464
OpenTelemetry::SDK.configure do |c|

instrumentation/grape/lib/opentelemetry/instrumentation/grape/custom_subscribers/endpoint_run.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
module OpenTelemetry
88
module Instrumentation
99
module Grape
10-
# Contains all custom subscriber classes that implement the ActiveSupport::Subscriber interface
11-
# Custom subscribers are needed to create a span at the start of an event, for example.
10+
# Contains custom subscribers that implement the ActiveSupport::Notifications::Fanout::Subscribers::Evented
11+
# interface. Custom subscribers are needed to create a span at the start of an event, for example.
12+
#
13+
# Reference: https://github.com/rails/rails/blob/05cb63abdaf6101e6c8fb43119e2c0d08e543c28/activesupport/lib/active_support/notifications/fanout.rb#L320-L322
1214
module CustomSubscribers
1315
# Implements the ActiveSupport::Subscriber interface to instrument the start and finish of the endpoint_run event
1416
class EndpointRun

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def endpoint_run_finish(_name, _id, payload)
2727
return unless span && token
2828

2929
if payload[:exception_object]
30-
handle_error(span, payload[:exception_object])
30+
handle_payload_exception(span, payload[:exception_object])
3131
else
3232
span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, payload[:endpoint].status)
3333
end
@@ -44,7 +44,7 @@ def endpoint_render(_name, start, _finish, _id, payload)
4444
'operation' => 'endpoint_render'
4545
}
4646
tracer.in_span(name, attributes: attributes, start_timestamp: start, kind: :server) do |span|
47-
handle_error(span, payload[:exception_object]) if payload[:exception_object]
47+
handle_payload_exception(span, payload[:exception_object]) if payload[:exception_object]
4848
end
4949
end
5050

@@ -63,7 +63,7 @@ def endpoint_run_filters(_name, start, finish, _id, payload)
6363
'grape.filter.type' => type.to_s
6464
}
6565
tracer.in_span(name, attributes: attributes, start_timestamp: start, kind: :server) do |span|
66-
handle_error(span, payload[:exception_object]) if payload[:exception_object]
66+
handle_payload_exception(span, payload[:exception_object]) if payload[:exception_object]
6767
end
6868
end
6969

@@ -77,7 +77,7 @@ def format_response(_name, start, _finish, _id, payload)
7777
'grape.formatter.type' => formatter_type(payload[:formatter])
7878
}
7979
tracer.in_span(name, attributes: attributes, start_timestamp: start, kind: :server) do |span|
80-
handle_error(span, payload[:exception_object]) if payload[:exception_object]
80+
handle_payload_exception(span, payload[:exception_object]) if payload[:exception_object]
8181
end
8282
end
8383

@@ -105,7 +105,10 @@ def run_attributes(payload)
105105
}
106106
end
107107

108-
def handle_error(span, exception)
108+
# ActiveSupport::Notifications will attach a `:exception_object` to the payload if there was
109+
# an error raised during the execution of the &block associated to the Notification.
110+
# This can be safely added to the span for tracing.
111+
def handle_payload_exception(span, exception)
109112
span.record_exception(exception)
110113
span.status = OpenTelemetry::Trace::Status.error("Unhandled exception of type: #{exception.class}")
111114
return unless exception.respond_to?('status') && exception.status
@@ -114,17 +117,17 @@ def handle_error(span, exception)
114117
end
115118

116119
def api_instance(endpoint)
117-
endpoint.options[:for].base.to_s
120+
endpoint.options[:for]&.base.to_s
118121
end
119122

120123
def request_method(endpoint)
121-
endpoint.options.fetch(:method).first
124+
endpoint.options[:method]&.first
122125
end
123126

124127
def path(endpoint)
125128
namespace = endpoint.routes.first.namespace
126129
version = endpoint.routes.first.options[:version] || ''
127-
prefix = endpoint.routes.first.options[:prefix].to_s || ''
130+
prefix = endpoint.routes.first.options[:prefix]&.to_s || ''
128131
parts = [prefix, version] + namespace.split('/') + endpoint.options[:path]
129132
parts.reject { |p| p.blank? || p.eql?('/') }.join('/').prepend('/')
130133
end

instrumentation/grape/lib/opentelemetry/instrumentation/grape/subscriber.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ def subscribe
1818

1919
private
2020

21+
# ActiveSupport::Notifications that can be subscribed to using the documented `.subscribe` interface.
22+
#
23+
# Reference: https://api.rubyonrails.org/classes/ActiveSupport/Notifications.html#method-c-subscribe
2124
SUBSCRIPTIONS = {
2225
endpoint_render: 'endpoint_render.grape',
2326
endpoint_run_filters: 'endpoint_run_filters.grape',
2427
format_response: 'format_response.grape'
2528
}.freeze
2629

30+
# ActiveSupport::Notifications to be subscribed to that implement the ActiveSupport::Subscriber interface.
31+
#
32+
# Reference: https://github.com/rails/rails/blob/05cb63abdaf6101e6c8fb43119e2c0d08e543c28/activesupport/lib/active_support/notifications/fanout.rb#L320-L322
2733
CUSTOM_SUBSCRIPTIONS = {
2834
endpoint_run: 'OpenTelemetry::Instrumentation::Grape::CustomSubscribers::EndpointRun'
2935
}.freeze

0 commit comments

Comments
 (0)