Rack 入门编辑

将 gem 添加到您的 Gemfile

gem 'elastic-apm'

创建一个文件 config/elastic_apm.yml

server_url: https://127.0.0.1:8200
secret_token: ''

在启动应用程序时包含中间件,启动(和停止)Elastic APM

# config.ru

app = lambda do |env|
  [200, {'Content-Type' => 'text/plain'}, ['ok']]
end

# Wraps all requests in transactions and reports exceptions
use ElasticAPM::Middleware

# Start an instance of the Agent
ElasticAPM.start(service_name: 'NothingButRack')

run app

# Gracefully stop the agent when process exits.
# Makes sure any pending transactions are sent.
at_exit { ElasticAPM.stop }

Sinatra 示例编辑

# Example config.ru

require 'sinatra/base'

class MySinatraApp < Sinatra::Base
  use ElasticAPM::Middleware

  # ...
end

# Takes optional ElasticAPM::Config values
ElasticAPM.start(app: MySinatraApp, ...)

# You can also do the following, which is equivalent to the above:
# ElasticAPM::Sinatra.start(MySinatraApp, ...)

run MySinatraApp

at_exit { ElasticAPM.stop }

Grape 示例编辑

# Example config.ru

require 'grape'

module Twitter
  class API < Grape::API
    use ElasticAPM::Middleware

  # ...
  end
end

# Start the agent and hook in your app
ElasticAPM::Grape.start(Twitter::API, config)

run Twitter::API