使用Raven.send_event发送其他信息

时间:2019-07-09 14:28:02

标签: ruby ruby-on-rails-5 sentry raven

我已将Sentry集成到Ruby On Rails应用程序中,并且希望在发生特定情况时发送自定义事件,我可以使用

发送事件
Raven.send_event({:message => 'Custom event'})

我如何发送与此相关的其他信息。我需要发送例如user_idemail_idlogin_name和其他自定义参数。

2 个答案:

答案 0 :(得分:1)

您可以使用Raven.user_context方法将user_context设置为raven

您可以编写一种方法来在Application Controller中设置上下文,并在操作前调用它

例如

Raven.user_context(
# a unique ID which represents this user
id: current_user.id, # 1

# the actor's email address, if available
 email: current_user.email, # "example@example.org"

 # the actor's username, if available
  username: current_user.username, # "foo"

  # the actor's IP address, if available
  ip_address: request.ip # '127.0.0.1'
)

您可以在应用程序控制器中编写为

class ApplicationController < ActionController::Base
before_action :set_raven_context, if: proc { Rails.env.production? } //If you want to set only in prod

def set_raven_context


Raven.user_context(
# a unique ID which represents this user
id: current_user.id, # 1

# the actor's email address, if available
 email: current_user.email, # "example@example.org"

 # the actor's username, if available
  username: current_user.username, # "foo"

  # the actor's IP address, if available
  ip_address: request.ip # '127.0.0.1'
)

#You can also set extra context using `Raven.extra_context`
Raven.extra_context app: url, environment: Rails.env, time: Time.now
end

end

答案 1 :(得分:0)

对于遇到类似问题的任何人,我设法通过在密钥extra下指定要发送给哨兵以进行进一步调试的任何其他密钥

    Raven.capture_message "custom message",
      logger: 'logger',
      extra: {
        time_at: Time.now
      },
      tags: {
        env: Rails.env
      }