-
Notifications
You must be signed in to change notification settings - Fork 134
Grape support #562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Grape support #562
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
4f00e21
Foundations for Grape support
estolfo b12f0a5
Only instrument endpoint_run and pass app to Grape.start
estolfo 4a998c0
Test Grape.start
estolfo 6ca2a13
Remove Grape app constant after test
estolfo b19206d
Add documentation for app parameter to Grape.start
estolfo aff2492
Add documentation for getting started with Grape
estolfo da49860
Add documentation for configuring Grape
estolfo 3d729c6
Test passing config values to Grape.start
estolfo 6dd0feb
Resolve rubocop complaints
estolfo 8ae18f6
Add Grape.start documentation to API docs
estolfo 86c38ad
Make method name clearer
estolfo 6d1458d
Grape should be tested as a framework in Jenkins
estolfo 4a3206d
Fix spacing
estolfo f4cebca
Correct documentation of config options order
estolfo 11ddddb
Support installing and testing against multiple frameworks
estolfo bcb5df5
Update framework info in transaction context for Grape
estolfo a6cb7c2
Add rails and grape spec
estolfo 70df966
Grape github repo name has a different pattern than the other frameworks
estolfo b2f820c
Only test grape master on newest ruby
estolfo 2b38625
Add message that grape test is skipped
estolfo 807203c
Test Grape and Rails together
estolfo f1c9c81
No need for SpecLogger in rails-grape spec
estolfo d9e86bd
Fix formatting
estolfo 27ce114
Remove unncessary test settings
estolfo 523cd2c
Move requiring gemspec in Gemfile so framework modules are required a…
estolfo 2a9996e
Remove one more unnecessary require
estolfo 6666074
Revert docs change about order that config options are applied
estolfo bad8409
Move changelog entry to asciidoc file
estolfo 42d30e4
Be explicit that scenario is grape and rack without rails
estolfo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,7 @@ FRAMEWORK: | |
|
||
- sinatra-2.0 | ||
- sinatra-1.4 | ||
|
||
- grape-1.2 | ||
|
||
- grape-1.2,rails-6.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
FRAMEWORK: | ||
- rails-master | ||
- sinatra-master | ||
- grape-master |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'elastic_apm/subscriber' | ||
require 'elastic_apm/normalizers/grape' | ||
|
||
module ElasticAPM | ||
# Module for starting the ElasticAPM agent and hooking into Grape. | ||
module Grape | ||
extend self | ||
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize | ||
# Start the ElasticAPM agent and hook into Grape. | ||
# | ||
# @param app [Grape::API] A Grape app. | ||
# @param config [Config, Hash] An instance of Config or a Hash config. | ||
# @return [true, nil] true if the agent was started, nil otherwise. | ||
def start(app, config = {}) | ||
config = Config.new(config) unless config.is_a?(Config) | ||
configure_app(app, config) | ||
|
||
ElasticAPM.start(config).tap do |agent| | ||
attach_subscriber(agent) | ||
end | ||
ElasticAPM.running? | ||
rescue StandardError => e | ||
config.logger.error format('Failed to start: %s', e.message) | ||
config.logger.debug "Backtrace:\n" + e.backtrace.join("\n") | ||
end | ||
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize | ||
|
||
private | ||
|
||
def configure_app(app, config) | ||
config.service_name ||= app.name | ||
config.framework_name ||= 'Grape' | ||
config.framework_version ||= ::Grape::VERSION | ||
config.logger ||= app.logger | ||
estolfo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
config.__root_path ||= Dir.pwd | ||
end | ||
|
||
def attach_subscriber(agent) | ||
return unless agent | ||
|
||
agent.instrumenter.subscriber = ElasticAPM::Subscriber.new(agent) | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
%w[endpoint_run].each do |lib| | ||
require "elastic_apm/normalizers/grape/#{lib}" | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module ElasticAPM | ||
module Normalizers | ||
module Grape | ||
# @api private | ||
class EndpointRun < Normalizer | ||
register 'endpoint_run.grape' | ||
|
||
TYPE = 'app' | ||
SUBTYPE = 'resource' | ||
|
||
FRAMEWORK_NAME = 'Grape' | ||
|
||
def normalize(transaction, _name, payload) | ||
transaction.name = endpoint(payload[:env]) | ||
unless transaction.config.framework_name == FRAMEWORK_NAME | ||
estolfo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
transaction.context.set_service(framework_name: FRAMEWORK_NAME, | ||
framework_version: ::Grape::VERSION) | ||
end | ||
[transaction.name, TYPE, SUBTYPE, nil, nil] | ||
end | ||
|
||
private | ||
|
||
def endpoint(env) | ||
route_name = env['api.endpoint']&.routes&.first&.pattern&.origin || | ||
env['REQUEST_PATH'] | ||
[env['REQUEST_METHOD'], route_name].join(' ') | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
if defined?(Grape) | ||
RSpec.describe Grape do | ||
describe '.start' do | ||
before do | ||
class GrapeTestApp < ::Grape::API | ||
use ElasticAPM::Middleware | ||
end | ||
ElasticAPM::Grape.start(GrapeTestApp, config) | ||
end | ||
|
||
after do | ||
ElasticAPM.stop | ||
Object.send(:remove_const, :GrapeTestApp) | ||
end | ||
|
||
context 'with no overridden config settings' do | ||
let(:config) { {} } | ||
it 'starts the agent' do | ||
expect(ElasticAPM::Agent).to be_running | ||
end | ||
end | ||
|
||
context 'a config with settings' do | ||
let(:config) { { service_name: 'Other Name' } } | ||
|
||
it 'sets the options' do | ||
expect(ElasticAPM.agent.config.options[:service_name].value) | ||
.to eq('Other Name') | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.