【问题标题】:Send additional information with Raven.send_event使用 Raven.send_event 发送附加信息
【发布时间】:2019-11-19 03:27:41
【问题描述】:

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

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

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

【问题讨论】:

  • 你可以使用Raven.user_context email: 'example@mail.com'
  • 我每次都需要发送特定的自定义信息,你建议我每次调用send_event之前都设置user_context 吗?
  • 你写一个方法在应用控制器中设置上下文并在操作之前调用它

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


【解决方案1】:

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

您可以编写一个方法来在应用程序控制器中设置上下文并在操作前调用它

例如

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

【讨论】:

  • 嗨@Sumanth Madishetty,感谢您的回答,不确定我是否可以在此处使用应用程序控制器作为每次发送更改所需的信息,并且我正在后台工作人员中执行此操作。
  • 我只是使用了应用程序控制器,因为我认为你每次都需要设置上下文,如果你想在显式方法中这样做,你也可以这样做
【解决方案2】:

除了@opensource-developer 答案: 你可以在这里找到更多可用的额外参数 => Sentry Usage Page

【讨论】:

    【解决方案3】:

    对于任何面临类似问题的人,我设法通过 extra 键来完成此操作,您可以指定要发送给哨兵以进行进一步调试的任何额外键

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-06
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      相关资源
      最近更新 更多