Help us understand the problem. What is going on with this article?

[Rails] Slackにエラー通知を行う

メモ

graphql_controllerの処理の流れ

エンドポイントをAPIモードのgraphql_controllerのexecuteメソッドに指定しているので、まずここが呼び出される。

(1) Webhook URLの取得

下記から、URLを取得する。

https://slack.com/services/new/incoming-webhook

(2) credentials.yml に秘匿情報を記載

  • webhook_url
  • channel

は隠していた方が良いと思うので、credentials.ymlに記載する

$ EDITOR="vi" bin/rails credentials:edit
credentials.yml
slack:
  webhook_url: https://hooks.slack.com/services/...
  error_channel: #{YOUR_CHANNEL}
  notification_channel: #{YOUR_CHANNEL}

(3) graphql_controllerに設定

graphql_controller.rb
class GraphqlController < ActionController::API
  def execute
    処理...  
  rescue => e
    slack_error_notifier(e, query, request, current_user)
  end

  private
    # https://api.slack.com/docs/message-attachments
    def slack_error_notifier(e, query, request, current_user)
      notifier = Slack::Notifier.new Rails.application.credentials.slack[:webhook_url].to_s, username: "#{YOUR_USERNAME}"
      attachments = {
        author_name: Rails.env.to_s,
        text: "■ *Request*\n" << "*#{request.headers[:REQUEST_METHOD]}* #{request.headers[:REQUEST_URI]}" \
              "\n\n■ *Time*\n" << "#{Time.zone.now}" \
              "\n\n■ *Current User*\n" << "*USER_ID* #{current_user&.id || "NONE"}" \
              "\n\n■ *User Agent*\n" << "#{request.headers[:HTTP_USER_AGENT]}" \
              "\n\n■ *IP*\n" << "#{request.headers[:REMOTE_ADDR]}" \
              "\n\n■ *Query*\n" << "#{query}" \
              "\n\n■ *Message*\n" << "#{e.message}" \
              "\n\n■ *Backtrace*\n" << e.backtrace.join("\n").to_s,
        color: "danger",
      }
      notifier.post attachments: [attachments], channel: "#{YOUR_CHANNEL}"
    end
end

(4) 通知の確認

  • requestの中身を全て確認する。
        text: "■ *Request*\n" + "#{request.headers.sort.map { |k, v| "#{k}:#{v}" }}" + 
              "\n\n■ *User Agent*\n" + "#{request.headers[:HTTP_USER_AGENT]}" + 
              "\n\n■ *IP*\n" + "Im P!!" + 
              "\n\n■ *Query*\n" + "#{query}" + 
              "\n\n■ *Message*\n" + "#{e.message}" + 
              "\n\n■ *Backtrace*\n" + "#{e.backtrace.join("\n")}",

参考

Railsヘッダーの中身について

エラーについて

Slackの通知カスタマイズ

Action Controllerについて

Action Dispatchについて

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away