Issues with Ruby elastic-apm gem when used with Grape

Im getting an error message NameError: uninitialized constant ElasticAPM::Grape

it looks like there is a check for if constants exist, so the below should not have reported nil according to line 46 on this file https://github.com/elastic/apm-agent-ruby/blob/c96d6fc99e9392d398c18c070b5f8d8b818e82a4/lib/elastic_apm.rb

config.ru:

## ...
require 'elastic-apm'
p defined?(::Grape) # => "constant"
p defined?(::Sinatra) # => "constant"
p defined?(::ElasticAPM) # => "constant"
p defined?(::ElasticAPM::Sinatra) # => nil
p defined?(::ElasticAPM::Grape) # => nil

## CORS
use Rack::Cors do
  allow do
    ## ...
  end
end

## map each app to a root url
app_roots = Rack::URLMap.new(
  '/' => AppCore::API,
  '/app_two' => AppTwo::API,
  '/app_three' => AppThree::API
)

## ...

if ENV['RACK_ENV'] != 'production'
  config = YAML.safe_load(File.read('./config/elastic_apm.yml'))
  ElasticAPM::Grape.start(AppCore::API, config)
  ElasticAPM::Grape.start(AppTwo::API, config)
  ElasticAPM::Grape.start(AppThree::API, config)
end

run Rack::Cascade.new [app_roots]

APM Agent language and version:
Ruby 2.6.5

Installed Gems:

gem 'awesome_print'
gem 'aws-sdk', require: ['aws-sdk-s3', 'aws-sdk-ses']
gem 'bitly', '~> 1.1.2'
gem 'bunny', '>= 2.9.2'
gem 'carrierwave'
gem 'carrierwave-aws'
gem 'carrierwave-mongoid', require: 'carrierwave/mongoid'
gem 'dotenv'
gem 'elastic-apm', '~> 1.0'
gem 'elasticsearch', '~> 6.1.0'
gem 'faraday', '~> 0.17.1'
gem 'faraday_middleware'
gem 'faraday_middleware-aws-sigv4', require: 'faraday_middleware/aws_sigv4'
gem 'geocoder'
gem 'gibberish'
gem 'grape'
gem 'grape-entity'
gem 'haml'
gem 'hashie'
gem 'humanize'
gem 'jwe', '0.4.0'
gem 'jwt', '2.2.1'
gem 'kaminari', '~> 1.0'
gem 'kaminari-mongoid', '~> 1.0'
gem 'lob'
gem 'mini_magick'
gem 'monetize'
gem 'money'
gem 'mongoid', '~> 6.4'
gem 'mongoid-encrypted-fields'
gem 'mustache'
gem 'phone'
gem 'postmark', '1.14.0'
gem 'prawn'
gem 'prawn-table'
gem 'pry'
gem 'puma'
gem 'rack'
gem 'rack-contrib'
gem 'rack-cors'
gem 'rack-test'
gem 'rake'
gem 'redis'
gem 'rest-client', require: 'rest_client'
gem 'rspec'
gem 'rubyXL'
gem 'sanitize', '~> 4.5.0'
gem 'sneakers'
gem 'sinatra'
gem 'symmetric-encryption', '~> 3.9.1'
gem 'tux'
gem 'twilio-ruby'

I'm sorry you're having trouble using the APM ruby agent with Grape. As you've noted in the source code, if ::Grape is defined, the agent will load the ElasticAPM::Grape module. So it's not clear from the config.ru why that's not happening.
Is there something special you're doing to require the grape gem? Can you check if the ::Grape is defined before you require the elastic-apm gem? In your Gemfile, do you have require: false for the grape gem, by any chance?

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.