Tuesday, September 27, 2022
HomeRuby On RailsError Monitoring from Scratch | Drifting Ruby

Error Monitoring from Scratch | Drifting Ruby


# Terminal
bin/rails middleware
contact tmp/caching-dev.txt
# Net Software
# config/software.rb
require_relative "boot"
require "rails/all"
Bundler.require(*Rails.teams)
require './lib/middlewares/fundamental.rb'

module TrackerExample
  class Software < Rails::Software
    config.load_defaults 7.0
    config.middleware.use CaptureExceptions
  finish
finish
# Net Software
# lib/middlewares/fundamental.rb
require_relative 'capture_exceptions.rb'
# Net Software
# lib/middlewares/capture_exceptions.rb
require 'internet/http'
class CaptureExceptions
  def initialize(app)
    @app = app
  finish

  def name(env)
    start
      @app.name(env)
    rescue StandardError => e
      @exception = e
      capture_exceptions(env)
      elevate e
    finish
  finish

  personal

  def capture_exceptions(env)
    uri = URI('http://localhost:3001/record_exceptions')
    Web::HTTP.post_form(uri,
      message: @exception.message,
      backtrace: @exception.backtrace[0..5].be a part of("n"),
      source_location: source_location,
      technique: env['REQUEST_METHOD'],
      uri: env['REQUEST_URL']
    )
  finish

  def source_location
    strains[start_line..end_line].be a part of
  finish

  def supply
    @exception.backtrace.first.break up(':')
  finish

  def file_location
    supply[0]
  finish

  def start_line
    [source[1].to_i - 5, 0].max
  finish

  def end_line
    supply[1].to_i + 5
  finish

  def strains
    File.readlines(file_location)
  finish

finish
# Tracker Software
# config/routes.rb
Rails.software.routes.draw do
  useful resource :record_exceptions, solely: :create
  root to: 'welcome#index'
finish
# Tracker Software
# app/controllers/record_exceptions_controller.rb
class RecordExceptionsController < ApplicationController
  skip_before_action :verify_authenticity_token

  def create
    Rails.cache.write((Time.present.to_f * 1000), payload)
    head :okay
  finish

  personal

  def payload
    {
      message: params[:message],
      backtrace: params[:backtrace],
      technique: params[:method],
      uri: params[:uri],
      source_location: params[:source_location]
    }.to_json
  finish
finish
# Tracker Software
# views/welcome/index.html.erb
<% Rails.cache.instance_variable_get(:@knowledge).keys.map  JSON.parse(Rails.cache.fetch(key)).merge(time: key) .reverse.every do |error| %>
  <robust>Technique:</robust> <%= error["method"] %><br>
  <robust>URI:</robust> <%= error["uri"] %><br>
  <robust>Time:</robust> <%= Time.at(error[:time].to_i / 1000) %>
  <robust>Backtrace:</robust> <%= uncooked error["backtrace"].break up("n").be a part of("<br>") %>
  <robust>Supply Location:</robust> <%= error["source_location"] %>
<% finish %>
# Tracker Software
# views/welcome/index.html.erb
<div class="accordion accordion-flush" id="accordionExample">
  <% Rails.cache.instance_variable_get(:@knowledge).keys.map  JSON.parse(Rails.cache.fetch(key)).merge(time: key) .reverse.every do |error| %>

    <div class="accordion-item">
      <h2 class="accordion-header" id="heading<%= error[:time].to_i %>">
        <button class="accordion-button collapsed" kind="button" data-bs-toggle="collapse" data-bs-target="#physique<%= error[:time].to_i %>">
          <%= error["message"] %>
        </button>
      </h2>
      <div id="physique<%= error[:time].to_i %>" class="accordion-collapse collapse" data-bs-parent="#accordionExample">
        <div class="accordion-body">

          <div class="card border-primary mb-3">
            <div class="card-header">Information</div>
            <div class="card-body text-primary">
              <p class="card-text">
                <robust>Technique</robust> <%= error["method"] %><br>
                <robust>URI</robust> <%= error["uri"] %><br>
                <robust>Time</robust> <%= Time.at(error[:time].to_i / 1000) %>
              </p>
            </div>
          </div>

          <div class="card border-primary mb-3">
            <div class="card-header">Backtrace</div>
            <div class="card-body text-primary">
              <p class="card-text">
                <pre><%= uncooked error["backtrace"].break up("n").be a part of("<br>") %></pre>
              </p>
            </div>
          </div>

          <div class="card border-primary mb-3">
            <div class="card-header">
              Supply Location:
              <%= error["backtrace"].break up("n").first %>
            </div>
            <div class="card-body text-primary">
              <p class="card-text">
                <pre><%= error["source_location"] %></pre>
              </p>
            </div>
          </div>

        </div>
      </div>
    </div>

  <% finish %>
</div>
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments